Posted by : Unknown Sabtu, 26 Maret 2016

NAMA : M . Fauzi Abdi
Nim      : 30814237



STMIK INDONESIA




Shape

Buatlah class Shape. class memiliki dua field Stringname dan size. class mempunyai method printShapeInfo, dimana hanya mengeluarkan nilai name dan field size dari object Shape. Juga memiliki method printShapeName dan printShapeSize, dimana mencetak nama dan size dari object, berturut-turut.
Menggunakan pewarisan, buat class Square dengan field yang sama dan method seperti itu dari class Shape. Class ini mempunyai dua tambahan field integer: length dan width. Method printShapeLength dan printShapeWidth yang mencetak panjang dan lebar object yang juga termasuk dalam class ini. Anda juga harus meng-override printShapeInfo untuk mencetak keluaran field tambahan dalam subclass juga.

Class Main :
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg30814237_mfauziabdi;
/**
 *
 * @author M Fauzi Abdi
 */
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        System.out.println("Created By : M. Fauzi Abdi ");
        System.out.println("NIM        : 30814237      ");
        
square persegi = new square("persegi");
Shape bentuk  = persegi; 
persegi.length = 37;
persegi.width = 15;

persegi.printShapeName();
persegi.printLength();
persegi.printWidth();
persegi.printShapeSize();
System.out.println("\n");
System.out.println("Hasilnya adalah..");
persegi.printShapeInfo();
}
}

Hasil Running :
Class Shape  :
 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg30814237_mfauziabdi;
/**
 *
 * @author M Fauzi Abdi
 */
public class Shape {
    
    String name, size;
public Shape(String name){
this.name = name;
}
public void printShapeInfo(){
System.out.println("Shape name : "+name);
System.out.println("Shape Size : "+size);
}
public void printShapeName(){
System.out.println("Shape name : "+name);
}
public void printShapeSize(){
System.out.println("Shape size : "+size);
}
    
}
Class Square :
 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg30814237_mfauziabdi;
/**
 *
 * @author M Fauzi Abdi
 */
class square extends Shape{
String name, size;
int length, width;
public square(String name){
super(name);
}
public void printShapeInfo(){
super.printShapeInfo();
}
public void printShapeName(){
super.printShapeName();
}
public void printShapeSize(){
super.size = String.valueOf(length*width);
super.printShapeSize();
}
public void printWidth(){
System.out.println("Lebar : "+width);
}
public void printLength(){
System.out.println("Panjang : "+length);
}
Binatang

Buatlah interface Animal yang mempunyai 2 method:eat dan move. Semua method ini tidak punya argumen atau nilai return. Method ini hanya mengeluarkan bagaimana object Animal makan dan bergerak. Sebagai contoh, seekor kelinci memakan wortel dan bergerak dengan melompat. Buat class Fish dan Bear yang menggunakan interface Animal. Terserah kepada Anda bagaimana menggunakan method eat dan move.

Class Animal :
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fauzi_animal;

/**
 *
 * @author M Fauzi Abdi
 */
public class Animal {
    
       public static void main(String[]args)
        {
            Fish F = new Fish();
            Bear B = new Bear();
            F.eat();
            System.out.print(" dan ");
            F.move();
            System.out.println();
            B.eat();
            System.out.print(" dan ");
            B.move();
        }

}
Hasil Running :


Class Fish :
 /
 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fauzi_animal;

/**
 *
 * @author M Fauzi Abdi
 */
class Fish extends Animal {
      public void eat()
        {
            System.out.print("Ikan makan cacing");
        }
        public void move()
        {
            System.out.print("Ia hanya dapat bergerak di air");
        }
    }
Class Bear :
 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fauzi_animal;

/**
 *
 * @author M Fauzi Abdi
 */
class Bear extends Animal {
    
    public void eat()
        {
            System.out.print("Beruang makan Ikan");
        }
        public void move()
        {
            System.out.print("Ia bergerak merangkak");
        }

    }
Terimaksih Sudah Berkunjung Semoga bermanfaat ^^

Welcome to My Blog

Popular Post

This Site

Followers

- Copyright © 2013 My World -Robotic Notes- Powered by Blogger -