0% found this document useful (0 votes)
79 views20 pages

Assignment 3-Instance Method and Method Overloading

Uploaded by

ibtehajr27
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)
79 views20 pages

Assignment 3-Instance Method and Method Overloading

Uploaded by

ibtehajr27
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

Lab Assignment 03

Course Code: CSE111

Course Title: Programming Language II

Topic: Instance Method and Method Overloading

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
Design the BankAccount class in such a way so that the following code
provides the expected output.

Driver Class Output

public class BankAccountTester{ Account No: 0


public static void main(String args[]){ Type: Not Set
BankAccount acc1 = new BankAccount(); -----1-----
[Link]([Link]()); Account information updated!
[Link]("-----1-----"); -----2-----
[Link](1456890,"Salary"); Account No: 1456890
[Link]("-----2-----"); Type: Salary
[Link]([Link]()); -----3-----
[Link]("-----3-----"); Account information updated!
BankAccount acc2 = new BankAccount(); -----4-----
[Link](1765498,"Student"); Account No: 1765498
[Link]("-----4-----"); Type: Student
[Link]([Link]());
}
}

Task 2
Design the Shape class with necessary properties to produce the given
output for the provided driver code.

Driver Class Output

public class ShapeTester{ Shape Name: Circle


public static void main(String args []){ Area: 78.54
Shape circle = new Shape(); 1---------------
Shape triangle = new Shape(); Shape Name: Triangle
Shape rectangle = new Shape(); Area: 14.0
2---------------
[Link]("Circle", 5); Shape Name: Rectangle
[Link]("Triangle", 4, 7); Area: 10.56
[Link]("Rectangle", 2.4, 4.4);
[Link]([Link]());
[Link]("1---------------");
[Link]([Link]());
[Link]("2---------------");
[Link]([Link]());
}
}

Task 3
Design the “Shelf” class with necessary properties, so that the given
output is produced for the provided driver code.

Driver Class Output

public class ShelfTester{ Shelf capacity: 0


public static void main(String [] args){ Number of books: 0
Shelf shelf = new Shelf(); 1---------------
[Link](); Zero capacity. Cannot add books.
[Link]("1---------------"); 2---------------
[Link](3); 3 books added to shelf
[Link]("2---------------"); 3---------------
[Link] = 7; Shelf capacity: 7
[Link](3); Number of books: 3
[Link]("3---------------"); 4---------------
[Link](); Exceeds capacity
[Link]("4---------------"); Shelf capacity: 7
[Link](5); Number of books: 3
[Link](); 6---------------
[Link] += 4; 5 books added to shelf
[Link]("6---------------"); Shelf capacity: 11
[Link](5); Number of books: 8
[Link]();
}
}

Task 4
Design the Library class with the necessary properties so that the
given output is produced for the provided driver code.
Driver Code Output

public class Tester{ 1-------------


public static void main(String[] args) { Book 'Ice' added to the library
Library a1 = new Library(); 2-------------
Maximum Capacity: 3
[Link](3);
Total Books: 1
[Link]("1-------------"); Book list:
[Link]("Ice"); Ice
[Link]("2-------------"); 3-------------
[Link](); Book 'Emma' added to the library
[Link]("3-------------"); Book 'Wings' added to the library
[Link]("Emma"); Maximum capacity exceeds. You can't add
more than 3 books
[Link]("Wings");
4-------------
[Link]("Next"); Maximum Capacity: 3
[Link]("4-------------"); Total Books: 3
[Link](); Book list:
Library a2 = new Library(); Ice
[Link](4); Emma
[Link]("5-------------"); Wings
5-------------
[Link]("Onnobhubon");
Book 'Onnobhubon' added to the library
[Link]("Ami"); Book 'Ami' added to the library
[Link]("6-------------"); 6-------------
[Link](); Maximum Capacity: 4
[Link]("7-------------"); Total Books: 2
[Link]("Deyal"); Book list:
Onnobhubon
[Link]("Himu");
Ami
[Link]("Megher Upor Bari"); 7-------------
[Link]("8-------------"); Book 'Deyal' added to the library
[Link](); Book 'Himu' added to the library
} Maximum capacity exceeds. You can't add
} more than 4 books
8-------------
Maximum Capacity: 4
Total Books: 4
Book list:
Onnobhubon
Ami
Deyal
Himu
Task 5
Design the TaxiLagbe class with necessary properties to produce the
given output for the provided driver code.

Driver Code Output

public class TaxiTester{ 1-------------


public static void main(String[] args) { Taxi number: 1010-01
TaxiLagbe taxi1 = new TaxiLagbe(); This taxi can cover Dhaka area
Total Passenger: 0
[Link]("1010-01", "Dhaka");
Passenger Lists:
[Link]("1-------------"); Total collected fare: 0 Taka
[Link](); 2-------------
[Link]("2-------------"); Dear Wilson! Welcome to TaxiLagbe
[Link]("Wilson", 105); 3-------------
[Link]("3-------------"); Taxi number: 1010-01
[Link](); This taxi can cover Dhaka area
Total Passenger: 1
[Link]("4-------------");
Passenger Lists:
[Link]("Walker", 100, "Wood", 200); Wilson
[Link]("5-------------"); Total collected fare: 105 Taka
[Link](); 4-------------
[Link]("6-------------"); Dear Walker! Welcome to TaxiLagbe
[Link]("Karen", 200); Dear Wood! Welcome to TaxiLagbe
[Link]("Donald", 130); 5-------------
Taxi number: 1010-01
[Link]("7-------------");
This taxi can cover Dhaka area
[Link](); Total Passenger: 3
[Link]("8-------------"); Passenger Lists:
TaxiLagbe taxi2 = new TaxiLagbe(); Wilson Walker Wood
[Link]("1010-02", "Khulna"); Total collected fare: 405 Taka
[Link]("Don", 115, "Parker", 215); 6-------------
Dear Karen! Welcome to TaxiLagbe
[Link]("9-------------");
Taxi Full! No more passengers can
[Link](); be added
} 7-------------
} Taxi number: 1010-01
This taxi can cover Dhaka area
Total Passenger: 4
Passenger Lists:
Wilson Walker Wood Karen
Total collected fare: 605 Taka
8-------------
Dear Don! Welcome to TaxiLagbe
Dear Parker! Welcome to TaxiLagbe
9-------------
Taxi number: 1010-02
This taxi can cover Khulna area
Total Passenger: 2
Passenger Lists:
Don Parker
Total collected fare: 330 Taka

Task 6
Design the Student class with the necessary properties to produce the
given output for the provided driver code.
Hint:
- A student having cgpa>=3.5 and credit>10 is eligible for
scholarship.
- A student with cgpa>=3.5 but <3.7 is eligible for Need-based
scholarship.
- A student having cgpa >=3.7 is eligible for Merit based
scholarship

Driver Code Output

public class StudentTester{ Name: Not Set


public static void main(String[] args) { Department: CSE
Student std1 = new Student(); CGPA: 0.0
Credits: 9
[Link]();
Scholarship Status: Not Set
[Link]("1---------------"); 1---------------
[Link]("Alif", 3.99, 12); 2---------------
[Link]("2---------------"); Alif is eligible for Merit based
[Link](); scholarship
[Link]("3---------------"); 3---------------
[Link](); Name: Alif
Department: CSE
Student std2 = new Student();
CGPA: 3.99
[Link]("Mim", 3.4); Credits: 12
Student std3 = new Student(); Scholarship Status: Merit based
[Link]("Henry", 3.5, 15, "BBA"); scholarship
[Link]("5---------------"); 5---------------
[Link](); Mim is not eligible for scholarship
[Link]("6---------------"); 6---------------
Henry is eligible for Need based
[Link](); scholarship
[Link]("7---------------"); 7---------------
[Link](); Name: Mim
Department: CSE
[Link]("8---------------");
CGPA: 3.4
[Link](); Credits: 9
} Scholarship Status: No scholarship
} 8---------------
Name: Henry
Department: BBA
CGPA: 3.5
Credits: 15
Scholarship Status: Need based
scholarship

Task 7
Complete the following Cart class to generate the given output from the tester code:
● A cart will have a cart number which will be assigned in create_cart() method.
● Each cart can hold up to 3 items (at max).
● Each cart must have two arrays to store items and their respective prices.
● The items inside a cart will be added in addItem() method only if the cart items do
not exceed 3.
● The giveDiscount() method saves the discount given to that cart object and
updates the price accordingly.

Driver Code Output

public class CartTester{ ====1====


public static void main(String [] args){ Table added to cart 1.
Cart c1 = new Cart (); You have 1 item(s) in your cart now.
Cart c2 = new Cart (); Chair added to cart 1.
Cart c3 = new Cart (); You have 2 item(s) in your cart now.
Television added to cart 1.
c1.create_cart(1); You have 3 item(s) in your cart now.
c2.create_cart(2); You already have 3 items on your cart
c3.create_cart(3); ====2====
[Link]("====1===="); Stove added to cart 2.
[Link]("Table", 3900.5); You have 1 item(s) in your cart now.
[Link]("Chair", 1400.76); ====3====
[Link](5400.87, "Television"); Chair added to cart 3.
[Link](5000.0, "Refrigerator"); You have 1 item(s) in your cart now.
Chair added to cart 3.
[Link]("====2===="); You have 2 item(s) in your cart now.
[Link]("Stove",439.90); ====4====
Your cart(c1) :
[Link]("====3===="); Table - 3900.5
[Link]("Chair",1400.5); Chair - 1400.76
[Link](3400.0, "Chair"); Television - 5400.87
Discount Applied: 0.0%
[Link]("====4===="); Total price: 10702.130000000001
[Link](); ====5====
Your cart(c2) :
[Link]("====5===="); Stove - 439.9
[Link](); Discount Applied: 0.0%
Total price: 439.9
[Link]("====6===="); ====6====
[Link](); Your cart(c3) :
[Link](10); Chair - 1400.5
Chair - 3400.0
[Link]("====7===="); Discount Applied: 0.0%
[Link](); Total price: 4800.5
} ====7====
} Your cart(c1) :
Table - 3900.5
Chair - 1400.76
Television - 5400.87
Discount Applied: 10.0%
Total price: 9631.917000000001

Task 8
Design the Reader class in such a way so that the following code provides the expected
output.
● A reader will have a name, capacity to read and an array of books they are reading.
● The initial capacity of a reader will be 0. The initial name will be “New user”.
Driver Code Expected Output

public class Reader_tester { 1 ==========


public static void main(String[] args){ A new reader is created!
Reader r1 = new Reader(); A new reader is created!
Reader r2 = new Reader(); 2 ==========
Name: Messi
[Link]("1 =========="); Capacity: 2
[Link]([Link]("Messi", 2)); Books:
[Link]([Link]("Ronaldo", 3)); No books added yet
3 ==========
[Link]("2 =========="); Name: Ronaldo
[Link](); Capacity: 3
Books:
[Link]("3 =========="); Book 1: Java
[Link]("Java"); Book 2: Python
[Link]("Python"); Book 3: C++
[Link]("C++"); 4 ==========
[Link](); No more capacity
5 ==========
[Link]("4 =========="); No more capacity
[Link]("C#"); 6 ==========
[Link]("Rust"); Name: Messi
[Link]("GoLang"); Capacity: 2
Books:
[Link]("5 =========="); Book 1: C#
[Link]("Python"); Book 2: Rust

[Link]("6 ==========");
[Link]();
}
}

Task 9

1 public class Task9 {


2 public int temp = 4;
3 public int sum;
4 public int y;
5 public int x;
6 public void methodA(int m){
7 int [] n = {2,5};
8 int x = 0;
9 y = y + m + [Link](x,m++)+(temp)+y;
10 x = this.x + 2 + (++n[0]);
11 sum = sum + x + y;
12 n[0] = sum + 2;
13 [Link](n[0] + x + " " + y+ " " + sum);
14 }
15 public int methodB(int m, int n){
16 int [] y = {1};
17 this.y = y[0] + this.y + m;
18 x = this.y + 2 + temp - n;
19 sum = x + y[0] + [Link];
20 [Link](y[0]+ x + " " + y[0] + " " +sum);
21 return y[0];
22 }
23 }

public class Tester9 { Outputs


public static void main(String [] args){
Task9 t1 = new Task9();
[Link](5);
[Link](3);
Task9 t2 = new Task9();
[Link](4);
}
}

Task 10

1 public class Maze{

2 public int x;

3 public void methodA(){

4 int m = 0, x = 9;

5 m = methodB(m-3)+x;

6 this.x = ++x;

7 [Link](this.x+" "+m);

8 methodB(x,m);

9 [Link](x+" "+(m+this.x));

10 methodB(m);

11 }

12 public int methodB(int y){

13 x=y*y;

14 [Link](x+" "+y);

15 return x-11;

16 }

17 public void methodB(int z, int x){

18 z=z-2;

19 x=this.x-2*x;

20 [Link](z+" "+this.x);
21 }

22 }

DRIVER CODE OUTPUTS

public class MazeTester{


public static void main(String args []){
Maze m1 = new Maze();
[Link]();
}
}

Task 11

1 public class Test11 {


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 [Link](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 [Link](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


public class Tester11 {
public static void main(String [] args){
Task11 t1 = new Task11();
[Link](2,3);
[Link](5,4);
}
}
Ungraded Tasks (Optional)
(You don’t have to submit the ungraded tasks)

Task 1
You are building a tracker system that will keep track of a person’s income and expenses.
● When the createTracker() method is invoked it sets the balance to 1.0 taka.
● The info() method returns a String with the trackers information.
● If the total balance becomes 0 after the expense() method is called it prints
“You’re broke!” . Again if the available balance is less than the expense it prints
“Not enough balance.”. Otherwise the method prints “Balance updated” after
updating the balance.
● The last expense and income history can be seen by using the history() method.
Driver Code Output

public class Tester4{ Name: null


public static void main(String[] args) { Current Balance: 0.0
MoneyTracker tr1 = new MoneyTracker(); 1 ==========
[Link]([Link]()); Name: John
[Link]("John"); Current Balance: 1.0
2 ==========
[Link]("1 ==========");
Balance Updated!
[Link]([Link]()); Name: John
[Link]("2 =========="); Current Balance: 1001.0
[Link](1000); 3 ==========
[Link]([Link]()); Balance Updated.
[Link]("3 =========="); Balance Updated.
[Link](800); Name: John
[Link](100); Current Balance: 101.0
[Link]([Link]()); 4 ==========
[Link]("4 =========="); Last added: 1000.0
Last spent: 100.0
[Link]();
5 ==========
[Link]("5 =========="); You're broke!
[Link](101); 6 ==========
[Link]("6 =========="); Not enough balance.
[Link](200); 7 ==========
[Link]("7 =========="); Balance Updated!
[Link](200); Last added: 200.0
[Link](); Last spent: 100.0
[Link]("8 =========="); 8 ==========
}
}
Task 2

1 public class Test2 {

2 int x = 3, y = 1, z = -4;

3 double p = 2.5;

4 public void methodA(int n, int x) {

5 this.x = methodB(x, n);

6 p = this.x + n % x * 2.0;

7 y = (z++) + methodB(z, (int) p) + (++z);

8 [Link](this.x + " " + (n + y) + " " + (x + z)) ;

9 }

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

11 int arr[] = {2, -5, 6};

12 arr[0] = arr[2] - this.x + n;

13 arr[1] = q - arr[1];

14 arr[2] = arr[q % 3] + arr[n % 2];

15 [Link](arr[0] + " " + arr[1] + " " + arr[2]) ;

16 return arr[1] + arr[2] - arr[0];

17 }

18 }

public class Tester2{ Outputs


public static void main(String [] args){
Test2 t = new Test2();
[Link](3, 4);
}
}
Task 3

1 public class Test3 {

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 [Link](x + " " + this.x + " " + sum);

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

8 [Link](y + " " + sum + " " + this.x);

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

10 [Link](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 Tester3{ Outputs
public static void main(String [] args){
Test3 t3 = new Test3();
[Link](7);
}
}

Task 4

Driver Code Output

public class CustomerTester { 1=====================


public static void main(String[] args) { Customer: John
Customer c1 = new Customer(); 2=====================
[Link]("John"); Apple added to cart
[Link]("1====================="); Orange added to cart
[Link](); Bread added to cart
[Link]("2====================="); Milk added to cart
[Link]("Apple", 2); Cart is full
[Link]("Orange", 5); 3=====================
[Link]("Bread", 5); Customer: John
[Link]("Milk", 3); Item: Apple Price: 2
[Link]("Eggs", 2); Item: Orange Price: 5
[Link]("3====================="); Item: Bread Price: 5
[Link](); Item: Milk Price: 3
[Link]("4====================="); 4=====================
[Link](); Total: 15
[Link]("5====================="); 5=====================
Customer c2 = new Customer(); Apple and Orange added to cart
[Link]("Jane"); Chocolates and Bread added to cart
[Link]("Apple", 2, "Orange", 5); Cart is full
[Link]("Chocolates", 15, "Bread", 5); 6=====================
[Link]("Milk", 3); Customer: Jane
[Link]("6====================="); Item: Apple Price: 2
[Link](); Item: Orange Price: 5
[Link]("7====================="); Item: Chocolates Price: 15
[Link](); Item: Bread Price: 5
} 7=====================
} Total: 27

Task 5
Driver Code Sample Output

public class CalculatorTester { 1=============


public static void main(String[] args) { 30
Calculator calc = new Calculator(); 2=============
45
[Link]("1============="); 3=============
[Link](10, 20); 42
[Link]("2============="); 4=============
[Link](5, 15, 25); 24
[Link]("3============="); 5=============
[Link](6, 7); Hello-Hello-Hello
[Link]("4============="); 6=============
[Link](2, 3, 4); Java-Java-Java-Java-Java
[Link]("5=============");
[Link]("Hello", 3);
[Link]("6=============");
[Link]("Java", 5);
}
}

Task 6

Driver Code Sample Output

public class LibraryTest { |---Book Customization---|


public static void main(String[] args) { Updated genre of "The Great Gatsby" to
Classic.
Book book1 = new Book(); Updated pages of "The Great Gatsby" to
[Link]("The Great Gatsby"); 180 pages.
Updated genre of "1984" to Dystopian.
Book book2 = new Book(); Updated pages of "1984" to 328 pages.
[Link]("1984", "George Orwell"); Updated pages of "To Kill a
Mockingbird" to 281 pages.
Book book3 = new Book();
[Link]("To Kill a Mockingbird", "Harper |---Library Inventory---|
Lee", "Fiction"); Title: The Great Gatsby, Author:
Unknown, Genre: Classic, Pages: 180
[Link]("|---Book Customization---|"); Title: 1984, Author: George Orwell,
[Link]("Classic"); Genre: Dystopian, Pages: 328
[Link](180); Title: To Kill a Mockingbird, Author:
Harper Lee, Genre: Fiction, Pages: 281
[Link]("Dystopian");
[Link](328);

[Link](281);

[Link]();

[Link]("|---Library Inventory---|");
[Link]();
[Link]();
[Link]();
}
}

Task 7

Driver Code Sample Output

public class MovieManagerTest { 1====================


public static void main(String[] args) { Added actor "Leonardo DiCaprio" to
Movie inception = new Movie(); "Inception".
[Link]("Inception", "Christopher Nolan", Added actor "Joseph Gordon-Levitt" to
8.8); "Inception".
Added actor "Ellen Page" to
[Link]("1===================="); "Inception".
[Link]("Leonardo DiCaprio", "Joseph Title: Inception
Gordon-Levitt"); Director: Christopher Nolan
[Link]("Ellen Page"); Rating: 8.8
[Link](); Actors: Leonardo DiCaprio, Joseph
Gordon-Levitt, Ellen Page
[Link]("2===================="); 2====================
Movie avengers = new Movie(); Added actor "Robert Downey Jr." to
[Link]("Avengers: Endgame", "Anthony "Avengers: Endgame".
Russo", 8.4); Added actor "Chris Evans" to
[Link]("Robert Downey Jr.", "Chris Evans", "Avengers: Endgame".
"Scarlett Johansson"); Added actor "Scarlett Johansson" to
[Link](); "Avengers: Endgame".
Title: Avengers: Endgame
[Link]("3===================="); Director: Anthony Russo
Movie parasite = new Movie(); Rating: 8.4
[Link]("Parasite", "Bong Joon-ho"); Actors: Robert Downey Jr., Chris
[Link]("Song Kang-ho", "Choi Woo-shik"); Evans, Scarlett Johansson
[Link](8.6); 3====================
[Link](); Added actor "Song Kang-ho" to
"Parasite".
[Link]("4===================="); Added actor "Choi Woo-shik" to
[Link](8.9); "Parasite".
[Link](); Updated rating of "Parasite" to 8.6
} Title: Parasite
} Director: Bong Joon-ho
Rating: 8.6
Actors: Song Kang-ho, Choi Woo-shik
4====================
Updated rating of "Parasite" to 8.9
Title: Parasite
Director: Bong Joon-ho
Rating: 8.9
Actors: Song Kang-ho, Choi Woo-shik
Task 8

Design the Course class with the necessary properties so that the
given output is produced for the provided driver code.

Driver Class Output

public class CourseTester2{ --------1--------


public static void main(String [] args){ Course details:
Course c1 = new Course(); Course Name: PL II
[Link]("PL II", "CS11"); Course Code: CS11
[Link]("--------1--------"); Course Syllabus:
[Link](); No content yet.
[Link]("--------2--------"); --------2--------
[Link]("Overloading"); Overloading was added.
[Link](); Course details:
[Link]("--------3--------"); Course Name: PL II
[Link]("Encapsulation"); Course Code: CS11
[Link]("Static", "Polymorphism"); Course Syllabus:
[Link](); Overloading
[Link]("--------4--------"); --------3--------
[Link]("Inheritance"); Encapsulation was added.
[Link]("--------5--------"); Static was added.
Course c2 = new Course(); Polymorphism was added.
[Link]("DS", "CS22"); Course details:
[Link]("Stack"); Course Name: PL II
[Link]("Recursion","Tree"); Course Code: CS11
[Link]("Heap","Hashing"); Course Syllabus:
[Link]("--------6--------"); Overloading, Encapsulation, Static,
[Link](); Polymorphism
} --------4--------
} Cannot add more content
--------5--------
Stack was added.
Recursion was added.
Tree was added.
Heap was added.
Cannot add more content
--------6--------
Course details:
Course Name: DS
Course Code: CS22
Course Syllabus:
Stack, Recursion, Tree, Heap

You might also like