10. [6 Points] Code Segment: Objects class has the A University wishes to keep i
ID: 3881529 • Letter: 1
Question
10. [6 Points] Code Segment: Objects class has the A University wishes to keep information on its students. The proposed Student following instance variables: studentNo: String studentName: String dateofBirth: Date tariffPoints: Integer Tariff Points represents the entry qualification achieved by a student, which is a number between 20 and 280. A class variable is also re Student instance is created quired, called noorstudents, which will be incremented each time a a. (4 Points] Decl are two constructors as follows; both constructors should increment the class variable appropriately: The first is a default constructor that has no parameters and sets the instance variables to either "not known" for the strings, 20 for the integer and 1st January 1995 for the date (assume there is a Date constructor that accepts dates in a string format). I. Il. The second takes 4 parameters, one for each of the instance variables [2 Points] Show an example (Java statements) how both constructors can be used to instantiate an object. b.Explanation / Answer
/*The whole answer is well commented so please create Student class and copy whole code
* In this class i will create four instance variable and one static variable
*
* I will use static variable to count object instances or numbers of students
* I am using static variable to count because the static variable belong to class
* not to the object, So we use static variable to define something common among all
* object of the class
*
* Sample output: Number of students: 2
* */
//Java program to create student info
import java.util.*; //to include Date
public class Student {
//Declaring instance variables
String studentNo;
String studentName;
Date dateOfBirth;
int tariffPoints;
//Declaring static class
static int noOfStudents;
//Default constructor to setting default values
public Student()
{
//Incrementing noOfStudents
noOfStudents ++;
studentNo="not known";
studentName="not known";
tariffPoints=10;
dateOfBirth=new Date(1,1,1995);
}
//parameterised constructor that takes 4 parameters and sets all
public Student(String studentNo,String studentName,int tariffPoint,Date dateOfBirth)
{
//Incrementing noOfStudent
noOfStudents++;
//this is used to reference current object
this.studentNo=studentName;
this.studentName=studentName;
this.tariffPoints=tariffPoint;
this.dateOfBirth=dateOfBirth;
}
public static void main(String[] args) {
//I am going to create two instance of Student
//with default values
Student student1=new Student();
//with passing values
Student student2=new Student("S1123","Alice",21,new Date(2,3,1993));
System.out.println("Number of students: "+noOfStudents);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.