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

CSE111 Lab Assignment 6 - Fall'24

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

CSE111 Lab Assignment 6 - Fall'24

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

Lab Assignment 06

Course Code: CSE111

Course Title: Programming Language II

Topic: Encapsulation, Static variable and Static Method

Number of Tasks: 11

[Submit all the Coding and Tracing Tasks (Task 1 to 11) in the Google Form
shared on buX]
[You are not allowed to change the driver codes of any of the tasks]

Task 1
Write the “Product” class to show the following output
Note: Make sure to use proper Encapsulation concepts for the setter & getter methods.
All the attributes should have Private access.

Driver Code Output

public class ProductTester{ < —--—----1—-------->


public static void main(String[] args) { Product Name: Unknown
System.out.println("< —--—----1—-------->"); Price: $0.0
Product product1 = new Product(); < —--—----2—-------->
product1.displayInfo(); Product Name: Laptop
System.out.println(); Price: $1200.0
System.out.println("< —--—----2—-------->"); Quantity: 10
Product product2 = new Product("Laptop", 1200.00); < —--—----3—-------->
product2.setQuantity(10); Retrieved Price: $1200.0
product2.displayInfo(true); Retrieved Quantity: 10
System.out.println("< —--—----3—-------->");
System.out.println("Retrieved Price: $" + product2.getPrice());
System.out.println("Retrieved Quantity: " + product2.getQuantity());
}
}
Task 2
Design the Passenger class in such a way that the following code provides the
expected output.
● Passenger class has two static variables no_of_passenger and total_fare.
● Each passenger has to pay 20 TK/Distance and extra 10 TK/BaggageWeight.
Given Code Expected Output

public class PassengerTester{ Total Passenger: 0


public static void main(String args[]){ Total Fare: 0.0 TK
System.out.println("Total Passenger: "+ Passenger.no_of_passenger); ==========1==========
System.out.println("Total Fare: "+ Passenger.total_fare + " TK"); Name: Lara
System.out.println("==========1=========="); Fare: 112.0 TK
Passenger p1 = new Passenger("Lara", 5.6); ==========2==========
p1.passengerDetails(); Name: Kevin
System.out.println("==========2=========="); Fare: 268.0 TK
Passenger p2 = new Passenger("Kevin", 10.0); ==========3==========
p2.storeBaggageWeight(6.8); Name: Robin
p2.passengerDetails(); Fare: 96.0 TK
System.out.println("==========3=========="); ==========4==========
Passenger p3 = new Passenger("Robin", 2.3); Total Passenger: 3
p3.storeBaggageWeight(5.0); Total Fare: 476.0 TK
p3.passengerDetails();
System.out.println("==========4==========");
System.out.println("Total Passenger: "+ Passenger.no_of_passenger);
System.out.println("Total Fare: "+ Passenger.total_fare + " TK");
}
}
Task 3
Design a Book class in such a way that the following code provides the expected
output.
● The Book class has two static variables: total_books_sold and total_revenue.
● Each book has a base price of 150 TK. If the discountPercentage is applied,
the book's price is reduced by that percentage.
● The Book class should have a method to calculate the price after the discount

Given Code Expected Output

public class BookTester { Total Books Sold: 0


public static void main(String[] args) { Total Revenue: 0.0 TK
System.out.println("Total Books Sold: " + Book.total_books_sold); ==========1==========
System.out.println("Total Revenue: "+Book.total_revenue + " TK"); Title: Java Programming
System.out.println("==========1=========="); Price after Discount: 135.0 TK
==========2==========
Book b1 = new Book("Java Programming", 10); // 10% discount Title: Python Programming
b1.bookDetails(); Price after Discount: 127.5 TK
==========3==========
System.out.println("==========2=========="); Title: Data Structures
Price after Discount: 142.5 TK
Book b2 = new Book("Python Programming", 15); // 15% discount ==========4==========
b2.bookDetails(); Total Books Sold: 3
Total Revenue: 405.0 TK
System.out.println("==========3==========");

Book b3 = new Book("Data Structures", 5); // 5% discount


b3.bookDetails();

System.out.println("==========4==========");
System.out.println("Total Books Sold: " + Book.total_books_sold);
System.out.println("Total Revenue: "+Book.total_revenue + " TK");
}
}
Task 4
Write a class called Circle with the required constructor and methods to get the
following output.
Subtasks:
1. Create a class called Circle.
2. Create the required constructor. Use Encapsulation to protect the variables.
[Hint: Assign the radius variable in private]
3. Create getRadius() and setRadius() method to access variables.
4. Create a method called area to calculate the area of circles.

Given Code Expected Output

public class CircleTester { Total Circle: 0


public static void main(String[] args) { 1---------------
System.out.println("Total Circle: "+ Circle.count); Total Circle: 1
Circle c1 = new Circle(4); First circle radius: 4.0
First circle area:
System.out.println("1---------------"); 50.26548245743669
System.out.println("Total Circle: "+ Circle.count); 2---------------
System.out.println("First circle radius: " + c1.getRadius()); Total Circle: 2
System.out.println("First circle area: " + c1.area()); Second circle radius: 5.0
System.out.println("2---------------"); Second circle area:
Circle c2 = new Circle(5); 78.53981633974483
3---------------
System.out.println("Total Circle: "+ Circle.count);
System.out.println("Second circle radius: " + c2.getRadius());
System.out.println("Second circle area: " + c2.area());
System.out.println("3---------------");
}
}
Task 5
Suppose you have opened a new library, from where your friends can borrow books.
Initially you have bought 3 books (Pather Panchali, Durgesh Nandini & Anandmath) each
of 3 copies only. Design the Borrower class in such a way that the following code
provides the expected output.
● You are given the arrays book_count and book_name to keep track of the number
of books available. For simplicity, assume that there will be no other books
in the library.
● You must reuse the remainingBooks() method when needed.

Given Code Expected Output

public class Tester{ Available Books:


public static void main(String args[]){ Pather Panchali: 3
Borrower.bookStatus(); Durgesh Nandini: 3
System.out.println("*********1*********"); Anandmath: 3
Borrower b1 = new Borrower("Nabila"); *********1*********
b1.borrowBook("Pather Panchali"); Name: Nabila
b1.borrowBook("Anandmath"); Books Borrowed:
b1.borrowerDetails(); Pather Panchali
System.out.println("*********2*********"); Anandmath
Borrower b2 = new Borrower("Sadia"); *********2*********
b2.borrowBook("Anandmath"); Name: Sadia
b2.borrowBook("Durgesh Nandini"); Books Borrowed:
b2.borrowBook("Pather Panchali"); Anandmath
b2.borrowerDetails(); Durgesh Nandini
System.out.println("*********3*********"); Pather Panchali
*********3*********
System.out.println(Borrower.remainingBooks("Anandmath")+" 1 copies of Anandmath is
copies of Anandmath is remaining."); remaining.
System.out.println("*********4*********"); *********4*********
Borrower b3 = new Borrower("Anika"); Available Books:
b3.borrowBook("Anandmath"); Pather Panchali: 1
Borrower.bookStatus(); Durgesh Nandini: 2
System.out.println("*********5*********"); Anandmath: 0
Borrower b4 = new Borrower("Oishi"); *********5*********
b4.borrowBook("Anandmath"); This book is not available.
b4.borrowBook("Durgesh Nandini"); Name: Oishi
b4.borrowerDetails(); Books Borrowed:
} Durgesh Nandini
}

public class Borrower{


public static int book_count[] = {3, 3, 3};
public static String book_name[] = {"Pather Panchali",
"Durgesh Nandini", "Anandmath"};

// Your Code here


}
Task 6
For this task, you need to design the Cargo class with appropriate static and
non-static variables and methods to produce this given output for the given tester
code.
Note: .load() method marks an object as selected for transport, and .unload() method
unmarked it. At a time, the transport capacity is 10.0 Tonnes. Each Cargo object is
initialized with 2 attributes from the constructor - the contents and the weight.
Carefully observe the outputs to identify the other attributes and design the class.

Given Code Expected Output

public class CargoTester { Cargo Capacity: 10.0


public static void main(String[] args) { 1====================
System.out.println("Cargo Capacity: "+ Cargo.capacity()); Cargo ID: 1, Contents: Industrial
System.out.println("1===================="); Machinery, Weight: 4.5, Loaded: false
Cargo a = new Cargo("Industrial Machinery", 4.5); 2====================
a.details(); Cargo 1 loaded for transport.
System.out.println("2===================="); 3====================
a.load(); Cargo ID: 2, Contents: Steel Ingot,
System.out.println("3===================="); Weight: 2.7, Loaded: false
Cargo b = new Cargo("Steel Ingot", 2.7); 4====================
b.details(); Cargo Capacity: 5.5
System.out.println("4===================="); 5====================
System.out.println("Cargo Capacity: "+ Cargo.capacity()); Cargo 2 loaded for transport.
System.out.println("5===================="); Cargo Capacity: 2.8
b.load(); 6====================
System.out.println("Cargo Capacity: "+ Cargo.capacity()); Cannot load cargo, exceeds weight
System.out.println("6===================="); capacity.
Cargo c = new Cargo("Tree Trunks", 3.6); 7====================
c.load(); Cargo ID: 3, Contents: Tree Trunks,
System.out.println("7===================="); Weight: 3.6, Loaded: false
c.details(); Cargo ID: 2, Contents: Steel Ingot,
b.details(); Weight: 2.7, Loaded: true
System.out.println("8===================="); 8====================
Cargo d = new Cargo("Processed Goods", 1.8); Cargo 4 loaded for transport.
d.load(); Cargo Capacity: 1.0
System.out.println("Cargo Capacity: "+ Cargo.capacity()); 9====================
System.out.println("9===================="); Cargo 2 unloaded.
b.unload(); Cargo Capacity: 3.7
System.out.println("Cargo Capacity: "+ Cargo.capacity()); 10====================
System.out.println("10===================="); Cargo 3 loaded for transport.
c.load(); 11====================
System.out.println("11===================="); Cargo ID: 2, Contents: Steel Ingot,
b.details(); Weight: 2.7, Loaded: false
System.out.println("Cargo Capacity: "+ Cargo.capacity()); Cargo Capacity: 0.09999999999999964
}
}
Task 7
Design a Student class in such a way that the following code provides the expected
output.
Driver Code Output

public class StudentTester { Total Student(s): 0


public static void main(String[] args) { CSE Student(s): 0
Student.printDetails(); Other Department Student(s): 0
System.out.println("--------------------"); --------------------
Student mikasa = new Student("Mikasa", 3.75); ID: 1
mikasa.individualDetail(); Name: Mikasa
System.out.println("--------------------"); CGPA: 3.75
Student.printDetails(); Department: CSE
System.out.println("--------------------"); --------------------
Student harry = new Student("Harry", 2.5, "Charms"); Total Student(s): 1
harry.individualDetail(); CSE Student(s): 1
System.out.println("--------------------"); Other Department Student(s): 0
Student.printDetails(); --------------------
System.out.println("--------------------"); ID: 2
Student levi = new Student("Levi", 3.33); Name: Harry
levi.individualDetail(); CGPA: 2.5
System.out.println("--------------------"); Department: Charms
Student.printDetails(); --------------------
} Total Student(s): 2
} CSE Student(s): 1
Other Department Student(s): 1
--------------------
ID: 3
Name: Levi
CGPA: 3.33
Department: CSE
--------------------
Total Student(s): 3
CSE Student(s): 2
Other Department Student(s): 1
Task 8
Design the Player class with the necessary property to produce the output
from the given driver code. Hint: The total number of players is maximum 11

Driver Code Output


public class PlayerTester{ Total number of players: 0
public static void main(String[] args) { 1---------------------------
System.out.println("Total number of players: " + Player Name: Neymar
Player.total); Jersey Number: 5
System.out.println("1------------------"); Country: Brazil
Player p1 = new Player("Neymar", "Brazil",5); ========================
System.out.println(p1.player_detail()); Total number of players: 1
System.out.println("==================="); Players enlisted so far: Neymar
Player.info(); 2---------------------------
System.out.println("2------------------"); Player Name: Ronaldo
Player p2 = new Player("Ronaldo", "Portugal", 7); Jersey Number: 7
System.out.println(p2.player_detail()); Country: Portugal
System.out.println("==================="); ========================
Player.info(); Total number of players: 2
System.out.println("3------------------"); Players enlisted so far: Neymar, Ronaldo
Player p3 = new Player("Messi", "Argentina", 6); 3---------------------------
System.out.println(p3.player_detail()); Player Name: Messi
System.out.println("==================="); Jersey Number: 6
Player.info(); Country: Argentina
System.out.println("4------------------"); ========================
Player p4 = new Player("Mbappe", "France", 10); Total number of players: 3
System.out.println(p4.player_detail()); Players enlisted so far: Neymar, Ronaldo,
System.out.println("==================="); Messi
Player.info(); 4---------------------------
} Player Name: Mbappe
} Jersey Number: 10
Country: France
========================
Total number of players: 4
Players enlisted so far: Neymar, Ronaldo,
Messi, Mbappe
Task 9
1. public class Tracing { Output
2. public static int x= 0, y = 0;
3. public int a, b;
4. public Tracing(int a, int b){
5. this.a = a;
6. this.b = b;
7. x+=1;
8. y+=2;
9. }
10. public void methodA(int a){
11. this.a = x+a;
12. this.b = this.b+ this.a +this.methodB();
13. System.out.println(this.a+" "+this.b+" "+x);
14. }
15. public int methodB(){
16. this.b = y - this.b + this.a;
17. System.out.println(this.a+" "+this.b+" "+x);
18. x += this.b;
19. return this.b;
20. }
21. public void methodB(Tracing t1){
22. t1.b = this.y - t1.b + this.b;
23. System.out.println(t1.a+" "+t1.b+" "+x);
24. }
25. }
26. public class Test9{
27. public static void main(String [] args){
28. Tracing t1= new Tracing(2, 3);
29. t1.methodA(1);
30. Tracing t2= new Tracing(3, 4);
31. t2.methodA(2);
32. t1.methodB(t2);
33. t2.methodB(t2);
34. }
35. }
Task 10

1 public class FinalT6A{ Outputs


2 public static int temp = 3;
3 public int sum;
4 public int y = 2;
5 public FinalT6A(int x, int p){
6 temp+=3;
7 y = temp - p;
8 sum = FinalT6A.temp + x;
9 System.out.println(x + " " + y+ " " + sum);
10 }
11 public void methodA(){
12 int x=0, y =0;
13 y = y + this.y;
14 x = this.y + 2 + temp;
15 sum = x + y + methodB(temp, y);
16 System.out.println(x + " " + y+ " " + sum);
17 }
18 public int methodB(int temp, int n){
19 int x = 0;
20 y = y + (++temp);
21 x = x + 2 + n;
22 sum = sum + x + y;
23 System.out.println(x + " " + y+ " " + sum);
24 return sum;
25 }
26 }
27 public class Test10{
28 public static void main(String [] args){
29 FinalT6A q1 = new FinalT6A(2,1);
30 q1.methodA();
31 q1.methodA();
32 }
33 }
Task 11
1 public class B{
2 public static int x;
3 public int y = 4;
4 public int temp = -5;
5 public int sum = 2;
6 public B(){
7 y = temp + 3 ;
8 sum = 3 + temp + 3;
9 temp-=2;
10 }
11 public B(B b){
12 sum = b.sum;
13 x = b.x;
14 b.methodB(1,3);//
15 }
16 public void methodA(int m, int n){
17 int x = 2;
18 y = y + m + (temp++);
19 x = x + 7 + n;
20 sum = sum + x + y;
21 System.out.println(x + " " + y+ " " + sum);
22 }
23 public void methodB(int m, int n){
24 int y = 0;
25 y = y + this.y;
26 x = this.y + 3 + temp;
27 methodA(x, y);
28 sum = x + y + sum;
29 System.out.println(x + " " + y+ " " + sum);
30 }
31 }
Consider the following code:
B b1 = new B(); x y sum
B b2 = new B(b1);
b1.methodA(3, 2);
b2.methodB(1, 2);
Ungraded Tasks (Optional)
(You don’t have to submit the ungraded tasks)

Task 1
Design the SultansDine class with the necessary property to produce the
output from the given driver code.
Subtaks:
1. Create SultansDine class
2. Create 2 static variable and 1 static array
3. Create 1 static method
4. Calculation of branch sell is given below
a. If sellQuantity < 10:
i. Branch_sell = quantity * 300
b. Else if sellQuantity < 20:
i. Branch_sell = quantity * 350
c. Else
i. Branch_sell = quantity * 400
5. Calculation of branch’s sell percentage = (branch’s sell / total sell)
* 100

Driver Code Output


public class SultansDineTester { Total Number of branch(s): 0
public static void main(String[] args) { Total Sell: 0 Taka
SultansDine.details(); 1===================
System.out.println("1==================="); Branch Name: Dhanmondi
Branch Sell: 10000 Taka
SultansDine dhanmondi = new SultansDine("Dhanmondi"); 2===================
dhanmondi.sellQuantity(25); Total Number of branch(s): 1
dhanmondi.branchInformation(); Total Sell: 10000 Taka
System.out.println("2==================="); Branch Name: Dhanmondi, Branch Sell: 10000 Taka
SultansDine.details(); Branch consists of total sell's 100.00
System.out.println("3==================="); 3===================
Branch Name: Baily Road
SultansDine baily_road = new SultansDine("Baily
Branch Sell: 5250 Taka
Road"); 4===================
baily_road.sellQuantity(15); Total Number of branch(s): 2
baily_road.branchInformation(); Total Sell: 15250 Taka
System.out.println("4==================="); Branch Name: Dhanmondi, Branch Sell: 10000 Taka
SultansDine.details(); Branch consists of total sell's 65.57
System.out.println("5==================="); Branch Name: Baily Road, Branch Sell: 5250 Taka
Branch consists of total sell's 34.43
SultansDine gulshan = new SultansDine("Gulshan"); 5===================
gulshan.sellQuantity(9); Branch Name: Gulshan
gulshan.branchInformation(); Branch Sell: 2700 Taka
System.out.println("6==================="); 6===================
SultansDine.details(); Total Number of branch(s): 3
} Total Sell: 17950 Taka
} Branch Name: Dhanmondi, Branch Sell: 10000 Taka
Branch consists of total sell's 55.71
Branch Name: Baily Road, Branch Sell: 5250 Taka
Branch consists of total sell's 29.25
Branch Name: Gulshan, Branch Sell: 2700 Taka
Branch consists of total sell's 15.04

Task 2
Implement the design of the Travel class so that the following output is produced.
Use Encapsulation to protect the variables. [Hint: Assign all the variables in
private]

Driver Code Output


public class TravelTester { No. of Traveller = 0
public static void main(String[] args) { 1================
System.out.println("No. of Traveller = " + Travel.getCount()); Source: Dhaka
System.out.println("1================"); Destination: India
Flight Time: 1:00
2================
Travel t1 = new Travel("Dhaka", "India"); Source: Kuala Lumpur
System.out.println(t1.displayTravelInfo()); Destination: Dhaka
System.out.println("2================"); Flight Time: 23:00
3================
Travel t2 = new Travel("Kuala Lampur", "Dhaka"); Source: Dhaka
Destination: Germany
t2.setTime(23);
Flight Time: 15:00
System.out.println(t2.displayTravelInfo()); 4================
System.out.println("3================"); Source: Malaysia
Destination: Canada
Travel t3 = new Travel("Dhaka", "New_Zealand"); Flight Time: 9:00
t3.setTime(15); 5================
t3.setDestination("Germany"); No. of Traveller = 4
System.out.println(t3.displayTravelInfo());
System.out.println("4================");

Travel t4 = new Travel("Dhaka", "India");


t4.setTime(9);
t4.setSource("Malaysia");
t4.setDestination("Canada");
System.out.println(t4.displayTravelInfo());
System.out.println("5================");

System.out.println("No. of Traveller = " + Travel.getCount());


}
}
Task 3

1. public class Maze{ Output


2. public static int x;
3. public void methodA(){
4. int m = 5;
5. x=11;
6. System.out.println(x+" "+m);
7. m=methodB(m-3)+x;
8. System.out.println(x+" "+(m));
9. methodB(x,m);
10. System.out.println(x+" "+m+x);
11. }
12. public int methodB(int y){
13. x=y*y;
14. System.out.println(x+" "+y);
15. return x+3;
16. }
17. public void methodB(int z, int x){
18. z=z-2;
19. x=x*1%z;
20. System.out.println(z+" "+x);
21. }
22. }
23. public class TestU3{
24. public static void main(String [] args){
25. Maze c = new Maze();
26. c.methodA();
27. c.methodB(-11, 45);
28. }
29. }
Task 4
Find the outputs after running the main() method in Test11 class.
1 public class Quiz1{ Outputs
2 public static int temp = 4;
3 public int sum;
4 public int y;
5 public Quiz1(){
6 y = temp - 1;
7 sum = temp + 1;
8 temp+=2;
9 }
10 public Quiz1(int p){
11 y = temp + p ;
12 sum = p + temp + 1;
13 temp-=1;
14 }
15 public void methodA(){
16 int x=0, y =0;
17 y = y + this.y;
18 x = this.y + 2 + temp;
19 sum = x + y + methodB(x, y);
20 System.out.println(x + " " + y+ " " + sum);
21 }
22 public int methodB(int m, int n){
23 int x = 0;
24 y = y + m + (++temp);
25 x = x + 2 + n;
26 sum = sum + x + y;
27 System.out.println(x + " " + y+ " " + sum);
28 return sum;
29 }
30 }
31 public class TestU4{
32 public static void main(String [] args){
33 Quiz1 q1 = new Quiz1();
34 q1.methodA();
35 q1.methodA();
36 Quiz1.temp+= 2;
37 Quiz1 q2 = new Quiz1(2);
38 q2.methodA();
39 q2.methodA();
40 }
41 }

You might also like