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
Β





