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"   } }
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...
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 );...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
what output is produced by the following code and explain how it works. public class A...
what output is produced by the following code and explain how it works. public class A { int a = 1; int b = 2; public int getSum(int a, int b) {     this.a+=a;     this.b+=b;     return this.a + this.b; } } public class B extends A { int a = 3; int b = 4; public int getSum(int a, int b) {     this.b=a;     super.b=b+b;     return super.a+this.b; } } public class q2 { public static void...