Results 1 to 10 of 10
- 09-09-2011, 07:02 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
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(); } } }Last edited by just_in_deed; 09-10-2011 at 05:54 AM.
- 09-09-2011, 01:49 PM #2
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?
- 09-09-2011, 08:58 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-10-2011, 05:58 AM #4
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
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
- 09-10-2011, 08:58 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Re: is there any way to optimize/shrink this code even more with same results?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-10-2011, 11:36 PM #6
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
Re: is there any way to optimize/shrink this code even more with same results?
thanks for your reply now I'm using a while loop instead of if{}else{}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(); }
- 09-10-2011, 11:41 PM #7
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
- 09-11-2011, 06:12 AM #8
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
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 05:27 PM.
- 09-11-2011, 02:28 PM #9
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?
- 09-11-2011, 05:27 PM #10
Member
- Join Date
- Aug 2010
- Posts
- 25
- Rep Power
- 0
Similar Threads
-
why is it so slow? need to optimize it?
By skarosg3 in forum Advanced JavaReplies: 15Last Post: 10-08-2010, 08:54 AM -
Optimize my Code please....
By mindblaster in forum New To JavaReplies: 5Last Post: 02-06-2010, 11:32 AM -
Sometimes get the right results sometimes dont
By Battlefeldt in forum New To JavaReplies: 0Last Post: 12-18-2009, 01:03 AM -
shrink and expand nemu when user click
By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 03-06-2009, 07:02 AM -
results to code disappear too fast for DOS window
By dubdubdub in forum New To JavaReplies: 3Last Post: 12-29-2007, 05:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks