THINK FIRST·CODE LATER

← All labs

Aliases and copy constructors

Problem

Write a class Point with private x and y, a normal constructor, a copy constructor Point(Point other) (it should call the first constructor with this(…)), move(dx, dy), equals(Point) and toString() → (3, 4).

In PointCopy.main, read x y dx dy and run exactly this experiment:

Point p = new Point(x, y);
Point alias = p;
Point copy = new Point(p);
alias.move(dx, dy);

Print p, alias, copy, then p == alias, p == copy, p.equals(copy); then move copy by the same amount and print p == copy and p.equals(copy) again. Predict every line before you run it.

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-2-data-1.txt

1 2 3 4
What your program must print for l11-2-data-1.txt
p     = (4, 6)
alias = (4, 6)
copy  = (1, 2)
p == alias: true
p == copy: false
p.equals(copy): false
after moving copy too:
p == copy: false
p.equals(copy): 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-2-data-1.txt

Windows PowerShell does not understand <:

Get-Content l11-2-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.