Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

Use a Point class with the distanceToOrigin method that returns the distance to the origin point (0; 0). The main method defined below (template) class creates an ArrayList of Point objects (initially empty), then prompt the user for pairs of x and y (integer) values in a continuous (innite) loop. The main method calls an insert method to insert each unique pair the user enters into the correct position in the list according to its distance from the origin (0; 0).

By constructing the list one point at a time, we have the opportunity to easily keep it sorted without having to sort it separately later. This technique is similar to insertion sort. Be sure to use the appropriate overloaded add method in the ArrayList class. If the user enters a duplicate point, it should be ignored (think about how can you tell if it's a duplicate). Whenever a new point is added to the list, the contents of the list should be printed, along with the point's distance from the origin.

What to implement

You will implement the method distanceToOrigin and insert in the provided code template.

3 Sample Output

Enter x, y values (type 0 0 to exit): 2 3

(2, 3); distance to origin: 3.61

Enter x, y values (type 0 0 to exit): 4 2

(2, 3); distance to origin: 3.61

(4, 2); distance to origin: 4.47

Enter x, y values (type 0 0 to exit): 2 3

Enter x, y values (type 0 0 to exit): 1 2

(1, 2); distance to origin: 2.24

(2, 3); distance to origin: 3.61

(4, 2); distance to origin: 4.47

Enter x, y values (type 0 0 to exit): 0 0

----------------------------------------------------------------------------------------------------------------------------------------------

package lab7;


import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;


public class Lab7 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

List lst = new ArrayList<>();

while(true) {

System.out.print("Enter x, y values (type 0 0 to exit):");

int x = in.nextInt();

int y = in.nextInt();

if (x == 0 && y == 0) {

break;

}

in.nextLine();

if(insert(new Point(x, y), lst)) {

System.out.println(print(lst));

}


}

in.close();

}

static String print(List lst) {

String ret = "";

for(Point p : lst) {

ret = ret + String.format("%s; distance to origin: %2.2fn", p, p.distanceToOrigin());

}

return ret;

}

static boolean insert(Point point, List lst) {

// TODO:

// insert "point" into "lst" so that "lst" remains sorted in increasing order by "distance to orgin"

// return true if "point" is not already in "lst"

// return false otherwise

}

}


class Point {

private int x, y;

private static Point zero = new Point(0, 0);

Point(int x, int y) {

this.x = x;

this.y = y;

}

double distance(Point that) {

return Math.sqrt(Math.pow(this.x - that.x, 2) + Math.pow(this.y - that.y, 2));

}

double distanceToOrigin() {

// TODO: return distance to (0, 0)

}

@Override

public boolean equals(Object that) {

boolean ret = false;

if(that instanceof Point) {

Point thatPoint = (Point) that;

ret = x == thatPoint.x && y == thatPoint.y;

}

return ret;

}

@Override

public String toString() { return String.format("(%d, %d)", x, y); }

}

Java, Programming

  • Category:- Java
  • Reference No.:- M92753077
  • Price:- $10

Priced at Now at $10, Verified Solution

Have any Question?


Related Questions in Java

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

Part a specification - robot simulationpart a

PART A Specification - Robot Simulation PART A Requirements To complete this assignment you will use the supplied eclipse project Robot P1/. It is already set up to execute a simple arm movement loop which you will build ...

Assignment - java program using array of objectsobjectives

Assignment - JAVA Program using array of objects Objectives - This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details - For this assignment, you are required to develop a Menu ...

Can someone kindly help me to consider whether java

Can someone kindly help me to consider whether Java provides the facility of operator overloading? If it does, may you kindly describe how overloading operators can be accomplished? If not, may you kindly describe why yo ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As