Java Program help cleanup
The code here is working fine but the problem lies with the if else statements. Not that they do not work they do. But having said that I would like to clean it up some. When I implemented the if else statement it was to distinguish between Graduate and Undergrad. I would like to consolidate some of this code by removing repeated code. Both of the Graduate and Undergrad are extended classes from a Student class which is abstract.
Any thoughts on this would be appreciated.
Code java:
<if (choice == 1) {
do {//do while block
try {//try catch block for exception handeling
// collect grades of tests
for (int i = 0; i < noTests; i++) {
System.out.printf("Enter grade number for %s %s %d: ", grad.getFirstname(), grad.getLastname(), i + 1);
grad.setGrades(scan.nextInt());
flag = false;
}
} catch (InputMismatchException IME) {//catch begin
System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput
scan.nextLine();
flag = true;//return to top if ture is false
}
} while (flag);//End do loop
do {//do while block
try {//try catch block for exception handeling
System.out.printf("Enter Midterm grade for: %s %s ", grad.getFirstname(), grad.getLastname());
grad.setmidtermgrad(scan.nextInt());
flag = false;
} catch (InputMismatchException IME) {//catch begin
System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput
scan.nextLine();
flag = true;//return to top if ture is false
}
} while (flag);//End do loop
do {
try {//try catch block for exception handeling
System.out.printf("Enter final grade for: %s %s ", grad.getFirstname(), grad.getLastname());
grad.setfinalgrade(scan.nextInt());
flag = false;
} catch (InputMismatchException IME) {//catch begin
System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput
scan.nextLine();
flag = true;//return to top if ture is false
}
} while (flag);//End do loopp
//collect information and display results
System.out.printf("Average grade for: %s %s %.2f\n", grad.getFirstname(), grad.getLastname(), grad.getGradeAvg());
System.out.printf("THE FINAL GRADE FOR STUDENT: %s %s %.2f\n", grad.getFirstname(), grad.getLastname(), grad.getFinalGrade());
} else if (choice == 2) {//begin else statement
do {//do while block
try {//try catch block for exception handeling
// collect grades of tests
for (int i = 0; i < noTests; i++) {
System.out.printf("Enter grade number %d: ", i + 1);
Grad.setGrades(scan.nextInt());
}
flag = false;
} catch (InputMismatchException IME) {//catch begin
System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput
scan.nextLine();
flag = true;//return to top if ture is false
}
} while (flag);//End do loop
do {
try {//try catch block for exception handeling
System.out.printf("Enter final grade for: %s %s ", Grad.getFirstname(), Grad.getLastname());
Grad.setfingrade(scan.nextInt());
flag = false;
} catch (InputMismatchException IME) {//catch begin
System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput
scan.nextLine();
flag = true;//return to top if ture is false
}
} while (flag);//End do loop
System.out.printf("Average grade for: %s %s %.2f\n", Grad.getFirstname(), Grad.getLastname(), Grad.getGradeAvg());
System.out.printf("THE FINAL GRADE FOR STUDENT: %s %s %.2f\n", Grad.getFirstname(), Grad.getLastname(), Grad.getFinGrade());
}>
Re: Java Program help cleanup
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: Java Program help cleanup
Re: Java Program help cleanup
If you use these code multiple times.
Code :
try {//try catch block for exception handeling
System.out.printf("Enter Midterm grade for: %s %s ", grad.getFirstname(), grad.getLastname());
grad.setmidtermgrad(scan.nextInt());
flag = false;
} catch (InputMismatchException IME) {//catch begin
System.err.printf("Exception!\nYou must enter a number. Please try again.\n", IME);//check for improper imput
scan.nextLine();
flag = true;//return to top if ture is false
}
You might be able to store those in a subclass.
And then you only need for example: subclass() to execute that code.
That saves some lines, and when you need to change something in that part you only have to do it one time and not multiple times.
Re: Java Program help cleanup
okay but I was thinking more about the Graduate and Undergrad choices and responses.
how can i get with the user choice of Graduate or Undergrad response to be coincided.
example for both Graduate and Undergrad the question of number of tests and the question of midterm and final grade without repeating those questions?