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 Triangle. Here’s the UML diagram: Triangle - a: MyPoint - b: MyPoint - c: MyPoint + Triangle(a: MyPoint, b: MyPoint, c: MyPoint) + equals(other: Triangle): boolean + toString(): String + perimeterLength(): double + read(sc: Scanner): Triangle Notes: The data fields represent the three corners of a triangle. The equals method should determine if this Triangle 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)”. 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 Triangle.

Solutions

Expert Solution

Please follow inline comments and screenshots for better understanding.

code...

MyPoint.java

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)"

    @Override

    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);

    }

}

Triangle.java

import java.util.Scanner;

// class Triangle

public class Triangle {

    // private MyPoints a,b,c

    private MyPoint a;

    private MyPoint b;

    private MyPoint c;

    // constructor

    public Triangle(MyPoint a, MyPoint b, MyPoint c) {

        this.a = a;

        this.b = b;

        this.c = c;

    }

    // toString method

    @Override

    public String toString() {

        return "a = "+a+", b = "+b+", c = "+c;

    }

    // equals method return true if all the points of both the triangle are same

    public boolean equals(Triangle other) {

        if (other != null) {

            return this.a.equals(other.a) && this.b.equals(other.b) && this.c.equals(other.c);

        }

        return false;

    }

    // return perimeter using distance method of MyPoing class

    public double perimeterLength() {

        double perimeter = a.distance(b) + b.distance(c) + a.distance(c);

        return perimeter;

    }

    // return a new Triangle instance by reading the points from read method of MyPoint class

    public static Triangle read() {

        Scanner sc = new Scanner(System.in); // creating Scanner instance

        // reading three points

        MyPoint a = MyPoint.read(sc);

        MyPoint b = MyPoint.read(sc);

        MyPoint c = MyPoint.read(sc);

        // returning new triangle instance

        return new Triangle(a, b, c);

    }

    // main method

    public static void main(String[] args) {

        // creating a Triangle instance using Trangle.read() method

        Triangle triangle1 = Triangle.read();

        // calling PerimeterLenght() to get perimeter

        System.out.format("Perimeter : %.2f\n", triangle1.perimeterLength());

        // printing the triangle

        System.out.println(triangle1);

        // creating a triangle my passing MyPoint instance

        Triangle triangle2 = new Triangle(new MyPoint(5.5, 4.0), new MyPoint(34.6, -3.3), new MyPoint(0, 6));

        // printing triangle2

        System.out.println(triangle2);

        // calling equals method to check both are equals

        System.out.println(triangle1.equals(triangle2));

    }   

}

outputs..


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.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class BowlerReader { private static final String FILE_NAME =...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class BowlerReader { private static final String FILE_NAME = "bowler.txt"; public static void main(String[] args) throws FileNotFoundException { System.out.println("Reading Data from file"); Scanner fileReader = new Scanner(new File(FILE_NAME)); System.out.printf("%-20s%-10s%-10s%-10s%-10s\n", "Sample Data", "Game 1", "Game 2", "Game 3", "Average"); int bowler = 1; while (fileReader.hasNext()) { String scores[] = fileReader.nextLine().split("\\s+"); double average = Integer.parseInt(scores[0]) + Integer.parseInt(scores[1]) + Integer.parseInt(scores[2]); average /= 3; System.out.printf("%-20s%-10s%-10s%-10s%-10.2f\n", "Bowler " + bowler, scores[0], scores[1], scores[2], average); bowler += 1; }...
submit the following files: Main.java Consider the following code: import java.util.Scanner; public class Main { public...
submit the following files: Main.java Consider the following code: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String input = ""; System.out.println("Enter the first number: "); input = in.nextLine(); int a = 0; int b = 0; a = Integer.parseInt(input); System.out.println("Enter the second number: "); input = in.nextLine(); b = Integer.parseInt(input); int result = 0; result = a/b; System.out.println("a/b : " + result); } Copy the code to your Main.java file...
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.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.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.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: ");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT