THINK FIRST·CODE LATER

← All labs

MyPoint: overloaded constructors and methods

Problem

Write a class MyPoint and a public class PointApp.

  • Two constructors: MyPoint() creates the origin by calling the other one with this(0, 0); MyPoint(double x, double y).
  • Two methods with the same name: distance(double x, double y) and distance(MyPoint other) — the second must reuse the first.
  • midpoint(MyPoint other) returns a new MyPoint; equals(MyPoint other) compares the coordinates; toString() gives (1.0, 2.0).

main reads the coordinates of two points p and q. For the input

0 0 3 4

the program prints

p = (0.0, 0.0), q = (3.0, 4.0)
p to q: 5.000
p to (3.0, 4.0): 5.000
p to the origin: 0.000
Midpoint: (1.5, 2.0)
Midpoint to p and to q: 2.500 and 2.500
p equals q: false
p equals a new point with the same coordinates: true

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l11-6-data-1.txt

0 0 3 4
What your program must print for l11-6-data-1.txt
p = (0.0, 0.0), q = (3.0, 4.0)
p to q: 5.000
p to (3.0, 4.0): 5.000
p to the origin: 0.000
Midpoint: (1.5, 2.0)
Midpoint to p and to q: 2.500 and 2.500
p equals q: false
p equals a new point with the same coordinates: true

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l11-6-data-1.txt

Windows PowerShell does not understand <:

Get-Content l11-6-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.