-
Problem with algorithm
I have almost all of this program functioning there's just some flaws here i need looked at please
The program is supposed to ask for the input of specific classes, only 1 of each class can be entered, the classes are stored in an array along with a corresponding array of Grades. The grades are then corresponded to quality points for determining GPA.
I have my while loops to validate whether a correct grade or class is entered. They are commented out because they were not working quite the way i wanted.
The do-while loop will ask if more classes should be entered, if yes, it returns to the loop body, if no it should break and then output the arrays and the GPA.
Code:
import javax.swing.*;
public class lab5 {
public static void main(String[] args ) {
String[] classes = new String[7];
String[] grades = new String[7];
int[] quality_points= new int[7];
int more = 0;
//start for loop
for(int i = 0; i <= classes.length; i++){
do{
// ask for classes
classes[i] = JOptionPane.showInputDialog( null, "Enter course taken (IT101, IT103, IT108, IT212, IT214, IT223, IT341)");
/* while (classes[i] != "IT101" | classes[i] != "IT103" |classes[i] != "IT108" | classes[i] != "IT212" | classes[i] != "IT214" | classes[i] != "IT223" |classes[i] != "IT341")
{
classes[i] = JOptionPane.showInputDialog( null, "Please enter a CORRECT course (IT101, IT103, IT108, IT212, IT214, IT223, IT341)");
}*/
// ask for grades
grades[i] = JOptionPane.showInputDialog( null, "Enter grade for " + classes[i]);
/*while (grades[i] != "F" | grades[i] != "A" | grades[i] != "A+" | grades[i] != "A-" | grades[i] != "B" | grades[i] != "B+" | grades[i] != "B-" | grades[i] != "C" | grades[i] != "C-" | grades[i] != "C+" | grades[i] != "D-" | grades[i] != "D" | grades[i] != "D+") {
grades[i] = JOptionPane.showInputDialog( null, "Please enter a CORRECT letter grade");
}*/
// convert grades to quality points
if (grades[i] == "A") {
quality_points[i] = 4;
if (grades[i] == "A+")
quality_points[i] = 4;
if (grades[i] == "A-")
quality_points[i] = 4;
}
else if (grades[i] =="B" ) {
quality_points[i] = 3;
if (grades[i] == "B+")
quality_points[i] = 3;
if (grades[i] == "B-")
quality_points[i] = 3;
}
else if (grades[i] =="C" ) {
quality_points[i] = 2;
if (grades[i] =="C+" )
quality_points[i] = 2;
if (grades[i] == "C-")
quality_points[i] = 2;
}
else if (grades[i] =="D" ) {
quality_points[i] = 1;
if (grades[i] == "D-")
quality_points[i] = 1;
if (grades[i] == "D+")
quality_points[i] = 1;
}
else {
quality_points[i] = 0;
}
// ask to add class
more = JOptionPane.showConfirmDialog(null, "Would you like to add another class?");
}while (more == JOptionPane.YES_OPTION);
break;
}
//calculate gpa
double gradestotal = Double.parseDouble( grades[0] + grades[1] + grades[2] + grades[3] + grades[4] + grades[5] + grades[6]);
double gpa = gradestotal/classes.length;
//output
JOptionPane.showMessageDialog(null, "Course Grade/n" + classes[0] + " " + grades[0] + "/n" + classes[1] + " " + grades[1] + "/n" + classes[2] + " " + grades[2] + "/n" + classes[3] + " " + grades[3] + "/n" + classes[4] + " " + grades[4] + "/n" + classes[5] + " " + grades[5] + "/n" + classes[6] + " " + grades[6] + "/n-----------------/n" + "Your GPA is: " + GPA );
}
}
Thanks
Albert:rolleyes:
-
Well there are a couple java basics that we need to go over that will help you in finishing this assignment (also why it wont be working in the various while loops, etc).
So let's go down your program one at a time. I will tell you what you are doing and then push you in the direction you may want to go. I obviously wont do your homework for you but hopefully this will help.
So, your first major statement you are going through the array of empty String classes to the length. That is you are assuming that seven classes will always be entered.
But right after that you have a do While loop that is true if they want to add another class. This means that someone could hit the while loop and hit yes over seven times, which is above the length of your for loop, not to mention that your i will not update to the next value.
So after that you get input from an input dialog and store it into the i'th spot in the array class. If you're while loop was uncommented out, you would be looking to see if the pointer of that array argument was the same as various string. What you want to do is use the String equals or equalsIgnoresCase methods to find out if they are the same Strings, not if they are the same pointer area.
Code:
String ar = "ar";
if(ar == "ar")
System.out.println("Might Happen");
else
System.out.println("Might Also Happen");
if(ar.equals("ar"))
System.out.println("Will happen");
else
System.out.println("Will NEVER happen");
Also you are doing a unary OR which isn't want you want in that while loop. You'd rather use the more commonly known or which is two pipes, that is
Code:
if(ar.equals("B") || ar.equals("C"))
You do about the same thing with Grades.
After that you want to convert the Grades into various number values. But when you do this the first thing you check is (again the wrong operator) Letter A. And then check if it's A+, A- etc. But If the Grade is A-, it will never get into the second check as it doesn't Equal the letter A
A != A-
in a nutshell.
With your calculation of GPA, I'm unsure what you are doing. You take the Grades ("A" "B" "C" "C") for example and concatenate them together and then get their double value? I think you meant quality_points maybe?
Hope this helps, still a lot of work to be done but it's very doable!