Create an arraylist of point objects

Assignment Help JAVA Programming
Reference no: EM131914413

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

}

Reference no: EM131914413

Questions Cloud

Precis for the assigned chapters of rise of the redengineers : The goal is to have you beable to pick out the overall argument and the argument/point of each chapter and represent itaccurately and concisely.
What is the rate of return on her investment : What is the rate of return on her investment? What is the margin on IT’s account when they first purchase the stock?
Confidence interval for the population proportion : A survey found that out of 200 workers, 168 said they were interrupted three or more times by phone messages, faxes, etc.
Low level of physical activity : In a study of women and heart disease, the following sample results were obtained: Among 10,239 women with low level of physical activity
Create an arraylist of point objects : 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
What will be the change in your profit : Suppose your company imports computer motherboards from Singapore. what will be the change in your profit?
Discuss circumstances in which entities are likely to use : The methods of earnings management including: accounting policy choice. Discuss the circumstances in which entities are likely to use each method.
P-value and the rejection point methods : Use a 0.05 significance level to test the claim that the mean head circumference of all two-month-old babies is equal to 40 cm.
Consider exchange-traded call option contract : Consider an exchange-traded call option contract to buy 500 shares with a strike price of $40 and maturity in four months.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd