0

I have this code so far:

//Carolina Dominguez
//Assignment 7

public class Student
{
    private String fname;
    private String lname;
    private String studentId;
    private double gpa;

    public Student(String studentFname,String studentLname,String stuId,double studentGpa)
    {
        fname     = studentFname;
        lname     = studentLname;
        studentId = stuId;
        gpa       = studentGpa;
    }

    public double getGpa()
    {
        return gpa;
    }

    public String getStudentId()
    {
        return studentId;
    }

    public String getName()
    {
        String name = lname + ", " + fname;
        return name;
    }
}

Now I need to create a mutator method that lets the client change the gpa. It must verify the gpa is in the range 0.0 to 4.0 (inclusive). If it is not valid, display an error message indicating the problem and end the program.

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by moutanna
0

Your mutator sould look like:

public void setGpa(double studentGpa)
{
if((studentGpa>=0.0) && (studentGpa<=4.0) {
//gpa is in the range 0.0 to 4.0 (inclusive).
gpa=studentGpa;
}else{
//display an error message 
System.out.println("Value out of range!");
//end the program
System.exit(0);
}
}

Hope it helps.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.