Minggu, 27 Oktober 2013

algoritma dan pemograman 3 (program 2)

Program 2

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

/**
 *
 * @author Syaefa
 */
public class PhytagoreanTripletToy {

    private int number=0;
    private int numberOfTriplet=0;
    final int MAX_OF_TRIPLETS = 100;

    private int[] tripletA = new int [MAX_OF_TRIPLETS];
    private int[] tripletB = new int [MAX_OF_TRIPLETS];
    private int[] tripletC = new int [MAX_OF_TRIPLETS];

    public PhytagoreanTripletToy(int number) {
        this.number = number;
        bruteForce();
    }
    public int getNumberOfTriplet() {
        return numberOfTriplet;
    }
    public int getNumber() {
        return number;
    }
    public void bruteForce () {
        for (int i=1;i<=number;i++) {
            for (int j=1;j<=number;j++) {
                for (int k=1;k<=number;k++) {
                    if (i*i == (j*j + k*k)) {
                        tripletA[numberOfTriplet] = i;
                        tripletB[numberOfTriplet] = j;
                        tripletC[numberOfTriplet] = k;
                        numberOfTriplet++;
                        if (numberOfTriplet == MAX_OF_TRIPLETS) return;
                    }
                }
            }
        }
    }
    public String toString() {
        String str="";
        for (int i=0;i<numberOfTriplet;i++)
            str += tripletA[i]+"\t"+tripletB[i]+"\t"+tripletC[i]+"\n";
        return str;
    }
    static void test () {
        System.out.println(new PhytagoreanTripletToy(10));
    }
    public static void main(String[] args) {
        test();
    }
}
    /**
     * @param args the command line arguments
     */

        // TODO code application logic here


    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

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