Setelah tadi bahas soal ulangan yang pertama mengenai array, sekarang lanjut ke soal kedua yaitu mengenai pemrograman OOP , berikut ini adalah soalnya :
Buatlah program berikut dengan Metode OOP untuk sistem Tiket Kereta Api Surabaya Malang
dengan ketentuan sebagai berikut
input : kelas = .. , jumlah penumpang = ..
output : total bayar = ..
keterangan : a . Kelas : - Ekonomi harga tiket Rp 15.000
- Bisnis harga tiket Rp 80.000
- Eksekutif harga tiket Rp 110.000
b. Total bayar : harga tiker * jumlah
Source codenya :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Ulangan;
import java.util.Scanner;
import javax.swing.JOptionPane;
/**
*
* @author Mirra
*/
public class OOP2 {
String kel;
int jumlah,total,kelas;
void input(){
System.out.print("Pilih 1 untuk Ekonomi \nPilih 2 untuk Bisnis \nPilih 3 untuk Eksekutif \n" );
Scanner inputkelas = new Scanner(System.in);
System.out.print("Kelas : " );
kelas = inputkelas.nextInt();
Scanner inputjumlah = new Scanner(System.in);
System.out.print("Jumlah : ");
jumlah= inputjumlah.nextInt();
}
void hitung(){
if(kelas == 1){
total = jumlah*15000;
}
else if (kelas == 2){
total = jumlah*80000;
}
else if (kelas == 3){
total = jumlah*110000;
}
}
void cetak(){
if (kelas == 1){
kel = "Ekonomi dengan harga 15.000";
}
else if (kelas == 2){
kel = "Bisnis dengan harga 80.000";
}
else if (kelas == 3){
kel = "Eksekutif dengan harga 110.000";
}
System.out.println("Anda memesan tiket : " +kel);
System.out.println("Total bayar :" +total);
}
public static void main(String[] args) {
OOP2 hasilku = new OOP2();
hasilku.input();
hasilku.hitung();
hasilku.cetak();
}
}
Hasil compilenya :
run:
Pilih 1 untuk Ekonomi
Pilih 2 untuk Bisnis
Pilih 3 untuk Eksekutif
Kelas : 2
Jumlah : 5
Anda memesan tiket : Bisnis dengan harga 80.000
Total bayar :400000
BUILD SUCCESSFUL (total time: 10 seconds)
No comments:
Post a Comment