THINK FIRST·CODE LATER

← All labs

Circle2D: objects as arguments

Problem

Write a class Circle2D (centre x, y and radius, all private) and a public class CircleApp.

  • Circle2D() creates the circle of radius 1 centred at the origin, through this(0, 0, 1); Circle2D(x, y, radius).
  • getArea(), getPerimeter() (use Math.PI).
  • contains(double px, double py) — is the point inside the circle (border included)?
  • contains(Circle2D other) — is the whole other circle inside this one?
  • overlaps(Circle2D other) — do the circles share at least one point?
  • toString().

main reads the first circle (x y radius), the second circle and a point. For the input

2 2 5.5 4 5 10.5 3 3

the program prints

First: circle at (2.0, 2.0) with radius 5.5
Area 95.03, perimeter 34.56
Second: circle at (4.0, 5.0) with radius 10.5
First contains the point (3.0, 3.0): true
First contains second: false
Second contains first: true
They overlap: true
First overlaps the default circle: 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-7-data-1.txt

2 2 5.5 4 5 10.5 3 3
What your program must print for l11-7-data-1.txt
First: circle at (2.0, 2.0) with radius 5.5
Area 95.03, perimeter 34.56
Second: circle at (4.0, 5.0) with radius 10.5
First contains the point (3.0, 3.0): true
First contains second: false
Second contains first: true
They overlap: true
First overlaps the default circle: 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-7-data-1.txt

Windows PowerShell does not understand <:

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