1
2
3
4
5
GeometricObject.java :
package yongheng;
public abstract class GeometricObject implements Comparable<GeometricObject>{
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
public static GeometricObject max(GeometricObject o1,GeometricObject o2){
if(o1.getArea() > o2.getArea())
return o1;
else
return o2;
}
protected GeometricObject(){
dateCreated = new java.util.Date();
}
protected GeometricObject(String color, boolean filled){
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getColor(){
return color;
}
public void setColor(String color){
this.color = color;
}
public boolean isFilled(){
return filled;
}
public void setFilled(boolean filled){
this.filled = filled;
}
public java.util.Date getDateCreated(){
return dateCreated;
}
@Override
public String toString(){
return "created on " + dateCreated + "\ncolor: " + color + "\nfilled: " + filled;
}
public abstract double getArea();
public abstract double getPrimeter();
@Override
public int compareTo(GeometricObject go){
if(this.getArea() > go.getArea())
return 1;
else if(this.getArea() < go.getArea())
return -1;
else
return 0;
}
}
Rectangle.java :
package yongheng;
public class Rectangle extends GeometricObject{
private double width;
private double height;
public Rectangle(){
super();
width = 0;
height = 0;
}
public Rectangle(double width, double height){
super();
this.width = width;
this.height = height;
}
public Rectangle(double width, double height, String color, boolean filled){
super(color,filled);
this.width = width;
this.height = height;
}
@Override
public double getArea(){
return width*height;
}
@Override
public double getPrimeter(){
return (width+height)*2;
}
}
Main.java :
package yongheng;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<Rectangle> list = new ArrayList<>();
list.add(new Rectangle(1,1));
list.add(new Rectangle(3,4));
list.add(new Rectangle(9,10));
list.add(new Rectangle(3,5));
list.add(new Rectangle(4,2));
list.add(new Rectangle(7,8));
list.add(new Rectangle(5,6));
list.add(new Rectangle(2,2));
Collections.sort(list);
for(Rectangle obj : list){
System.out.println(obj.getArea());
}
}
}
6.
class circle:
package yongheng;
public class Circle {
private double radius = 1.0D;
public Circle(){
radius = 1.0D;
}
public Circle(double radius){
this.radius = radius;
}
public double getArea(){
return Math.PI * radius * radius;
}
}
class comparable:
package yongheng;
public class ComparableCircle extends Circle implements Comparable<ComparableCircle>{
public ComparableCircle(){
super();
}
public ComparableCircle(double radius){
super(radius);
}
@Override
public int compareTo(ComparableCircle obj) {
if(this.getArea() > obj.getArea()){
return 1;
}
else if(this.getArea() < obj.getArea()){
return -1;
}
else
return 0;
}
}
Main:
package yongheng;
public class Main {
public static void main(String[] args){
ComparableCircle c1 = new ComparableCircle(2.0);
ComparableCircle c2 = new ComparableCircle(3.0);
System.out.println(c1.compareTo(c2));
}
}
7.
GeometricObject 类同第五题的一样
interface Colorable:
package yongheng;
public interface Colorable {
void howToColor();
}
class Square:
package yongheng;
public class Square extends GeometricObject implements Colorable{
private double side = 1.0D;
public Square(){
super();
side = 1.0D;
}
public Square(double side, String color, boolean filled){
super(color,filled);
this.side = side;
}
@Override
public void howToColor() {
System.out.println(getColor());
}
@Override
public double getArea() {
return side*side;
}
@Override
public double getPrimeter() {
return side*4.0;
}
}
Main:
package yongheng;
public class Main {
public static void main(String[] args){
GeometricObject arr[] = new GeometricObject[5];
arr[0] = new Square(1,"white",false);
arr[1] = new Square(2,"blue",true);
arr[2] = new Square(3,"black",true);
arr[3] = new Square(1,"red",true);
arr[4] = new Square(1,"yellow",false);
Square temp = null;
for(GeometricObject obj : arr){
if(obj instanceof Square){
temp = (Square)obj;
if(temp.isFilled())
temp.howToColor();
}
}
}
}
8.
class MyStack:
package yongheng;
import java.util.ArrayList;
public class MyStack implements Cloneable{
private ArrayList<Object> list = new ArrayList<Object>();
public boolean isEmpty(){
return list.isEmpty();
}
public int getSize(){
return list.size();
}
public Object peek(){
return list.get(list.size()-1);
}
public Object pop(){
Object o = list.get(list.size()-1);
list.remove(getSize()-1);
return o;
}
public void push(Object o){
list.add(o);
}
@Override
public String toString(){
return "stack: " + list.toString();
}
@SuppressWarnings("unchecked")
@Override
public Object clone() throws CloneNotSupportedException{
MyStack tStack = (MyStack)super.clone();
tStack.list = (ArrayList<Object>)list.clone();
return tStack;
}
}
Main:
package yongheng;
import java.util.Date;
public class Main {
public static void main(String[] args) throws CloneNotSupportedException{
MyStack stack = new MyStack();
for(int i = 0; i < 10; ++i)
stack.push(i);
MyStack nstack = (MyStack)stack.clone();
for(int i = 0; i < 10; ++i)
nstack.pop();
System.out.println(stack.getSize());
System.out.println(nstack.getSize());
}
}
9
10
11
12
13
class Course:
package yongheng;
public class Course implements Cloneable{
private String courseName;
private String[] students = new String[100];
private int numberOfStudents;
public Course(String courseName){
this.courseName = courseName;
}
public void addStudent(String student){
students[numberOfStudents++] = student;
}
public String[] getStudents(){
return students;
}
public int getNumberOfStudents(){
return numberOfStudents;
}
public String getCourseName(){
return courseName;
}
@Override
public Object clone(){
Course course = null;
try{
course = (Course)super.clone();
course.courseName = courseName;
course.numberOfStudents = numberOfStudents;
course.students = students.clone();
}
catch(CloneNotSupportedException ex){
}
return course;
}
}
Main:
package yongheng;
import java.util.Date;
public class Main {
public static void main(String[] args){
Course course = new Course("java");
for(int i = 0; i < 10; ++i)
course.addStudent(i + "t");
Course ct = (Course)course.clone();
ct.addStudent("zhijun");
System.out.println(course.getNumberOfStudents());
System.out.println(ct.getNumberOfStudents());
}
}