Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pbo;
- public class Benda {
- String nama;
- int ukuran;
- public Benda() {
- System.out.println("Aktif");
- }
- public Benda(String nama, int ukuran) {
- this.nama = nama;
- this.ukuran = ukuran;
- }
- public Benda(String nama) {
- this.nama = nama;
- }
- public Benda(int ukuran) {
- this.ukuran = ukuran;
- }
- void info(){
- System.out.println(this.nama +" "+ this.ukuran);
- }
- //void tanpa parameter
- void cekBenda(){
- System.out.println("Aku Lemari");
- }
- //void dengan parameter
- void setBenda(String b){
- System.out.println("Aku "+ b);
- }
- //void dengan 3 parameter
- void setBenda(String b, Integer x, Integer y){
- System.out.println("Aku "+ b
- +" berukuran "+ ( x * y ));
- }
- public String pilihBenda( String bentuk ){
- String hasil;
- if ( bentuk.equalsIgnoreCase("Kotak") ){
- hasil = "Lemari";
- }else if( bentuk.equalsIgnoreCase("bulat") ){
- hasil = "Bola";
- }else{
- hasil = "Slime";
- }
- return hasil;
- }
- public Double ukurBenda( String bentuk ){
- Double hasil;
- if ( bentuk.equalsIgnoreCase("Kotak") ){
- hasil = Double.valueOf(50 * 10);
- }else if( bentuk.equalsIgnoreCase("bulat") ){
- hasil = (4 * 3.14) * ( 50 ^ 2 ); //4 x phi x (j x j)
- }else{
- hasil = 0.0;
- }
- return hasil;
- }
- public Boolean adakahBenda( ){
- if( this.nama == null ){
- return true;
- }
- return false;
- }
- }
- class MainTest{
- public static void main(String[] args){
- Benda b = new Benda();
- b.info(); //akan null dan 0
- Benda b1 = new Benda("Buku", 60);
- b1.info();
- Benda b2 = new Benda("Gayung");
- b2.info();
- Benda b3 = new Benda(20);
- b3.info();
- Double x = b.ukurBenda("kotak"); //return
- System.out.println( "Ukuran kotak adalah: "+ x );
- b.setBenda("Payung");
- b.setBenda("Buku", 10, 2);
- System.out.println( b.ukurBenda("bulat") ); //return
- b.cekBenda();
- b.nama = "Tahu";
- b.nama = "Bola";
- b.nama = "Penggaris";
- System.out.println( b.adakahBenda());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement