Question by Joel C: Java coding question using Constructor?
I am taking a an online Java Programming class using “Java Programming” by Joyce Farrell. I am trying to figure out the coding on an exercise from Chapter 3 and the instructor is not responding so I wanted to see if someone here had the answer.
We just learned constructors and we are supposed to be writing a class called student with fields for ID Number, number of credit hours, number of points earned, and grade point average (GPA). I can figure out how to create the methods to assign values to all the fields, but GPA needs to equal points earned divided by credit hours. So far, GPA has always returned a double “0.0” or a “NaN” and I have tried using the get/set methods and everything I can think of. I am not looking for a specific code as much as guidance as to how to go about coding it since I have tried everything but still want to figure it out myself.

Here is the question as written in the book:
“Create a class named Student. A Student has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. A Student also has a field for grade point average. Include a method to compute the grade point average field by dividing points by credit hours earned. Write methods to display the values in each Student field. Save this class as”

Thank you for your help!

Best answer:

Answer by Frank
Based on what I’ve seen other students do, I’m guessing that in the constructor you are calculating the GPA and storing it in a field. That is incorrect.

Define a method to calculate the GPA on the fly. It does not store the GPA in a field for use later. It calculates and returns the GPA using the current field values for credit hours and points. This method does not get called in the constructor. It gets called later, after the other values have been set.

What do you think? Answer below!

2 thoughts on “Java coding question using Constructor?”

  1. if your return value is 0.0, maybe one of your local variables is a Int not a double? if not, use unary cast operator to force the gpa to return a double

    GPA = (double) Grade point earned/ credit hours

  2. A constructor is different from a method. When I do…
    Car myCar = new Car();
    the ‘new Car()’ is invoking the constructor of the class and putting it as an Object on the JRE heap.

    A class may have more than one constructor.
    Car yourCar = new Car( 4 );
    /// the class or blueprint we are using

    public class Car {
    int numWheels;

    // constructor(s)
    public Car() {
    numWheels = 44;

    public Car( int num ) {
    numWheels = num;
    // the methods are for when this OBJECT is on the heap.
    // accesor method
    public int getNumWheels() {
    return numWheels;
    // mutator method
    public void setNumWheels( int newNum ) {
    numWheels = newNum;

    The methods, we use when the Objects are running in memory. We would use a main program class and make Objects

    class TestCars {
    Car myCar;
    Car yourCar;
    public static void main( String [] args ) {
    myCar = new Car();
    yourCar = new Car( 4 );

    yourCar.setNumWheels( 16 );
    System.out.println( “Your Car now has: ” + yourCar.numWheels + ” wheels”);

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.