0% found this document useful (0 votes)
49 views11 pages

Introduction To Programming Language II (Java) : Section A

The document provides instructions for a bonus assignment for an Introduction to Programming Language II (Java) course. It specifies formatting requirements and submission details. Penalties are outlined for cheating or not following instructions. The assignment contains two sections - Section A requires code snippets as examples in the answers, and Section B requires creating a class with specific requirements and testing it with a main method in another class. Section C requires creating trace tables to simulate program execution.

Uploaded by

Fast Security
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views11 pages

Introduction To Programming Language II (Java) : Section A

The document provides instructions for a bonus assignment for an Introduction to Programming Language II (Java) course. It specifies formatting requirements and submission details. Penalties are outlined for cheating or not following instructions. The assignment contains two sections - Section A requires code snippets as examples in the answers, and Section B requires creating a class with specific requirements and testing it with a main method in another class. Section C requires creating trace tables to simulate program execution.

Uploaded by

Fast Security
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Md.

Nizam Uddin Shamrat 2019100010009

Southeast University
Department of Computer Science and Engineering/School of Science and Engineering
Introduction to Programming Language II (Java)

Bonus Assignment Fall 20 Duration: 1 week

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.

1) What is the use of the ‘this’ keyword?

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.

2) What is method overloading?

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.

4) Discuss static members (variables and methods).

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 {

public static void main(String[] args) {

Calculator valueCalculator = new Calculator(100, 20);


valueCalculator.setWorkMode("Value Inputted");
System.out.println(valueCalculator.getWorkMode() +" : "
+valueCalculator.getFirstNumber()
+","+valueCalculator.getSecondNumber());
valueCalculator.add();
valueCalculator.sub();
valueCalculator.multi();
valueCalculator.div();
valueCalculator.setFirstNumber(20);
valueCalculator.setSecondNumber(5);
System.out.println("New value to "+valueCalculator.getWorkMode()
+"Mode : "
+valueCalculator.getFirstNumber()
+","+valueCalculator.getSecondNumber());

Calculator resultCalculator = new Calculator(20);


resultCalculator.setWorkMode("Result inputted");
System.out.println(resultCalculator.getWorkMode()+" :
"+resultCalculator.getResult());
resultCalculator.add(50,20);
resultCalculator.sub(20,17);
resultCalculator.multi(5,4);
resultCalculator.div(10,2);
resultCalculator.setResult(50);
System.out.println("New Result To "+resultCalculator.getWorkMode()
+" Mode : "+resultCalculator.getResult());

}
}

class Calculator {

private String workMode;


private int firstNumber;
private int secondNumber;
private int result;

public Calculator(int firstNumber, int secondNumber) {


this.firstNumber = firstNumber;
this.secondNumber = secondNumber;
}
public Calculator(int result) {
this.result = result;
}
public String getWorkMode() {
return workMode;
}
public void setWorkMode(String workMode) {
this.workMode = workMode;
}
public int getFirstNumber() {
return firstNumber;
}
public void setFirstNumber(int firstNumber) {
this.firstNumber = firstNumber;
}
public int getSecondNumber() {
return secondNumber;
}

public void setSecondNumber(int secondNumber) {


this.secondNumber = secondNumber;
}

public int getResult() {


return result;
}

public void setResult(int result) {


this.result = result;
}

public void add() {


int output = firstNumber + secondNumber;
System.out.println(output);
}

public void add(int firstNumber, int secondNumber) {


int output = result + firstNumber + secondNumber;
System.out.println(output);
}

public void sub() {


int output = firstNumber - secondNumber;
System.out.println(output);
}

public void sub(int firstNumber, int secondNumber) {


int output = result + firstNumber - secondNumber;
System.out.println(output);
}

public void multi() {


int output = firstNumber * secondNumber;
System.out.println(output);
}

public void multi(int firstNumber, int secondNumber) {

int output = result + (firstNumber * secondNumber);


System.out.println(output);
}

public void div() {


int output = firstNumber / secondNumber;
System.out.println(output);
}

public void div(int firstNumber, int secondNumber) {


int output = result + (firstNumber / secondNumber);
System.out.println(output);
}
}
Section C
Create trace tables to simulate the execution of the programs given below. The value of
the variables a and b are the second last and last digits of your ID respectively. For
instance if your ID is 2015100000213, a =1 and b=3.

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;
}
}

public class Test{


public static void main(String[]args){
Test4 obj = new Test4();
obj.methodA(a, b);
obj = new Test4();
obj.methodA(a, b);
}
}
Id : 2019100010009
last two digit : 0,9
sum y x y msg @a1 Mg1 x Y Mg Mg1 x Output
2[]
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

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;
}
}

public class Test{


public static void main(String[]args){
FinalT6A q1 = new FinalT6A(a, b);
q1.methodA();
FinalT6A q2 = new FinalT6A(a, b);
q1.methodA();
}
}

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;
}
}

public class Test{


public static void main(String[]args){
FinalT6A q1 = new FinalT6A(a, b);
q1.methodA();
FinalT6A q2 = new FinalT6A(a, b);
q1.methodA();
}
}

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

You might also like