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

CSE111 Lab Assignment 2 - Fall'24

Uploaded by

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

CSE111 Lab Assignment 2 - Fall'24

Uploaded by

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

Lab Assignment 02

Course Code: CSE111

Course Title: Programming Language II

Topic: OOP Basics, Instance Variable and Instance Method

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
You are given the following “University” class:

public class University{


public String name;
public String country;
}
Now write a Java tester class named “UniversityTester”.

a. Write the main method and create 2 objects of University class and print the location of
the objects and print the instance variables of the objects. Are the location of the objects
the same?

b. Now change the instance variables of the first object.


name = “Imperial College London”
country = “England”

Now change the instance variables of the second object.


name = “Brac University”
country = “Bangladesh”

Now check if the instance variables of both objects have changed or not and whether the
instance variables of both objects are of the same value or not.

Task 2

Design the “Student” class so that the main method prints the following:

Tester Class Output


public class StudentTester1{ Name of the Student: Default
public static void main(String [] args){ ID of the Student: 0
Student s1 = new Student(); Name of the Student: Bob
System.out.println("Name of the Student: "+s1.name); ID of the Student: 123
System.out.println("ID of the Student: "+s1.id);
s1.name = “Bob”;
s1.id = 123;
System.out.println("Name of the Student: "+s1.name);
System.out.println("ID of the Student: "+s1.id);
}
}
Task 3

Design the CSECourse class to generate the correct output from the driver code provided
below:

Driver Code Output

public class CourseTester{ Course Name: Programming


public static void main(String args []){ Language II
CSECourse c1 = new CSECourse(); Course Code: CSE111
System.out.println("Course Name: "+c1.courseName); Credit: 3
System.out.println("Course Code: "+c1.courseCode);
System.out.println("Credit: "+c1.credit);
}
}

Task 4
Design the “ImaginaryNumber” to generate the output given below:

Tester Class Output

public class Tester6{ 0 + 0i


public static void main(String [] args){ 1********
ImaginaryNumber num1 = new ImaginaryNumber(); 3 + 7i
num1.printNumber(); 2********
System.out.println("1********"); 1 + 9i
num1.realPart=3;
num1.imaginaryPart=7;
num1.printNumber();
System.out.println("2********");
ImaginaryNumber num2 = new ImaginaryNumber();
num2.realPart=1;
num2.imaginaryPart=9;
num2.printNumber();
}
}
Task 5

Design the Course class to generate the correct output from the driver code provided
below:
Driver Code Output

public class Tester5{ ========== 1 ==========


public static void main(String[] args) { Course Name:
Course c1 = new Course(); Programming Language I
Course c2 = new Course(); Course Code: CSE110
System.out.println("========== 1 =========="); Course Credit: 3
c1.updateDetails("Programming Language I","CSE110", 3); ========== 2 ==========
c1.displayCourse(); Course Name: Data
System.out.println("========== 2 =========="); Structures
c2.updateDetails("Data Structures","CSE220",3); Course Code: CSE220
c2.displayCourse(); Course Credit: 3
System.out.println("========== 3 =========="); ========== 3 ==========
c1.updateDetails("Programming Language II","CSE111",3); Course Name:
c1.displayCourse(); Programming Language II
} Course Code: CSE111
} Course Credit: 3

Task 6

Implement the “Assignment” class with necessary properties, so that


the given output is produced for the provided driver code.

Driver Class Output

public class AssignmentTester{ Number of tasks: 0


public static void main(String [] args){ Difficulty level: null
Assignment as1 = new Assignment(); Submission required: false
as1.printDetails(); 1---------------
System.out.println("1---------------"); Number of tasks: 11
as1.tasks = 11; Difficulty level: Moderate
as1.difficulty = "Moderate"; Submission required: true
as1.submission = true; 2---------------
as1.printDetails(); Assignment will not require
System.out.println("2---------------"); submission
System.out.println(as1.makeOptional()); 3---------------
System.out.println("3---------------"); Number of tasks: 11
as1.printDetails(); Difficulty level: Moderate
System.out.println("4---------------"); Submission required: false
Assignment as2 = new Assignment(); 4---------------
as2.tasks = 12; Number of tasks: 12
as2.difficulty = "Hard"; Difficulty level: Hard
as2.submission = false; Submission required: false
as2.printDetails(); 5---------------
System.out.println("5---------------"); Submission is already not required
System.out.println(as2.makeOptional());
}
}

Task 7

Design the CellPhone class so that the main method of tester class can produce the
following output:

Tester Class Output

public class Tester9{ Phone Model unknown


public static void main(String[]args){ Contacts Stored 0
CellPhone phone1 = new CellPhone(); 1##################
Contact Stored
phone1.printDetails();
===================
phone1.model ="Nokia 1100"; Phone Model Nokia 1100
System.out.println("1##################"); Contacts Stored 1
phone1.storeContact("Joy - 01834"); Stored Contacts:
System.out.println("==================="); Joy - 01834
phone1.printDetails(); 2##################
System.out.println("2##################"); Contact Stored
phone1.storeContact("Toya - 01334"); Contact Stored
===================
phone1.storeContact("Aayan - 01135");
Phone Model Nokia 1100
System.out.println("==================="); Contacts Stored 3
phone1.printDetails(); Stored Contacts:
System.out.println("3##################"); Joy - 01834
phone1.storeContact("Sani - 01441"); Toya - 01334
System.out.println("==================="); Aayan - 01135
phone1.printDetails(); 3##################
} Memory full. New contact can't be
} stored.
===================
Phone Model Nokia 1100
Contacts Stored 3
Stored Contacts:
Joy - 01834
Toya - 01334
Aayan - 01135

Task 8

Create an Employee class to provide the expected output.


● An employee will have a name, salary and designation.
● The name will be assigned inside the newEmployee() method
● Whenever a New Employee joins his/her salary will be Tk. 30,000 and the
designation will be junior.
● Employees with salaries greater than Tk. 50,000 and Tk. 30,000 need to pay 30%
and 10% of salary as tax respectively.
● Employees can be promoted to senior, lead and manager positions. Based on their
promotion they will get an increment of Tk. 25,000, Tk. 50,000 and Tk. 75,000
respectively.

Driver Code Expected Output

public class Tester3{ 1 ==========


public static void main(String[] args){ Employee Name: Harry Potter
Employee Salary: 30000.0 Tk
Employee emp1 = new Employee(); Employee Designation: junior
Employee emp2 = new Employee(); 2 ==========
Employee emp3 = new Employee(); Employee Name: Hermione Granger
Employee Salary: 30000.0 Tk
emp1.newEmployee("Harry Potter"); Employee Designation: junior
emp2.newEmployee("Hermione Granger"); 3 ==========
emp3.newEmployee("Ron Weasley"); Employee Name: Ron Weasley
System.out.println("1 =========="); Employee Salary: 30000.0 Tk
emp1.displayInfo(); Employee Designation: junior
System.out.println("2 =========="); 4 ==========
emp2.displayInfo(); No need to pay tax
System.out.println("3 =========="); 5 ==========
emp3.displayInfo(); Harry Potter has been promoted to lead
System.out.println("4 =========="); New Salary: 80000.00 Tk
emp1.calculateTax(); 6 ==========
System.out.println("5 =========="); Harry Potter Tax Amount: 24000.00 Tk
emp1.promoteEmployee("lead"); 7 ==========
System.out.println("6 =========="); Employee Name: Harry Potter
emp1.calculateTax(); Employee Salary: 80000.0 Tk
System.out.println("7 =========="); Employee Designation: lead
emp1.displayInfo(); 8 ==========
System.out.println("8 =========="); Ron Weasley has been promoted to manager
emp3.promoteEmployee("manager"); New Salary: 105000.00 Tk
System.out.println("9 =========="); 9 ==========
emp3.calculateTax(); Ron Weasley Tax Amount: 31500.00 Tk
System.out.println("10 =========="); 10 ==========
emp3.displayInfo(); Employee Name: Ron Weasley
} Employee Salary: 105000.0 Tk
} Employee Designation: manager
Task 9
Consider the following class:

public class Human{


public int age;
public double height;
}

Show the output of the following sequence of statements:

Human h1 = new Human(); Output


Human h2 = new Human();
h1.age = 21;
h1.height = 5.5;
System.out.println(h1.age);
System.out.println(h1.height);
h2.height = h1.height - 3;
System.out.println(h2.height);
h2.age = h1.age++;
System.out.println(h1.age);
h2 = h1;
System.out.println(h2.age);
System.out.println(h2.height);
h2.age++;
h2.height++;
System.out.println(h1.age);
System.out.println(h1.height);
h1.age = ++h2.age;
System.out.println(h2.age);
System.out.println(h2.height);
Task 10
Consider the following class:

public class Student{


public String name;
public double cgpa;
}

Show the output of the following sequence of statements:

Student s1 = new Student(); Output


Student s2 = new Student();
Student s3 = null;
s1.name = "Student One";
s1.cgpa = 2.3;
s3 = s1;
s2.name = "Student Two";
s2.cgpa = s3.cgpa + 1;
s3.name = "New Student";
System.out.println(s1.name);
System.out.println(s2.name);
System.out.println(s3.name);
System.out.println(s1.cgpa);
System.out.println(s2.cgpa);
System.out.println(s3.cgpa);
s3 = s2;
s1.name = "old student";
s2.name = "older student";
s3.name = "oldest student";
s2.cgpa = s1.cgpa - s3.cgpa + 4.5;
System.out.println(s1.name);
System.out.println(s2.name);
System.out.println(s3.name);
System.out.println(s1.cgpa);
System.out.println(s2.cgpa);
System.out.println(s3.cgpa);
Task 11

1 public class Task11 {


2 public int p = 3, y = 2, sum;
3 public void methodA(){
4 int x = 0, y = 0;
5 y = y + this.y;
6 x = sum + 2 + p;
7 sum = x + y + methodB(p, y);
8 System.out.println(x + " " + y+ " " + sum);
9 }
10 public int methodB(int p, int n){
11 int x = 0;
12 y = y + (++p);
13 x = x + 2 + n;
14 sum = sum + x + y;
15 System.out.println(x + " " + y+ " " + sum);
16 return sum;
17 }
18 }

Driver code:
public class Tester11 { Outputs
public static void main(String [] args){
Task11 t1 = new Task11 (); x y Sum
t1.methodA();
t1.methodA();
}
}
Ungraded Tasks (Optional)
(You don’t have to submit the ungraded tasks)

Task 1

Complete the “Cat” class so the main method produces the following output:

Test Class Output


public class Test7{ ===================
public static void main(String [] args){ White cat is sitting
Cat c1 = new Cat(); ===================
System.out.println("==================="); Black cat is sitting
c1.printCat(); ===================
c1.color = "Black"; Brown cat is jumping
System.out.println("===================");
c1.printCat();
c1.color = "Brown";
c1.action = "jumping";
System.out.println("===================");
c1.printCat();
}
}

Task 2

Complete the Bird class so that main method produces the following output:

Test class Output


public class Test8{ Parrot has flown up 3 feet.
public static void main(String args[]) { Squawk
Bird b1 = new Bird(); Parrot cannot fly down 5 feet.
b1.name = "Parrot"; Parrot has flown down 2 feet.
b1.flyUp(3); Parrot has flown down 1 feet and
b1.makeNoise(); landed.
b1.flyDown(5); Eagle has flown up 5 feet.
b1.flyDown(2); Eagle has flown down 5 feet and
b1.flyDown(1); landed.
Bird b2 = new Bird(); Squee
b2.name = "Eagle";
b2.flyUp(5);
b2.flyDown(5);
b2.makeNoise();
}
}

Task 3
Implement the “ChickenBurger” class with necessary properties, so that
the given output is produced for the provided driver code.
[Note:
1. There are four available spice levels: Mild, Spicy, Naga and
Extreme. You can store these values in a String array.
2. You might need to use the .equals() method to compare two string
values.]

Driver Class Output

public class BurgerMaker{ Sesame


public static void main(String [] args){ 200
Less
ChickenBurger b1 = new ChickenBurger(); Not Set
System.out.println(b1.bun); ----------1----------
Cannot serve now. Customize Spice Level
System.out.println(b1.price);
first.
System.out.println(b1.sauceOption); ----------2----------
System.out.println(b1.spiceLevel); This spice level is unavailable.
Spice level set to Spicy.
System.out.println("----------1----------");
----------3----------
System.out.println(b1.serveBurger()); The burger is being served:-
System.out.println("----------2----------"); Bun Type: Sesame
Price: 200
b1.customizeSpiceLevel("Extreme Jhaal");
Sauce Option: Less
b1.customizeSpiceLevel("Spicy"); Spice Level: Spicy
System.out.println("----------3----------"); ----------4----------
Spice level set to Naga.
System.out.println(b1.serveBurger()); ----------5----------
System.out.println("----------4----------"); The burger is being served:-
ChickenBurger b2 = new ChickenBurger(); Bun Type: Brioche
Price: 250
b2.bun = "Brioche"; Sauce Option: Regular
b2.price += 50; Spice Level: Naga
b2.sauceOption = "Regular";
b2.customizeSpiceLevel("Naga");
System.out.println("----------5----------");
System.out.println(b2.serveBurger());
}
}

Task 4

Implement the “MobilePhone” class with necessary properties to produce


the given output for the provided driver code.

Driver Class Output

public class MobilePhoneTester{ Total Contacts: 0


public static void main(String args []){ Contact List:
MobilePhone m1 = new MobilePhone(); 1----------------
MobilePhone m2 = new MobilePhone(); The contact of John is added.
m1.setContactCapacity(5); The contact of Maria is added.
m2.setContactCapacity(100); 2----------------
m1.details(); Total Contacts: 2
System.out.println("1----------------"); Contact List:
m1.addContact("John", 9866); John:9866
m1.addContact("Maria", 7865); Maria:7865
System.out.println("2----------------"); 3----------------
m1.details(); Calling John . . .
System.out.println("3----------------"); 4----------------
m1.makeCall(9866); The contact of Henry is added.
System.out.println("4----------------"); 5----------------
m1.addContact("Henry", 2365); Calling 7552 . . .
System.out.println("5----------------"); Calling Henry . . .
m1.makeCall(7552); 6----------------
m1.makeCall(2365); The contact of Gomes is added.
System.out.println("6----------------"); The contact of Antony is added.
m1.addContact("Gomes", 4589); Storage Full!!
m1.addContact("Antony", 8421); 7----------------
m1.addContact("Tony", 5789); Total Contacts: 5
System.out.println("7----------------"); Contact List:
m1.details(); John:9866
} Maria:7865
} Henry:2365
Gomes:4589
Antony:8421
Task 5

1 public class Test1{

2 public int sum;

3 public int y;

4 public void methodA(){

5 int x=2, y =3;

6 int [] msg ={3, 7};

7 y = this.y + msg[0];

8 methodB(++msg[1], msg[0]);

9 x = x + this.y + msg[1];

10 sum = x + y + msg[0];

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

12 }

13 public void methodB(int mg2, int mg1){

14 int x = 0;

15 y = this.y + mg2;

16 x = x + 19 + mg1;

17 sum = this.sum + x + y;

18 mg2 = y + mg1;

19 mg1 = mg2 + x + 2;

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


21 }

22 }

public class Tester5{


public static void main (String args[]){ Outputs
Test1 t1 = new Test1();
t1.methodB(5,-8);
Test1 t2 = new Test1();
t2.methodA();
}
}

You might also like