Introduction To Programming Language II (Java) : Section A
Introduction To Programming Language II (Java) : Section A
Southeast University
Department of Computer Science and Engineering/School of Science and Engineering
Introduction to Programming Language II (Java)
Instructions:
● Replace the blue texts above with your credentials
● You must not change the format of this document in any way
● Every answer must be written in your own words
● Answers must be on point as unnecessary information will be penalized
● If you use pen and paper, you must maintain chronological order
● Name your file in the following format: Name - ID. Example: John Doe -
2015100000213
● Final file must be converted to PDF
● File must be uploaded to provided Google Form in due time; no email submissions
will be accepted
Penalties:
● If you cheat, you will receive 0 marks among other potential disciplinary actions
Section A
All the answers in this section must contain code snippets as examples.
When we want to use class variable in a scope where variable or parameter with the same
name exists then we need to use this keyword.
Method with the same name with different parameter in same class is called method
overloading.
3) Discuss the == operator and equals method when it comes to comparing Strings.
== Operator check the reference address of string variable is same or not and equal () check
the value of the string same or not.
When we declare a variable or method static then we can use those variable or method
without creating the object.
Section B
1) Create a class based on a real world setting of your choosing. The class must have:
At least 4 private variables with at least 2 different data types
At least 2 constructors
Get/set methods for the class variables
At least 2 overloaded methods
Afterwards, create another class (with a main method) to test everything in the previously
created class.
public class Main {
}
}
class Calculator {
1)
class Test4{
public int sum;
public int y;
public void methodA(int x, int y){
int [] msg = new int[1];
msg[0] = x+1;
y = this.y + methodB(msg[0]);
x = y + methodB(msg, msg[0]);
sum = x + y + msg[0];
System.out.println(x + " " + y+ " " + sum);
}
private int methodB(int mg2[], int mg1){
int x = 0;
y = y + mg2[0];
x = x + 33 + mg1;
sum = sum + x + y;
mg2[0] = y + mg1;
mg1 = mg1 + x + 2;
System.out.println(x + " " + y+ " " + sum);
return sum;
}
private int methodB(int mg1){
int x = 0;
int y = 0;
y = y + mg1;
x = x + 33 + mg1;
sum = sum + x + y;
this.y = mg1 + x + 2;
System.out.println(x + " " + y+ " " + sum);
return y;
}
}
0 0 0 9 @a Index 1 0 0
1 :0
0+1=
1
0+1
=1
0+3 0+1=
3+1 1
=34
0+34+1= 1+34+2
35 = 37
34 1 35
1+107= @ 1 0
108 a1
37+1= 0+33
38 +1=3
4
35+34+3 38+1 1+34+2
8 =107 =39 =37
34 38 107
108+1+3
9=148
108 1 148
2)
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;
}
}
Id : 2019100010009
last two digit : 0,9
temp sum Y x p x y tem n x Output
p
4 0 1 0 9
4+1=5
5-9=
-4
5+0=5
0 0 0 -4 5
0+-4=
-4
-4+2+5=
3
5 -4 0
-4+5+1=2
0+3+-4= -1
5-1+2= 6
-1 2 6
-4+3+6=5
3 -4 5
4 0 1 0 9
4+1=5
5-9=
-4
5+0=5
0 -4 5
0 0
2+2+5=9 0+2=2
5 2 0
2+5+1= 8 0+3+2=5
5+5+8=18
5 8 18
9+2+18=2
9
9 2 29
3)
class FinalT6A{
public static 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;
}
}
Hint: static variable will not have its value resetted when new objects are created.
Id: 2019100010009
last two digits: 0, 9
temp sum y x p X y temp n x Output
4 0 1 0 9
4+1=
5
5-9= -4
5+0=5
0 -4 5
0 0
0-4
= -4
-4+2+5
=3
5 -4 0
-4+5
+1=2
0+3-
4= -1
5-1+2 =6
-1 2 6
3-4+6 =5
3 -4 5
5 0 1 0 9
5+1=
6
5-9= -4
5+0=5
0 -4 5
0 0+2=2
2+2+6=1
0
6 2 0
0+3+
2 =5
2+6+1=
9
5+9+5=19
5 9 19
10+2+19
=31
10 2 31