1 Attachment(s)
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.
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();
}
}
}
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?
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
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
Re: is there any way to optimize/shrink this code even more with same results?
Quote:
Originally Posted by
just_in_deed
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
Re: is there any way to optimize/shrink this code even more with same results?
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{}
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
Re: is there any way to optimize/shrink this code even more with same results?
I think it's fixed now:
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");
}
}
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);
}
}
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?
Re: is there any way to optimize/shrink this code even more with same results?
yes it's working fine now