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 2
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();
}
}
Complete the Cat class so the main method above produces the following output:
White cat is sitting
Black cat is sitting
Brown cat is jumping
Red cat is purring
Blue cat is sitting
Purple cat is jumping
Task 3
Using the StudentDriver class and its outputs given below, write the Student class:
public class StudentDriver |
{ |
public static void main(String[] args) |
{ |
Student s1; |
s1= new Student(); |
System.out.println(s1); |
System.out.println(s1.nameDao()); |
System.out.println(s1.boloToAmiKe()); |
System.out.println(s1.addressDao()); |
System.out.println(s1.cgpaDao()); |
s1.nameBoshao(Tonmoy Dewanjee); |
s1.addressBoshao(Mirpur); |
s1.idBoshao(16301157); |
s1.cgpaBoshao(4.0); |
System.out.println(s1.nameDao()); |
System.out.println(s1.boloToAmiKe()); |
System.out.println(s1.addressDao()); |
System.out.println(s1.cgpaDao()); |
Student s2 = new Student(Azibun Nuder,16301045,Uttara,4.0); |
System.out.println(s2); |
System.out.println(s2.nameDao()); |
System.out.println(s2.boloToAmiKe()); |
System.out.println(s2.addressDao()); |
System.out.println(s2.cgpaDao()); |
Student s3 = new Student(); |
System.out.println(s3); |
System.out.println(s3.nameDao()); |
System.out.println(s3.boloToAmiKe()); |
System.out.println(s3.addressDao()); |
System.out.println(s3.cgpaDao()); |
s1.standUp(); |
s2.standUp(); |
System.out.println(s1.tellMeYourName()); |
System.out.println(s2.tellMeYourName()); |
s1.call(Sumit Dutta); |
s2.call(Ananya Ritu); |
System.out.println(s1.add2Numbers(2,3)); |
} |
} |
Output:
[email protected] |
Ei name e kono student nai |
Student ei nai, abar id 😛 |
Naam nai .. thikana ashbe koi theke? |
-4.0 |
Tonmoy Dewanjee |
16301157 |
Mirpur |
4.0 |
[email protected] |
Azibun Nuder |
16301045 |
Uttara |
4.0 |
[email protected] |
Ei name e kono student nai |
Student ei nai, abar id 😛 |
Naam nai .. thikana ashbe koi theke? |
-4.0 |
Tonmoy Dewanjee is now standing up!! |
Azibun Nuder is now standing up!! |
Sir, my name is Tonmoy Dewanjee |
Sir, my name is Azibun Nuder |
Tonmoy Dewanjee: Hey, Sumit Dutta, Sir is calling you!! |
Azibun Nuder: Hey, Ananya Ritu, Sir is calling you!! |
5 |
Task 4
Write a function trim() that will take an array of characters as an input and remove multiple consecutive spaces from the array (You cannot use the String class in java). Following example code generates the output below:
public class Trim{
public static char [] trim(char [] input){
//Your code here
}
public static void main(String [] args){
char [] input = {T,h,i,s, , , , , ,i,s, , ,
, ,a, , , , ,t,e,s,t,.}; for (int i = 0; i< input.length; i++){
System.out.print(input[i]);
}
System.out.println(); char [] output = trim(input); for (int i = 0; i< output.length; i++){
System.out.print(output[i]);
}
System.out.println();
} }
This is a test. This is a test.
Task 5
Write a Java Code of a program that reads in a string (UPPER-case letters only) from the user and prints the letter that occurs second most often.
For example, if the user enters the word REFERENCES, your program should print the character R because the word has 4 E, 2 R, and all other character only once.
Notes:
s.length() returns the length of a string s.
s.charAt(int index) returns the character at the specified index of string s. An index ranges from 0 to length() 1.
Task 6
Write a Java Code of a function that takes an array of integers (of positive and negative numbers) and the length or size of the array as parameters, then modifies the array by getting rid of the negative elements (the numbers after the removed one will have to shift forward to fill the gap of course). If there are multiple negative elements, remove all of those. The function returns the new size of the array.
Task7
Write the removeOdd function below which takes in an array of numbers that has even and odd numbers mixed. This function removes the odd numbers and returns a compact array which only has the even numbers. For example output of the following code is:
Sample Input
21 33 44 66 11 1 88 45 10 9
Sample Output
44 66 88 10
public class Test{
public static int [] removeOdd (int [] input){
//Your code here
}
public static void main(String [] args){
int [] mixedArray = {21, 33, 44, 66, 11, 1, 88, 45, 10, 9}; for (int i = 0; i < mixedArray.length; i++) {
System.out.print(mixedArray[i] + );
}
System.out.println();
int [] noOdd = removeOdd(mixedArray); for (int i = 0; i < noOdd.length; i++) {
System.out.print(noOdd[i] + );
}
}
}
Task 8
public class FinalT6A{ |
public int temp = 4; |
private int sum; |
private int y = 1; |
public FinalT6A(int x, int p){ |
temp+=1; |
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 + 3 + 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 9
1. |
class msgClass{ |
2. |
public int content; |
3. |
} |
1. |
public class Q5{ |
2. |
private int sum; |
3. |
private int y; |
4. |
public int x; |
5. |
public Q5(){ |
6. |
sum = 3; |
7. |
x = 1; |
8. |
y = 6; |
9. |
} |
10. |
public void methodA(){ |
11. |
int x=1, y=1; |
12. |
msgClass [] msg = new msgClass[1]; |
13. |
msgClass myMsg = new msgClass(); |
14. |
myMsg.content = this.x; |
15. |
msg[0] = myMsg; |
16. |
msg[0].content = this.y + myMsg.content; |
17. |
this.y = this.y + methodB(msg[0]); |
18. |
y = methodB(msg[0]) + this.y; |
19. |
x = y + methodB(msg, msg[0]); |
20. |
sum = x + y + msg[0].content; |
21. |
System.out.println(x + + y+ + sum); |
22. |
} |
23. |
private int methodB(msgClass [] mg2, msgClass mg1){ |
24. |
int x = 1; |
25. |
y = y + mg2[0].content; |
26. |
mg2[0].content = y + mg1.content; |
27. |
x = x + 3 + mg1.content; |
28. |
sum = sum + x + y; |
29. |
mg1.content = sum mg2[0].content ; |
30. |
System.out.println(this.x + + this.y+ + sum); |
31. |
return sum; |
32. |
} |
33. |
private int methodB(msgClass mg1){ |
34. |
int x = 1, y = 1; |
35. |
y = sum + mg1.content; |
36. |
this.y = y + mg1.content; |
37. |
x = this.x + 3 + mg1.content; |
38. |
sum = sum + x + y; |
39. |
this.x = mg1.content + x + 2; |
40. |
System.out.println(x + + y+ + sum); |
41. |
return y; |
42. |
} |
43. |
} |
Write the output of the following code: [Answer on the question paper]
Q5 q = new Q5(); q.methodA(); |
x |
y |
sum |
Task 10
public class Quiz3A{ |
public int temp = 4; |
public int sum; |
public int y; |
public Quiz3A(){ |
y = temp 1; |
sum = temp + 1; |
temp+=2; |
} |
public Quiz3A(int k){ |
temp = temp++; |
sum = ++temp + k; |
y = sum 1; |
} |
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:
Quiz3A a1 = new Quiz3A(); a1.methodB(1,2); Quiz3A a2 = new Quiz3A(3); a2.methodB(2,4); a1.methodB(2,1); a2.methodB(1,3); |
x |
y |
sum |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Task 11
public class FinalT6A{ |
public 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(); FinalT6A q2 = new FinalT6A(4,5); q2.methodA(); |
x |
y |
sum |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Task 12
1 |
public class Scope{ |
2 |
public int x = 1; |
3 |
public int y = 100; |
4 |
public void met1(){ |
5 |
int x = 3; |
6 |
x = this.x + 1; |
7 |
y = y + this.x + 1; |
8 |
x = y + met2(x+y) + y; |
9 |
System.out.println(x); |
10 |
System.out.println(y); |
11 |
} |
12 |
public int met2(int y){ |
13 |
System.out.println(x); |
14 |
System.out.println(y); |
15 |
this.x = x + y; |
16 |
this.y = this.y + 200; |
17 |
return x + y; |
18 |
} |
19 |
} |
What is the output of the following code sequence?Scope q2 = new Scope(); q2.met1(); q2.met2(); q2.met1(); q2.met2();
Task 13
public class FinalT6A{ |
public int temp = 4; |
private int sum; |
private int y = 1; |
public FinalT6A(int x, int p){ |
temp+=1; |
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 + 3 + 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(); |
x |
y |
sum |
|
|
|
q1.methodA(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Task 14
4. |
class msgClass{ |
5. |
public int content; |
6. |
} |
44. |
public class Q5{ |
45. |
private int sum; |
46. |
private int y; |
47. |
public int x; |
48. |
public Q5(){ |
49. |
sum = 3; |
50. |
x = 1; |
51. |
y = 6; |
52. |
} |
53. |
public void methodA(){ |
54. |
int x=1, y=1; |
55. |
msgClass [] msg = new msgClass[1]; |
56. |
msgClass myMsg = new msgClass(); |
57. |
myMsg.content = this.x; |
58. |
msg[0] = myMsg; |
59. |
msg[0].content = this.y + myMsg.content; |
60. |
this.y = this.y + methodB(msg[0]); |
61. |
y = methodB(msg[0]) + this.y; |
62. |
x = y + methodB(msg, msg[0]); |
63. |
sum = x + y + msg[0].content; |
64. |
System.out.println(x + + y+ + sum); |
65. |
} |
66. |
private int methodB(msgClass [] mg2, msgClass mg1){ |
67. |
int x = 1; |
68. |
y = y + mg2[0].content; |
69. |
mg2[0].content = y + mg1.content; |
70. |
x = x + 3 + mg1.content; |
71. |
sum = sum + x + y; |
72. |
mg1.content = sum mg2[0].content ; |
73. |
System.out.println(this.x + + this.y+ + sum); |
74. |
return sum; |
75. |
} |
76. |
private int methodB(msgClass mg1){ |
77. |
int x = 1, y = 1; |
78. |
y = sum + mg1.content; |
79. |
this.y = y + mg1.content; |
80. |
x = this.x + 3 + mg1.content; |
81. |
sum = sum + x + y; |
82. |
this.x = mg1.content + x + 2; |
83. |
System.out.println(x + + y+ + sum); |
84. |
return y; |
85. |
} |
86. |
} |
Write the output of the following code: [Answer on the question paper]
Q5 q = new Q5(); q.methodA(); |
x |
y |
sum |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Task 15
public class FinalT6A{ |
public 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 16
7. |
class msgClass{ |
8. |
public int content; |
9. |
} |
87. |
public class Q5{ |
88. |
private int sum; |
89. |
private int y; |
90. |
public int x; |
91. |
public Q5(){ |
92. |
sum = 8; |
93. |
x = 2; |
94. |
y = 4; |
95. |
} |
96. |
public void methodA(){ |
97. |
int x=0, y=0; |
98. |
msgClass [] msg = new msgClass[1]; |
99. |
msgClass myMsg = new msgClass(); |
100 |
. myMsg.content = this.x; |
101 |
. msg[0] = myMsg; |
102 |
. msg[0].content = this.y + myMsg.content; |
103 |
. this.y = this.y + methodB(msg[0]); |
104 |
. y = methodB(msg[0]) + this.y; |
105 |
. x = y + methodB(msg, msg[0]); |
106 |
. sum = x + y + msg[0].content; |
107 |
. System.out.println(x + + y+ + sum); |
108 |
. } |
109 |
. private int methodB(msgClass [] mg2, msgClass mg1){ |
110 |
. int x = 0; |
111 |
. y = y + mg2[0].content; |
112 |
. mg2[0].content = y + mg1.content; |
113 |
. x = x + 30 + mg1.content; |
114 |
. sum = sum + x + y; |
115 |
. mg1.content = sum mg2[0].content ; |
116 |
. System.out.println(this.x + + this.y+ + sum); |
117 |
. return sum; |
118 |
. } |
119 |
. private int methodB(msgClass mg1){ |
120 |
. int x = 0, y = 0; |
121 |
. y = sum + mg1.content; |
122 |
. this.y = y + mg1.content; |
123 |
. x = this.x + 30 + mg1.content; |
124 |
. sum = sum + x + y; |
125 |
. this.x = mg1.content + x + 2; |
126 |
. System.out.println(x + + y+ + sum); |
127 |
. return y; |
128 |
. } |
129 |
.} |
Write the output of the following code: [Answer on the question paper]
Q5 q = new Q5(); q.methodA(); |
x |
y |
sum |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Task 17
public class FinalT6A{ |
public int temp = 1; |
private int sum; |
private int y = 2; |
public FinalT6A(int x, int p){ |
temp+=1; |
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 + 3 + 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 + 4 + n; |
sum = sum + x + y; |
System.out.println(x + + y+ + sum); |
return sum; |
} |
} |
What is the output of the following code sequence? [Answer on question paper]
FinalT6A q1 = new FinalT6A(5,6); q1.methodA(); q1.methodA(); |
x |
y |
sum |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Task 18
10. |
class msgClass{ |
11. |
public int content; |
12. |
} |
130. |
public class Q5{ |
131. |
private int sum; |
132. |
private int y; |
133. |
public int x; |
134. |
public Q5(){ |
135. |
sum = 1; |
136. |
x = 2; |
137. |
y = 3; |
138. |
} |
139. |
public void methodA(){ |
140. |
int x=1, y=1; |
141. |
msgClass [] msg = new msgClass[1]; |
142. |
msgClass myMsg = new msgClass(); |
143. |
myMsg.content = this.x; |
144. |
msg[0] = myMsg; |
145. |
msg[0].content = this.y + myMsg.content; |
146. |
this.y = this.y + methodB(msg[0]); |
147. |
y = methodB(msg[0]) + this.y; |
148. |
x = y + methodB(msg, msg[0]); |
149. |
sum = x + y + msg[0].content; |
150. |
System.out.println(x + + y+ + sum); |
151. |
} |
152. |
private int methodB(msgClass [] mg2, msgClass mg1){ |
153. |
int x = 1; |
154. |
y = y + mg2[0].content; |
155. |
mg2[0].content = y + mg1.content; |
156. |
x = x + 4 + mg1.content; |
157. |
sum = sum + x + y; |
158. |
mg1.content = sum mg2[0].content ; |
159. |
System.out.println(this.x + + this.y+ + sum); |
160. |
return sum; |
161. |
} |
162. |
private int methodB(msgClass mg1){ |
163. |
int x = 5, y = 6; |
164. |
y = sum + mg1.content; |
165. |
this.y = y + mg1.content; |
166. |
x = this.x + 7 + mg1.content; |
167. |
sum = sum + x + y; |
168. |
this.x = mg1.content + x + 8; |
169. |
System.out.println(x + + y+ + sum); |
170. |
return y; |
171. |
} |
172. |
} |
Write the output of the following code: [Answer on the question paper]
Q5 q = new Q5(); |
x |
y |
sum |
q.methodA(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Task 19
public class FinalT6A{ |
public int temp = 3; |
private int sum; |
private int y = 2; |
public FinalT6A(int x, int p){ |
temp+=3; |
y = temp p; |
sum = temp + x; |
} |
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(3,2); FinalT6A q2 = new FinalT6A(2,3); q1.methodA(); q2.methodA(); |
X |
y |
sum |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Task 20
public class MidQ3A{ |
public int sum; |
public int y; |
public void methodA(){ |
int x=0, y =0, k = 0; |
int [] msg = new int[1]; |
msg[0] = 5; |
while (k < 2){ |
y = y + msg[0]; |
x = y + methodB(msg, k);; |
sum = x + y + msg[0]; |
System.out.println(x + + y+ + sum); |
k++; |
} |
} |
private int methodB(int [] mg2, int mg1){ |
int x = 0; |
y = y + mg2[0]; |
x = x + 3 + mg1; |
sum = sum + x + y; |
mg2[0] = y + mg1; |
mg1 = mg1 + x + 2; |
System.out.println(x + + y+ + sum); |
return mg1; |
} |
} |
In the above program show the values that are going to be printed as output if you run the methodA() on an instance of Class MidQ3A.
Task 21
public class MidQ3B{ |
public int sum; |
public int y; |
public void methodA(){ |
int x=0, y =0, k = 0; |
int [] msg = new int[1]; |
msg[0] = 5; |
while (k < 2){ |
y = y + msg[0] + 1; |
x = y + methodB(msg, k); |
sum = x + y + msg[0]; |
System.out.println(x + + y+ + sum); |
k++; |
} |
} |
private int methodB(int [] mg2, int mg1){ |
int x = 0; |
y = y + mg2[0]; |
x = x + 2 + mg1; |
sum = sum + x + y; |
mg2[0] = y + mg1 -2; |
mg1 = mg1 + x + 1; |
System.out.println(x + + y+ + sum); |
return mg1; |
} |
} |
In the above program show the values that are going to be printed as output if you run the methodA() on an instance of Class MidQ3B.
Task: 22
class A{ |
public 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 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 23
class A{ |
public int temp = 4; |
public int sum; |
public int y; |
public A(){ |
y = temp 2; |
sum = temp + 3; |
temp-=2; |
} |
public void methodA(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); |
} |
} |
class B{ |
public int x; |
public int temp = -5; |
public int y = 3; |
public int sum = 2; |
public B(){ |
y = temp + 3 ; |
sum = 3 + temp + 2; |
temp-=1; |
} |
public B(B b){ |
sum = b.sum; |
x = b.x; |
} |
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); |
} |
public void methodA(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); |
} |
} |
Consider the following code:
A a1 = new A(); B b1 = new B(); B b2 = new B(b1); a1.methodA(1, 1); b1.methodA(1, 2); b2.methodB(3, 2); |
x |
y |
sum |
|
|
|
|
|
|
|
|
|
Task 24
1 |
class A{ |
2 |
public int temp = 4; |
3 |
public int sum = 1; |
4 |
public int y = 2; |
5 |
public A(){ |
6 |
y = temp 2; |
7 |
sum = temp + 3; |
8 |
temp-=2; |
9 |
} |
10 |
public void methodA(int m, int n){ |
11 |
int x = 0; |
12 |
y = y + m + (temp++); |
13 |
x = x + 2 + n; |
14 |
sum = sum + x + y; |
15 |
System.out.println(x + + y+ + sum); |
16 |
} |
17 |
} |
18 |
class B{ |
19 |
public int x = 1; |
21 |
public int y = 33; |
22 |
public int temp = 7; |
23 |
public int sum = 6; |
24 |
public B(){ |
25 |
y = temp + 3 ; |
26 |
sum = 3 + temp + 2; |
27 |
temp-=1; |
28 |
} |
29 |
public B(B b){ |
30 |
sum = b.sum; |
31 |
x = b.x; |
32 |
} |
33 |
public void methodA(int m, int n){ |
34 |
int x = 0; |
35 |
y = y + m + (++temp); |
36 |
x = x + 7 + n; |
37 |
sum = sum + x + y; |
38 |
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 + this.sum; |
|
System.out.println(x + + y+ + sum); |
|
} |
|
} |
Consider the following code:
A a1 = new A(); B b1 = new B(); B b2 = new B(b1); a1.methodA(1, 1); b1.methodA(1, 2); b2.methodB(3, 2); |
x |
Y |
sum |
|
|
|
|
|
|
|
|
|
Task 25
public class A{ |
public int temp = 3; |
public int sum; |
public int y; |
public A(){ |
y = temp 1; |
sum = temp + 2; |
temp-=2; |
} |
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(x + + y+ + sum); |
} |
} |
public class B { |
public int x = 1; |
public int sum = 2; |
public int temp = 3; |
public int y = 5; |
public B(){ |
y = temp + 1 ; |
x = 3 + temp + x; |
temp-=2; |
} |
public B(B b){ |
sum = b.sum + sum; |
x = b.x + x; |
} |
public void methodA(int m, int [] n){ |
int x = 0; |
y = y + m + (temp++); |
x = x + 5 + (++n[0]); |
sum = sum + x + y; |
n[0] = sum + 7; |
System.out.println(x + + y+ + sum); |
} |
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(x + + y[0]+ + sum); |
} |
} |
Consider the following code:
int x[] = {23}; A a1 = new A(); B b1 = new B(); B b2 = new B(b1); a1.methodA(1, x); b2.methodB(3, 2); a1.methodA(1, x); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Task 26
public class FinalT6A{ |
public int temp = 4; |
private int sum; |
private int y = 1; |
public FinalT6A(int x, int p){ |
temp+=1; |
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 + 3 + 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 27
13. |
class msgClass{ |
14. |
public int content; |
15. |
} |
173. |
public class Q5{ |
174. |
private int sum; |
175. |
private int y; |
176. |
public int x; |
177. |
public Q5(){ |
178. |
sum = 3; |
179. |
x = 1; |
180. |
y = 6; |
181. |
} |
182. |
public void methodA(){ |
183. |
int x=1, y=1; |
184. |
msgClass [] msg = new msgClass[1]; |
185. |
msgClass myMsg = new msgClass(); |
186. |
myMsg.content = this.x; |
187. |
msg[0] = myMsg; |
188. |
msg[0].content = this.y + myMsg.content; |
189. |
this.y = this.y + methodB(msg[0]); |
190. |
y = methodB(msg[0]) + this.y; |
191. |
x = y + methodB(msg, msg[0]); |
192. |
sum = x + y + msg[0].content; |
193. |
System.out.println(x + + y+ + sum); |
194. |
} |
195. |
private int methodB(msgClass [] mg2, msgClass mg1){ |
196. |
int x = 1; |
197. |
y = y + mg2[0].content; |
198. |
mg2[0].content = y + mg1.content; |
199. |
x = x + 3 + mg1.content; |
200. |
sum = sum + x + y; |
201. |
mg1.content = sum mg2[0].content ; |
202. |
System.out.println(this.x + + this.y+ + sum); |
203. |
return sum; |
204. |
} |
205. |
private int methodB(msgClass mg1){ |
206. |
int x = 1, y = 1; |
207. |
y = sum + mg1.content; |
208. |
this.y = y + mg1.content; |
209. |
x = this.x + 3 + mg1.content; |
210. |
sum = sum + x + y; |
211. |
this.x = mg1.content + x + 2; |
212. |
System.out.println(x + + y+ + sum); |
213. |
return y; |
214. |
} |
215. |
} |
Write the output of the following code: [Answer on the question paper]
Q5 q = new Q5(); q.methodA(); |
x |
y |
sum |
|
|
|
|
Reviews
There are no reviews yet.