CS204 Object Oriented Programming and Design Patterns
Lab
Assignment 7
Thursday, 20 May 2021
Name: Ajit Kumar
Roll Number: BTECH/15163/19
Branch: IT
Semester: IV
Objective: To understand and the concept of Abstract classes and Interfaces
1. Create an abstract class Compartment to represent a rail coach. Provide an
abstract function notice in this class. Derive FirtClass, Ladies, General,
Luggage classes from the compartment class. Override the notice function
in each of them to print notice suitable to the type of the compartment.
Create a class TetCompartment. Write main function to do the following:
Declare an array of Compartment of size 10. Create a compartment of a
type as decided by a randomly generated integer in the range 1 to 4. Check
the polymorphic behaviour of the notice method.
Ans: Code of Program:
//OPD Lab Assignment 7A
import java.util.*;
abstract class Compartment {
public abstract void notice();
}
class FirstClass extends Compartment {
@Override
public void notice() {
System.out.println("Notice: You are in FirstClass");
}
}
class General extends Compartment {
@Override
public void notice() {
System.out.println("Notice: You are in General");
}
}
class Ladies extends Compartment {
@Override
public void notice() {
System.out.println("Notice: You are in Ladies");
}
}
class Luggage extends Compartment {
@Override
public void notice() {
System.out.println("Notice: You are in Luggage");
}
}
public class labAssignment7A {
public static void main(String[] args) {
Compartment[] compartments = new Compartment[10];
Random rand = new Random();
for (int i = 0; i < 10; i++) {
int randomNum = rand.nextInt(4) + 1;
if (randomNum == 1)
compartments[i] = new FirstClass();
else if (randomNum == 2)
compartments[i] = new Ladies();
else if (randomNum == 3)
compartments[i] = new General();
else if (randomNum == 4)
compartments[i] = new Luggage();
compartments[i].notice();
}
}
}
Output:
2. Write a program in java which implement interface Student which has two
methods Display_Grade and Attendance for PG_Students and UG_Students
(PG_Students and UG_Students are two different classes for Post Graduate
and Under Graduate Students respectively).
Ans: Code of Program:
//OPD Lab Assignment 7B
interface Student {
void Display_Grade();
void Display_Atten();
}
class PG_Student implements Student {
String name, grade;
int m1, m2, m3, attendance, total;
PG_Student(String name, int m1, int m2, int m3, int
attendance) {
this.name = name;
this.m1 = m1;
this.m2 = m2;
this.m3 = m3;
this.attendance = attendance;
}
void Display() {
System.out.println("Name is " + name);
System.out.println("Marks are " + m1 + " " + m2 + " " +
m3);
}
public void Display_Atten() {
System.out.println("The attendance is " + attendance);
}
public void Display_Grade() {
total = m1 + m2 + m3;
if (total > 240)
grade = "A";
else if (total > 180)
grade = "B";
else if (total > 120)
grade = "C";
else
grade = "D";
System.out.println("The Grade is " + grade);
}
}
class UG_Student implements Student {
String name, grade;
int m1, m2, m3, attendance, total;
UG_Student(String name, int m1, int m2, int m3, int
attendance) {
this.name = name;
this.m1 = m1;
this.m2 = m2;
this.m3 = m3;
this.attendance = attendance;
}
void Display() {
System.out.println("Name is " + name);
System.out.println("Marks are " + m1 + " " + m2 + " " +
m3);
}
public void Display_Atten() {
System.out.println("The attendance is " + attendance);
}
public void Display_Grade() {
total = m1 + m2 + m3;
if (total > 240)
grade = "A";
else if (total > 180)
grade = "B";
else if (total > 120)
grade = "C";
else
grade = "D";
System.out.println("The Grade is " + grade);
}
}
public class labAssignment7B {
public static void main(String[] args) {
PG_Student pg = new PG_Student("Ajit", 80, 70, 88, 46);
pg.Display();
pg.Display_Atten();
pg.Display_Grade();
UG_Student ug = new UG_Student("Kumar", 65, 75, 90, 40);
ug.Display();
ug.Display_Atten();
ug.Display_Grade();
}
}
Output: