0% found this document useful (0 votes)
5 views

CSE111-Lab-Assignment-7-Fall24

This document outlines Lab Assignment 07 for the course CSE111, focusing on Inheritance and Overriding in programming. It consists of 11 tasks that require students to implement various classes and methods to produce specified outputs using driver code. Students must submit coding tasks via Google Form and handwritten tracing tasks to their lab instructors.

Uploaded by

Fairah Asma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

CSE111-Lab-Assignment-7-Fall24

This document outlines Lab Assignment 07 for the course CSE111, focusing on Inheritance and Overriding in programming. It consists of 11 tasks that require students to implement various classes and methods to produce specified outputs using driver code. Students must submit coding tasks via Google Form and handwritten tracing tasks to their lab instructors.

Uploaded by

Fairah Asma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Lab Assignment 07

Course Code: CSE111

Course Title: Programming Language II

Topic: Inheritance and Overriding

Number of Tasks: 11

[Submit all the Coding Tasks (Task 1 to 8) in the Google Form shared on buX
before the next lab. Submit the Tracing Tasks (Task 9 to 11) handwritten to your
Lab Instructors at the beginning of the lab]
[You are not allowed to change the driver codes of any of the tasks]

Task 1
Given the following classes, write the code for the BBAStudent class so that
the following output is printed when we run the TestStudent class.

Driver Code and Parent Class Output

public class Student{ Name: Default Department: BBA


private String name = "Just a Student"; 1---------------
private String department = "nothing"; Name: Humty Dumty Department: BBA
2---------------
public void setDepartment(String dpt){ Name: Little Bo Peep Department: BBA
this.department = dpt;
}
public void setName(String name){
this.name = name;
}
public void details(){
System.out.println("Name : " + name + "
Department: " + department);
}
}

//Tester Class
public class TestStudent{
public static void main(String [] args){
BBAStudent b1 = new BBAStudent();
BBAStudent b2 = new BBAStudent("Humty Dumty");
BBAStudent b3 = new BBAStudent("Little Bo
Peep");
b1.details();
System.out.println("1---------------");
b2.details();
System.out.println("2---------------");
b3.details();
}
}
Task 2
Design the CheckingAccount class derived from the Account class with
appropriate attributes and properties so that the driver code can generate
the output given below.

Driver Code and Parent Class Output


public class Account{ Total Checking Accounts: 0
public double balance = 0.0; Account Balance: 0.0
Account Balance: 100.0
public Account(double balance){ Account Balance: 200.0
this.balance = balance; Total Checking Accounts: 3
}
public double showBalance(){
return balance;
}
}

//Tester Class
public class TestAccount{
public static void main(String [] args){
System.out.println("Total Checking Accounts:
"+CheckingAccount.count);
CheckingAccount c1 = new CheckingAccount();
System.out.println("Account Balance: " + c1.showBalance());
CheckingAccount c2 = new CheckingAccount(100.0);
System.out.println("Account Balance: " + c2.showBalance());
CheckingAccount c3 = new CheckingAccount(200.0);
System.out.println("Account Balance: " + c3.showBalance());
System.out.println("Total Checking Accounts:
"+CheckingAccount.count);
}
}
Task 3
Design the Dog and Cat class derived from the Animal class with appropriate
attributes and properties so that the driver code can generate the output given
below.

Driver Code and Parent Class Output

public class Animal { 1.========


public String name; Name: Buddy
public int age; Age: 5
public String color; Color: Brown
public Animal(String name, int age, String color) { Breed: Bulldog
this.name = name; 2.========
this.age = age; Name: Kitty
this.color = color; Age: 3
} Color: White
public void makeSound() { Breed: Persian
System.out.println("Animal makes a sound"); 3.========
} Brown color Buddy is barking
public String info() { 4.========
return "Name: "+name+"\nAge: "+age+"\nColor: White color Kitty is meowing
"+color+"\n";
}
}

public class AnimalTester {


public static void main(String[] args) {
Dog dog = new Dog("Buddy", 5, "Brown", "Bulldog");
Cat cat = new Cat("Kitty", 3, "White", "Persian");
System.out.println("1.========");
System.out.println(dog.info());
System.out.println("2.========");
System.out.println(cat.info());
System.out.println("3.========");
dog.makeSound();
System.out.println("4.========");
cat.makeSound();
}
}
Task 4
Given the following classes, write the code for the Vehicle2010 class to
print the following output when we run the Vehicle2010User class.

Driver Code and Parent Class Output


public class Vehicle{ (0,0)
public int x; (-1,-1)
public int y; (0,0)
(1,1)
public void moveUp(){ (2,0)
y = y+1;
}
public void moveDown(){
y = y-1;
}
public void moveLeft(){
x = x-1;
}
public void moveRight(){
x = x+1;
}
public String toString(){
return "("+ x + ","+ y + ")";
}
}

//Tester Class
public class Vehicle2010User{
public static void main(String[] args){
Vehicle2010 car1 = new Vehicle2010();
System.out.println(car1);
car1.moveLowerLeft();
System.out.println(car1);

Vehicle2010 car2 = new Vehicle2010();


System.out.println(car2);
car2.moveUpperRight();
System.out.println(car2);
car2.moveLowerRight();
System.out.println(car2);
}
}
Task 5
Design the ComplexNumber class with the necessary property to produce the output from
the given driver code.

Driver Code and Parent Class Output


public class RealNumber { RealPart: 1.0
public double realValue; ImaginaryPart: 1.0
public RealNumber() { ----------------
this(0.0); RealPart: 5.0
} ImaginaryPart: 7.0
public RealNumber(double realValue) {
this.realValue = realValue;
}
public String toString(){
return "RealPart: " + realValue;
}

}
public class ComplexNumberTester {
public static void main(String[] args) {
ComplexNumber cn1 = new ComplexNumber();
System.out.println(cn1);
System.out.println("----------------");
ComplexNumber cn2 = new ComplexNumber(5.0, 7.0);
System.out.println(cn2);
}
}

Task 6
Design the Manager and Developer class derived from the Employee class with
appropriate attributes and properties so that the driver code can generate
the output given below. [Hint:
Manager:
1. Adds a bonus to the base salary if the manager works more than 40 hours.
2. If the manager works more than 100 hours, the full amount is approved; if they
work more than 80 hours, half the amount is approved. Otherwise, the increment
is denied.
Developer:
1. Adds $700 to the base salary if the developer works with Java programming
language.]
Driver Code and Parent Class Output

public class Employee { 1.==========


public String name; Name: Neymar
private double baseSalary; Base Salary: $1000.0
private int hoursWorked; Work Hours: 45
Bonus: 10.0 %
public Employee(String name, double baseSalary, int Final Salary: $1100.0
hoursWorked){ 2.==========
this.name = name; Increment denied.
this.baseSalary = baseSalary; 3.==========
this.hoursWorked = hoursWorked; $50 Increment approved.
} 4.==========
public double getBaseSalary() { 5.==========
return baseSalary; Name: Neymar
} Base Salary: $1050.0
public void setBaseSalary(double baseSalary) { Work Hours: 85
this.baseSalary = baseSalary; Bonus: 10.0 %
} Final Salary: $1155.0
public int getHoursWorked() { 6.==========
return hoursWorked; 7.==========
} Name: Messi
public void setHoursWorked(int hoursWorked) { Base Salary: $1000.0
this.hoursWorked = hoursWorked; Work Hours: 50
} Language: Java
public void displayInfo() { Final Salary: $1700.0
System.out.println("Name: " + name); 8.==========
System.out.println("Base Salary: $" + baseSalary); Name: Chiesa
System.out.println("Work Hours: " + hoursWorked); Base Salary: $1000.0
} Work Hours: 50
} Language: Javascript
Final Salary: $1000.0
public class EmployeeTester {
public static void main(String[] args) {
Manager neymar = new Manager("Neymar",1000, 45, 10);
Developer messi = new Developer("Messi",1000,50,"Java");
Developer chiesa = new Developer("Chiesa", 1000, 50,
"Javascript");
neymar.calculateSalary();
System.out.println("1.==========");
neymar.displayInfo();
System.out.println("2.==========");
neymar.requestIncrement(100);
System.out.println("3.==========");
neymar.setHoursWorked(85);
neymar.requestIncrement(100);
System.out.println("4.==========");
neymar.calculateSalary();
System.out.println("5.==========");
neymar.displayInfo();
System.out.println("6.==========");
messi.calculateSalary();
System.out.println("7.==========");
messi.displayInfo();
System.out.println("8.==========");
chiesa.calculateSalary();
System.out.println("9.==========");
chiesa.displayInfo();
}
}

Task 7
Design the CinemexTicket class derived from the MovieTicket Class so that the given
output is produced:
❖ The seatTypes and seatPrices arrays contain the type of the seat and its
corresponding price
❖ Night show charge (15% of ticket price) will be applicable if the time is
between 6:00 PM - 11:00 PM
❖ Unique id for a ticket is generated by:
MovieName-FirstLetterOfSeatType-TicketCount
❖ You may need to use .split() and Integer.parseInt() built-in methods

Driver Code and Parent Class Output


public class MovieTicket { Total movie ticket(s): 1
public static String [] seatTypes = {"Regular", "Premium", 1============================
"IMAX 3D"}; Ticket price is calculated
public static double [] seatPrices = {300.0, 450.0, 600.0}; successfully.
public static int nightShowCharge = 15; 2============================
private String movie; Ticket ID: Deadpool and
public String showtime; Wolverine-R-1
public String date; Movie: Deadpool and Wolverine
private double price; Showtime: 18:30
public String seat; Date: July 24, 2024
Genre: Action-Comedy
public MovieTicket(String movie, String date, String showtime, Seat Type: Regular
double price) { Price(tk): 345.0
this.movie = movie; Status: Not Paid
this.showtime = showtime; 3============================
this.date = date; Payment Successful.
this.price = price; 4============================
this.seat = "Not Selected"; Ticket ID: Deadpool and
} Wolverine-R-1
public void setPrice(double price) { Movie: Deadpool and Wolverine
this.price = price; Showtime: 18:30
} Date: July 24, 2024
public double getPrice() { Genre: Action-Comedy
return price; Seat Type: Regular
} Price(tk): 345.0
public String getMovie() { Status: Paid
return movie; 5============================
} Total movie ticket(s): 2
6============================
public String toString() { Ticket price is calculated
return "Movie: " + movie + "\nShowtime: " + showtime + successfully.
"\nDate: " + date; 7============================
} Payment Successful.
} 8============================
Ticket ID: Twisters-P-2
//Driver Code Movie: Twisters
public class Tester { Showtime: 10:00
public static void main(String[] args) { Date: August 10, 2024
CinemexTicket ticket1 = new CinemexTicket("Deadpool and Genre: Sci-Fi
Wolverine", "18:30", "Action-Comedy", "July 24, 2024"); Seat Type: Premium
System.out.println("Total movie ticket(s): " + Price(tk): 450.0
CinemexTicket.getTotalTickets()); Status: Paid
System.out.println("1============================"); 9============================
ticket1.calculateTicketPrice(); Ticket price is already paid!
System.out.println("2============================");
System.out.println(ticket1);
System.out.println("3============================");
System.out.println(ticket1.confirmPayment());
System.out.println("4============================");
System.out.println(ticket1);
System.out.println("5============================");
CinemexTicket ticket2 = new CinemexTicket("Twisters", "10:00",
"Sci-Fi", "August 10, 2024", "Premium");
System.out.println("Total movie ticket(s): " +
CinemexTicket.getTotalTickets());
System.out.println("6============================");
ticket2.calculateTicketPrice();
System.out.println("7============================");
System.out.println(ticket2.confirmPayment());
System.out.println("8============================");
System.out.println(ticket2);
System.out.println("9============================");
System.out.println(ticket2.confirmPayment());
}
}
Task 8
Design the KKTea (parent) and KKFlavouredTea (child) classes so that the following
output is produced. The KKFlavouredTea class should inherit KKTea and KKTea should
inherit the Tea class. Note that:
● An object of either class represents a single box of teabags.
● Each tea bag weighs 2 grams.
● The status of an object refers to whether it is sold or not

Driver Code and Parent Class Output


public class Tea { --------1---------
public String name; Name: KK Regular Tea, Price: 250
public int price; Status: false
public boolean status; Weight: 100, Tea Bags: 50
--------2---------
public Tea(String name, int price) { Total Sales: 0
this.name = name; KK Regular Tea: 0
this.price = price; --------3---------
this.status = false; --------4---------
} Name: KK Regular Tea, Price: 470
Status: true
public void productDetail() { Weight: 200, Tea Bags: 100
System.out.println("Name: " + name + ", Price: " + --------5---------
price); Total Sales: 2
System.out.println("Status: " + status); KK Regular Tea: 2
} --------6---------
} --------7---------
//Driver Code Name: KK Jasmine Tea, Price: 260
public class TeaTester{ Status: false
public static void main(String[] args) { Weight: 100, Tea Bags: 50
KKTea t1 = new KKTea(250, 50); --------8---------
System.out.println("--------1---------"); Name: KK Honey Lemon Tea, Price: 270
t1.productDetail(); Status: false
System.out.println("--------2---------"); Weight: 90, Tea Bags: 45
KKTea.totalSales(); --------9---------
System.out.println("--------3---------"); --------10---------
KKTea t2 = new KKTea(470, 100); Total Sales: 5
KKTea t3 = new KKTea(360, 75); KK Regular Tea: 2
KKTea.updateSoldStatusRegular(t1); KK Flavoured Tea: 3
KKTea.updateSoldStatusRegular(t2);
System.out.println("--------4---------");
t2.productDetail();
System.out.println("--------5---------");
KKTea.totalSales();
System.out.println("--------6---------");
KKFlavouredTea t4 = new KKFlavouredTea("Jasmine", 260, 50);
KKFlavouredTea t5 = new KKFlavouredTea("Honey Lemon", 270,
45);
KKFlavouredTea t6 = new KKFlavouredTea("Honey Lemon", 270,
45);
System.out.println("--------7---------");
t4.productDetail();
System.out.println("--------8---------");
t6.productDetail();
System.out.println("--------9---------");
KKFlavouredTea.updateSoldStatusFlavoured(t4);
KKFlavouredTea.updateSoldStatusFlavoured(t5);
KKFlavouredTea.updateSoldStatusFlavoured(t6);
System.out.println("--------10---------");
KKTea.totalSales();
}
}
Task 9
1 public class A{
2 public int temp = 4;
3 public int sum = 1;
4 public int y = 2;
5 public A(){
6 y = temp - 2;
7 sum = temp + 3;
8 temp-=2;
9 }
10 public void methodA(int m, int n){
11 int x = 0;
12 y = y + m + (temp++);
13 x = x + 2 + n;
14 sum = sum + x + y;
15 System.out.println(x + " " + y+ " " + sum);
16 }
17 }
18 public class B extends A {
19 public int x;
20 public B(){
21 y = temp + 3 ;
22 sum = 3 + temp + 2;
23 temp-=1;
24 }
25 public B(B b){
26 sum = b.sum;
27 x = b.x;
28 }
29 public void methodB(int m, int n){
30 int y =0;
31 y = y + this.y;
32 x = this.y + 2 + temp;
33 methodA(x, y);
34 sum = x + y + super.sum;
35 System.out.println(x + " " + y+ " " + sum);
36 }
37 }

A a1 = new A(); x y sum


B b1 = new B();
B b2 = new B(b1);
a1.methodA(1, 1);
b1.methodA(1, 2);
b2.methodB(3, 2);
Task 10
1 public class A{
2 public static int temp = 10;
3 public int sum = 1;
4 public int y = 2, x = 11;
5 public A(){
6 y = temp - 2;
7 sum = temp + 3;
8 temp-=2;
9 }
10 public void methodA(int m, int n){
11 int x = 0;
12 y = y + m + (this.temp++);
13 x = x + 2 + n;
14 sum = sum + x + y;
15 System.out.println(x + " " + y+ " " + sum);
16 }
17 }
18 public class B extends A{
19 public static int x = 7;
20 public B(){
21 temp = temp + 3 ;
22 sum = 3 + temp + 2 + sum;
23 super.temp-=1;
24 }
25 public B(B b){
26 sum = b.sum;
27 x = b.x;
28 }
29 public void methodB(int m, int n){
30 int y =0;
31 y = y + this.y;
32 x = this.y + 2 + temp;
33 methodA(x, y);
34 sum = x + y + super.sum;
35 System.out.println(x + " " + y+ " " + sum);
36 }
37 }

A a1 = new A(); x y sum


B b1 = new B();
B b2 = new B(b1);
a1.methodA(1, 1);
b1.methodA(1, 2);
b2.methodB(3, 2);
Task 11
1 public class A{
2 public static int temp = 3;
3 public int sum;
4 public int y;
5 public A(){
6 y = temp - 1;
7 sum = temp + 2;
8 temp-=2;
9 }
10 public void methodA(int m, int [] n){
11 int x = 0;
12 y = y + m + (temp++);
13 x = x + 2 + (++n[0]);
14 sum = sum + x + y;
15 n[0] = sum + 2;
16 System.out.println(x + " " + y+ " " + sum);
17 }
18 }
19 class B extends A {
20 public static int x = 1;
21 public B(){
22 y = temp + 1 ;
23 x = 3 + temp + x;
24 temp-=2;
25 }
26 public B(B b){
27 sum = b.sum + super.sum;
28 x = b.x + x;
29 }
30 public void methodB(int m, int n){
31 int [] y = {0};
32 super.y = y[0] + this.y + m;
33 x = super.y + 2 + temp - n;
34 methodA(x, y);
35 sum = x + y[0] + super.sum;
36 System.out.println(x + " " + y[0]+ " " + sum);
37 }
38 }
int x[] = {23};
A a1 = new A();
B b1 = new B();
B b2 = new B(b1);
a1.methodA(1, x);
b2.methodB(3, 2);
a1.methodA(1, x);
Ungraded Tasks (Optional)
(You don’t have to submit the ungraded tasks)

Task 1

Design the class Dog so that the desired outputs are generated properly.
Driver Code and Parent Class Expected Output

public class AnimalTester{ 1-------------


public static void main(String args[]){ Legs: 4
Animal a1 = new Animal(); Sound: Not defined
System.out.println("1-------------"); 2-------------
a1.details(); The dog says hello!
System.out.println("2-------------"); 3-------------
Dog d1 = new Dog(); Name: Pammy
d1.name = "Pammy"; Legs: 4
System.out.println("3-------------"); Sound: Not defined
System.out.println("Name: " + d1.getName()); 4-------------
d1.details(); 5-------------
System.out.println("4-------------"); Legs: 4
d1.updateSound("Bark"); Sound: Bark
System.out.println("5-------------");
d1.details();
}
}

public class Animal{


public int legs = 4;
public String sound = "Not defined";

public void details(){


System.out.println("Legs: "+legs);
System.out.println("Sound: "+sound);
}
}

Task 2
Design the ScienceExam class with the necessary property to produce the
output from the given driver code.

Driver Code Output


public class Exam { Marks: 100 Time: 90 minutes Number of Parts: 4
public int marks; ---------------------
public int time; Maths, English, Physics, HigherMaths
Part 1 - Maths
public Exam(int marks) { Part 2 - English
this.marks = marks; Part 3 - Physics
this.time = 60; Part 4 - HigherMaths
}
public String examSyllabus() { =====================
return "Maths, English"; Marks: 100 Time: 120 minutes Number of Parts: 5
} ---------------------
public String examParts() { Maths, English, Physics, HigherMaths, Drawing
return "Part 1 - Maths\nPart 2 - Part 1 - Maths
English\n"; Part 2 - English
} Part 3 - Physics
} Part 4 - HigherMaths
Part 5 - Drawing
//Tester Class
public class ExamTester {
public static void main(String[] args) {
ScienceExam ex1 = new ScienceExam(100, 90,
"Physics", "HigherMaths");
System.out.println(ex1);
System.out.println("---------------------");
System.out.println(ex1.examSyllabus());
System.out.println(ex1.examParts());
System.out.println("=====================");
ScienceExam ex2 = new ScienceExam(100, 120,
"Physics", "HigherMaths", "Drawing");
System.out.println(ex2);
System.out.println("---------------------");
System.out.println(ex2.examSyllabus());
System.out.println(ex2.examParts());
}
}
Task 3
1 public class A {
2 public static int temp = 4;
3 public static int x = -10;
4 public int sum = 0;
5 public int y = 0;
7 public A() {
8 y = temp - 2;
9 sum = temp + 1;
10 temp -= 2;
11 }
13 public void methodA(int m, int n) {
14 int x = 0;
15 y = y + m + (temp++);
16 x = x + 1 + n;
17 sum = sum + x + y;
18 System.out.println(x + " " + y + " " + sum);
19 }
20 }
22 public class B extends A {
23 public static int x = 0;
24 public int sum = -6;
25 public B() {
26 super();
27 sum = 0;
28 y = temp + 3;
29 super.sum = 3 + temp + 2;
30 temp -= 2;
31 }
33 public B(B b) {
34 super();
35 if (b == null) {
36 y = temp + 3;
37 sum = 3 + temp + 2;
38 temp -= 2;
39 } else {
40 sum = b.sum + super.sum;
41 x = b.x;
42 b.methodB(2, 3);
43 }
44 }
46 public void methodB(int m, int n) {
45 int y = 0;
46 y = y + this.y;
47 x = y + 2 + (++temp);
48 methodA(x, y);
49 sum = x + y + sum;
50 System.out.println(x + " " + y + " " + sum);
51 }
52 }

Write the output of the following code:

public class Tester { Output:


public static void main(String[] args) {
A a1 = new A(); x y sum
B b1 = new B();
B b2 = new B(b1);
b1.methodA(2, 3);
b2.methodB(3, 8);
}
}

You might also like