I. Write a java source code for a class named student. Use the java keyword this in
Question:
I. Write a java source code for a class named student. Use the java keyword this in its TWO forms of usage to refer to the referenced objectg; and to call a constructor from another constructor everywhere you can. Make sure all the data field members (data fields)are private.
1. The class Student represents a typical student(in the form of a Student's Java object). Write the Student class that is briefly described below:
A university student object, an instance (object) of Student class, has the following:
-state(expressed via its private data fields) can be defined as the student's name, address, major, and GPA,
-behaviour(expressed via its methods) is to update the address and to recalculate the GPA.
1) The student data fields name, address, major are EACH of String data type; the GPA data field is a double data type.
2) Write two constructors. Use the Java this keyword to call one of the constructors from the other. The first constructor takes a name, address,and major. It also should have a call to computeGPA() method(see below how) to set the GPA
3) The second constructor only takes as formal parameters a name and address. Use this Java keyword to call the first constructor. For the major parameter of the first constructor, use the String value"undeclared".
4)Implement set methods(also called mutators, setters) for the data fields' instance variables:name, address, and major, i.e. do not implement set method for the GPA datat field[Since it is calculated/set by the computeGPA() method
5) Implement get methods(also called accessors, getters) for all of the instance variables(i.e., incl. for the GPA)
6) Write the computeGPA() method. In it, to calculate the GPA, generate and return a random double between 0.5 and 4.0 [Here, use the random() method of the Math class(import it in the begining of the program with import java.lang.Math directive)]
7) Include a toString() method to return the output for each student in the following format(notice, this is just an example):
.When you prepare the format of the GPA number, make sure that you hae only 2 digits after the decimal point- for that purpose, to acheive it, make sure that you write inside the toString() method the following formatting method: String.format(" %4, 2f", GPA) to format properly the value of the GPA from the respective object.
II . Test your Student class as follows;
1. Once you have completed writing the Student class, write a short test program(typically called driver program) that create a few(3 or 4) students and exercises the various class methods. name the test program TestStudent.java
a. Create 4 student objects for 4 students-name the objects: student1, student2, student3, and student4
b.Print them:using println(student1)[etc., for the other 3 students]; Notice: println(student1) will automatically call the toString() method of the object student1 as you defined it within the class Student, described above in II.(7).
C. Change the 4 students objects: Call some of the set methods and the computeGPA() method on some of the Student objects[e.g., set the new major of one student, set the new address for another, re-compute the GPA for a third student, etc.]
d. Print again each of the student objects( to show/demonstrate the change); again, using println(student1), [for the other 3 students], and again, it will automatically call the toString() of the object student1 as you defined it within the class Student descibed above in(7).
Business Statistics a decision making approach
ISBN: 978-0133021844
9th edition
Authors: David F. Groebner, Patrick W. Shannon, Phillip C. Fry