Tracking Grades
A teacher wants a program to keep track of grades for students and decides to create a student class for his program as follows:

Each student will be described by three pieces of data: his/her name, his/her score on test #1, and his/her score on test #2.

There will be one constructor, which will have one argument—the name of the student.

There will be three methods:
getName, which will return the student's name;

inputGrades, which will prompt for and read in the student's test grades;

and getAverage, which will compute and return the student's average.

Below you will find an incomplete definition for the Student class. Create it as and save it to your directory. Complete the class definition as follows:

Declare the instance data (name, score for test1, and score for test2). Create a Scanner object for reading in the scores. Add the missing method headers. Add the missing method bodies.

Create file (see below). It contains a driver program that declares two Student objects. Save it to your directory and use the inputGrades method to read in each student's test scores, then use the getAverage method to find their average. Print the average with the student's name, e.g.,
"The average for Joe is 87."
You can use the getName method to print the student's name.
Add statements to your Grades program that print the values of your Student variables directly, e.g.:

System.out.println("Student1: " + student1);
This should compile, but notice what it does when you run it—nothing very useful! When an object is printed, Java looks for a toString method for that object. This method must have no parameters and must return a String. If such a method has been defined for this object, it is called and the string it returns is printed. Otherwise the default toString method, which is inherited from the Object class, is called; it simply returns a unique hexadecimal identifier for the object such as the ones you saw above. Add a toString method to your Student class that returns a string containing the student's name and test scores, e.g.:
Name: Joe Test1: 85 Test2: 91
Note that the toString method does not call System.out.println—it just returns a string. Recompile your Student class and the Grades program (you shouldn't have to change the Grades program—you don't have to call toString explicitly). Now see what happens when you print a student object—much nicer!
// ****************************************************************
// Define a student class that stores name, score on test 1, and
// score on test 2. Methods prompt for and read in grades,
// compute the average, and return a string containing student’s info.
// ****************************************************************
import java.util.Scanner;
public class Student
//declare instance data
public Student(String studentName)
//add body of constructor
//inputGrades: prompt for and read in student's grades for test1 and test2.
//Use name in prompts, e.g., "Enter's Joe's score for test1".
public void inputGrades()
//add body of inputGrades
//getAverage: compute and return the student's test average
//add header for getAverage
//add body of getAverage
//getName: print the student's name
//add header for printName
//add body of printName

// ****************************************************************
// Use Student class to get test grades for two students
// and compute averages
// ****************************************************************

public class Grades
public static void main(String[] args)
Student student1 = new Student("Mary");
//create student2, "Mike"
//input grades for Mary
//print average for Mary
//input grades for Mike
//print average for Mike

Representing Names
Write a class Name that stores a person's first, middle, and last names and provides the following methods:

public Name(String first, String middle, String last)—constructor. The name should be stored in the case given; don't convert to all upper or lower case.

public String getFirst()—returns the first name

public String getMiddle()—returns the middle name

public String getLast()—returns the last name

public String firstMiddleLast()—returns a string containing the person's full name in order, e.g., "Mary Jane Smith".

public String lastFirstMiddle()—returns a string containing the person's full name with the last name first followed by a comma, e.g., "Smith, Mary Jane".

public boolean equals(Name otherName)—returns true if this name is the same as otherName. Comparisons should not be case sensitive. (Hint: There is a String method equalsIgnoreCase that is just like the String method equals except it does not consider case in doing its comparison.)

public String initials()—returns the person's initials (a 3-character string). The initials should be all in upper case, regardless of what case the name was entered in. (Hint: Instead of using charAt, use the substring method of String to get a string containing only the first letter—then you can upcase this one-letter string.

public int length()—returns the total number of characters in the full name, not including spaces.

Now write a program that prompts for and reads in two names from the user (you'll need first, middle, and last for each), creates a Name object for each, and uses the methods of the Name class to do the following:

For each name, print

first-middle-last version

last-first-middle version


Tell whether or not the names are the same.
Rational Number
Develop a class Rational, that represents rational numbers internally by two integers for nominator and denominator. The class should provide the following methods:

public Rational(int nominator, int denominator) - constructor. Creates a rational number for the representation of nominator/ denominator
public double getValue() - returns the value of the rational number as a double
public int getNominator() - returns the nominator
public int getDenominator() - returns the denominator
public Rational add(Rational op2) - returns a rational number that is the sum of the rational number itself and the operand op2
You have to mantain your rational numbers always in the most simple form. In order to achieve this, you will implement the following two helper methods:

private static int gcd(int num1, int num2) - it returns the greatest common divisor of integers num1 and num2
private void simplify() - it reduces an object of type Rational by dividing both nominator and denominator by their greatest common divisor
Write a program fragment (as a static method of some class TestRational), that takes two rational numbers as an imput from the user and prints their sum. In order to make this nicely, you will implement a toString method for class Rational, that produces a nice output of rational numbers in several lines, e.g. 1/3 is printed as:


This article has been dead for over six months. Start a new discussion instead.