Task 1
Consider the following code:
class msgClass{
public int content;
}
public class FinalT5A{
private int sum = 2;
private int y = 1;
public int x = 1;
public void methodA(){
int x=0, y =0, i = 0;
while (i < 2){
msgClass myMsg = new msgClass();
myMsg.content = this.x;
this.y = this.y + methodB(myMsg, myMsg.content);
System.out.println(x + " " + y+ " " + sum);
y = this.y / 2;
x = y + sum/2 - i;
sum = x + y + myMsg.content;
i++;
}
System.out.println(x + " " + y+ " " + sum);
}
private int methodB(msgClass mg2, int mg1){
int x = 0;
y = y + mg2.content;
mg2.content = y + mg1;
x = this.x + 3 + mg1;
sum = sum + x + y;
System.out.println(this.x + " " + this.y+ " " + sum);
mg2.content = sum - mg1 ;
return sum;
}
}
What is the output of the following code sequence?
FinalT5A fT5A = new FinalT5A(); fT5A.methodA();
x
y
sum
Task 2
public class FinalT6A{
public static int temp = 3;
private int sum;
private int y = 2;
public FinalT6A(int x, int p){
temp+=3;
y = temp - p;
sum = temp + x;
System.out.println(x + " " + y+ " " + sum);
}
public void methodA(){
int x=0, y =0;
y = y + this.y;
x = this.y + 2 + temp;
sum = x + y + methodB(temp, y);
System.out.println(x + " " + y+ " " + sum);
}
public int methodB(int temp, int n){
int x = 0;
y = y + (++temp);
x = x + 2 + n;
sum = sum + x + y;
System.out.println(x + " " + y+ " " + sum);
return sum;
}
}
What is the output of the following code sequence?
FinalT6A q1 = new FinalT6A(2,1); q1.methodA(); q1.methodA();
x
y
sum
Task 3
public class Quiz1{ public static int temp = 4; public int sum; public int y; public Quiz1(){ y = temp - 1; sum = temp + 1; temp+=2; }
public Quiz1(int p){ y = temp + p ; sum = p+ temp + 1; temp-=1; }
public void methodA(){ int x=0, y =0; y = y + this.y; x = this.y + 2 + temp; sum = x + y + methodB(x, y);
System.out.println(x + " " + y+ " " + sum);
}
public int methodB(int m, int n){ int x = 0; y = y + m + (++temp); x = x + 2 + n; sum = sum + x + y;
System.out.println(x + " " + y+ " " + sum); return sum;
} }
Consider the following code:
Quiz1 q1 = new Quiz1(); q1.methodA(); q1.methodA(); Quiz1.temp+= 2;
Quiz1 q2 = new Quiz1(2); q2.methodA(); q2.methodA();
x
y
sum
Task 4
class A{
public static int temp = 4;
public int sum;
public int y;
public A(){
y = temp - 2;
sum = temp + 1;
temp-=2;
}
public void methodA(int m, int n){
int x = 0;
y = y + m + (temp++);
x = x + 1 + n;
sum = sum + x + y;
System.out.println(x + " " + y+ " " + sum);
}
}
class B{
public static int x;
public int y = 5;
public int temp = -5;
public int sum = 2;
public B(){
y = temp + 3 ;
sum = 3 + temp + 2;
temp-=2;
}
public B(B b){
sum = b.sum;
x = b.x;
b.methodB(2,3);
}
public void methodA(int m, int n){
int x = 2;
y = y + m + (temp++);
x = x + 5 + n;
sum = sum + x + y;
System.out.println(x + " " + y+ " " + sum);
}
public void methodB(int m, int n){
int y = 0;
y = y + this.y;
x = this.y + 2 + temp;
methodA(x, y);
sum = x + y + sum;
System.out.println(x + " " + y+ " " + sum);
}
}
Consider the following code:
A a1 = new A();
B b1 = new B(); B b2 = new B(b1); b1.methodA(1, 2); b2.methodB(3, 2);
x
y
sum
Task 5
class A{
public static int temp = 3;
public int sum;
public int y;
public int x;
public A(){
int temp = -3;
int sum = 7;
y = temp - 5;
sum = temp + 2;
temp-=2;
this.x = sum + temp + y;
}
public A(int y, int temp){
y = temp - 1+ x;
sum = temp + 2 -x;
temp-=2;
}
public void methodA(int m, int [] n){
int x = 0;
y = y + m + methodB(x,m)+(temp++)+y;
x = this.x + 2 + (++n[0]);
sum = sum + x + y;
n[0] = sum + 2;
System.out.println(n[0] + x + " " + y+ " " + sum + n[0]);
}
public int methodB(int m, int n){
int [] y = {0};
this.y = y[0] + this.y + m;
x = this.y + 2 + temp - n;
sum = x + y[0] + this.sum;
System.out.println(y[0]+ x + "this.temp" + y[0]+ " " +sum+ y[0]);
return y[0];
}
}
class B{
public int y=1;
public int temp=-3;
public int x = 1;
public static int sum = 2;
public B(){
y = temp + 1 ;
x = 3 + temp + x;
temp-=2;
}
public B(B b){
sum = b.sum + this.sum;
x = b.x + x;
b.methodB(3,5);
}
public void methodA(int m, int [] n){
int x = 0;
y = y + m + (temp++);
x = x + 2 + (++n[0]);
sum = sum + x + y;
n[0] = sum + 2;
System.out.println(temp + x + " " + y+ " " + sum + temp);
}
public void methodB(int m, int n){
int [] y = {0};
this.y = y[0] + this.y + m;
x = this.y + 2 + temp - n;
methodA(x, y);
sum = x + y[0] + this.sum;
System.out.println(n + x + " " + y[0]+ " " + sum + n);
}
}
Consider the following code:
int x[] = {35};
A a1 = new A();
A a2 = new A(-5,-7);
B b1 = new B();
B b2 = new B(b1);
a1.methodA(1, x);
b2.methodB(3, 2);
a2.methodA(1, x);
Task 6
class msgClass{
public int content;
}
public class Quiz3{
private int sum;
private int y;
public static int x;
public Quiz3(){
sum = 5;
x = 2;
y = 2;
}
public Quiz3(int k){
sum = sum + k;
y = 3;
x += 2;
}
public void methodA(){
int x=1, y=1;
msgClass [] msg = new msgClass[1];
msgClass myMsg = new msgClass();
myMsg.content = Quiz3.x;
msg[0] = myMsg;
msg[0].content = this.y + myMsg.content;
this.y = this.y + methodB(msg[0]);
y = methodB(msg[0]) + this.y;
x = y + methodB(msg, msg[0]);
sum = x + y + msg[0].content;
System.out.println(x + " " + y+ " " + sum);
}
private int methodB(msgClass [] mg2, msgClass mg1){
int x = 2;
y = y + mg2[0].content;
mg2[0].content = y + mg1.content;
x = x + 2 + mg1.content;
sum = sum + x + y;
mg1.content = sum - mg2[0].content ;
System.out.println(Quiz3.x + " " + this.y+ " " + sum);
return sum;
}
public int methodB(msgClass mg1){
int x = 1, y = 2;
y = sum + mg1.content;
this.y = y + mg1.content;
x = Quiz3.x + 5 + mg1.content;
sum = sum + x + y;
Quiz3.x = mg1.content + x + 3;
System.out.println(x + " " + y+ " " + sum);
return y;
}
}
Consider the following code:
Quiz3 a1 = new Quiz3(); Quiz3 a2 = new Quiz3(5); msgClass msg = new msgClass(); a1.methodA(); a2.methodB(msg);
x
y
sum
Task 7
Study the following output
Code
Output
public class StudentTest{
public static void main(String [] args){
Student s1 = new Student();
System.out.println(s1.getName());
Student s2 = new Student("Matin");
System.out.println(s2.getName());
Student s3 = new Student("Saad");
System.out.println(s3.getName());
System.out.println(Student.numberOfStudents);
}
}
default name
Matin
Saad
3
Write the code for the Student class so that the StudentTest class generates the output shown above.
Task 8
Output:
========================
Name: Saad Abdullah
Department: CSE
List of courses
========================
CSE 110 Programming Language I
CSE 111 Programming Language-II
========================
========================
Name: Mumit Khan
Department: CSE
List of courses
========================
CSE 220 Data Structures
CSE 221 Algorithms
CCSE 230 Discrete Mathematics
========================
========================
Name: Sadia Kazi
Department: CSE
List of courses
========================
CSE 310 Object Oriented Programming
CSE 320 Data Communications
CSE 340 Computer Architecture
========================
Tester:
public class TestTeacher{
public static void main(String [] args){
Teacher t1 = new Teacher("Saad Abdullah", "CSE");
Teacher t2 = new Teacher("Mumit Khan", "CSE");
Teacher t3 = new Teacher("Sadia Kazi", "CSE");
Course c1 = new Course("CSE 110 Programming Language I"); Course c2 = new Course("CSE 111 Programming Language-II"); Course c3 = new Course("CSE 220 Data Structures"); Course c4 = new Course("CSE 221 Algorithms");
Course c5 = new Course("CCSE 230 Discrete Mathematics");
Course c6 = new Course("CSE 310 Object Oriented
Programming");
Course c7 = new Course("CSE 320 Data Communications");
Course c8 = new Course("CSE 340 Computer Architecture"); t1.addCourse(c1); t1.addCourse(c2); t2.addCourse(c3); t2.addCourse(c4); t2.addCourse(c5); t3.addCourse(c6); t3.addCourse(c7); t3.addCourse(c8); t1.printDetail(); t2.printDetail(); t3.printDetail();
} }
Write the Teacher and Course classes so that the TestTeacher class produces the outputs given above
Task 9
Consider the following code:
public class AccountTester
{
public static void main(String[] args)
{
System.out.println(Account.interestRate);
Account a1 = new Account();
System.out.println(a1.nameKi());
System.out.println(a1.balanceKi());
a1.nameBoshao("Mashrafe Murtaza"); a1.balanceBoshao(5000); System.out.println(a1.nameKi());
System.out.println(a1.balanceKi());
Account a2 = new Account("Mustafizur Rahman",50);
System.out.println(a2.nameKi()); System.out.println(a2.balanceKi()); a1.withdraw(4900); a2.withdraw(25); a1.withdraw(4899);
}
}
Write the account class given that the output for the above code is :
5.0
Default Account
0.0
Mashrafe Murtaza
5000.0
Mustafizur Rahman
50.0
The account balance after deducting withdraw amount is equal to or less than minimum. Cannot withdraw The account balance after deducting withdraw amount is equal to or less than minimum. Cannot withdraw
Withdraw successful! New balance is: 101.0
Task 10
Complete the Student class so that the main method prints the following:
Name of the Student: Bob
ID of the Student: 1
Name of the Student: Tom
ID of the Student: 2
Name of the Student: Jack
ID of the Student: 3
Name of the Student: Jill ID of the Student: 4 public class Student{
//Your code here
}
public class Printer{
public void printDetail(Student s){
System.out.println("Name of the Student: "+s.name);
System.out.println("ID of the Student: "+s.id);
}
}
public class Test{
public static void main(String [] args){ Student s1 = new Student("Bob", 1);
Student s2 = new Student("Tom", 2);
Student s3 = new Student("Jack", 3);
Student s4 = new Student("Jill", 4); Printer pr = new Printer(); pr.printDetail(s1); pr.printDetail(s2); pr.printDetail(s3); pr.printDetail(s4);
}
}
Task 11
Task 1 public class Dog {
private String color = "Black";
//your code here
}
public class Quiz {
public static void main (String[] args){
Dog odie = new Dog("Red"); Dog goofy = new Dog("Blue"); odie.bark(); goofy.bark(); odie.changeColor("Brown"); odie.bark();
}
}
//Complete the Dog class so the main method above produces the following output:
Red dog is barking
Blue dog is barking
Brown dog is barking
Task 12
Page 1 of 1 SET-A
public class Cat{
public String color = "White"; public String action = "sitting"; //your code here
}
public class Test{
public static void main(String [] args){ Cat c1 = new Cat();
Cat c2 = new Cat("Black");
Cat c3 = new Cat("Brown", "jumping"); Cat c4 = new Cat("Red", "purring");
c1.printCat(); c2.printCat(); c3.printCat(); c4.printCat(); c1.changeColor("Blue"); c3.changeColor("Purple"); c1.printCat(); c3.printCat(); }
}
Output:
White cat is sitting Black cat is sitting jumping cat is Brown purring cat is Red Blue cat is sitting
Purple cat is Brown