This is my class:
/** * The StudentReport1 class has fields for name, grade, school, of a student * * @author Luis Fierro * @version 4/05/2014 */ public class StudentReport1 { // instance variables - replace the example below with your own private String name; private String sGrade; private String sName; /** * Constructor for objects of class StudentReport1 * @param n The student's name * @param sg The student's grade in school * @param sn The student's school name * */ public StudentReport1(String n, String sg, String sn) { // initialise instance variables name = n; sGrade = sg; sName = sn; } /** * setName method * * @param n The student's name */ public void setName(String n) { name = n; } /** * setSGrade method * * @param sg The student's grade in school */ public void setSGrade(String sg) { sGrade = sg; } /** * setSName method * * @param sn The student's school name */ public void setSName(String sn) { sName = sn; } /** * getName method * @return n Return the name of the student */ public String getName() { return name; } /** * getSGrade method * @return sg Return the student's grade in school * */ public String getSGrade() { return sGrade; } /** * getSName method * @return sn Return the student's school name */ public String getSName() { return sName; } public String toString() { return "Name: " + name + "\nGrade: " + sGrade + "\nSchool" + sName; } }
Then this is what I use to test my class:
Everything compiles without errors, but when I run this in order to test everything it ask me my information and my gradesimport javax.swing.*; /** * The ReportGenerator class uses the StudentReport1 class to simulate the report * of a student. Holds fields for number of classes,grades received, average, and gpa. * * @author Luis Fierro * @version 4/16/2014 */ public class ReportGenerator { /** * getData method creates a StudentReport1 object * pobulated with data from the user * * @return A reference to StudentReport1 */ public static StudentReport1 setData() { //variables to hold the information String name; String schoolGrade; String schoolName; //read the information name=JOptionPane.showInputDialog("Enter your name: "); schoolGrade=JOptionPane.showInputDialog("Enter your grade in school: "); schoolName=JOptionPane.showInputDialog("Enter the name of your school: "); //create a StudentReport entry StudentReport1 data = new StudentReport1(name, schoolGrade, schoolName); //return a reference to the object return data; } public static double getAverage(double total, int numOfClasses) { return total / numOfClasses; } public static double getGpa(double average) { return (average*4)/100; } /** * showData method shows the data stored in * the StudentReport1 object * * @param data The data to show */ public static void getData(StudentReport1 data, double average, double gpa) { JOptionPane.showMessageDialog(null, "Name :" + data.getName() + "\nGrade" + data.getSGrade() + "\nSchool: " + data.getSName() + "\nAverage: " + average + "\nGPA: " + gpa + "."); } public static void main(String [] args) { // declare variables int numOfGrades=0; //number of classes of the student int grades; //grades received in each class double average; //average= sum of all grades / number of classes double gpa; //average converted to scale 1-4 int total=0; //total of the grades String trash; //to convert s tring to double setData(); //ask number of grades trash=JOptionPane.showInputDialog("Number of grades:"); numOfGrades=Integer.parseInt(trash); //get the grades added together in order to calculate the average for (int i = 1; i <=numOfGrades; i++) { trash=JOptionPane.showInputDialog("Test " + i + " : " ); grades=Integer.parseInt(trash); total+=grades; } // Get the average average = getAverage(numOfGrades, total); // Get the gpa gpa = getGpa(average); JOptionPane.showMessageDialog(null, "The students average is: " + average + "\nThe student's GPA is: " + gpa); } }
on my exams, but then it only displays the gpa and average, it should display the user information too. And for some reason
it is giving me some weird numbers for the average and the gpa but I checked my calculations and they are right.
So basically my two questions are
How can it display the user information too?
Why the calculations don't work?