Question

public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX;...

public class Point {

int x;

int y;

public Point(int initialX, int initialY){

x = initialX;

y= initialY;

}

public boolean equals (Object o){

if (o instanceof Point){

Point other = (Point)o;

return (x == other.x && y == other.y);

}else{

return false;

}

}

}

We haev defined "equals" method for our class using "instanceof".

We define and use instances (or objects) of this class in the following scenarios. In each case, specify what is the output. (hint: there is no error in the code - focus on understanding how reference types work).

Scenario 1:

public static void main(String[] args){

Point p1 = new Point(7, 2);

Point p2 = new Point(7, 2);

Point p3 = p2;

System.out.println((p2==p3)) + " " + (p1 == p2));

}

Scenario 2:

public static void main(String[] args){

Point p1 = new Point(7, 2);

Point p2 = new Point(7, 2);

Point p3 = p2;

System.out.println(p2.equals(3) + " " + p1.equals(p2));

}

Homework Answers

Answer #1

Scenario 1:

public static void main(String[] args){

Point p1 = new Point(7, 2); //A Point object p1 initialized

Point p2 = new Point(7, 2);//A Point object p2 initialized

Point p3 = p2; //Here Point p3 is given reference of Point P2

System.out.println((p2==p3)) + " " + (p1 == p2));

}
Therefore p2 and p3 has same reference,p2==p3 (true)
p2 and p1 do not have same reference,p2==p1 (false)

output : true false

Scenario 2:

public static void main(String[] args){

Point p1 = new Point(7, 2);

Point p2 = new Point(7, 2);

Point p3 = p2;

System.out.println(p2.equals(3) + " " + p1.equals(p2));

}
equal function checks object type and there element values.
if two objects are of same type and there corresponding values are same,then equal() function will return true.

output : true true

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
(Java) Write the missing equals method needed for the Point class shown on the screen to...
(Java) Write the missing equals method needed for the Point class shown on the screen to work with this program. The missing method would be added to the Point class. class PointTestP3 {   public static void main(String[] args) {    Point p1 = new Point(5, 6);     Point p2 = new Point(5, 6);        System.out.println(p1.equals(p2)); // prints "true"   } }
class Ex1{ 2. public static void main(String args[]){ 3. int x = 10; 4. int y...
class Ex1{ 2. public static void main(String args[]){ 3. int x = 10; 4. int y = new Ex1().change(x); 5. System.out.print(x+y); 6. } 7. int change(int x){ 8. x=12; 9. return x; 10. } 11. } Can you please explain this entire code and what is happening?
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
What is the output of the following code segment? public class Exception { ... static int...
What is the output of the following code segment? public class Exception { ... static int divider(int x, int y) { try { return x/y; } catch (ArrayIndexOutOfBoundsException a) { System.out.print("A"); return 1; } } static int computer(int x, int y) { try { return divider(x,y); } catch (NullPointerException b) { System.out.print("B"); } return 2; } public static void main(String args[]){ try { int i = computer (100, 0); } catch (ArithmeticException c) { System.out.print("C"); } } } ABC A...
import java.io.PrintStream; import java.util.Arrays; public class joker { public static int smallest(int[] v1, int[] v2) {...
import java.io.PrintStream; import java.util.Arrays; public class joker { public static int smallest(int[] v1, int[] v2) { return 0; } public static int[] convert1(String str) { return new int[1]; } public static String convert2(int[] v) { return ""; } public static void main(String[] args) { testSmallest(); testConvert(); } public static void testSmallest() { System.out.println("Testing your method smallest(...)"); int[][] testVectors1 = new int[][]{{1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3}, {1, 2, 3}, {2, 3, 4}}; int[][] testVectors2 = new...
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1 = new String("hello"); String s2 = "hello"; String s3 = "hello";    System.out.println(s1 == s3); System.out.println(s1.equals(s3)); System.out.println(s2 == s3); } } When we run the program, the output is: false true true Explain why this is the output, using words and/or pictures.
Which method is correct to access the value of count? public class Person { private String...
Which method is correct to access the value of count? public class Person { private String name; private int age; private static int count = 0; } A. private int getCount() {return (static)count;} B. public static int getCount() {return count;} C. public int getCount() {return static count;} D. private static int getCount() {return count;} How can you print the value of count? public class Person { private String name; private int age; private static int count=0; public Person(String a, int...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
Predict the output from the following program. Select all lines of output that are produced by...
Predict the output from the following program. Select all lines of output that are produced by this program. If there are computed values in an output line, you must select the line with the correct value as computed by the program. public class WalkToSchool20 {         public static void main (String[] args) {                 Pedestrian p1 = new Pedestrian ( 1 );                 Pedestrian p2 = new Pedestrian ( 2 );                 Pedestrian p3 = new Pedestrian ( 3 );...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT