Results 1 to 10 of 10
  1. #1
    just_in_deed is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default is there any way to optimize/shrink this code even more with same results?

    What this code is suposed to do is to ask for the number of school qualifications and get the average qualification then it asks if you desire to keep adding more qualifications. is you agree the program continues till you want to stop using it.

    Java Code:
    import javax.swing.JOptionPane;
    
    public class prom {
    	
    	public static void main(String[] args){
    		
    		String a = "";   String b= "";
    		
    		captura();
    		JOptionPane.showMessageDialog( null, "Queres ingresar mas materias\n1 = SI\n2 = NO" );
    		b = JOptionPane.showInputDialog(a, JOptionPane.INFORMATION_MESSAGE);
    		int z = Integer.parseInt(b);
    		
    		if( z == 1 ){
    			
    			procesa(z);
    		
    			}else{
    			
    				System.out.println("Gracias por usar promediadior v1.0");
    			}
    		
    	}
    	
    	public static void muestra(double a){
    		
    		JOptionPane.showMessageDialog(null, "El promedio es : " + a );
    	}
    	
    	public static void promedia(int num_mat, double sumatoria){
    		
    		double z;
    		
    		z = sumatoria / num_mat;
    		
    		muestra(z);
    	}
    
    	public static void procesa(int x){
    		
    		String a = "";
    		String b = "";
    		int v;
    		double w = 0.00;
    		
    		for(v = 0; v < x; v++){
    			
    			JOptionPane.showMessageDialog( null, "Ingresa calificaion" );
    			b = JOptionPane.showInputDialog( a );
    			w += Double.parseDouble(b);
    		}
    		
    		promedia(x, w);
    	}
    	
    	public static void captura(){
    		
    		String a = "";  String c = "";
    		String b = "";  String d = "";
    		int z;
    		JOptionPane.showMessageDialog( null, "cuantas materias quieres ingresar?\t" );
    		b = JOptionPane.showInputDialog(a);
    		z = Integer.parseInt(b);
    		JOptionPane.showMessageDialog( null, "Quieres ingresar " + b + " materias\n1 = SI\n2 = NO");
    		d = JOptionPane.showInputDialog(c, JOptionPane.INFORMATION_MESSAGE);
    		int v = Integer.parseInt(d);
    		if( v == 1 ){
    			
    			procesa(z);
    		}else{
    			captura();
    		}
    	}
    }
    Attached Files Attached Files
    Last edited by just_in_deed; 09-10-2011 at 06:54 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: is there any way to optimize/shrink this code even more with same results?

    Can document what the code is supposed to do so that we can see if your version of code does that?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default Re: is there any way to optimize/shrink this code even more with same results?

    Also note that your captura() method uses tail recursion; if you select 'NO' a couple of million times you'll get a StackOverflowException ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    just_in_deed is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default Re: is there any way to optimize/shrink this code even more with same results?

    thanks I'm now figuring out how to use tail recursion instead of standard recursion

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default Re: is there any way to optimize/shrink this code even more with same results?

    Quote Originally Posted by just_in_deed View Post
    thanks I'm now figuring out how to use tail recursion instead of standard recursion
    You only want to use tail recursion for languages that know how to remove it; Java doesn't remove tail recursion so you shouldn't use it. Us a loop instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    just_in_deed is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default Re: is there any way to optimize/shrink this code even more with same results?

    Java Code:
    public static void captura(){
    		
    		String a = "";  String c = "";
    		String b = "";  String d = "";
    		int z;
    		JOptionPane.showMessageDialog( null, "cuantas materias quieres ingresar?\t" );
    		b = JOptionPane.showInputDialog(a);
    		z = Integer.parseInt(b);
    		JOptionPane.showMessageDialog( null, "Quieres ingresar " + b + " materias\n1 = SI\n2 = NO");
    		d = JOptionPane.showInputDialog(c, JOptionPane.INFORMATION_MESSAGE);
    		int v = Integer.parseInt(d);
    		while( v == 1 ){
    			
    			procesa(z);
    		}
    			captura();
    		
    	}
    thanks for your reply now I'm using a while loop instead of if{}else{}

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: is there any way to optimize/shrink this code even more with same results?

    You still call captura from within captura. That's recursion.
    The whole of captura should be a loop instead of doing a recursive call.

    Where does the value of v get changed inside of the loop? => infinite loop

  8. #8
    just_in_deed is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default Re: is there any way to optimize/shrink this code even more with same results?

    I think it's fixed now:

    Java Code:
    public static void main(String[] args){
    		
    		String a = "";   String b= "";
    		
    		captura();
    		JOptionPane.showMessageDialog( null, "Queres ingresar mas materias\n1 = SI\n2 = NO" );
    		b = JOptionPane.showInputDialog(a, JOptionPane.INFORMATION_MESSAGE);
    		int z = Integer.parseInt(b);
    		
    		if( z == 1 ){
    			
    			captura();
    		
    			}else{
    			
    				System.out.println("Gracias por usar promediadior v1.0");
    			}
    		
    	}

    Java Code:
    public static void captura(){
    		
    		String a = "";  String c = "";
    		String b = "";  String d = "";
    		int z;
    		JOptionPane.showMessageDialog( null, "cuantas materias quieres ingresar?\t" );
    		b = JOptionPane.showInputDialog(a);
    		z = Integer.parseInt(b);
    		JOptionPane.showMessageDialog( null, "Quieres ingresar " + b + " materias\n1 = SI\n2 = NO");
    		d = JOptionPane.showInputDialog(c, JOptionPane.INFORMATION_MESSAGE);
    		int v = Integer.parseInt(d);
    		if( v == 1 ){
    			
    			procesa(z);
    		}
    	}
    Last edited by just_in_deed; 09-11-2011 at 06:27 PM.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: is there any way to optimize/shrink this code even more with same results?

    Does it do what you want when you execute it?

  10. #10
    just_in_deed is offline Member
    Join Date
    Aug 2010
    Posts
    25
    Rep Power
    0

    Default Re: is there any way to optimize/shrink this code even more with same results?

    yes it's working fine now

Similar Threads

  1. why is it so slow? need to optimize it?
    By skarosg3 in forum Advanced Java
    Replies: 15
    Last Post: 10-08-2010, 09:54 AM
  2. Optimize my Code please....
    By mindblaster in forum New To Java
    Replies: 5
    Last Post: 02-06-2010, 12:32 PM
  3. Sometimes get the right results sometimes dont
    By Battlefeldt in forum New To Java
    Replies: 0
    Last Post: 12-18-2009, 02:03 AM
  4. shrink and expand nemu when user click
    By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-06-2009, 08:02 AM
  5. results to code disappear too fast for DOS window
    By dubdubdub in forum New To Java
    Replies: 3
    Last Post: 12-29-2007, 06:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •