Jumat, 31 Maret 2017

Luas dan Keliling Bangun Datar (2 Dimensi)

Di  bawah ini merupakan beberapa source code  dan output untuk mencari luas dan keliling dari beberapa bangun datar (2 dimensi) dalam Java.

1. Persegi


  • Source Code

    DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
    1. import java.util.Scanner;
    2.  
    3. public class persegi
    4. {
    5.     public static void main(String []args){
    6.         int sisi, luas, keliling;
    7.        
    8.         Scanner O=new Scanner (System.in);
    9.         System.out.print("masukkan sisinya= ");
    10.         sisi=O.nextInt();
    11.        
    12.         luas=(sisi*sisi);
    13.         System.out.println("luas dari persegi adalah= "+luas);
    14.        
    15.         keliling=(4*sisi);
    16.         System.out.println("keliling dari persegi adalah= "+keliling);
    17.     }
    18. }

    • Output





    2. Persegi Panjang


    • Source Code

    DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
    1. import java.util.Scanner;
    2.  
    3. public class persegi_panjang
    4. {
    5.    public static void main(String[] args){
    6.        int panjang, lebar, luas, keliling;
    7.        
    8.        Scanner S = new Scanner (System.in);
    9.        System.out.print("masukkan panjangnnya= ");
    10.        panjang=S.nextInt();
    11.        
    12.        System.out.print("masukkan lebarnya= ");
    13.        lebar=S.nextInt();
    14.        
    15.        luas=(panjang*lebar);
    16.        System.out.println("luas persegi panjang adalah= "+luas);
    17.        
    18.        keliling=2*(panjang+lebar);
    19.        System.out.println("keliling persegi panjang adalah= "+keliling);
    20.     }
    21. }

    • Output



    3. Segitiga


    • Source Code

    DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
    1. import java.util.Scanner;
    2.  
    3. public class segitiga
    4. {
    5.     public static void main(String[]args){
    6.         int sisimiring1, sisimiring2, alas, tinggi, luas, keliling;
    7.        
    8.         Scanner F=new Scanner(System.in);
    9.         System.out.print("masukkan alasnya= ");
    10.         alas=F.nextInt();
    11.        
    12.         System.out.print("masukkan tingginya= ");
    13.         tinggi=F.nextInt();
    14.        
    15.         System.out.print("masukkan sisi miring 1= ");
    16.         sisimiring1=F.nextInt();
    17.        
    18.         System.out.print("masukkan sisi miring 2= ");
    19.         sisimiring2=F.nextInt();
    20.        
    21.         luas=(alas*tinggi)/2;
    22.         System.out.println("luas segitiga adalah= "+luas);
    23.        
    24.         keliling=alas+sisimiring1+sisimiring2;
    25.         System.out.println("keliling segitiga adalah= "+keliling);
    26.     }
    27. }

    • Output



    4. Lingkaran


    • Source Code

    DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
    1. import java.util.Scanner;
    2.  
    3. public class lingkaran
    4. {
    5.    public static void main(String [] args){
    6.        double phi=3.14, r, luas, keliling;
    7.        Scanner I=new Scanner (System.in);
    8.        System.out.print("masukkan jari-jari lingkarannya= ");
    9.        r=I.nextInt();
    10.        
    11.        luas=phi*r*r;
    12.        System.out.println("luas lingkaran adalah= "+luas);
    13.        
    14.        keliling=2*phi*r;
    15.        System.out.println("keliling lingkaran adalah= "+keliling);
    16.    }
    17. }

    • Output





    5. Jajar Genjang


    • Source Code



    DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
    1. import java.util.Scanner;
    2.  
    3. public class jajar_genjang
    4. {
    5.    public static void main(String[] args) {
    6.        int alas, sisimiring, tinggi, luas, keliling;
    7.        
    8.        Scanner input = new Scanner(System.in);
    9.        System.out.println("masukkan alasnya= ");
    10.        alas = input.nextInt();
    11.        
    12.        System.out.println("masukkan sisi miringnya= ");
    13.        sisimiring = input.nextInt();
    14.        
    15.        System.out.println("masukkan tingginya= ");
    16.        tinggi = input.nextInt();
    17.        
    18.        luas = alas*tinggi;
    19.        System.out.println("luas jajar genjang adalah= "+luas);
    20.        
    21.        keliling = (2*alas)+(2*sisimiring);
    22.        System.out.println("keliling jajar genjang adalah= "+keliling);
    23.     }
    24. }

    • Output


    Senin, 06 Maret 2017

    Composition, Enumerations, and Static Class Members

    1. Composition

    Composition adalah dimana hubungan suatu object bergantung dengan objek lainnya.
    contoh code sebagai berikut:

    8.7 Class Date

    1. public class Date  
    2.  {  
    3.    private int month;  
    4.    private int day;  
    5.    private int year;  
    6.    private static final int[] daysPerMonth = //days in each month    
    7.    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};    
    8.    //contruktor : call checkMonth to confirm proper value for month;    
    9.    //call checkDay to confirm proper value for day    
    10.    public Date (int theMonth, int theDay, int theYear)    
    11.    {    
    12.    month = checkMonth(theMonth);    
    13.    year = theYear;    
    14.    day = checkDay(theDay);    
    15.    System.out.printf ("Date object constructor for date %s\n", this);    
    16.    }    
    17.    //utility method to confirm proper month value    
    18.    private int checkMonth(int testMonth)    
    19.    {    
    20.    if (testMonth > 0 && testMonth <= 12)    
    21.     return testMonth;    
    22.    else    
    23.     throw new IllegalArgumentException("Month must be 1 - 12");    
    24.    }    
    25.    //utility method to confirm proper day value based on month and year    
    26.    private int checkDay(int testDay)    
    27.    {    
    28.    //check if day in range for month    
    29.    if(testDay > 0 && testDay <= daysPerMonth[month])    
    30.     return testDay;    
    31.    //check for leap year    
    32.    if(month == 2 && testDay == 29 && ( year % 400 == 0 || (year % 4 == 0 && 100 != 0)))    
    33.     return testDay;    
    34.    throw new IllegalArgumentException("day out-of-range for the specified month and year");    
    35.    }    
    36.    //return a String of the form month/day/year    
    37.    public String toString()    
    38.    {    
    39.    return String.format("%d/%d/%d", month, day, year);    
    40.    }    
    41.  }

    8.8 Class Employee

    1.  public class Employee  
    2.  {  
    3.    private String firstName;    
    4.    private String lastName;    
    5.    private Date birthDate;    
    6.    private Date hireDate;    
    7.    //contsructor to initialize name, birth date and hire date    
    8.    public Employee(String first, String last, Date dateOfBirth, Date dateOfHire)    
    9.    {    
    10.    firstName = first;    
    11.    lastName = last;    
    12.    birthDate = dateOfBirth;    
    13.    hireDate = dateOfHire;    
    14.    }    
    15.    //convert Employee to String format    
    16.    public String toString()    
    17.    {    
    18.    return String.format ("%s, %s Hired: %s Birthday: %s", lastName, firstName, hireDate, birthDate);    
    19.    }    
    20.  }

    8.9 Class Employee Test

    1. public class EmployeeTest  
    2.  {  
    3.    public static void main (String[] agrs)    
    4.    {    
    5.    Date birth = new Date (7, 24, 1949);    
    6.    Date hire = new Date (3, 12, 1988);    
    7.    Employee employee = new Employee ("Bob", "Blue", birth, hire);    
    8.    System.out.println(employee);    
    9.    }    
    10.  }

    Output :


    2. Enumerations

    Enumeration adalah kumpulan nama-nama konstant yang didefinisikan sebagai tipe data baru. Sebuah objek bertipe Enumeration dapat mengakses isi dari kumpulan nilai pada enumeration. contoh code sebagai berikut:

    8.10  Book

    1. public enum Book  
    2.  {  
    3.    //declare constants of enum type    
    4.    JHTP("Java How to Program","2012"),    
    5.    CHTP("C How to Program", "2007"),    
    6.    IW3HTP("Internet & World Wide Web How to Program", "2008"),    
    7.    CPPHTP("C++ How to Program", "2012"),    
    8.    VBHTP("Visual Basic 2010 How to Program", "2011"),    
    9.    CSHARPHTP("Visual C# 2010 How to Program", "2011");    
    10.    //instance fields    
    11.    private final String title; //book title    
    12.    private final String copyrightYear; //copyright year    
    13.    //enum constructor    
    14.    Book (String bookTitle, String year)    
    15.    {    
    16.    title = bookTitle;    
    17.    copyrightYear = year;    
    18.    }    
    19.    //accessor for field title    
    20.    public String getTitle()    
    21.    {    
    22.    return title;    
    23.    }//end method getTitle    
    24.    //accessor for field copyrightYear    
    25.    public String getCopyrightYear()    
    26.    {    
    27.    return copyrightYear;    
    28.    }    
    29.  }

    8.11 Enum Test

    1. public class EnumTest    
    2.  {    
    3.    public static void main (String[] args)    
    4.    {    
    5.    System.out.println ("All books:\n");    
    6.    //print all books in enum Book    
    7.    for (Book book : Book.values())    
    8.     System.out.printf ("%-10s%-45s%s\n", book, book.getTitle(), book.getCopyrightYear());    
    9.    System.out.println ("\nDisplay a range of enum constants: \n");    
    10.    //print first four books    
    11.    for (Book book : EnumSet.range (Book.JHTP, Book.CPPHTP))    
    12.     System.out.printf ("%-10s%-45s%s\n", book, book.getTitle(), book.getCopyrightYear());    
    13.    }    
    14.  }

    Output


    3. Static Class Members

    Apabila sebuah variabel didefinisikan static didalam sebuah class, maka untuk memanggil variabel tersebut kita tidak perlu membuat objek dari class tersebut, namun langsung bisa memanggil variabel tersebut dari nama class dimana dia dideklarasikan. contoh code sebagai berikut:

    8.12   Employee1

    1. public class Employee1  
    2.  {  
    3.    private String firstName;  
    4.    private String lastName;  
    5.    private static int count = 0;  
    6.    public Employee1(String first, String last)  
    7.    {  
    8.      firstName=first;  
    9.      lastName=last;  
    10.      ++count;  
    11.      System.out.printf( "Employee constructor: %s %s; count = %d\n",  
    12.        firstName, lastName, count);  
    13.    }  
    14.    public String getFirstName()  
    15.    {  
    16.      return firstName;  
    17.    }  
    18.    public String getLastName()  
    19.    {  
    20.      return lastName;  
    21.    }  
    22.    public static int getCount()  
    23.    {  
    24.      return count;  
    25.    }

    8.13 Employee1 Test

    1.  public class Employee1Test  
    2.  {  
    3.    public static void main(String[] args)  
    4.    {  
    5.      System.out.printf("Employees before instantiation: %d\n", Employee1.getCount() );  
    6.      Employee1 e1 = new Employee1("Susan", "Baker");  
    7.      Employee1 e2 = new Employee1("Bob", "Blue");  
    8.      System.out.println("\nEmployees after instantiation: ");  
    9.      System.out.printf("via e1.getCount(): %d\n", e1.getCount());  
    10.      System.out.printf("via e2.getCount(): %d\n", e2.getCount());  
    11.      System.out.printf("via Employee1.getCount(): %d\n", Employee1.getCount());  
    12.      System.out.printf("\nEmployee 1: %s %s\nEmployee 2: %s %s\n",  
    13.        e1.getFirstName(), e1.getLastName(),  
    14.        e2.getFirstName(), e2.getLastName());  
    15.      e1 = null;  
    16.      e2 = null;  
    17.    }  
    18.  }


    Output

    Rangkuman Buku System Analysis and Design

    BAB 3: Requirements Determination -           Fase Analisis Fase analisis menentukan garis besar tujuan bisnis untuk sistem, menentukan...