Question

In: Computer Science

Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...

Consider the following class:
import java.util.Scanner;
public class MyPoint {
private double x;
private double y;
public MyPoint() {
this(0, 0);
}
public MyPoint(double x, double y) {
this.x = x;
this.y = y;
}
// Returns the distance between this MyPoint and other
public double distance(MyPoint other) {
return Math.sqrt(Math.pow(other.x - x, 2) +
Math.pow(other.y - y, 2));
}
// Determines if this MyPoint is equivalent to another MyPoint
public boolean equals(MyPoint other) {
return this.x == other.x && this.y == other.y;
}
// Returns a String like “(4.5, -4.8)”
public String toString() {
return "(" + x + ", " + y + ")";
}
// Creates and returns a new MyPoint based on data from a
// Scanner. If there’s nothing to be read in, it returns null.
public static MyPoint read(Scanner sc) {
if (!sc.hasNext())
return null;
double x = sc.nextDouble();
double y = sc.nextDouble();
return new MyPoint(x, y);

}
}
Your job is to write a class called Rectangle. Here’s the UML diagram:

Rectangle

- a: MyPoint
- b: MyPoint
- c: MyPoint
- d: MyPoint
+ Rectangle(a: MyPoint, b: MyPoint, c: MyPoint, d: MyPoint)
+ equals(other: Rectangle): boolean
+ toString(): String
+ perimeterLength(): double
+ read(sc: Scanner): Rectangle
Notes:
● The data fields represent the four corners of a rectangle.
● The equals method should determine if this Rectangle equals other. Hint: use the
equals method of MyPoint.
● The toString method should return a String like “a = (5.5, 4.0), b = (34.6, -3.3), c = (0, 6),
d = (0.6, 6.6)”.
● The perimeterLength method should return the length of the perimeter. Hint: use the
distance method of MyPoint.
● The read method should be defined as usual; it should be similar to the read method in
part 3 of Codelab Project 2. That is, if there’s nothing more to read in, return null.
Otherwise, read in the appropriate number of MyPoints (hint: use the read method of
MyPoint) and return a new Rectangle.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks


//Rectangle.java

import java.util.Scanner;

public class Rectangle {
        // attributes
        private MyPoint a;
        private MyPoint b;
        private MyPoint c;
        private MyPoint d;

        // constructor taking values for all 4 corner points
        public Rectangle(MyPoint a, MyPoint b, MyPoint c, MyPoint d) {
                this.a = a;
                this.b = b;
                this.c = c;
                this.d = d;
        }

        // returns true if this rectangle equals to other
        public boolean equals(Rectangle other) {
                return a.equals(other.a) && b.equals(other.b) && c.equals(other.c)
                                && d.equals(other.d);
        }

        // returns a String containing rectangle info
        public String toString() {
                return "a = " + a + ", b = " + b + ", c = " + c + ", d = " + d;
        }

        // returns perimeter of rectangle
        public double perimeterLength() {
                // finding length and width from corner points, returning perimeter
                // using formula 2(l+w)
                double length = a.distance(b);
                double width = a.distance(c);
                return 2 * (length + width);
        }

        // reads 4 points, then creates and returns a Rectangle.
        public static Rectangle read(Scanner sc) {
                MyPoint p1 = MyPoint.read(sc);
                MyPoint p2 = MyPoint.read(sc);
                MyPoint p3 = MyPoint.read(sc);
                MyPoint p4 = MyPoint.read(sc);
                return new Rectangle(p1, p2, p3, p4);
        }

}

Related Solutions

Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass;...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass; double velocity; double totalkineticEnergy;    Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the objects mass in kilograms"); mass = keyboard.nextDouble();    System.out.println ("Please enter the objects velocity in meters per second: "); velocity = keyboard.nextDouble();    double actualTotal = kineticEnergy (mass, velocity); System.out.println ("Objects Kinetic Energy: " + actualTotal); } public static double kineticEnergy (double mass, double velocity) { double totalkineticEnergy =...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following:...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following: // // 1.) Define a private instance variable which can // hold a reference to a Random object. // // 2.) Define a constructor which takes a seed value. // This seed will be used to initialize the // aforementioned Random instance variable. // // 3.) Define a static method named numberToDirection // which takes a direction number and returns a String // representing...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main {   public static void main( String[] args ) {     Scanner myInput = new Scanner(System.in); // Create a Scanner object     System.out.println("Enter (3) digits: ");     int W = myInput.nextInt();     int X = myInput.nextInt();     int Y = myInput.nextInt();      } } Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first < second) return "less than"; else if (first == second) return "equal to"; else return "greater than";       }    // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int first = input.nextInt(); System.out.print("Enter second integer: "); int second = input.nextInt(); System.out.println("The first integer is " + comparison(first, second) + " the...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r =...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r = new Random(seed); }    public static String numberToDirection(int a){ if(a==0) return "North";    if(a==1) return "NorthEast"; if(a==2) return "East"; if(a==3) return "Southeast"; if(a==4) return "South"; if(a==5) return "Southwest"; if(a==6) return "West";    if(a==7) return "Northwest";    return "Invalid Direction" ; } public String randomDirection(){ return numberToDirection(r.nextInt()% 4 + 1); } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter seed: ");...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public static void main(String[] args) {        System.out.println("This program will ask the user for three sets of two numbers and will calculate the average of each set.");        Scanner input = new Scanner(System.in);        int n1, n2;        System.out.print("Please enter the first number: ");        n1 = input.nextInt();        System.out.print("Please enter the second number: ");        n2 =...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT