Solution Uas

  • Uploaded by: M Fachrurrozi
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Solution Uas as PDF for free.

More details

  • Words: 595
  • Pages: 6
Alternative Solutions: OOP IF Bilingual Class IF4B and IF4C Lecturer: M. Fachrurrozi 1. Type A: /*Shape.java*/ public abstract class Shape { private int color; public Shape() { } abstract float getVolume(); protected float getLuas(){ float retval=0; return retval; } protected float getKeliling(){ float retval=0; return retval; } public int getColor(){ return this.color; } public void setColor(int newColor){ this.color=newColor; } } /*Triangle.java*/ public class Triangle extends Shape{ private float area; private int height; public Triangle() { } public float getArea(){ return this.area; } public void setArea(float newArea){ this.area=newArea; } public int getHeight(){ return this.height; } public void setHeight(int newHeight){ this.height=newHeight; } public float getVolume(){ return 0; } 1

protected float getLuas(){ return this.getArea()*this.getHeight()/2; } protected float getKeliling(){ return 0; } public static void main(String[] args){ Triangle T1=new Triangle(); T1.setArea(10); T1.setHeight(5); System.out.println("T1 Luas: "+T1.getLuas()); }

} Output: T1 Luas: 25.0

/*Cube.java*/ public class Cube extends Shape{ private int side; public Cube() { } public float getSide(){ return this.side; } public void setSide(int newSide){ this.side=newSide; } public float getVolume(){ return this.getSide()*this.getSide()*this.getSide(); } protected float getLuas(){ return 6*this.getSide()*this.getSide(); } protected float getKeliling(){ return 12*this.getSide(); } public static void main(String[] args){ Cube C1=new Cube(); C1.setSide(4); System.out.println("C1 Volume: "+C1.getVolume()); System.out.println("C1 Luas: "+C1.getLuas()); System.out.println("C1 Keliling: "+C1.getKeliling()); } } Output: C1 Volume: 64.0 C1 Luas: 96.0 C1 Keliling: 48.0 2

1. Type B: /*Shape.java*/ public interface Shape { //we are not allowed to use identifier protected public float getVolume(); public float getLuas(); public float getKeliling(); } /*Triangle.java*/ public class Triangle implements Shape{ private float area; private int height; public Triangle() { } public float getArea(){ return this.area; } public void setArea(float newArea){ this.area=newArea; } public int getHeight(){ return this.height; } public void setHeight(int newHeight){ this.height=newHeight; } public float getVolume(){ return 0; } public float getLuas(){ return this.getArea()*this.getHeight()/2; } public float getKeliling(){ return 0; } public static void main(String[] args){ Triangle T1=new Triangle(); T1.setArea(10); T1.setHeight(5); System.out.println("T1 Luas: "+T1.getLuas()); } } Output: T1 Luas: 25.0 /*Cube.java*/ public class Cube implements Shape{ private int side; 3

public Cube() { } public float getSide(){ return this.side; } public void setSide(int newSide){ this.side=newSide; } public float getVolume(){ return this.getSide()*this.getSide()*this.getSide(); } public float getLuas(){ return 6*this.getSide()*this.getSide(); } public float getKeliling(){ return 12*this.getSide(); } public static void main(String[] args){ Cube C1=new Cube(); C1.setSide(4); System.out.println("C1 Volume: "+C1.getVolume()); System.out.println("C1 Luas: "+C1.getLuas()); System.out.println("C1 Keliling: "+C1.getKeliling()); }

} Output: C1 Volume: 64.0 C1 Luas: 96.0 C1 Keliling: 48.0

4

2. Type A and B /*Fibonacci.java*/ public class Fibonacci { static int f1=1; //can be change to 0 for type B static int f2=2; //can be change to 1 for type B public boolean IsFiboNumber(int x){ int temp1=f1,temp2=f2,fn,i=0,counter=0; while(i<=x&&counter==0){ fn=temp1+temp2; if(fn==x){ counter++; } else{ temp1=temp2; temp2=fn; i++; } } if(x==this.f1||x==this.f2||counter!=0){ return true; } else{ return false; } } public void PrintFibo(int min, int max){ /* Initial State (IS): * min<=max; min>=0, max>=0*/ if(max==min){ if(this.IsFiboNumber(min)){ System.out.print(min+" "); } System.out.println(" :end"); } else{ if(this.IsFiboNumber(min)){ System.out.print(min+" "); this.PrintFibo(min+1,max); } else{ this.PrintFibo(min+1,max); } } } public int NbFibo(int min, int max){ /* Initial State (IS): * min<=max;min>=0, max>=0*/ if(max==min){ if(this.IsFiboNumber(min)){ return 1; 5

} else{ } } else{

}

return 0;

if(this.IsFiboNumber(min)){ return 1+this.NbFibo(min+1,max); } else{ return this.NbFibo(min+1,max); }

} public int SumFibo(int min, int max){ /* Initial State (IS): * min<=max;min>=0, max>=0 */ if(max==min){ if(this.IsFiboNumber(min)){ return min; } else{ return 0; } } else{ if(this.IsFiboNumber(min)){ return min+this.SumFibo(min+1,max); } else{ return this.SumFibo(min+1,max); } } } public static void main (String[] args){ Fibonacci F1=new Fibonacci(); F1.PrintFibo(10,100); System.out.println("Counting Number 10-100: "+F1.NbFibo(10,100)); F1.PrintFibo(1,50); System.out.println("Summary 1-50: "+F1.SumFibo(1,50)); }

} Output: 13 21 34 55 89 :end Counting Number 10-100: 5 1 2 3 5 8 13 21 34 :end Summary 1-50: 87

6

Related Documents

Solution Uas
May 2020 29
Uas
April 2020 50
Solution
November 2019 65
Solution
June 2020 30
Program Uas-bn & Uas
May 2020 48

More Documents from "esi oktavia"

Alpro If Bil
December 2019 14
Solution Uas
May 2020 29
Strukdat Ifb
December 2019 24
Alpro Ifb
December 2019 19
Strukdat Ifa
December 2019 25