0% found this document useful (0 votes)
58 views15 pages

CSE111 Lab Assignment 4 - Fall'24

Uploaded by

ahnafashiqueadi
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)
58 views15 pages

CSE111 Lab Assignment 4 - Fall'24

Uploaded by

ahnafashiqueadi
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/ 15

Lab Assignment 04

Course Code: CSE111

Course Title: Programming Language II

Topic: Constructor, Constructor Overloading and Multiclass


Problem

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 Student class in such a way that it produces the following output.
Driver Code Expected Output

public class StudentTester{ Harry


public static void main(String[] args){ Harry Potter
Student s1 = new Student("Harry", "CSE"); CSE
System.out.println(s1.name); CS
s1.updateName("Harry Potter");
System.out.println(s1.name);
System.out.println(s1.prog);
s1.updateProgram("CS");
String prog = s1.accessProgram();
System.out.println(prog);
}
}

Task 2
Design the Toy class in such a way that it produces the following output
Driver Code Expected Output

public class ToyTester{ A new toy has been made!


public static void main(String[] args){ 1=================
Toy t1 = new Toy("Car", 230); 2=================
System.out.println("1================="); Car
t1.updatePrice(250); price: 250 Taka
System.out.println("2================="); 3=================
System.out.println(t1.name); A new toy has been made!
t1.showPrice(); 4=================
System.out.println("3================="); Changing old name: Robot
Toy t2 = new Toy("Robot", 450); new name: Autobot
System.out.println("4================="); 5=================
t2.updateName("Autobot"); Autobot
System.out.println("5================="); price: 450 Taka
System.out.println(t2.name);
t2.showPrice();
}
}
Task 3
Design the Shape2D class in such a way that it produces the following output.

Driver Code Expected Output

public class Shape2DTester { A Square has been created with


public static void main(String[] args) { length: 5
Shape2D sq = new Shape2D(); ---------1----------
System.out.println("---------1----------"); The area of the Square is: 25.0
sq.area(); ---------2----------
System.out.println("---------2----------"); A Rectangle has been created with
Shape2D rectangle = new Shape2D(5,6); length: 5 and breadth: 6
System.out.println("---------3----------"); ---------3----------
rectangle.area(); The area of the Rectangle is: 30.0
System.out.println("---------4----------"); ---------4----------
Shape2D tri1 = new Shape2D(5,6,"Triangle"); A Triangle has been created with
System.out.println("---------5----------"); height: 5 and base: 6
tri1.area(); ---------5----------
System.out.println("---------6----------"); The area of the Triangle is: 15.0
Shape2D tri2 = new Shape2D(5,6,7); ---------6----------
System.out.println("---------7----------"); A Triangle has been created with
tri2.area(); the following sides: 5, 6, 7
System.out.println("---------8----------"); ---------7----------
} The area of the Triangle is: 14.69
} ---------8----------

Task 4
Write “Student“ class to show the following expected outputs
Note:
❖ A student can’t take any course until the CGPA is set.
❖ A student cannot take more than 4 courses.
❖ A student with CGPA below 3 cannot take more than 3 courses.
Driver Code Expected Output

public class StudentDriver { A student with ID 12345678 has


public static void main(String[] args){ been created.
Student student1 = new Student(12345678); 1-----------------
System.out.println("1-----------------"); Failed to add CSE110
student1.addCourse("CSE110"); Set CG first
System.out.println("2-----------------"); 2-----------------
student1.storeCG(2.5); Student ID: 12345678, CGPA: 2.5
student1.addCourse("CSE110"); Added courses are:
student1.addCourse("ENG101"); CSE110 ENG101
student1.showAdvisee(); 3-----------------
System.out.println("3-----------------"); Student ID: 12345678, CGPA: 2.5
student1.removeAllCourse(); No courses added.
student1.showAdvisee(); 4-----------------
System.out.println("4-----------------"); Student ID: 54652365, CGPA: 2.5
student1.storeID(54652365); Added courses are:
String[] courses = {"SOC101","CSE111","ENG102"}; SOC101 CSE111 ENG102
student1.addCourse(courses); 5-----------------
student1.showAdvisee(); Failed to add CSE230
System.out.println("5-----------------"); CG is low. Can't add more than 3
student1.addCourse("CSE230"); courses.
student1.showAdvisee(); Student ID: 54652365, CGPA: 2.5
System.out.println("6-----------------"); Added courses are:
Student student2 = new Student(975738383,3.7); SOC101 CSE111 ENG102
System.out.println("7-----------------"); 6-----------------
String[] courses2 = A student with ID 975738383 and
{"CSE220","PHY112","MAT120","BUS101","CHN101"}; cgpa 3.7 has been created.
student2.addCourse(courses2); 7-----------------
student2.showAdvisee(); Failed to add CHN101
} Maximum 4 courses allowed.
} Student ID: 975738383, CGPA: 3.7
Added courses are:
CSE220 PHY112 MAT120 BUS101

Task 5
Design the Triangle Class that will produce the following output. We will consider both triangles
to have the same sides if all sides are equal in the same orientation/sequence only.
Types of Triangle:
● Equilateral: When all sides in the same orientation are equal.
● Isosceles: When any two sides of a triangle in the same orientation are equal.
● Scalene: When all sides are of different lengths.
Driver Code Output

public class TriangleTester{ Three sides of the triangle are: 4,


public static void main(String args[]){ 4, 4
Triangle t1 = new Triangle(); Perimeter: 12
Triangle t2 = new Triangle(); --------1--------
Triangle t3 = new Triangle(); This is an Equilateral Triangle.
Triangle t4 = new Triangle(); --------2--------
Three sides of the triangle are: 4,
t1.updateSides(4, 4, 4); 5, 6
t2.updateSides(4, 5, 6); Perimeter: 15
t3.updateSides(4, 5, 6); This is a Scalene Triangle.
t4.updateSides(5, 4, 6); --------3--------
Three sides of the triangle are: 5,
t1.triangleDetails(); 4, 6
System.out.println("--------1--------"); Perimeter: 15
System.out.println(t1.printTriangleType()); This is a Scalene Triangle.
System.out.println("--------2--------"); --------4--------
t3.triangleDetails(); Addresses are different but the
System.out.println(t3.printTriangleType()); sides of the triangles are equal.
System.out.println("--------3--------"); --------5--------
t4.triangleDetails(); Addresses, length of the sides and
System.out.println(t4.printTriangleType()); perimeter all are different.
System.out.println("--------4--------"); --------6--------
t2.compareTrinagles(t3); These two triangle objects have the
System.out.println("--------5--------"); same address.
t1.compareTrinagles(t2); --------7--------
System.out.println("--------6--------"); Only the perimeter of both triangles
t1 = t2; is equal.
t1.compareTrinagles(t2);
System.out.println("--------7--------");
t3.compareTrinagles(t4);
}
}
Task 6
Write the Teacher and Course classes so that the TestTeacher class produces the outputs given.
Hint: A teacher can add a maximum of 3 courses.

Driver Code Output

public class TestTeacher{ A new teacher has been


public static void main(String [] args){ created
Teacher t1 = new Teacher("Matin Saad Abdullah","MSA"); A new teacher has been
Teacher t2 = new Teacher("Mumit Khan","MMK"); created
Teacher t3 = new Teacher("Sadia Hamid Kazi","SKZ"); A new teacher has been
Course c1 = new Course("CSE 110"); created
Course c2 = new Course("CSE 111"); 1========================
Course c3 = new Course("CSE 220"); Name: Matin Saad Abdullah
Course c4 = new Course("CSE 221"); Initial: MSA
Course c5 = new Course("CSE 230"); List of courses:
Course c6 = new Course("CSE 310"); CSE 110
Course c7 = new Course("CSE 320"); CSE 111
Course c8 = new Course("CSE 340"); 2========================
t1.addCourse(c1); Name: Mumit Khan
t1.addCourse(c2); Initial: MMK
t2.addCourse(c3); List of courses:
t2.addCourse(c4); CSE 220
t2.addCourse(c5); CSE 221
t3.addCourse(c6); CSE 230
t3.addCourse(c7); 3========================
t3.addCourse(c8); Name: Sadia Hamid Kazi
System.out.println("1========================"); Initial: SKZ
t1.printDetail(); List of courses:
System.out.println("2========================"); CSE 310
t2.printDetail(); CSE 320
System.out.println("3========================"); CSE 340
t3.printDetail();
}
}

Task 7
Design the required class/es so that the following output is generated. Read the following
description:
1. You may assume that to board a bus, a student must have the bus pass, and his/her
destination must match the route of the bus.
2. Additionally, the default maximum capacity of the bus is 2.

Driver Code Output

public class BracuStudentTester { 1===============


public static void main(String[] args) { Student Name: Afif
BracuStudent st1 = new BracuStudent("Afif", "Mirpur"); Lives in Mirpur
System.out.println("1==============="); Have Bus Pass? false
Student Name: Shanto
BracuStudent st2 = new BracuStudent("Shanto", Lives in Motijheel
"Motijheel"); Have Bus Pass? false
BracuStudent st3 = new BracuStudent("Taskin", "Mirpur"); 2===============
st1.showDetails(); Student Name: Taskin
st2.showDetails(); Lives in Mirpur
System.out.println("2==============="); Have Bus Pass? false
3===============
st3.showDetails();
Bus Route: Mirpur
System.out.println("3==============="); Passenger Count: 0 (Max: 2)
BracuBus bus1 = new BracuBus("Mirpur"); Passengers on Board:
BracuBus bus2 = new BracuBus("Azimpur", 5); Bus Route: Azimpur
bus1.showDetails(); Passenger Count: 0 (Max: 5)
bus2.showDetails(); Passengers on Board:
System.out.println("4==============="); 4===============
5===============
st2.getPass(); Student Name: Shanto
st3.getPass(); Lives in Motijheel
System.out.println("5==============="); Have Bus Pass? true
st2.showDetails(); Student Name: Taskin
st3.showDetails(); Lives in Mirpur
System.out.println("6==============="); Have Bus Pass? true
bus1.board(); 6===============
No passengers
System.out.println("7==============="); 7===============
bus1.board(st1, st2); You don't have a bus pass!
System.out.println("8==============="); You got on the wrong bus!
st1.getPass(); 8===============
st2.updateHome("Mirpur"); Student Name: Afif
st1.showDetails(); Lives in Mirpur
Have Bus Pass? true
st2.showDetails();
Student Name: Shanto
System.out.println("9==============="); Lives in Mirpur
bus1.board(st1); Have Bus Pass? true
bus1.board(st2, st3); 9===============
System.out.println("10==============="); Afif boarded the bus.
bus1.showDetails(); Shanto boarded the bus.
} Bus is full!
10===============
} Bus Route: Mirpur
Passenger Count: 2 (Max: 2)
Passengers on Board:
Afif Shanto

Task 8
Design the Student and the Usis class so that the following output is produced.
Note:
● A student's email, password, and login status are null by default while creating an object
of the Student class.
● Your code should satisfy the conditions mentioned in the output only.
● Usis class will have two instance variables: totalAdvisee and an array of Student type to
store the student object. The array will be updated inside advising() method only when
the advising is successful.
● Usis can take at most 5 advisees.

Driver Code Expected Output

public class UsisTester { Student object is created


public static void main(String[] args) { Student object is created
Student rakib = new Student("Rakib", 12301455, 1*************
"CSE"); Usis is ready to use!
2*************
Student roy = new Student("Roy", 12501345, "CS"); Email and password need to be set.
System.out.println("1*************"); 3*************
Usis usisObj = new Usis(); Please login to advise courses!
System.out.println("2*************"); 4*************
usisObj.login(rakib); 5*************
System.out.println("3*************"); Login successful
usisObj.advising(rakib); 6*************
System.out.println("4*************"); You haven't selected any courses.
rakib.email = "[email protected]"; 7*************
You need special approval to take
rakib.password = "1234"; more than 3 courses.
System.out.println("5*************"); 8*************
usisObj.login(rakib); Advising successful!
System.out.println("6*************"); 9*************
usisObj.advising(rakib); Total Advisee: 1
System.out.println("7*************"); Name: Rakib ID: 12301455
usisObj.advising(rakib, "CSE110", "PHY111", "MAT110", Department: CSE
"CSE260"); Advised Courses:
System.out.println("8*************"); CSE110 PHY111 MAT110
==============
usisObj.advising(rakib, "CSE110", "PHY111", 10*************
"MAT110"); Login successful
System.out.println("9*************"); 11*************
usisObj.allAdviseeInfo(); Advising successful!
System.out.println("10*************"); 12*************
roy.email = "[email protected]"; Total Advisee: 2
roy.password = "abcd"; Name: Rakib ID: 12301455
usisObj.login(roy); Department: CSE
System.out.println("11*************"); Advised Courses:
usisObj.advising(roy, "CSE110", "ENG101", "PHY112"); CSE110 PHY111 MAT110
System.out.println("12*************"); ==============
usisObj.allAdviseeInfo(); Name: Roy ID: 12501345
Department: CS
} Advised Courses:
} CSE110 ENG101 PHY112
==============
Task 9
1 public class B{
2 public int x = 3, y = 5, temp = -5, sum = 2;
3 public B(){
4 y = temp + 3 ;
5 sum = 3 + temp + 2;
6 temp-=2;
7 }
8 public B(B b){
9 sum = b.sum;
10 x = b.x + 2;
11 b.methodB(2,3);
12 }
13 public void methodA(int m, int n){
14 int x = 2;
15 y = y + m + (temp++);
16 x = x + 5 + n;
17 sum = sum + x + y;
18 System.out.println(x + " " + y+ " " + sum);
19 }
20 public void methodB(int m, int n){
21 int y = 0;
22 y = y + this.y;
23 x = this.y + 2 + temp;
24 methodA(x, y);
25 sum = x + y + sum;
28 System.out.println(x + " " + y+ " " + sum);
27 }
28 }

public class Tester9 { Outputs


public static void main(String args []){
B b1 = new B();
B b2 = new B(b1);
b1.methodA(1, 2);
b2.methodB(3, 2);
}
}

Task 10
1 public class msgClass{
2 public int content;
3 }
4 class FinalT5A{
5 public int sum = 2, y = 1, x = 1;
6 public void methodA(){
7 int x=6, y =0;
8 msgClass myMsg = new msgClass();
9 myMsg.content = this.x;
10 x = x + myMsg.content;
11 this.y = this.y + methodB(myMsg, myMsg.content);
12 System.out.println(x + " " + this.y+ " " + sum);
13 y = this.y/2 + this.x;
14 x = y + sum/2;
15 sum = x + y + myMsg.content;
16 System.out.println(x + " " + y+ " " + sum);
17 }
18 public int methodB(msgClass mg2, int mg1){
19 int x = 0;
20 y = y + mg2.content;
21 mg2.content = y + mg1;
22 x = this.x + 3 + mg1;
23 sum = sum + x + y;
24 System.out.println(this.x + " " + this.y+ " " + sum);
25 mg2.content = sum - mg1 ;
26 return sum;
27 }
28 }

DRIVER CODE OUTPUTS

public class Tester10{


public static void main(String args []){
FinalT5A fT5A = new FinalT5A();
fT5A.methodA();
}
}

Task 11
1 public class A{
2 public int temp = 3, sum = 9, y = 4, x = 0;
3 public A(){
4 int sum = 7;
5 y = temp - 5;
6 sum = temp + 2;
7 temp-=2;
8 this.x = sum + temp + y;
9 }
10 public A(int y, int temp){
11 y = temp - 1 + x;
12 sum = temp + 2 -x;
13 temp-=2;
14 }
15 public void methodA(int m, int [] n){
16 int x = 0;
17 y = y + m + methodB(x,m);
18 x = this.x + 2 + (++n[0]);
19 sum = sum + x + y;
20 n[0] = sum + 2;
21 System.out.println(n[0] + " " + y+ " " + sum);
22 }
23 public int methodB(int m, int n){
24 int [] y = {0};
25 this.y = y[0] + this.y + m;
26 x = this.y + 2 + temp - n;
27 sum = x + y[0] + this.sum;
28 System.out.println(y[0]+ " "+ temp + " " + sum);
29 return y[0];
30 }
31 }

Driver Code Output


public class Tester11 {
public static void main(String args[]){
int x[] = {35};
A a1 = new A();
A a2 = new A(-5,-7);
a1.methodA(1, x);
a2.methodA(1, x);
}
}
Ungraded Tasks (Optional)
(You don’t have to submit the ungraded tasks)

Task 1
Design the Parcel class in such a way that it produces the following output.

NOTE: For the method calcFee(), if the delivery location is Dhanmondi, then the location
charge will be 50 taka or else it’ll be free. Also, while calculating total fee, if the product weight is
0 the total_fee would also be 0.
Formula: fee = (weight * 20) + location_charge (if any)

Driver Code Expected Output

public class ParcelDriver { Set name first


public static void main(String[] args){ Name: Spongebob
Parcel p1 = new Parcel(); Total Weight: 0
p1.printDetails(); Total Fee: 0.0
p1.name = "Spongebob"; 1**************
p1.printDetails(); Name: Bob the Builder
System.out.println("1**************"); Total Weight: 15
Parcel p2 = new Parcel("Bob the Builder"); Total Fee: 300.0
p2.weight = 15; 2**************
p2.calcFee("Gulshan"); Updated Weight: 40
p2.printDetails(); Name: Bob the Builder
System.out.println("2**************"); Total Weight: 40
p2.addWeight(25); Total Fee: 800.0
p2.calcFee("Banani"); 3**************
p2.printDetails(); Updated Weight: 25
System.out.println("3**************"); Name: Dora the Explorer
Parcel p3 = new Parcel("Dora the Explorer", 10); Total Weight: 25
p3.addWeight(15); Total Fee: 550.0
p3.calcFee("Dhanmondi");
p3.printDetails();
}
}

Task 2
Design the program to get the output as shown.

Hints:
● Create an array in the Team class to store the player’s object
● Use constructor overloading technique for Team class
public class TeamTester { Output:
public static void main(String[] args) {
Team: Bangladesh
Team b = new Team(); List of players:
b.updateName("Bangladesh"); Name: Mashrafi
Age: 42, Total Matches: 100
Player mashrafi = new Player("Mashrafi", 42, 100); Name: Tamim
b.addPlayer(mashrafi); Age: 35, Total Matches: 70
==============
Player tamim = new Player("Tamim", 35, 70); Team: Australia
b.addPlayer(tamim); List of players:
Name: Ponting
b.printDetail(); Age: 50, Total Matches: 300
System.out.println("=============="); Name: Lee
Age: 49, Total Matches: 200
Team a = new Team("Australia");
Player ponting = new Player("Ponting", 50, 300);
a.addPlayer(ponting);
Player lee = new Player("Lee", 49, 200);
a.addPlayer(lee);
a.printDetail();
}
}

Task 3

1 public class TracingX {


2 public int x, y = 1;
3 public int metA(int y){
4 y += x + 3;
5 int temp = y + this.y;
6 if (temp % 2 == 0){
7 return temp;
8 }
9 TracingX t = new TracingX();
10 t.y = this.x - (++x) + t.x;
11 this.y = y + t.metA(t.x);
12 System.out.println(x +" "+ y +" "+temp);
13 return temp+this.y;
14 }
15 }

Driver code: Output:


public class TesterX {
public static void main(String[] args) {
TracingX t1 = new TracingX();
t1.y = t1.x = 5;
TracingX t2 = new TracingX();
t2.x = t1.metA(2);
t2.y = t2.metA(4);
System.out.println(t1.y +t1.x +" "+t2.x +" "+t2.y);
}
}

You might also like