1.設計一個抽象類別圖形類,在該類中包含有至少兩個抽象方法求周長和求面積,分別定義圓形類、長方形類、矩形類來繼承圖形類,並實現上述兩個方法。並建立執行個體驗證。
2.package com.majing;3.4.public class test {5.public static void main(String[] args) {6.Roundness r = new Roundness(3);7.System.out.println(r.perimeter());8.System.out.println(r.area());9.10.Rectangle rec = new Rectangle(5,6);11.System.out.println(rec.perimeter());12.System.out.println(rec.area());13.14.Matrix m = new Matrix(12);15.System.out.println(m.perimeter());16.System.out.println(m.area());17.}18.19.}20.21.abstract class Graph {22.abstract double perimeter();23.abstract double area();24.}25.26.class Roundness extends Graph27.{28.private int radius;29.30.Roundness(int r)31.{32.radius = r;33.}34.35.double perimeter()36.{37.return 2*3.1415*radius;38.}39.40.double area()41.{42.return 3.14159*3.14159*radius;43.}44.}45.46.class Rectangle extends Graph47.{48.private double length;49.private double width;50.51.Rectangle(double l, double w)52.{53.length = l;54.width = w;55.}56.57.double perimeter()58.{59.return 2*(length + width);60.}61.62.double area()63.{64.return length*width;65.}66.}67.68.class Matrix extends Graph69.{70.private double side;71.72.Matrix(double s)73.{74.side = s;75.}76.double perimeter()77.{78.return 4*side;79.}80.81.double area()82.{83.return side*side;84.}85.}
2.建立以下幾個介面:進食的介面、睡覺的介面、思考的介面、說話的介面。分別定義貓類、人類、鳥類來選擇實現上述的介面,並建立執行個體驗證。
package com.majing;public class test2 {public static void main(String[] args) {Cat c = new Cat();c.eat_things();c.say_ways();c.sleep_ways();Human h = new Human();h.eat_things();h.say_ways();h.sleep_ways();h.think_things();Birds b = new Birds();b.eat_things();b.sleep_ways();b.say_ways();}}interface Eat{void eat_things();}interface Sleep{void sleep_ways();}interface Think{void think_things();}interface Say{void say_ways();}class Cat implements Eat,Sleep,Say{public void eat_things(){System.out.println("Cat eat fish");}public void sleep_ways(){System.out.println("Cats sleep at cattery");}public void say_ways(){System.out.println("cats say miao miao");}}class Human implements Eat,Sleep,Think,Say{public void eat_things(){System.out.println("Human eat food and fruits");}public void sleep_ways(){System.out.println("Human sleep in their beds");}public void think_things(){System.out.println("Cars, house, mate");}public void say_ways(){System.out.println("They use their languages");}}class Birds implements Eat,Sleep,Say{public void eat_things(){System.out.println("Birds eat bugs");}public void sleep_ways(){System.out.println("Birds sleep on branch");}public void say_ways(){System.out.println("Birds usually sing");}}