Question
need some help on these a little contested
right now the answers i have are
18 : d
19 : e
20 : c
would appriacte why i am right or wrong thank you

19. The default constructor sets x and y to (0,0) by calling the second constructor. What could be used to replace / missing
20. Which of the following correctly implements a mutator method for point? a. public double getX() { return x; public double
culesive 17. Consider the following class definition. public class Whatsit private int length; private int width: public int

sorry i added the picture of 17 by mistake
18. Which of the following correctly implements the equals method? public boolean equals (Point p) { return (x == Point. && Y


Questions 18 - 20 pertain to the following class, Point: public class Point{ private double x; private double y; public Point
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Question 18:

Answer: (d)

Explanation: Even though you haven't provided the image for option (d), it should be like return (x == p.x && y == p.y);

Question 19:

Answer: (e)

Explanation: Only option (e) initializes the values of x and y to the values provided in arguments a and b.

Question 20:

Answer: (c)

Explanation: a mutator sets the values of instance variables (variable of the object) to the one provided as arguments, and this is only done by option (c).

Code:

// Point.java

public class Point {
   private double x;
   private double y;

   // this default constructor will call the second constructor
   public Point() {
       this (0, 0);
   }
   // this second constructor sets the values of x and y
   // to the one provided as arguments
   public Point(double a, double b) {
       x = a;
       y = b;
   }
   // this mutator changes the values of x and y by the one provided
   public void setCoordinates(double a, double b) {
       x = a;
       y = b;
   }
   // equals method checks equality of coordinates of point object provided
   // as arguments by the coordinates of the current object and returs true if
   // both are equal else returns false
   public boolean equals(Point p) {
       return (x == p.x && y == p.y);
   }

   // this is an extra method added by me, to print the coordiantes
   public String toString() {
       return ("x: "+x+" y: "+y);
   }  
}

// PointTest.java

public class PointTest {
   public static void main(String[] args) {
       // declaring three objects of Point Class
       Point p1 = new Point(1, 2);
       Point p2 = new Point();
       Point p3 = new Point(1, 2);

       // priting the three objects
       System.out.println(p1);
       System.out.println(p2);
       System.out.println(p3);

       // printing the equality of three objects
       System.out.println();
       // this should print false
       System.out.println("p1 equals p2: "+p1.equals(p2));
       // this should also print false
       System.out.println("p2 equals p3: "+p2.equals(p3));
       // this should print true
       System.out.println("p3 equals p1: "+p3.equals(p1));
   }
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
need some help on these a little contested right now the answers i have are 18...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Which of the following is true about interfaces: An interface can have only non abstract methods....

    Which of the following is true about interfaces: An interface can have only non abstract methods. All methods in an interface must be abstract. A class can only implement one interface. None of the items listed. Can not contain constants but can have variables. What is the rule for a super reference in a constructor? It must be in the parent class' constructor. You cannot use super in a constructor. It must be the last line of the constructor in...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • We have written the following Class1 class: class Class1 implements SecretInterface { // instance variable private...

    We have written the following Class1 class: class Class1 implements SecretInterface { // instance variable private int x = 0; // constructor public Class1(int x) { this.x = x; } // instance methods public double myMethod1(double x, double y) { return x - y; } public void myMethod2(int x) { System.out.println(x); } public String myMethod3(String x) { return "hi " + x; } } We have also written the following Class2 class: class Class2 implements SecretInterface { // instance variable...

  • Why is my program returning 0 for the area of a triangle? public class GeometricObjects {...

    Why is my program returning 0 for the area of a triangle? public class GeometricObjects {    private String color = " white ";     private boolean filled;     private java.util.Date dateCreated;     public GeometricObjects() {         dateCreated = new java.util.Date();     }     public GeometricObjects(String color, boolean filled) {         dateCreated = new java.util.Date();         this.color = color;         this.filled = filled;     }     public String getColor() {         return color;     }     public void setColor(String color)...

  • write a completed program (included the main) for the Q: add an equals method to each...

    write a completed program (included the main) for the Q: add an equals method to each of the Rectangle circle and triangle classes introduced in this chapter. two shapes are considered equal if their fields have equivalent values. based on public class Circle implements Shape f private double radius; // Constructs a new circle with the given radius. public Circle (double radius) f this.radius - radius; // Returns the area of this circle. public double getArea) return Math.PI *radius *...

  • Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g...

    Receiveing this error message when running the Experts code below please fix ----jGRASP exec: javac -g GeometricObject.java GeometricObject.java:92: error: class, interface, or enum expected import java.util.Comparator; ^ 1 error ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. 20.21 Please code using Java IDE. Please DO NOT use Toolkit. You can use a class for GeometricObject. MY IDE does not have access to import ToolKit.Circle;import. ToolKit.GeometricObject;.import ToolKit.Rectangle. Can you code this without using the ToolKit? Please show an...

  • this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea {...

    this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea { public static void main(String[] args) { Scanner scnr = new Scanner(System.in);    Triangle triangle1 = new Triangle(); Triangle triangle2 = new Triangle(); // Read and set base and height for triangle1 (use setBase() and setHeight())    // Read and set base and height for triangle2 (use setBase() and setHeight())    // Determine larger triangle (use getArea())    private int base; private int height; private...

  • Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface....

    Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface. Couldn't figure it out. the compare to method should print 0, 1, or -1 import java.util.*; public class RectangleMain {    public static void main(String [] args) throws CloneNotSupportedException    {        /*ComparableRectangleAlsoCloneable obj1 = new ComparableRectangleAlsoCloneable(4, 5);        ComparableRectangleAlsoCloneable obj2 = (ComparableRectangleAlsoCloneable)obj1.clone();               System.out.println(obj1.toString());        System.out.println(obj1 == obj2); //false        System.out.println(obj2.toString());*/               Scanner...

  • Java Help 2. Task: Create a client for the Point class. Be very thorough with your...

    Java Help 2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below: ---After declaration, constructors invoked--- Using toString(): First point is (0, 0) Second point is (7, 13) Third point is (7, 15) Second point (7, 13) lines up vertically with third point (7, 15) Second point (7, 13) doesn't line up horizontally with third point (7, 15) Enter the x-coordinate for first...

  • Hello I have a question. I would like to check if the code follows this requirement....

    Hello I have a question. I would like to check if the code follows this requirement. Rectangle.h - A complete Rectangle Class including both declaration and definition appRectangle,cpp separate in two different files the class definition and implementation Code: rectangle.h // Rectangle class declaration. class Rectangle { private: double width; double length; public: void setWidth(double); void setLength(double); double getWidth() const; double getLength() const; double getArea() const; }; //************************************************** // setWidth assigns a value to the width member. * //************************************************** void...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT