Grading Program - Having a hard time

Hi, I am making a grading program for my intro java class. We are required to make our own class and then use a "driver" class to test our template. The problem I am running into is that I do not know why some variables are being returned while others are not.

In the program, we are to create a class that gets the student id/quiz1 score, quiz2, midterm, and final. The grades are weighted. I will paste the StudentRecord class, the DriverStudentRecord, and then a sample output. Please help. I have been banging my head into this book but nothing is jumping out at me on how to fix it. Thanks!

StudentRecord

import java.util.*;

publicclass StudentRecord{

privateint studentID, quizOne, quizTwo, midTerm, finalExam, overallScore,

quizTotal;

privatechar letterGrade;

publicvoid readInput()

{

Scanner keyboard =new Scanner(System.in);

System.out.println("Please enter the Student's ID#: ");

studentID = keyboard.nextInt();

System.out.println("Please enter the score for Quiz 1: ");

quizOne = keyboard.nextInt();

System.out.println("Please enter the score for Quiz 2: ");

quizTwo = keyboard.nextInt();

System.out.println("Please enter the score for the Midterm: ");

midTerm = keyboard.nextInt();

System.out.println("Please enter the score for the Final Exam: ");

finalExam = keyboard.nextInt();

}

publicvoid calculation()

{

quizTotal = quizOne + quizTwo;

quizTotal = quizTotal * (25/100);

midTerm = midTerm * (25/100);

finalExam = finalExam * (50/100);

overallScore = quizTotal + midTerm + finalExam;

}

publicchar letterGrade()

{

if (overallScore >= 90)

return'A';

elseif ((overallScore >= 80) && (overallScore < 90))

return'B';

elseif ((overallScore >= 70) && (overallScore < 80))

return'C';

elseif ((overallScore >= 60) && (overallScore < 70))

return'D';

elseif ((overallScore >= 0) && (overallScore < 60))

return'F';

else

return'Z';

}

publicvoid writeOutput()

{

System.out.println("Student# " + studentID +" grade report");

System.out.println("Quiz one = " + quizOne);

System.out.println("Quiz two = " + quizTwo);

System.out.println("Quiz totals = " + quizTotal);

System.out.println("Midterm = " + midTerm);

System.out.println("Final = " + finalExam);

System.out.println("Overall Score = " + overallScore);

System.out.println("Final Letter Grade = " );

}

DriverStudentRecord

publicclass DriverStudentRecord{

publicstaticvoid main(String[] args){

StudentRecord testStudent =new StudentRecord();

testStudent.readInput();

testStudent.calculation();

testStudent.letterGrade();

testStudent.writeOutput();

}

}

& Sample output

Please enter the Student's ID#:

999

Please enter the score for Quiz 1:

10

Please enter the score for Quiz 2:

8

Please enter the score for the Midterm:

90

Please enter the score for the Final Exam:

70

Student# 999 grade report

Quiz one = 10

Quiz two = 8

Quiz totals = 0

Midterm = 0

Final = 0

Overall Score = 0

Final Letter Grade =

Why are only quiz1 and 2 responses being carried over, but the rest are not? Does this look okay so far? I have never written my own class like this. Lastly, how do I get the letterGrade to print its return value? Thanks all! I really do appreciate it.

[5829 byte] By [eristica] at [2007-11-27 6:14:36]
# 1
quizTotal = quizTotal * (25/100);What is 25 / 100?
CaptainMorgan08a at 2007-7-12 17:24:15 > top of Java-index,Java Essentials,New To Java...
# 2
the quizzes are 25% of the students total grade. So I add up the quiz scores then multiply it by .25
eristica at 2007-7-12 17:24:15 > top of Java-index,Java Essentials,New To Java...
# 3
> the quizzes are 25% of the students total grade. So> I add up the quiz scores then multiply it by .25I don't think you catch my drift. Try adding this and see what it outputs.System.out.println(25 / 100);
CaptainMorgan08a at 2007-7-12 17:24:15 > top of Java-index,Java Essentials,New To Java...
# 4
I didn't get anything.Nothing posted.
eristica at 2007-7-12 17:24:15 > top of Java-index,Java Essentials,New To Java...
# 5

> I didn't get anything.Nothing posted.

If you run the one line of code I gave you above, it outputs 0. 25 and 100 are both ints, so when you divide them, the result will also be an int. Since 0.25 can't be expressed as an int, the .25 is truncated. Just try dividing by 4 instead of multiplying by 25/100.

CaptainMorgan08a at 2007-7-12 17:24:15 > top of Java-index,Java Essentials,New To Java...
# 6
I added that line you gave me to the writeOutput() part, at the end. I wonder why nothing showed up...?
eristica at 2007-7-12 17:24:15 > top of Java-index,Java Essentials,New To Java...
# 7

Your getting there, but not quite where you want to be. I recommend the following:

1) in your calculation method, do not alter the values of your quizes, midterm, and final. Use the weights (25, 50, etc) in your final calculation only.

2) Calculate the final grade in the calculation method and don't forget to display the variable in the final display.

2) Check your answers to see if it makes sense. For instance do a run where every score is 100. The answer will not be what you expect and you will have to correct something to get it where it should be.

petes1234a at 2007-7-12 17:24:15 > top of Java-index,Java Essentials,New To Java...
# 8

Ok, did what you said... here is the change

public void calculation()

{

quizTotal = quizOne + quizTwo;

quizTotal = quizTotal / 4;

midTerm = midTerm / 4;

finalExam = finalExam / 2;

overallScore = quizTotal + midTerm + finalExam;

}

Output

Student# 999 grade report

Quiz one = 10

Quiz two = 8

Quiz totals = 4

Midterm = 17

Final = 45

Overall Score = 66

Final Letter Grade =

0 (this must be the 25/100 you told me)

How do I get the char letter grade to output ?

eristica at 2007-7-12 17:24:15 > top of Java-index,Java Essentials,New To Java...
# 9
System.out.println("Final Letter Grade = " + letterGrade());Your grading scale is lenient. At my school, a 66 is definitely an F.
CaptainMorgan08a at 2007-7-12 17:24:15 > top of Java-index,Java Essentials,New To Java...
# 10
Ahh, I didn't know I could call my letterGrade thing (not sure what its called, mehod?class?) in a statement like that.Ouch...66? I had a class once where only 93 and above was an A, and it cascaded down similarly, but it wasn't the whole school system.
eristica at 2007-7-12 17:24:15 > top of Java-index,Java Essentials,New To Java...
# 11
> Ouch...66? I had a class once where only 93 and> above was an A, and it cascaded down similarly, but> it wasn't the whole school system.Yup, that's what every class is for me. 93+ is an A, 85+ is a B, 77+ is a C, etc.
CaptainMorgan08a at 2007-7-12 17:24:15 > top of Java-index,Java Essentials,New To Java...
# 12

> Your getting there, but not quite where you want to

> be. I recommend the following:

>

> 1) in your calculation method, do not alter the

> values of your quizes, midterm, and final. Use the

> weights (25, 50, etc) in your final calculation

> only.

> 2) Calculate the final grade in the calculation

> method and don't forget to display the variable in

> the final display.

> 2) Check your answers to see if it makes sense. For

> instance do a run where every score is 100. The

> answer will not be what you expect and you will have

> to correct something to get it where it should be.

How do I go about doing 1) ? I don't know how to access the variables without altering the new ones. Do you mean create new variables, assign the original values to the new ones, then do the calculations?

2) I'll paste my letterGrade() into my calculation part to keep it in one.

3) I'll check , but we were told that quiz 1 and 2 are only out of 10 points, so the assumption is that the user knows this. The other 2 (midterm/final) are out of 100.

eristica at 2007-7-12 17:24:15 > top of Java-index,Java Essentials,New To Java...
# 13

I went through uni with a chineese bloke, apparently if you get less than 50% for anything (once) they kick you out of school and make you a farmer. Tough call on a six year old.

eristic,

I know you're learning, and you appear to be making good progress, so feel free to ignore this small criticism... your readInput and writeOutput methods would be better placing in your driver class. Apart from thay you're coming along fine.

1)

> ... do not alter thevalues of your quizes, midterm, and final.

> Use the weights (25, 50, etc) in your final calculation only.

I think pete means (but I can't talk for him) something like :public void calculation() {

score = (quiz1+quiz2)/4.0 + midTerm/4.0 + finalExam/2.0;

}

2) You forgot something on this line:

System.out.println("Final Letter Grade = " );

3) test it... work out a bunch (6 should do) students scores by hand, or with a calculator, or use excel... and then run them through your program. Are the answers what you expected?

corlettka at 2007-7-12 17:24:15 > top of Java-index,Java Essentials,New To Java...
# 14

> I went through uni with a chineese bloke, apparently

> if you get less than 50% for anything (once) they

> kick you out of school and make you a farmer. Tough

> call on a six year old.

>

> eristic,

>

> I know you're learning, and you appear to be making

> good progress, so feel free to ignore this small

> criticism... your readInput and writeOutput methods

> would be better placing in your driver class. Apart

> from thay you're coming along fine.

>

> 1)

> > ... do not alter thevalues of your quizes, midterm,

> and final.

> > Use the weights (25, 50, etc) in your final

> calculation only.

>

> I think pete means (but I can't talk for him)

> something like :public void calculation()

> {

> score = (quiz1+quiz2)/4.0 + midTerm/4.0 +

> finalExam/2.0;

>}

> ) You forgot something on this line:

>System.out.println("Final Letter Grade = " );

> test it... work out a bunch (6 should do) students

> scores by hand, or with a calculator, or use excel...

> and then run them through your program. Are the

> answers what you expected?

I was under the impression that the DriverStudentRecord is just a title for a test class? Instead of saying test, the prof. said Driver. Does driver mean something more than that? Also, I'm just following the structure that I've seen in my book so far. What advantage/benefit is there for putting those 2 in the driver? (that way I can understand why they are better suited there).

eristica at 2007-7-12 17:24:15 > top of Java-index,Java Essentials,New To Java...
# 15

CODE UPDATED:

StudentRecord:

// ***************************************************************************

// Title: ClassRecord.java

// Class: CSC-201 (Summer 2007)

// Assignment: HW #2

// Author: Me

// Last Modified:

//

// Description:

//

// Assumptions:

//

// Deficiencies:

//

// <-- text in file should be no wider than 80 characters so text doesn't wrap

//when printed in portrait format -->

// ***************************************************************************

// ***************************************************************************

// REQUIRED PACKAGES

// ***************************************************************************

// ***************************************************************************

// INSTANCE VARIABLES

// ***************************************************************************

// ***************************************************************************

// CONSTRUCTORS

// ***************************************************************************

/* Default constructor */

/* Comprehensive constructor */

// ***************************************************************************

// MUTATOR METHODS

// ***************************************************************************

// ***************************************************************************

// ACCESSOR METHODS

// ***************************************************************************

// ***************************************************************************

// PUBLIC INTERFACE METHODS

// ***************************************************************************

// Precondition:

// Postcondition:

// ***************************************************************************

// PRIVATE HELPER METHODS

// ***************************************************************************

// Precondition:

// Postcondition:

import java.util.*;

public class StudentRecord {

private int studentID, quizOne, quizTwo, midTerm, finalExam, overallScore;

private char letterGrade;

public void readInput()

{

Scanner keyboard = new Scanner(System.in);

System.out.println("Please enter the Student's ID#: ");

studentID = keyboard.nextInt();

System.out.println("Please enter the score for Quiz 1: ");

quizOne = keyboard.nextInt();

System.out.println("Please enter the score for Quiz 2: ");

quizTwo = keyboard.nextInt();

System.out.println("Please enter the score for the Midterm: ");

midTerm = keyboard.nextInt();

System.out.println("Please enter the score for the Final Exam: ");

finalExam = keyboard.nextInt();

}

public void calculation()

{

overallScore = (quizOne + quizTwo) * 5 / 4 + (midTerm / 4) +

finalExam / 2;

if (overallScore >= 90)

letterGrade = 'A';

else if ((overallScore >= 80) && (overallScore < 90))

letterGrade = 'B';

else if ((overallScore >= 70) && (overallScore < 80))

letterGrade = 'C';

else if ((overallScore >= 60) && (overallScore < 70))

letterGrade = 'D';

else

letterGrade = 'F';

}

public void writeOutput()

{

System.out.println("Student# " + studentID + " grade report");

System.out.println("Quiz one = " + quizOne);

System.out.println("Quiz two = " + quizTwo);

System.out.println("Midterm = " + midTerm);

System.out.println("Final = " + finalExam);

System.out.println("Overall Score = " + overallScore);

System.out.println("Final Letter Grade = " + letterGrade);

}

DriverStudentRecord

public class DriverStudentRecord {

public static void main(String[] args) {

StudentRecord testStudent = new StudentRecord();

testStudent.readInput();

testStudent.calculation();

testStudent.writeOutput();

}

}

Sample Output

Please enter the Student's ID#:

123

Please enter the score for Quiz 1:

7

Please enter the score for Quiz 2:

8

Please enter the score for the Midterm:

84

Please enter the score for the Final Exam:

93

Student# 123 grade report

Quiz one = 7

Quiz two = 8

Midterm = 84

Final = 93

Overall Score = 85

Final Letter Grade = B

Hmm... now to figure out what are the accessor and mutator methods? I didn't really have any I think?

Message was edited by:

eristic

Message was edited by:

eristic

eristica at 2007-7-21 21:48:33 > top of Java-index,Java Essentials,New To Java...
# 16

Sorry for the long break... I got interested in the footy.

In My Humble Opinion (IMHO) ... This is all pretty airy ***** stuff, and there really is no "one true and correct" way to write any program... BUT it has to do with "seperation of concerns". Which boils down to "a class should do one thing. It should do it completely, and do it well"

Again IMHO... the bestest ever programing paradigm for "seperation of concerns" is MVC - Model, View Controler.

- The model is your representation of the data... in your case a students grades. So that's one class, it stores and manipulates students grades.

- The view is your interface to the data... the presentation layer. In this case that'd be your readInput and writeOutput.

- The controller is the class which orcestrates whatever operations you are performing on the data... in this case that's your main method.

This stuff becomes more important as your programs and the systems they implement get bigger, more complex, more expensive, more risky, and more longer lived.

This is vaery simple program (compared to say an internet banking system, just for example) so this stuff is by no means necessary for this magnitude of program, but I reckon it's worth paying attention to good design early in your experiences, so that you form good habits up front, and don't subsequently have unlearn bad habits (like me, old ANSI C hack that I am) in the future.

So Me thinks a good design for this problem would be:

Grade - stores and calculates a students marks for an attempt at a particular subject (the model)

GradeTerminal - provides methods for the data entry and display of a Grade... it will have the Console and all your System.in and System.out calls. (the view)

GradeControler (or GradeDriver, it will create Grade's and call methods on the GradeTerminal to orcestrate data entry, and display. (the controler)

I fully realise that all sounds pretty complicated to you right now... but have a bit of a think about adding lists of subject attempts (grades), and students, and teachers, and keeping track of students progression through their respective courses of study... and lets make available on the web, and we'll need a nice fat client for data entry... and well the bigger and more complex (blah blah blah) the system gets the more important it is to disipline yourself to practicing these simple techniques.

I hope that's clear enough... if it isn't then just ignore me for the time being... when you're ready to study CS coz you're thinking you might want to do it for a living then come back and dig up this old post.

You really are going quit well you know... you allready ask good questions, and that's a big part of learning.

Cheers. Keith.

Message was edited by: corlettk - typos again

corlettka at 2007-7-21 21:48:33 > top of Java-index,Java Essentials,New To Java...
# 17
> // <-- text in file should be no wider than 80 characters so text doesn't wrap> //when printed in portrait format -->Poppycock. Don't print source code. Trees can't read it.
corlettka at 2007-7-21 21:48:33 > top of Java-index,Java Essentials,New To Java...
# 18

> INSTANCE VARIABLE

Is a toffy nosed way of saying "an attribute"

> MUTATOR METHOD

Is a toffy nosed way of saying "a setter"

> ACCESSOR METHOD

Is a toffy nosed way of saying "a getter"

for exampleclass Student

{

// a private attribute

private int studentId;

//a public mutator

public setStudentId(int studentId) {this.studentId = studentId; }

//a public accessor

public getStudentId() {return this.studentId; }

// default (no-args) constructor

public Student() {}

// comprehensive (all-args) constructor

// this is increasing considered a poor design by the way,

// because it needs changing allmost every time the class does

// and (as classes grow) it means passing a long list of ordinal args

// which is cumbersome, and therefore tends to be buggy)

public Student(int studentId, String name, .....) {

this.setStudentId(studentId);

this.setName(name);

.....

}

.....

}

corlettka at 2007-7-21 21:48:33 > top of Java-index,Java Essentials,New To Java...
# 19

I do appreciate your input. I will be honest and say it sounds very complex, but I know that its better to have no habits and learn the right way then to have poor ones and try to break them. I will most definately keep those ideas in mind and will refer to that train of thought more often.

I'm not sure I have any mutators or accessors since none of my code uses the set/get commands. Our prof. has an example online, but I'm not sure where I would need it in mine, since it has covered the scope of the scenario given to us.

I see the attributes, that is straight forward thanks to your explanation. I will keep looking into this, to refine it and keep learning. Thank you.

eristica at 2007-7-21 21:48:33 > top of Java-index,Java Essentials,New To Java...