// JP opened flex table

Click to See Complete Forum and Search --> : help needed with programming assignment


intence
March 22nd, 2007, 09:20 PM
For my assignment I am to collect letter grades for the listed courses, then have the course, course grade, and an average GPA displayed at the end. I used one of my old assignments as template. An A = 4, B = 3, C = 2, D = 1, and F = 0.

Im stuck on trying to have a string collected instead of an integer for the letter grade. When I change it i get an error and not sure what to do. Also having trouble displaying average GPA from collected courses. Any help would be greatly appreciated and I wouldn't mind paying you for your time.

Thanks




import javax.swing.JOptionPane;

public class Lab45
{
public static void main(String[] args)
{
String outputString = "";
double totaCourses = 0;
boolean moreCourses = true;
do {
// get course
String course = getCourse();
// get grade
int grade = getGrade(course);
// add to output
outputString += formatInfoFor(course, grade);
// update running totals
totaCourses += course(grade);
// ask if more
moreCourses = askIfMoreCourses();
} while (moreCourses);
// print GPA
printGrade(outputString, totaCourses);
}

public static String getCourse()
{
return JOptionPane.showInputDialog(null,
"Enter course taken (IT101, IT103, IT108, IT212, IT214, IT223, IT341)");
}

public static int getGrade(String courseTitle)
{

int grade = Integer.parseInt(
JOptionPane.showInputDialog(null,
"Enter grade for: " + courseTitle + "."));
return grade;

}

public static String formatInfoFor(String cd,
int number)
{
return cd + " " + number + "\n";
}

public static double course(int grade)
{

double amount = (grade);

return amount;
}

public static boolean askIfMoreCourses()
{
int response = Integer.parseInt(
JOptionPane.showInputDialog(null,
"Do you have another course?\n\n1 - yes\n2 - no"));
return (response == 1);
}

public static void printGrade(String outputString,
double courses)
{
outputString += "\n\nGPA: "
+ (courses);
JOptionPane.showMessageDialog(null, outputString);
}
}

IllegalCharacter
March 22nd, 2007, 09:25 PM
What is the error you are getting? I'm going to guess something about Integer.parseInt throwing an exception.

intence
March 22nd, 2007, 09:27 PM
once i do this


import javax.swing.JOptionPane;

public class Lab445
{
public static void main(String[] args)
{
String outputString = "";
double totaCourses = 0;
boolean moreCourses = true;
do {
// get course
String course = getCourse();
// get grade
String grade = getGrade(course);
// add to output
outputString += formatInfoFor(course, grade);
// update running totals
totaCourses += course(grade);
// ask if more
moreCourses = askIfMoreCourses();
} while (moreCourses);
// print GPA
printGrade(outputString, totaCourses);
}

public static String getCourse()
{
return JOptionPane.showInputDialog(null,
"Enter course taken (IT101, IT103, IT108, IT212, IT214, IT223, IT341)");
}

public static String getGrade(String courseTitle)
{


return JOptionPane.showInputDialog(null,
"Enter grade for: " + courseTitle + ".");


}

public static String formatInfoFor(String cd,
int number)
{
return cd + " " + number + "\n";
}

public static double course(int grade)
{

double amount = (grade);

return amount;
}

public static boolean askIfMoreCourses()
{
int response = Integer.parseInt(
JOptionPane.showInputDialog(null,
"Do you have another course?\n\n1 - yes\n2 - no"));
return (response == 1);
}

public static void printGrade(String outputString,
double courses)
{
outputString += "\n\nGPA: "
+ (courses);
JOptionPane.showMessageDialog(null, outputString);
}
}



Lab445.java:23: formatInfoFor(java.lang.String,int) in Lab445 cannot be applied to (java.lang.String,java.lang.String)
outputString += formatInfoFor(course, grade);
^
Lab445.java:25: cannot find symbol

HighCommander4
March 22nd, 2007, 09:55 PM
You need to convert the string "grade" into an integer. There is a method called Integer.parseInt() that does that.

do {
// get course
String course = getCourse();
// get grade
String grade = getGrade(course);
int gradeInt = Integer.parseInt(grade);
// add to output
outputString += formatInfoFor(course, gradeInt);
// update running totals
totaCourses += course(gradeInt);
// ask if more
moreCourses = askIfMoreCourses();
} while (moreCourses);

dlorde
March 23rd, 2007, 06:15 AM
But shouldn't grade be an integer not a String anyway? Suppose you wanted to compare it with a pass/fail mark, or with another student grade?

Everything should be made as simple as possible, but not simpler...
A. Einstein

//JP added flex table