Results 1 to 4 of 4
Thread: Matt
- 04-26-2010, 06:55 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
Matt
I need help on following homework.
Project Summary: Write a class to manage student grades and an application that uses it to produce a summary report and answer queries about grades.
Part 1
Write a class, called "Grades", to store a set of grades (each grade is a real value from 0 to 100).
The class should define (as part of the API unless stated otherwise):
A (private) array of integers to store the grades it receives.
A constructor that specifies the maximum number of grades it can hold.
A method called "addGrade" that receives a grade and stores it.
A method called "numGrades" that returns the number of grades added so far.
A function called "letterGrade" that takes a numeric grade and returns a character (A,B,C,D or F) representing the corresponding letter grade (on a ten point scale). (e.g. 90-100:A, 80-90:B, etc)
A method called "howMany" that takes a letter grade and returns the number of students that earned that letter grade.
A toString method that returns a string containing all the numeric grades, formatted nicely as you see fit. (you may find this method handy for debugging).
Notes:
None of the methods in your class should do any input from a file or the keyboard, or do any output the the display.
When instantiating an array, its size doesn't have to be a literal or constant; it may be a variable or an expression.
The index used for an array reference may be an expression.
Methods in your class may call other methods in the class.
(e.g. letterGrade may be called whenver you need to convert from a numeric to letter grade.)
You don't need to declare or use more than one array.
Only two of the methods above require a loop; no nested loops are required.
Part 2
Write an application using your class.
Part 2A: Read and output the grades
Your application should read a sequence numeric grades from a file named "quizgrades.txt" and store the grades in an instance of your class (using the addGrade() method). You should read as many grades as the file contains. As your read in each grade, print it along with its corresponding letter grade.
For example, quizgrades.txt might contain:
75 90 83
95
20 48
and your output might look like this:
75 C
90 A
83 B
95 A
20 F
48 F
Only one loop is necessary; don't write more than one for this part.
After reading in and outputting the grades, use the numGrades() method of the instance to output the number of grades entered, and use the toString() method of the instance to output the contents of the instance.
For example:
6 Grades: 75 90 83 95 20 48
Part 2B: Answer queries about grades
Repeatedly prompt the user for a letter grade. For each grade entered, report to the user how many students earned that letter grade (use the howMany() method). Stop when the user enters no grade. Windows allows you to terminate the input with a CTRL-Z as the first character on a line followed by Enter; Linux uses CTRL-D anywhere without reqiring enter. Instead, you may use a sentinal to stop (See text Section 6.3.1 Reading Data from the User [page 284]).
For example, the interaction might look like this:
Which grade do you want to see> F
F occured 2 times
Which grade do you want to see> B
F occured 1 times
Which grade do you want to see> D
D occured 0 times
Which grade do you want to see>
Operation Completed
As you are writing the program, make sure your program uses correct indentation and defines meaningful variable names.
What I have done so far is:
import java.util.Scanner;
import java.io.File;
class Grades {
private int[] grade;
int []grades = new int [4];
public Grades(int maxGrades){
}
public void addGrade(int grade){
}
public void numGrades(){
}
public String letterGrade(int gradeValue){
String gradeLetter="F";
if(gradeValue>=90 && gradeValue<=100){
gradeLetter = "A";
}
if(gradeValue>=80 && gradeValue<90){
gradeLetter = "B";
}
if(gradeValue>=70 && gradeValue<80){
gradeLetter = "C";
}
if(gradeValue>=60 && gradeValue<70){
gradeLetter = "D";
}
if(gradeValue>=50 && gradeValue<60){
gradeLetter = "E";
}
return gradeLetter;
}
public int howMany(String gradeInLetter){
int noOfStudents=0;
noOfStudents++;
return noOfStudents;
}
public String toString(){
return ;
}
}
public class Homework {
public static void main (String[]args) throws Exception {
File inputFile = new File( "quizgrades.txt" );
Scanner scan = new Scanner( inputFile ) ;
int []grade = new int [6];
grade[0] = scan.nextInt();
grade[1] = scan.nextInt();
grade[2] = scan.nextInt();
grade[3] = scan.nextInt();
grade[4] = scan.nextInt();
grade[5] = scan.nextInt();
for( int i=0; i < grade.length; i ++){
System.out.println (i + grade[i] );
}
}
}
-
- 04-26-2010, 09:16 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
Did some progress but looks all messy. Is there anybody to help this newbiee.
import java.util.Scanner;
import java.io.File;
class Grades {
private int[] grade;;
public Grades(int maxGrades){
grade = new int[maxGrades];
}
public void addGrade(int grade){
int x ;
grade [x] = grade;
x++;
}
public void numGrades(){
}
public static char letterGrade(int gradeValue){
char gradeLetter='F';
if(gradeValue>=90 && gradeValue<=100){
gradeLetter = 'A';
}
if(gradeValue>=80 && gradeValue<90){
gradeLetter = 'B';
}
if(gradeValue>=70 && gradeValue<80){
gradeLetter = 'C';
}
if(gradeValue>=60 && gradeValue<70){
gradeLetter = 'D';
}
if(gradeValue>=50 && gradeValue<60){
gradeLetter = 'E';
}
return gradeLetter;
}
public int howMany(char gradeInLetter){
int y = 0;
for (x = 0; x < maxGrade[]; x++)
if (letterGrade.x = gradeLetter)
y++;
return y;
}
public String toString(){
String s;
while(s+= grade[x] + grades);
return s;
}
}
public class Bhusal_hw5 {
public static void main (String[]args) throws Exception {
File inputFile = new File( "quizgrades.txt" );
Scanner scan = new Scanner( inputFile ) ;
Grades grade = new Grades(20);
while ( scan.hasNext( ) ){
int quizGrade = scan.nextInt();
grade.addGrade( quizGrade);
Grades.letterGrade( quizGrade);
System.out.println(grade + Grades.letterGrade);
}
Scanner scan = new Scanner (System.in);
Systtem.out.println("Which grade do you want to see> ");
while(scan.hasNext())
grades.howMany(letterGrade);
System.out.println ("F occured" + grades.howMany(letterGrade));
System.out.println("Operation completed");
}
}
- 04-26-2010, 10:11 PM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
You were asked to use CODE tags. Doing so will go a long way toward encouraging us to attempt to read your code and help you. Also take the time to format and indent your code properly. It is a sign of respect to those you are asking to look at your code, it helps you to think clearly, and it gives you another opportunity to read and perhaps debug your code.
Make your class public.Java Code:import java.util.Scanner; import java.io.File; class Grades {
No bonus points for extra ;s. Also, variable names for arrays and other collections should be plural. Programming is confusing enough without adding to it unnecessarily.Java Code:private int[] grade;;
You have a problem here. Every time this method is called, a new int x will be created on the stack and implicitly initialized to 0 (because you did not explicitly initialize it). That means you are always assigning to grade[0]. You increment x after the assignment, but then when you exit the method, x is thrown away. Also, you are confusing matters for yourself by naming the int parameter with the same name as your int array. It compiles and runs, because the array is an instance variable, and the compiler recognizes that, but it's confusing to a reader of your code.Java Code:public Grades(int maxGrades){ grade = new int[maxGrades]; } public void addGrade(int grade){ int x ; grade [x] = grade; x++; }
Didn't get to this yet?Java Code:public void numGrades(){ }
Use "else" and you don't have to repeat all these comparisons, simplifying your code. Your specification does not call for assigning any E grades. It also says that there won't be any grades over 100, but if there are, do you really think they should get an F? That's what your code does now.Java Code:public static char letterGrade(int gradeValue){ char gradeLetter='F'; if(gradeValue>=90 && gradeValue<=100){ gradeLetter = 'A'; } if(gradeValue>=80 && gradeValue<90){ gradeLetter = 'B'; } if(gradeValue>=70 && gradeValue<80){ gradeLetter = 'C'; } if(gradeValue>=60 && gradeValue<70){ gradeLetter = 'D'; } if(gradeValue>=50 && gradeValue<60){ gradeLetter = 'E'; } return gradeLetter; }
This coding style is legal, but a very bad habit. Use {s or keep everything on one line. The code itself, apart from the style, makes no sense. What is letterGrade.x? What is gradeLetter? Why are you trying to assign gradeLetter to letterGrade.x (whatever that is)?Java Code:public int howMany(char gradeInLetter){ int y = 0; for (x = 0; x < maxGrade[]; x++) if (letterGrade.x = gradeLetter) y++;
Huh? Where did x come from? This is just very confused. Go back to your text and your notes and think this through.Java Code:return y; } public String toString(){ String s; while(s+= grade[x] + grades);
Java Code:return s; } }You are calling the letterGrade() method, but throwing away its result. The next line makes no sense -- there is no public static letterGrade variable in the Grades class, and you don't want one. Your grade variable is your whole Grades object, so this next line will invoke its toString() method. I don't think that's what you want here.Java Code:public class Bhusal_hw5 { public static void main (String[] args) throws Exception { File inputFile = new File( "quizgrades.txt" ); Scanner scan = new Scanner( inputFile ) ; Grades grade = new Grades(20); while ( scan.hasNext( ) ){ int quizGrade = scan.nextInt(); grade.addGrade( quizGrade); Grades.letterGrade( quizGrade);
Again, bad style, and this next line is definitely not what you want.Java Code:System.out.println(grade + Grades.letterGrade); } Scanner scan = new Scanner (System.in); Systtem.out.println("Which grade do you want to see> "); while(scan.hasNext()) grades.howMany(letterGrade);
That gives you plenty to tackle for now. Report back and let's see how you do.Java Code:System.out.println ("F occured" + grades.howMany(letterGrade)); System.out.println("Operation completed"); } }
-Gary-


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks