Sunteți pe pagina 1din 6

2010-CE-061

MOIZ AHMED SIDDIQUI

LAB 08
TASK 01
Develop a registration system for a University. It should consist of three classes namely Student, Teacher, and Course. For our example, a student needs to have a name, roll number, address and GPA to be eligible for registration. Therefore choose appropriate data types for encapsulating these properties in a Student object/s. Similarly a teacher needs to have name, address, telephone number, and a degree (or a list of degrees) he has received. Finally courses must have a name, students (5 students) registered for the course, and a teacher assigned to conduct the course. Create an object of Course with 5 Students and a Teacher. A call to a method, say printDetails(), of the selected course reference should print name of the course, details of the teacher assigned to that course, and names and roll numbers of the students enrolled with the course.

CODE
class Student { String name; int rollNo; String add; double gpa; Student ( String name, int rollNo, String add, double gpa) { this.name = name; this.rollNo = rollNo; this.add = add; this.gpa = gpa; } } class Teacher { String name; String add; int tel; String deg; Teacher ( String name, String add, int tel, String deg ) { this.name = name; this.add = add; this.tel = tel; this.deg = deg; } } class Course {

24

2010-CE-061

MOIZ AHMED SIDDIQUI

String name; Student std[] = new Student[5]; Teacher teach; Course ( String name, Teacher teach, Student std[] ) { this.name = name; this.std = std; this.teach = teach; } void printDetails() { System.out.println("Course Name: "+this.name); System.out.println("Course Teacher: "+this.teach.name); for ( int loop = 0 ; loop < 5 ; loop++ ) { System.out.println((loop+1)+". "+this.std[loop].name+" ("+this.std[loop].rollNo+")"); } }} public class Prog29 { public static void main(String[] args) { Student std[] = new Student[5]; std[0] = new Student("Moiz",136,"Johar Mor",2.9); std[1] = new Student("Ali",66,"Gulshan",2.8); std[2] = new Student("Saad",176,"Eid Gah",2.86); std[3] = new Student("Faisal",172,"Anna Mor",2.6); std[4] = new Student("Mujtaba",177,"Scheme33",2.8); Teacher teach = new Teacher( "Sir Khurram", "R-431, Gulistna-e-Jogar", 34567892, "M.S in Computer Engg" ); Course c = new Course( "OOP", teach, std ); c.printDetails(); }}

OUTPUT

25

2010-CE-061

MOIZ AHMED SIDDIQUI

TASK 02
Create an inheritance hierarchy of Vehicle: Car, Bus and Truck, with the SchoolBus further extending the hierarchy through the Bus. Each of the derived class objects should be packaging information about category and passenger carrying capacity of that vehicle. Provide a method that is common to all derived classes, such as getPassengerCapacity (), with class Truck having an additional method called getLoadingCapacity () that should be returning that truck's loading capacity in tons. Create objects of all the derived classes in a test class and store the references in an array (4 Elements) of type Vehicle, the top-level class of this hierarchy. Provide logic so that at every run of the program any one of the four references stored in the array is randomly selected and the methods of that object are called polymorphically. Every random access must result in display of category and passenger carrying capacity of that particular vehicle, except in the case of a Truck object, where the call must result in the display of category, passenger carrying capacity and the cargo loading capacity.

CODE
import java.util.Random; //This is the class that will be inherited and this all variables and methods will be in all inherited classes! class Vehicle { public int doors; public int seats; public int wheels; Vehicle() { wheels=4; doors=4; seats=4; } public void print() {} int getPassengerCapacity(){ return seats; }} //This class inherits Vehicle.java class Car extends Vehicle { public void print() { System.out.println("This car has "+seats+" Seats, "+doors+" Doors "+ "and "+wheels+" wheels"); // Changing print method for Car class } }

26

2010-CE-061

MOIZ AHMED SIDDIQUI

//This class inherits Vehicle.java class Bus extends Vehicle { Bus(){ doors=2; seats=50; } public void print() { System.out.println("This Bus has "+seats+" Seats, "+doors+" Doors "+ "and "+wheels+" wheels"); // Changing print method for Bus class } } //This class inherits Vehicle.java class Truck extends Vehicle { int capacity= 500; Truck(){ // Changing values of doors, seats using default constructor of TRUCK class doors=2; seats=2; } int getLoadingCapacity(){ // Unique method only for truck class return capacity; } public void print() { System.out.println("This Truck has "+seats+" Seats, "+doors+" Doors, "+ +wheels+" wheels "); // Changing print method for Truck class } } // Using interface because we cannot apply multiple inheritance but can use multiple interfaces // Changing values of doors, seats using default constructor of BUS class

interface veh {

27

2010-CE-061 public int doors=4; public int seats=4; public int wheels=4; public void print(); public int getPassengerCapacity(); } interface vehbus extends veh { public int seats=50; public int wheels=4; } interface schvehbus extends vehbus { public String schname="SSUET";

MOIZ AHMED SIDDIQUI

} class schoolbus implements schvehbus { public void print() { System.out.println("School Bus of " + schname +" has "+seats+" Seats, "+doors+" Doors "+ "and "+wheels+" wheels"); } public int getPassengerCapacity(){ return seats; } } //This class tests the classes that inherit Vehicle.java and schoolbus.java (implemented through interfaces) class vt { public static void main(String args[]) {

Vehicle poly[] = new Vehicle[3]; poly[0]= new Car(); // Same as Vehicle poly[0] = new Car() OR Car poly[0] = new Car() poly[1]= new Bus(); poly[2]= new Truck();

28

2010-CE-061 Random number = new Random(); int a= number.nextInt(3); // Generates Random Number from 0 to 2

MOIZ AHMED SIDDIQUI

if(a==0||a==1) { poly[a].print(); int b= poly[a].getPassengerCapacity(); System.out.println("Passenger Capacity is " + b); } else if(a==2) { poly[a].print(); int b= poly[a].getPassengerCapacity(); System.out.println("Passenger Capacity is " + b); Truck moiztruck = new Truck(); // Creating obj of Truck because getLoadingCapacity is not in Vehicle int ttruckloadcap= moiztruck.getLoadingCapacity(); System.out.println("Loading Capacity of Truck is " + ttruckloadcap + " tons"); } }}

OUTPUT

29

S-ar putea să vă placă și