Ad

LAB TASK 5 (CONSTRUCTOR)


Task 2: Make a class Student, which has following attributes:
1.      ID
2.      Name
3.      CGPA
4.      UniversityName
Now create a method displayInformation(), which prints id, name, cgpa and university name of students. You also have to create parameterized constructor. Access modifier for all attribute is private.
Create a test class StudentTest and instantiate four student objects with following name, Ali, Ahmad, Tooba and Fatima and for each object call displayInformation() method.
Note: No need to write code for extra getters and setters.


public class Student1
{
private String id;
private String name;
private double cgpa;
static String universityName = "COMSATS";

public void displayInformation(String id,String name,double cgpa)
{
this.id = id;
this.name = name;
this.cgpa = cgpa;
System.out.println("ID: " +id+ " Name: " +name+ " CGPA: " +cgpa+ " University: " +universityName);
}
public Student1(String id,String name)
{
this.id = id;
this.name = name;

System.out.println(id+ " is student of " +universityName);

}
}

COPY THIS CODE THEN PASTE IN NEW FILE:

public class StudentTest
{
public static void main(String args[])
{
System.out.println("IDs of students enrolled in oop:\n");
Student1 obj1 = new Student1("SP19-BSE-046","Usman Hashmi");
Student1 obj2 = new Student1("SP19-BSE-346"," Hashmi");
Student1 obj3 = new Student1("SP19-BSE-246","moeen");
Student1 obj4 = new Student1("SP19-BSE-146","Habib");
obj1.displayInformation("SP19-BSE-046","Usman Hashmi",3.00);
obj2.displayInformation("SP19-BSE-346"," Hashmi",3.90);
obj3.displayInformation("SP19-BSE-246","moeen",3.50);
obj4.displayInformation("SP19-BSE-146","Habib",3.60);



}
}

OUTPUT:





Task 4: Make a class Employee. Employee class has one static variable count (initialize it to zero), and two attributes firstName and lastName. All attributes are private. (Variable count maintains a count of the number of object of class Employee). Create a parameterized constructor. In this constructor, you have to increment the count and print the information of employee. Create a static method int employyeCount() which returns count of employees.

Create a test class with EmployeeTest. In main method, instantiate two objects (obj1, obj2) of employees. You have to print number of employees before instantiation and after instantiation. Also, print the first name of both employees.
Note: No need to create extra getters and setters.

Post a Comment

0 Comments