package polymorphismtoy;
/**
*
* @author depi.ariyanto
*/
public class PolymorphismToy {
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("Luasgeometry :"+theG.area());
System.out.println("Keliling geometry :"+theG.circumference());
System.out.println("=======================================");
}
}
abstract class GeometryToy{
abstract public String getGeometry();
abstract public double area();
abstract public double circumference();
}
class RectangleToy extends GeometryToy{
double width,height;
public RectangleToy(double newWidth,double newHeight){
width = newWidth;
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;
}
}
output;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package phytagoreantriplettoy;
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();
}
}
outputnya;/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package phytagoreantriplettoy;
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();
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package phytagoreantriplettoy;
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();
}
}
outputnya:
Tidak ada komentar:
Posting Komentar