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

"R-431. Student std[] = new Student[5]."Scheme33". }} OUTPUT 25 .teach = teach.66.86). std[4] = new Student("Mujtaba".name+" ("+this.println("Course Teacher: "+this.8). Gulistna-e-Jogar".177.rollNo+")").176.8). loop++ ) { System.name = name.2.2.out. "M. Course ( String name.2010-CE-061 MOIZ AHMED SIDDIQUI String name. "+this."Johar Mor".printDetails().println("Course Name: "+this.teach.std = std.S in Computer Engg" ).std[loop]. Course c = new Course( "OOP".2.out.std[loop]. for ( int loop = 0 .9). std[3] = new Student("Faisal". std[2] = new Student("Saad"."Gulshan".name).172.out.6). } void printDetails() { System. std[0] = new Student("Moiz". Teacher teach = new Teacher( "Sir Khurram".name). } }} public class Prog29 { public static void main(String[] args) { Student std[] = new Student[5]. loop < 5 . System."Eid Gah". this. std ).println((loop+1)+". this. std[1] = new Student("Ali". teach. Teacher teach. c. Teacher teach. 34567892.2.136.2. Student std[] ) { this."Anna Mor".

where the call must result in the display of category.2010-CE-061 MOIZ AHMED SIDDIQUI TASK 02 Create an inheritance hierarchy of Vehicle: Car. the top-level class of this hierarchy. //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 wheels. Each of the derived class objects should be packaging information about category and passenger carrying capacity of that vehicle. Bus and Truck. CODE import java.out. passenger carrying capacity and the cargo loading capacity. Provide a method that is common to all derived classes.util. Create objects of all the derived classes in a test class and store the references in an array (4 Elements) of type Vehicle. 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. with the SchoolBus further extending the hierarchy through the Bus. }} //This class inherits Vehicle. doors=4. with class Truck having an additional method called getLoadingCapacity () that should be returning that truck's loading capacity in tons. Every random access must result in display of category and passenger carrying capacity of that particular vehicle. such as getPassengerCapacity (). // Changing print method for Car class } } 26 . public int seats. seats=4. } public void print() {} int getPassengerCapacity(){ return seats. except in the case of a Truck object.Random.java class Car extends Vehicle { public void print() { System. Vehicle() { wheels=4. "+doors+" Doors "+ "and "+wheels+" wheels").println("This car has "+seats+" Seats.

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

poly[2]= new Truck(). public int wheels=4. 28 .java and schoolbus. MOIZ AHMED SIDDIQUI } class schoolbus implements schvehbus { public void print() { System. } public int getPassengerCapacity(){ return seats. public int seats=4. } interface schvehbus extends vehbus { public String schname="SSUET". // Same as Vehicle poly[0] = new Car() OR Car poly[0] = new Car() poly[1]= new Bus().out. poly[0]= new Car(). public void print(). public int wheels=4. } } //This class tests the classes that inherit Vehicle.java (implemented through interfaces) class vt { public static void main(String args[]) { Vehicle poly[] = new Vehicle[3]. } interface vehbus extends veh { public int seats=50.println("School Bus of " + schname +" has "+seats+" Seats.2010-CE-061 public int doors=4. "+doors+" Doors "+ "and "+wheels+" wheels"). public int getPassengerCapacity().

} }} OUTPUT 29 .getPassengerCapacity().getLoadingCapacity(). } else if(a==2) { poly[a].out. int b= poly[a].getPassengerCapacity().out.2010-CE-061 Random number = new Random().println("Passenger Capacity is " + b). int a= number.nextInt(3).out. System.print(). // Creating obj of Truck because getLoadingCapacity is not in Vehicle int ttruckloadcap= moiztruck. System.print(). int b= poly[a].println("Loading Capacity of Truck is " + ttruckloadcap + " tons"). // Generates Random Number from 0 to 2 MOIZ AHMED SIDDIQUI if(a==0||a==1) { poly[a]. System. Truck moiztruck = new Truck().println("Passenger Capacity is " + b).

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.