-
public class Exercise14_2 {
// Main method
public static void
main(String[] args) {
// Create two
comarable rects
ComparableCircle circle1 = new
ComparableCircle(5);
ComparableCircle circle2 = new
ComparableCircle(15);
//
Display the max rect
ComparableCircle circle3 =
(ComparableCircle)(circle1, circle2);
n(
n(circle3);
}
}
class ComparableCircle extends Circle
implements Comparable {
/** Construct a ComparableRectangle
with specified properties */
public
ComparableCircle(double radius) {
super(radius);
}
/** Implement the compareTo method
defined in Comparable */
public int compareTo(Object o) {
if (getRadius() > ((ComparableCircle)
o).getRadius())
return 1;
else if
(getRadius() < ((ComparableCircle) o).getRadius())
return -1;
else
return 0;
}
}
//: Find a maximum object
class Max {
/** Return the maximum of two objects
*/
public
static Comparable max
(Comparable o1, Comparable o2) {
if (eTo(o2) > 0)
return o1;
else
return o2;
}
}
public class Exercise14_4 {
/** Main method
*/
public
static void main(String[] args) {
House house1 = new House(1, 1750.50);
House house2 = (House)();
n(nBuilt() == nBuilt());
}
}
class House implements Cloneable,
Comparable {
private int id;
private double area;
private whenBuilt;
public House(int id, double area) {
= id;
= area;
whenBuilt = new ();
}
public double getId() {
return id;
}
public double getArea() {
return area;
}
public getWhenBuilt() {
return whenBuilt;
}
/** Override the protected
clone method defined in the Object
class, and strengthen its accessibility
*/
public
Object clone() {
try {
House house =
(House)();
ilt
= ()(());
return house;
}
catch (CloneNotSupportedException ex) {
return null;
}
}
/**
Implement the compareTo method defined in
Comparable */
public int compareTo(Object o) {
if (area > ((House)o).area)
return 1;
else if (area < ((House)o).area)
return -1;
else
return 0;
}
}
public class Exercise14_6 {
public static
void main(String[] args) {
Rectangle11_6
obj1 = new Rectangle11_6();
Rectangle11_6 obj2 = new
Rectangle11_6();
n((obj2));
n(eTo(obj2));
}
}
// : The Rectangle class that extends
GeometricObject
class Rectangle11_6
extends GeometricObject implements Comparable {
private double
width;
private
double height;
/** Default constructor */
public Rectangle11_6() {
this(1.0, 1.0);
}
/** Construct a rectangle with width
and height */
public Rectangle11_6(double width,
double height) {
= width;
= height;
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double
width) {
= width;
}
/** Return height */
public double
getHeight() {
return height;
}
/** Set a new height */
public void
setHeight(double height) {
= height;
}
/** Implement
the getArea method in GeometricObject */
public double
getArea() {
return width*height;
}
/** Implement
the getPerimeter method in GeometricObject */
public double
getPerimeter() {
return 2*(width
+ height);
}
/**
Override the toString method defined in the Object
class */
public
String toString() {
return
}
public int compareTo(Object
obj) {
if (a() >
((Rectangle11_6)obj).getArea())
return 1;
else if (a() <
((Rectangle11_6)obj).getArea())
return -1;
else
return 0;
}
public boolean equals(Object obj) {
return a() ==
((Rectangle11_6)obj).getArea();
}
}
public class
Exercise14_8 {
public static void main(String[] args)
{
new Exercise14_8();
}
public Exercise14_8() {
GeometricObject[] a = {new Circle(5),
new Circle(6),
new
Rectangle(2, 3), new Rectangle(2, 3)};
n(
}
public static double
sumArea(GeometricObject[] a) {
double sum = 0;
for
(int i = 0; i < i++)
sum += a[i].getArea();
return sum;
}
// : The Rectangle class that extends
GeometricObject
class Rectangle extends
GeometricObject {
private double width;
private double height;
/** Construct a rectangle
with width and height */
public Rectangle(double width, double
height) {
= width;
=
height;
}
/**Return width*/
public double
getWidth() {
return width;
}
/**Set a new width*/
public void
setWidth(double width) {
= width;
}
/**Return
height*/
public
double getHeight() {
return height;
}
/**Set a new
height*/
public
void setHeight(double height) {
=
height;
}
/**Implement the getArea method in
GeometricObject*/
public double getArea() {
return width*height;
}
/**Implement the
getPerimeter method in GeometricObject*/
public double
getPerimeter() {
return 2*(width
+ height);
}
/**Override the equals method defined
in the Object class*/
public boolean equals(Rectangle
rectangle) {
return (width == th()) &&
(height ==
ght());
}
/**Override the toString method defined
in the Object class*/
public String toString() {
return
}
}
}
import .*;
public class Exercise14_10 {
static
MyCalendar calendar = new MyCalendar();
public static void main(String[] args)
{
int month = () + 1;
int
year = ();
if ( > 2)
n(
else if ( == 2) {
//use user-defined month
and year
year = nt(args[1]);
month = nt(args[0]);
(,
year);
(,
month - 1);
}
else if ( == 1)
{
//use user-
defined month for the current year
month = nt(args[0]);
(,
month-1);
}
//set date to the first day in a month
(, 1);
//print calendar for the month
printMonth(year, month);
}
static void printMonth(int
year, int month) {
//get start day
of the week for the first date in the month
int startDay = getStartDay();
//get number of days in the
month
int numOfDaysInMonth =
Month();
//print
headings
printMonthTitle(year,
month);
//print body
printMonthBody(startDay,
numOfDaysInMonth);
}
static int getStartDay() {
return (_OF_WEEK);
}