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

CSE111 Lab Assignment 9 - Summer'24

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

CSE111 Lab Assignment 9 - Summer'24

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Lab Assignment 09

Course Code: CSE111

Course Title: Programming Language II

Topic: Inheritance & Method Overriding + Review

Number of Tasks: 11
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 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 updateDepartment(String dpt){ Name : Little Bo Peep Department: BBA
this.department = dpt;
}
public void updateName(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

Given the following classes, write the code for the Vehicle2010 class to
print the following output when we run the Vehicle2010User class.

Driver Code 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 void position(){
System.out.println("("+ x + ","+ y + ")");
}
}

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

Vehicle2010 car2 = new Vehicle2010();


car2.position();
car2.moveUpperRight();
car2.position();
car2.moveLowerRight();
car2.position();
}
}
Task 3
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 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 4
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 Output
public class Animal { 1.========
public String name; Name: Buddy
public int age; Age: 5
public String color; Color: Brown
Breed: Bulldog
2.========
public Animal(String name, int age, String color) { Name: Kitty
this.name = name; Age: 3
this.age = age; Color: White
this.color = color; Breed: Persian
} 3.========
Brown color Buddy is barking
public void makeSound() {
4.========
System.out.println("Animal makes a sound"); White color Kitty is meowing
}
public String info() {
return "Name: "+name+"\nAge: "+age+"\nColor: "+color+"\n";
}
}
//Tester Class
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 5

Implement the design of the Smartphone class so that the following


output is produced. For simplicity, assume that a smartphone can have a
maximum of 10 features.

Driver Code Output

public class SmartPhoneTester{ 1===================


public static void main(String[] args) { Feature can not be added without
Smartphone s1 = new Smartphone(); phone name
2===================
System.out.println("1===================");
Phone Name: Samsung Note 20
s1.addFeature("Display", "6.1 inch"); Display: 6.1 inch
System.out.println("2==================="); 3===================
s1.updateName("Samsung Note 20"); 4===================
s1.addFeature("Display", "6.1 inch"); Phone Name: Iphone 12 Pro
Display: 6.2 inch
s1.printDetail();
Ram: 6 GB
System.out.println("3==================="); 5===================
Smartphone s2 = new Smartphone("Iphone 12 Pro"); Phone Name: Iphone 12 Pro
s2.addFeature("Display", "6.2 inch"); Display: 6.2 inch, Amoled panel
s2.addFeature("Ram", "6 GB"); Ram: 6 GB, DDR5
System.out.println("4==================="); 6===================
s2.printDetail();
s2.addFeature("Display", "Amoled panel");
s2.addFeature("Ram", "DDR5");
System.out.println("5===================");
s2.printDetail();
System.out.println("6===================");
}
}
Task 6
Implement the Bus class so that the following output is produced.

Driver Code Output

public class BusTester{ Capacity: 4


public static void main(String args[]){ Destination: Jatrabari
Bus b1 = new Bus(4, "Jatrabari"); 1--------------
System.out.println("1--------------"); Capacity: 10
Bus b2 = new Bus(10, "Gazipur"); Destination: Gazipur
System.out.println("2--------------"); 2--------------
b1.addPassenger("Fahim", "Mirpur"); Sorry Fahim! The bus won't stop at
System.out.println("3--------------"); Mirpur
b1.addPassenger("Anika", "Jatrabari"); Use another bus.
System.out.println("4--------------"); 3--------------
b1.addPassenger("Ali"); Anika is added to the bus.
System.out.println("5--------------"); 4--------------
b1.addPassenger("Zafar"); Ali is added to the bus.
System.out.println("6--------------"); Ali will get off at the last stop
b1.addPassenger("Mim", "Badda"); 5--------------
b1.addPassenger("Nowrin"); Zafar is added to the bus.
System.out.println("7--------------"); Zafar will get off at the last stop
b1.addPassenger("Walid", "Jatrabari"); 6--------------
} Sorry Mim! The bus won't stop at Badda
} Use another bus.
Nowrin is added to the bus.
Nowrin will get off at the last stop
7--------------
Bus is full.
Task 7

Implement the design of the Account class so that the following output
is produced:

Driver Code Output

public class AccountTester{ Total account holders: 0


public static void main(String[] args) { 1================
System.out.println("Total account holders: " + Account.count); Name: Abdul
Age: 45
System.out.println("1================");
Occupation: Service Holder
Account p1 = new Account("Abdul",45,"Service Holder",500000); Total Amount: 800000
p1.addMoney(300000); 2================
p1.printDetails(); Name: Rahim
System.out.println("2================"); Age: 55
Occupation: Businessman
Account p2 = new Account("Rahim",55,"Businessman",700000);
Total Amount: 0
p2.withdrawMoney(700000); 3================
p2.printDetails(); Insufficient money for
System.out.println("3================"); withdrawal!
Account p3 = new Account("Ashraf",62,"Govt.Officer",200000); Name: Ashraf
p3.withdrawMoney(250000); Age: 62
Occupation: Govt.Officer
p3.printDetails();
Total Amount: 200000
System.out.println("4================"); 4================
System.out.println("Total account holders: " + Account.count); Total account holders: 3
}
}
Task 8
Implement the Student class so that the following output is produced.

Driver Code Output

public class StudentTester2{ Creating Student Number: 1


public static void main(String[] args) { 1----------------
Student s1 = new Student("Naruto", "CSE"); Naruto is from CSE department.
Serial of Naruto among all students' is: 1
System.out.println("1----------------");
Serial of Naruto in CSE department is: 1
s1.individualInfo(); ################
System.out.println("################"); Total Students: 1
Student.totalInfo(); Total CSE Students: 1
System.out.println("=================="); Total BBA Students: 0
==================
Student s2 = new Student("Sakura", "BBA");
Creating Student Number: 2
System.out.println("2----------------"); 2----------------
s2.individualInfo(); Sakura is from BBA department.
System.out.println("################"); Serial of Sakura among all students' is: 2
Student.totalInfo(); Serial of Sakura in BBA department is: 1
System.out.println("=================="); ################
Total Students: 2
Student s3 = new Student("Shikamaru", "CSE");
Total CSE Students: 1
System.out.println("3----------------"); Total BBA Students: 1
s3.individualInfo(); ==================
System.out.println("################"); Creating Student Number: 3
Student.totalInfo(); 3----------------
Shikamaru is from CSE department.
System.out.println("==================");
Serial of Shikamaru among all students' is: 3
Student s4 = new Student("Deidara", "BBA"); Serial of Shikamaru in CSE department is: 2
System.out.println("4----------------"); ################
s4.individualInfo(); Total Students: 3
System.out.println("################"); Total CSE Students: 2
Student.totalInfo(); Total BBA Students: 1
==================
}
Creating Student Number: 4
} 4----------------
Deidara is from BBA department.
Serial of Deidara among all students' is: 4
Serial of Deidara in BBA department is: 2
################
Total Students: 4
Total CSE Students: 2
Total BBA Students: 2
Task 9

1 public class Test1 {

2 int x = 2, y = 4, sum = 3;

3 int arr[] = {x, y, sum};

4 public void methodA(int x) {

5 arr[0] += methodB(y, this.x) + methodC(x);

6 System.out.println(x + " " + this.x + " " + sum);

7 arr[1] += this.x * (++y) / (sum % x);

8 System.out.println(y + " " + sum + " " + this.x);

9 arr[2] += methodC(x) + methodB(this.x, sum);

10 System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);

11 }

12 public int methodB(int q, int n) {

13 int arr2[] = {7, 8};

14 int a = (arr2[0]++) - q;

15 int b = (++arr2[1]) - n;

16 return a + b;

17 }

18 public int methodC(int z) {

19 z = sum + methodB(x, sum) - z;

20 return z/2;

21 }

22 }
public class Tester1{ Outputs
public static void main(String [] args){
Test1 t1 = new Test1();
t1.methodA(7);
}
}

Task 10

1 public class Test3 {


2 int x = 2, y = 4, z = 5;
3 double p = 0.0;
4 public void methodA(int x, int m) {
5 this.x = methodC(this.x);
6 p = x + this.x % m * 3.0;
7 y = y + methodB(x, this.x);
8 System.out.println(this.x +" " + x + y + " " + p) ;
9 }
10 public int methodB(int q, int n) {
11 int arr[] = {3,4,5};
12 arr[0] = arr[0] + this.x + n;
13 arr[1] = q + arr[1];
14 System.out.println(arr[0] +" " + arr[1] + " " + arr[2]) ;
15 return arr[1] + arr[2];
16 }
17 public int methodC(int y) {
18 if(y % 2 == 0) {
19 int temp = methodB(2, y);
20 return temp;
21 }
22 else{
23 return 4;
24 }
25 }
26 }

Driver Code Output


Test3 t3 = new Test3();
t3.methodA(2,3);
t3.methodB(5,4);

Task 11

1 public class Quiz3A{


2 public static int temp = 4;
3 public static int y;
4 public int sum;
5 public Quiz3A(){
6 int y = 7;
7 y = temp - 1;
8 sum = Quiz3A.temp + 1 + y;
9 temp+=2;
10 }
11 public Quiz3A(int k){
12 temp = temp++;
13 sum = ++temp + k;
14 Quiz3A.y = (sum++) - 1;
15 System.out.println(Quiz3A.y+" "+temp+" "+y);
16 }
17 public int methodB(int m, int n){
18 int x = 0;
19 y = this.y + m + (++temp);
20 x = x + 2 + n;
21 sum = sum + x + y;
22 System.out.println(x + " " + this.y+ " " + sum);
23 return sum;
24 }
25 }

Driver Code Output

public class Tester2{


public static void main(String args[]){
Quiz3A a1 = new Quiz3A();
a1.methodB(1,2);
Quiz3A a2 = new Quiz3A(3);
a2.methodB(2,4);
a1.methodB(2,1);
}
}

You might also like