prototype0628
prototype0628

Reputation: 23

Java - object can't store data

My dad and I narrowed it down to the object can't store the data. also, I'm new to java. The code is supposed to print in the console the first name, last name, grade, and grade average. I think the problem is in public double getCalcGraeAverage() but correct me if wrong, please.

import java.util.Scanner;
/**
 * Write a description of class Student here.
 * 
 * @author XXXX
 * @version XXXX
 */
public class Student
{
    String firstName;
    String lastName;
    int gradeLevel;
    double gradeAverage;
    int totalAssignments;
    double totalPoints;

      /**
     * Create a new student with student's name, grade, and average
     */
    public Student(String newFirstName, String newLastName, int newGradeLevel, double newGradeAverage)
    {
        firstName = newFirstName;
        lastName = newLastName;
        gradeLevel = newGradeLevel;
        gradeAverage = newGradeAverage = 0.0;
    }

      /**
     * Return the student's first name. 
     */
    public String getFirstName()
    {
        return firstName;
    }

      /**
     * Return the student's last name. 
     */
    public String getLastName()
    {
        return lastName;
    }

      /**
     * Return the grade level of the student. 
     */
    public int getGradeLevel()
    {
        return gradeLevel;
    }

         /**
     * Calculates grade average. 
     */
    public double getCalcGradeAverage()
    {

       double gradeAverage = totalAssignments / totalPoints;
       return gradeAverage;
    }

    public static void main (String[] args)
    { 
        Student student1 = new Student ("XXXX", "XXXX", 11, 0.0);

        System.out.println("The student's first name is: " + student1.getFirstName());
        System.out.println("The student's last name is: " + student1.getLastName());
        System.out.println("The student's grade level is: " + student1.getGradeLevel());

        System.out.println("Please enter the total assignment points the student has earned: ");

        Scanner input = new Scanner(System.in);
        Double totalAssignments  = input.nextDouble();

        System.out.println("Please enter the number of assignments given: ");

        double totalpoints  = input.nextDouble();

        System.out.println(student1.getFirstName() + " " + student1.getLastName() +  " average grade is" + student1.getCalcGradeAverage());
    }
}

Upvotes: 1

Views: 71

Answers (2)

Enjy
Enjy

Reputation: 255

When writing your code, you declare variables for the student class with class scope.

int totalAssignments;
double totalPoints;

those class scope variable are used in the method :getCalcGradeAverage() totalAssignments of Student and totalPoints of student are used in this method

When you create a new Student those variable are equals to zero because not affected by a value in your constructor.

in the main method when you writes :

Double totalAssignments  =

you declare a new variable named "totalAssignments" with a method scope.When the method ends, the variable reference goes away and there is no way to access that variable any longer. you can consider that the variable decalred is not the same that the student variable: student.totalAssignments is always equals to zero because no value affected to him.

Then assuming that you can do that :

student1.totalAssignments  = input.nextInt();
student1.totalPoints  = input.nextDouble();

Upvotes: 0

Manos Nikolaidis
Manos Nikolaidis

Reputation: 22224

In your code you are :

  1. creating a Student student1 object
  2. reading totalAssignments, totalpoints from System.in
  3. calling student1.getCalcGradeAverage()

between steps 2 and 3 you have to set the fields totalAssignments, totalpoints of student to the values you read or they will retain their default values of zero. E.g.

student1.totalAssignments = totalAssignments;
student1.totalpoints = totalpoints;

Also, since totalAssignments is of type int, you probably want to read it as:

int totalAssignments  = input.nextInt();

Upvotes: 1

Related Questions