Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume you are given a java file as below: import java.util.Scanner; public clas

ID: 3767423 • Letter: A

Question

Assume you are given a java file as below:

import java.util.Scanner;

public class CompareStrings{

public static void main(String args[]){

String a="CyberMonday";

String b="CyberMonday";

//***First comparison

if(a=="CyberMonday")

System.out.println("It is Monday");

else

System.out.println("It is not Monday");

//***Second comparison

if(a==b)

System.out.println("It is Monday");

else

System.out.println("It is not Monday");

//***Third comparison

Scanner s=new Scanner(System.in);

String c=s.nextLine();

// Please enter CyberMonday in terminal

if(a==c)

System.out.println("It is Monday");

else

System.out.println("It is not Monday");

}

}

This program is used to test the results by comparing string variables with same

value in different cases. There are three string variables in this program, which are

“a”, “b” and “c”. Variables “a” and “b” are initialized as “CyberMonday” in

program. However, variable “c” is initialized by user input. After input, the value

of c should also be “CyberMonday”.

Three comparisons are conducted in this program. Initially, we make a comparison

of variable “a” and string “CyberMonday”. After that we compare variable “a”

with variable “b”. Last the variable “a” and variable “c” are compared. Since

these three variables store the same content “CyberMonday”, we expect the

string“It is Monday”printed three times in output.

Please answer the following questions based on this program.

1) What is the real output for the above Java program if the input is “CyberMonday”? Please try to run this program in your machine.

2) Compile this java file into .class file and use command javap –c to disassemble the generated .class file. Then extract the unformmated instructions from javap output for statement “if(a==c)” and write it down.

3) Based on your answer in question (2), explain why condition a==c is true (or false) when input is “CyberMonday”?

4) How to modify above source code in Java to generate expected result when input is “CyberMonday”?

5) If you are only provided with a .class file compiled from above source code. Can you just modify the .class file to make the program print out “It is Monday” three times no matter what is the input for c? If yes, please explain your steps.

Explanation / Answer

1)
It is Monday
It is Monday
CyberMonday
It is not Monday

2)
70: astore 4
72: aload_1
73: aload 4
75: if_acmpne 89
78: getstatic #3 // Field java/lang/System.out:Ljava/
io/PrintStream;
81: ldc #4 // String It is Monday
83: invokevirtual #5 // Method java/io/PrintStream.printl
n:(Ljava/lang/String;)V
86: goto 97
89: getstatic #3 // Field java/lang/System.out:Ljava/
io/PrintStream;
92: ldc #6 // String It is not Monday
94: invokevirtual #5 // Method java/io/PrintStream.printl
n:(Ljava/lang/String;)V


3)
   if (a == c)
   is false , the equals operator will not check the content of the strings
   it will checks for hash code of the a and b objects
4)

if (a.equals(c)) is true, this will checks the content of string a and b;
Expected output:
It is Monday
It is Monday
CyberMonday
It is Monday