-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCircle.java
More file actions
39 lines (37 loc) · 2.05 KB
/
TestCircle.java
File metadata and controls
39 lines (37 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class TestCircle { // Save as "TestCircle.java"
public static void main(String[] args) {
// Declare an instance of Circle class called c1.
// Construct the instance c1 by invoking the "default" constructor
// which sets its radius and color to their default value.
Circle c1 = new Circle();
// Invoke public methods on instance c1, via dot operator.
/*System.out.println(c1.radius);
Not visable to the program*/
System.out.println("The circle has radius of "
+ c1.getRadius() + " and area of " + c1.getArea());
// Declare an instance of class circle called c2.
// Construct the instance c2 by invoking the second constructor
// with the given radius and default color.
Circle c2 = new Circle(2.0);
// Invoke public methods on instance c2, via dot operator.
System.out.println("The circle has radius of "
+ c2.getRadius() + " and area of " + c2.getArea());
Circle c3 = new Circle(3.0, "red");
System.out.println("The circle has radius of "
+ c3.getRadius() + " and area of " + c3.getArea() + " and is " + c3.getColor());
Circle c4 = new Circle(2.0, "red"); // construct an instance of Circle
c4.setRadius(5.0); // change radius
System.out.println("radius is: " + c4.getRadius()); // Print radius via getter
c4.setColor("blue"); // Change color
System.out.println("color is: " + c4.getColor()); // Print color via getter
// You cannot do the following because setRadius() returns void,
// which cannot be printed.
//System.out.println(c4.setRadius(4.0));
Circle c = new Circle(5.0);
System.out.println(c.toString()); // explicit call
Circle c6 = new Circle(1.2);
System.out.println(c6.toString()); // explicit call
System.out.println(c6); // println() calls toString() implicitly, same as above
System.out.println("Operator '+' invokes toString() too: " + c6); // '+' invokes toString() too
}
}