Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-09-2007, 08:42 PM
Senior Member
 
Join Date: Jun 2007
Posts: 114
Albert is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-13-2007, 05:31 PM
Member
 
Join Date: Jul 2007
Posts: 44
susan is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with equation in my algorithm romina New To Java 2 07-20-2007 09:53 AM
Help with algorithm susan New To Java 1 07-14-2007 12:26 AM
Help me with this algorithm Marcus Advanced Java 3 07-02-2007 03:30 PM
Help with Algorithm Daniel Advanced Java 2 07-02-2007 07:51 AM
Algorithm problem Marcus Advanced Java 2 07-01-2007 03:37 AM


All times are GMT +3. The time now is 12:46 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org