The data fields A private String data field for the students

The data fields: A private String data field for the student\'s name A private String data field for the student\'s V_number A private real(double) data field for the student\'s GPA The constructors and other methods (9 methods in total): A default constructor that sets the name to \"John Doe\", the V_number to \"V00000000\", and the GPA to 4.0 A constructor that accepts the student\'s name, V_number, and GPA. These values should be assigned to the object\'s data fields (use \"this\" key word) Accessor (get) and mutator (set) methods for student\'s name, V_number, and GPA (6 methods). In the setGAP method, use an if...else to ensure the value is valid (0 to 4.0) toString method that returns the values of all the data fields Draw the Unified Modeling Language (UML) diagram for the Student class. Here is an example of UML (\'+\' means public, \'-\' means privative, and underline for the static field) Implement the Student class. Write a test program to create a Student object with no-arg constructor and display all the data fields in the object (name, V_number. and GPA). create another Student object with arg constructor and display (using toString method) the data fields. Then invokes mutator method to change the GPA twice (use invalid and valid number), and displays the data fields in the object. The name of student #1 is John Doe The V_number of student #1 is V00000000 The gpa of student #1 is 4.0 Student #2 is name: Linda Smith; V_number: V00123456; gpa 2.8 GPA must be between 0.0 and 4.0. Value not changed. Student #2 is name: Linda Smith; V_number: V00123456; gpa: 3.2 Press any key to continue...

Solution

UML

class Student   
- private String studentName;
- private String V_Number;
- private double GPA;
+ public Student()    }
+ public Student(String studentName,String V_Number,double GPA)   
+public void setStudentName(String name) //set methods
+public void setV_Number(String V)
+ public void setGPA(double gpa)
+ public String getStudentname()   
+ public String getV_Number()
+ public double getGPA()
+ public String toString()

import java.util.*;
import java.lang.*;
import java.io.*;

class Student   
{
   private String studentName;
   private String V_Number;
   private double GPA;
   public Student() //default constructor
   {
       this.studentName=\"John Doe\";
       this.V_Number=\"V00000000\";
       this.GPA=4.0;
   }
   public Student(String studentName,String V_Number,double GPA) parameterized constructor
   {
       this.studentName = studentName;
       this.V_Number = V_Number;
       this.GPA = GPA;
   }
   public void setStudentName(String name) //set methods
   {
       studentName=name;
   }
   public void setV_Number(String V)
   {
       V_Number = V;
   }
   public void setGPA(double gpa)
   {
       if(gpa >0 && gpa<4) //check for GPA range
       GPA=gpa;
       else
       System.out.println(\"GPA must be between 0.0 and 4.0\");
   }
   public String getStudentname() //get methods
   {
       return studentName;
   }
   public String getV_Number()
   {
       return V_Number;
      
   }
   public double getGPA()
   {
       return GPA;
   }
   public String toString() // toString() method
{
return studentName+\" \"+V_Number+\" \"+GPA;
}
}

class TestStudent
{
   public static void main (String[] args) throws java.lang.Exception
   {
       Student s1 = new Student();
       System.out.println(\"The name of the student s1 is \" +s1.getStudentname());
       System.out.println(\"The V_Number of the student s1 is \" +s1.getV_Number());
       System.out.println(\"The GPA of the student s1 is \"+ s1.getGPA());
      
       Student s2 =new Student(\"Linda Smith\",\"V00123456\",2.8);
       System.out.println(\"Student s2 is \" +s2.toString());
      
       s2.setGPA(-3.1);
       s2.setGPA(3.2);
       System.out.println(\"Student s2 is \" +s2.toString());
      
   }
}

output:

Success time: 0.05 memory: 711168 signal:0

 The data fields: A private String data field for the student\'s name A private String data field for the student\'s V_number A private real(double) data field
 The data fields: A private String data field for the student\'s name A private String data field for the student\'s V_number A private real(double) data field

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site