java语言程序设计基础篇第十三章编程练习题

1
2
3
4
/*
我在 java核心技术 里面看到了一个示例代码挺符合这个题目,不过我想吐槽那里面写的好麻烦,满满的全是方法的调用,感觉还不如用c写出来简洁呢
*/
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());
    }
}
  • 9
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值