Minggu, 27 Oktober 2013

aloritma dan pemograman 3

Program 1

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package polymorphismToyJava;

public class PolymorphismToyJava {
    static void test(){
        RectangleToy theRectangle = new RectangleToy(100,200);
        SquareToy theSquare = new SquareToy(100);
        CircleToy theCircle = new CircleToy(100);

        printGeometry(theRectangle);
        printGeometry(theSquare);
        printGeometry(theCircle);
     }
    public static void main(String[]args){
            test();
    }

static void printGeometry(GeometryToy theG) {
    System.out.println("======================================");
    System.out.println("Geometry          :"+theG.getGeometry());
    System.out.println("Luas geometri     :"+theG.area());
    System.out.println("Keliling geometri :"+theG.circumference());
 }
}

abstract class GeometryToy{
    abstract public String getGeometry();
    abstract public double area();
    abstract double circumference();
}

class RectangleToy extends GeometryToy{
    double width,height;
    public RectangleToy(double newWidht, double newHeight){
        width = newWidht;
        height = newHeight;
    }
public String getGeometry(){
    return "Persegi Panjang";
    }
public double area() {
    return width * height;
}
public double circumference (){
    return 2 * (width + height);
 }
}

class SquareToy extends GeometryToy {
    double side;
    public SquareToy (double newSide){
        side = newSide;
   }
    public String getGeometry() {
        return "Bujur Sangkar";
 }
    public double area() {
        return side * side;
    }
    public double circumference(){
        return 4 * side;
    }
}
class CircleToy extends GeometryToy {
    final double PI=3.14159;
    double radius;
    public CircleToy (double newRadius) {
        radius = newRadius;
   }
    public String getGeometry() {
        return "Lingkaran";
    }
    public double area () {
        return PI * radius * radius;
    }
    public double circumference () {
        return 2.0 * PI * radius;
    }
}

    /**
     * @param args the command line arguments
     */


        // TODO code application logic here

Tidak ada komentar:

Posting Komentar