0% found this document useful (0 votes)
76 views

Contoh Java

This document contains code for several Java programs: 1. A program that converts decimal numbers to Roman numerals by using arrays to store the Roman numeral values and conditionals to determine the proper concatenation based on the decimal number. 2. A similar program that converts decimals to Roman numerals without arrays by using conditionals and concatenation. 3. A cash register program that takes the price and quantity of an item as input, calculates the total, and then applies a discount based on the total amount to output the final price.

Uploaded by

Sunarya Raya
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Contoh Java

This document contains code for several Java programs: 1. A program that converts decimal numbers to Roman numerals by using arrays to store the Roman numeral values and conditionals to determine the proper concatenation based on the decimal number. 2. A similar program that converts decimals to Roman numerals without arrays by using conditionals and concatenation. 3. A cash register program that takes the price and quantity of an item as input, calculates the total, and then applies a discount based on the total amount to output the final price.

Uploaded by

Sunarya Raya
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

import java.io.

*;

class DeretBilangan

public static void main(String[]args)throws Exception


{
BufferedReader kata = new BufferedReader(new InputStreamReader(System.in));

System.out.print(" Masukkan Jumlah Deret: ");


int deret =Integer.parseInt(kata.readLine());
String hasil = "";
for (int i = 1 ;i<= deret ;i++ )

for (int j = 1;j<= i ;j++ )

hasil = hasil+j;

hasil = hasil+"\n";

}
System.out.println(hasil);

}
}

import java.util.*;

public class KonversiDesimalToRomawi {


public static void main (String args[])
{
int bil;
String tampil="";

//inisialisasi berupa array


String [] biasa = {"","I","II", "III", "IV", "V",
"VI", "VII", "VIII", "IX"};
String [] sepuluh = {"", "X", "XX","XXX","XL"};
String [] limapuluh = {"", "L", "LX", "LXX", "LXXX","XC"};
String [] seratus = {"", "C", "CC", "CCC", "CD"};
String [] limaratus = {"", "D", "DC", "DCC", "DCCC", "CM"};
String [] seribu = {"", "M", "MM", "MMM", "Mv", "v"};

// inputan dari keyboard


System.out.print("Masukkan bilangan desimal : ");
Scanner obj = new Scanner(System.in);
bil = obj.nextInt();

//kondisi dimana angka tdk lebih dari 5000


if (bil>5000)
{
System.out.println("Maaf.., Angka Desimal Yang Di "+
"Inputkan Harus Di Bawah 5000");

}
else
{
//defenisi
int lmrts = bil % 1000;
int srts = lmrts % 500;
int lmpl = srts % 100;
int spl = lmpl % 50;
int ak = spl % 10;

int a = bil/1000;
tampil += ""+seribu[a];

//aturan konversi desimal ke romawi


if ( (lmrts >=900) && (lmrts <= 999))
{
tampil += "CM";

//90
if( (lmpl >= 90) && (lmpl <= 99) )
{
tampil += "XC";
int f= ak/1;
tampil += ""+biasa[f];
}
else
{
int d = lmpl/50;
tampil += ""+limapuluh[d];
int e = spl/10;
tampil += ""+sepuluh[e];
int f= ak/1;
tampil += ""+biasa[f];
}

}
else
{
int b = lmrts/500;
tampil += ""+limaratus[b];

//400
if ( (srts >= 400)&& (srts <= 499) )
{
tampil += "CD";

//90
if( (lmpl >= 90) && (lmpl <= 99) )
{
tampil += "XC";
int f= ak/1;
tampil += ""+biasa[f];
}
else
{
int d = lmpl/50;
tampil += ""+limapuluh[d];
int e = spl/10;
tampil += ""+sepuluh[e];
int f= ak/1;
tampil += ""+biasa[f];
}

}
else
{
int c = srts/100;
tampil += ""+seratus[c];
if((lmpl >= 90)&&(lmpl <= 99))
{
tampil += "XC";
int f= ak/1;
tampil += ""+biasa[f];
}
else
{
int d = lmpl/50;
tampil += ""+limapuluh[d];
int e = spl/10;
tampil += ""+sepuluh[e];
int f= ak/1;
tampil += ""+biasa[f];
}
}
}
System.out.println("Maka angka Romawinya "+tampil);

}
}

Coding Program bisa di download di sini

Program ke dua
import java.io.*;

public class konversi


{
public static void main (String[]args)
{
InputStreamReader in= new InputStreamReader (System.in);
BufferedReader rd = new BufferedReader(in);

String input=null;
int angka=0;

try
{
System.out.print("Masukkan Bilangan Bulat anda: ");
input=rd.readLine();

angka = Integer.parseInt(input);

if(angka<1||angka>5000)
{
System.out.println("Tahun harus dalam jangkuan 1....5000");
}
else{
while (angka>=1000){
System.out.print("M");
angka=angka-1000;
}
if (angka>=500){
if(angka>=900){
System.out.print("CM");
angka=angka-900;
}
else{
System.out.print("D");
angka=angka-500;
}
}
while (angka>=100){
if(angka>=400){
System.out.print("CD");
angka=angka-400;
}
else{
System.out.print("C");
angka=angka-100;
}
}
if(angka>=50){
if(angka>=90){
System.out.print("XC");
angka=angka-90;
}
else{
System.out.print("L");
angka=angka-50;
}
}
while(angka>=10){
if(angka>=40){
System.out.print("XL");
angka=angka-40;
}
else{
System.out.print("X");
angka=angka-10;
}
}
if(angka>=5){
if(angka==9){
System.out.print("IX");
angka=angka-9;
}
else{
System.out.print("V");
angka=angka-5;
}
}
while(angka>=1){
if(angka==4){
System.out.print("IV");
angka=angka-4;
}
else{
System.out.print("I");
angka=angka-1;
}
}
}

{
System.out.println("");
}
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
System.exit(1);
}
catch(NumberFormatException nfe)
{
System.out.println(nfe.getMessage());
System.exit(1);
}
}
}

import java.io.*;
public class ProgramKasir
{
public static void main(String args[])throws IOException
{
//membuat turunan class untuk input dari user
BufferedReader dataIn=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Berapa Harga Barang :");
int a = Integer.parseInt(dataIn.readLine());
System.out.print("Berapa Jumlah Barang yang diambil:");
int b = Integer.parseInt(dataIn.readLine());
int total =(a*b);

System.out.println("Harga barang = Rp "+a);


System.out.println("Jmlah barang = "+b);

System.out.println("Total harga sebelum diskon =Rp"+total);


if(total>10000 & total<=100000)
{
System.out.print("Total harga yang harus dibayar (setelah diskon) =Rp ");
System.out.println(total-(total * 0));
}
else if(total>100000 & total<=500000)
{
System.out.print("Total harga yang harus dibayar (setelah diskon) =Rp ");
System.out.println(total-(total * 0.05));
}
else if(total>500000 & total<=1000000)
{
System.out.print("Total harga yang harus dibayar (setelah diskon) =Rp ");
System.out.println(total-(total * 0.1));
}
else
{
System.out.print("Total harga yang harus dibayar (setelah diskon) =Rp ");
System.out.println(total-(total * 0.15));
}

}
}

You might also like