java語言程式設計基礎篇第十三章編程練習題__編程

來源:互聯網
上載者:User
1234/*我在 java核心技術 裡面看到了一個範例程式碼挺符合這個題目,不過我想吐槽那裡面寫的好麻煩,滿滿的全是方法的調用,感覺還不如用c寫出來簡潔呢*/5GeometricObject.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());    }}910111213class 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());    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.