ATM (bahasa Indonesia: Anjungan Tunai Mandiri atau dalam bahasa Inggris: Automated Teller Machine) adalah sebuah alat elektronik yang melayani nasabah bank untuk mengambil uang dan mengecek rekening tabungan mereka tanpa perlu dilayani oleh seorang "teller" manusia.
Menggunakan ATM, pelanggan dapat mengakses akun bank mereka untuk membuat penarikan, uang tunai melalui kartu debit, dan memeriksa saldo rekening mereka serta membeli kredit ponsel prabayar . Jika mata uang yang ditarik dari ATM berbeda dengan rekening bank dalam mata uang (misalnya: Penarikan Yen Jepang dari rekening bank berisi Dolar AS), uang tersebut akan dikonversi pada nilai tukar resmi pasar. Dengan demikian, ATM sering memberikan salah satu kemungkinan harga tukar terbaik resmi untuk wisatawan asing, dan juga banyak digunakan untuk tujuan ini.
Sekarang saya akan menunujukkan cara membuat ATM sederhana dengan Java. dengan panduan dari buku “Java How To Program” karya Paul Deitel dan Harvey Deitel.
Berikut ini adalah Source Codenya :
1. Account
http://pastebin.com/6DA5GfUe
2. Bank Data Base
http://pastebin.com/waF5zYeP
3. Withdrawal
http://pastebin.com/ZkZJsRCG
4. Balance Inquiry
http://pastebin.com/n6kHi3hx
5. Deposit Slot
http://pastebin.com/kxrDtaqY
6. Deposit
http://pastebin.com/EGMbCPYv
7. Keypad
http://pastebin.com/GQu577qk
8. Screen
http://pastebin.com/zT5xN4sJ
9. Cash Dispenser
http://pastebin.com/BFF7sYrq
10. Transaction
http://pastebin.com/rq5LMSfX
11. ATM
http://pastebin.com/VAYQVL0E
12. ATM Case Study
http://pastebin.com/YaWpwVR1
Koneksinya akan menjadi seperti ini:
Hasil Run:
Terimakasih...
Monday, December 26, 2016
Tuesday, November 22, 2016
Desimal jadi Hexadesimal
Kodenya
import java.util.Scanner;
class DecimalToHexExample
{
public static void main(String args[])
{
Scanner input = new Scanner( System.in );
System.out.print("Enter a decimal number : ");
int num =input.nextInt();
// calling method toHexString()
String str = Integer.toHexString(num);
System.out.println("Method 1: Decimal to hexadecimal: "+str);
}
}
Hasilnya:
import java.util.Scanner;
class DecimalToHexExample
{
public static void main(String args[])
{
Scanner input = new Scanner( System.in );
System.out.print("Enter a decimal number : ");
int num =input.nextInt();
// calling method toHexString()
String str = Integer.toHexString(num);
System.out.println("Method 1: Decimal to hexadecimal: "+str);
}
}
Hasilnya:
Contoh Program If Else
Kodenya:
import java.util.Scanner;
public class Everything {
public static void main(String[] args) {
Scanner masukan = new Scanner (System.in);
double x=0,z=0, e=0,n=0;
int nilai=0;
System.out.println("all in One");
System.out.println("mau apa??");
System.out.println("1. pembagian ");
System.out.println("2. perkalian");
System.out.println("3. beli rumah angker");
System.out.println("4. makan makanan mahal");
nilai = masukan.nextInt();
if (nilai==1){
System.out.println("masukkan nilai 1");
x = masukan.nextInt();
System.out.println("masukan nilai 2");
z = masukan.nextInt();
System.out.println("");
System.out.println("hasilnya adalah");
System.out.println(x/z);
}
//kalian
else if (nilai==2){
System.out.println("masukkan nilai 1");
x = masukan.nextInt();
System.out.println("masukan nilai 2");
z = masukan.nextInt();
System.out.println("");
System.out.println("hasilnya adalah");
System.out.println(x*z);
}
//rumah
else if (nilai==3) {
System.out.println("harga rumah" );
z=z+100;
System.out.println(z);
System.out.println(" mau di renovasi() atau diperbaiki(2)");
nilai= masukan.nextInt();
if (nilai==1) {
System.out.println("ya? tammbah 300");
e=z+300;
System.out.println("jimlah total"+ e);
}
else if (nilai==2) {
System.out.println("ya? tambah 500");
e=z+500;
System.out.println(e);
}
}
//makanan
if (nilai==4) {
System.out.println("Zen Resto");
System.out.println("pilih apa yang kamu mau");
System.out.println("1. nasgor");
System.out.println("2. bebek");
nilai=masukan.nextInt();
switch (nilai) {
case 1 :
n = n+1000;
System.out.println(n);
case 2 :
n= n+2000;
System.out.println(n);
System.out.println("mmakan lesehan (1), Apa di Bungkus (2)");
nilai =masukan.nextInt();
if (nilai==1) {
e = n +1000;
System.out.println("nambah 1000, jadi total = " + e);
} else if (nilai==2) {
e= n + 3000;
System.out.println("nambah 3000, jadi total = " + e);
}
}
}
}
}
Hasilnya:
import java.util.Scanner;
public class Everything {
public static void main(String[] args) {
Scanner masukan = new Scanner (System.in);
double x=0,z=0, e=0,n=0;
int nilai=0;
System.out.println("all in One");
System.out.println("mau apa??");
System.out.println("1. pembagian ");
System.out.println("2. perkalian");
System.out.println("3. beli rumah angker");
System.out.println("4. makan makanan mahal");
nilai = masukan.nextInt();
if (nilai==1){
System.out.println("masukkan nilai 1");
x = masukan.nextInt();
System.out.println("masukan nilai 2");
z = masukan.nextInt();
System.out.println("");
System.out.println("hasilnya adalah");
System.out.println(x/z);
}
//kalian
else if (nilai==2){
System.out.println("masukkan nilai 1");
x = masukan.nextInt();
System.out.println("masukan nilai 2");
z = masukan.nextInt();
System.out.println("");
System.out.println("hasilnya adalah");
System.out.println(x*z);
}
//rumah
else if (nilai==3) {
System.out.println("harga rumah" );
z=z+100;
System.out.println(z);
System.out.println(" mau di renovasi() atau diperbaiki(2)");
nilai= masukan.nextInt();
if (nilai==1) {
System.out.println("ya? tammbah 300");
e=z+300;
System.out.println("jimlah total"+ e);
}
else if (nilai==2) {
System.out.println("ya? tambah 500");
e=z+500;
System.out.println(e);
}
}
//makanan
if (nilai==4) {
System.out.println("Zen Resto");
System.out.println("pilih apa yang kamu mau");
System.out.println("1. nasgor");
System.out.println("2. bebek");
nilai=masukan.nextInt();
switch (nilai) {
case 1 :
n = n+1000;
System.out.println(n);
case 2 :
n= n+2000;
System.out.println(n);
System.out.println("mmakan lesehan (1), Apa di Bungkus (2)");
nilai =masukan.nextInt();
if (nilai==1) {
e = n +1000;
System.out.println("nambah 1000, jadi total = " + e);
} else if (nilai==2) {
e= n + 3000;
System.out.println("nambah 3000, jadi total = " + e);
}
}
}
}
}
Hasilnya:
Bubble Sort
Kodenya:
import java.util.Scanner;
class BubbleSort {
public static void main(String []args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);
System.out.println("Input number of integers to sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) /* For descending order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
System.out.println("Sorted list of numbers");
for (c = 0; c < n; c++)
System.out.println(array[c]);
}
}
Hasilnya
import java.util.Scanner;
class BubbleSort {
public static void main(String []args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);
System.out.println("Input number of integers to sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
for (c = 0; c < ( n - 1 ); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) /* For descending order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
System.out.println("Sorted list of numbers");
for (c = 0; c < n; c++)
System.out.println(array[c]);
}
}
Hasilnya
Binary jadi Desimal
Kodenya
import java.util.Scanner;
class BinaryToDecimal {
public static void main(String args[]){
Scanner input = new Scanner( System.in );
System.out.print("Enter a binary number: ");
String binaryString =input.nextLine();
System.out.println("Output: "+Integer.parseInt(binaryString,2));
}
}
Hasilnya
import java.util.Scanner;
class BinaryToDecimal {
public static void main(String args[]){
Scanner input = new Scanner( System.in );
System.out.print("Enter a binary number: ");
String binaryString =input.nextLine();
System.out.println("Output: "+Integer.parseInt(binaryString,2));
}
}
Hasilnya
Reverse Menggunakan For
Kodenya
import java.util.Scanner;
class ForLoopReverseDemo
{
public static void main(String args[])
{
int num=0;
int reversenum =0;
System.out.println("Input your number and press enter: ");
//This statement will capture the user input
Scanner in = new Scanner(System.in);
//Captured input would be stored in number num
num = in.nextInt();
/* for loop: No initialization part as num is already
* initialized and no increment/decrement part as logic
* num = num/10 already decrements the value of num
*/
for( ;num != 0; )
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
System.out.println("Reverse of specified number is: "+reversenum);
}
}
Hasilnya
import java.util.Scanner;
class ForLoopReverseDemo
{
public static void main(String args[])
{
int num=0;
int reversenum =0;
System.out.println("Input your number and press enter: ");
//This statement will capture the user input
Scanner in = new Scanner(System.in);
//Captured input would be stored in number num
num = in.nextInt();
/* for loop: No initialization part as num is already
* initialized and no increment/decrement part as logic
* num = num/10 already decrements the value of num
*/
for( ;num != 0; )
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
System.out.println("Reverse of specified number is: "+reversenum);
}
}
Hasilnya
Reverse dengan menggunakan Rekursi
Kodenya:
import java.util.Scanner;
class RecursionReverseDemo
{
//A method for reverse
public static void reverseMethod(int number) {
if (number < 10) {
System.out.println(number);
return;
}
else {
System.out.print(number % 10);
//Method is calling itself: recursion
reverseMethod(number/10);
}
}
public static void main(String args[])
{
int num=0;
System.out.println("Input your number and press enter: ");
Scanner in = new Scanner(System.in);
num = in.nextInt();
System.out.print("Reverse of the input number is:");
reverseMethod(num);
System.out.println();
}
}
Hasilnya:
import java.util.Scanner;
class RecursionReverseDemo
{
//A method for reverse
public static void reverseMethod(int number) {
if (number < 10) {
System.out.println(number);
return;
}
else {
System.out.print(number % 10);
//Method is calling itself: recursion
reverseMethod(number/10);
}
}
public static void main(String args[])
{
int num=0;
System.out.println("Input your number and press enter: ");
Scanner in = new Scanner(System.in);
num = in.nextInt();
System.out.print("Reverse of the input number is:");
reverseMethod(num);
System.out.println();
}
}
Hasilnya:
Reverse menggunakan While
Kodenya
import java.util.Scanner;
class ReverseNumberWhile
{
public static void main(String args[])
{
int num=0;
int reversenum =0;
System.out.println("Input your number and press enter: ");
//This statement will capture the user input
Scanner in = new Scanner(System.in);
//Captured input would be stored in number num
num = in.nextInt();
//While Loop: Logic to find out the reverse number
while( num != 0 )
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
System.out.println("Reverse of input number is: "+reversenum);
}
}
import java.util.Scanner;
class ReverseNumberWhile
{
public static void main(String args[])
{
int num=0;
int reversenum =0;
System.out.println("Input your number and press enter: ");
//This statement will capture the user input
Scanner in = new Scanner(System.in);
//Captured input would be stored in number num
num = in.nextInt();
//While Loop: Logic to find out the reverse number
while( num != 0 )
{
reversenum = reversenum * 10;
reversenum = reversenum + num%10;
num = num/10;
}
System.out.println("Reverse of input number is: "+reversenum);
}
}
Hasilnya:
Menjumlahkan Array
Kodenya
class SumOfArray{
public static void main(String args[]){
int[] array = {10, 20, 30, 40, 50, 10};
int sum = 0;
//Advanced for loop
for( int num : array) {
sum = sum+num;
}
System.out.println("Sum of array elements is:"+sum);
}
}
hasilnya
class SumOfArray{
public static void main(String args[]){
int[] array = {10, 20, 30, 40, 50, 10};
int sum = 0;
//Advanced for loop
for( int num : array) {
sum = sum+num;
}
System.out.println("Sum of array elements is:"+sum);
}
}
hasilnya
Menduplikat
Kodenya
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class duplicate
{
public void countDupChars(String str){
//Create a HashMap
Map<Character, Integer> map = new HashMap<Character, Integer>();
//Convert the String to char array
char[] chars = str.toCharArray();
/* logic: char are inserted as keys and their count
* as values. If map contains the char already then
* increase the value by 1
*/
for(Character ch:chars){
if(map.containsKey(ch)){
map.put(ch, map.get(ch)+1);
} else {
map.put(ch, 1);
}
}
//Obtaining set of keys
Set<Character> keys = map.keySet();
/* Display count of chars if it is
* greater than 1. All duplicate chars would be
* having value greater than 1.
*/
for(Character ch:keys){
if(map.get(ch) > 1){
System.out.println("Char "+ch+" "+map.get(ch));
}
}
}
public static void main(String a[]){
duplicate obj = new duplicate();
System.out.println("String: BeginnersBook.com");
System.out.println("-------------------------");
obj.countDupChars("BeginnersBook.com");
System.out.println("\nString: ChaitanyaSingh");
System.out.println("-------------------------");
obj.countDupChars("ChaitanyaSingh");
System.out.println("\nString: #@$@!#$%!!%@");
System.out.println("-------------------------");
obj.countDupChars("#@$@!#$%!!%@");
}
}
Hasilnya
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class duplicate
{
public void countDupChars(String str){
//Create a HashMap
Map<Character, Integer> map = new HashMap<Character, Integer>();
//Convert the String to char array
char[] chars = str.toCharArray();
/* logic: char are inserted as keys and their count
* as values. If map contains the char already then
* increase the value by 1
*/
for(Character ch:chars){
if(map.containsKey(ch)){
map.put(ch, map.get(ch)+1);
} else {
map.put(ch, 1);
}
}
//Obtaining set of keys
Set<Character> keys = map.keySet();
/* Display count of chars if it is
* greater than 1. All duplicate chars would be
* having value greater than 1.
*/
for(Character ch:keys){
if(map.get(ch) > 1){
System.out.println("Char "+ch+" "+map.get(ch));
}
}
}
public static void main(String a[]){
duplicate obj = new duplicate();
System.out.println("String: BeginnersBook.com");
System.out.println("-------------------------");
obj.countDupChars("BeginnersBook.com");
System.out.println("\nString: ChaitanyaSingh");
System.out.println("-------------------------");
obj.countDupChars("ChaitanyaSingh");
System.out.println("\nString: #@$@!#$%!!%@");
System.out.println("-------------------------");
obj.countDupChars("#@$@!#$%!!%@");
}
}
Hasilnya
Contoh Vector
Kodenya
import java.util.*;
public class VectorExample {
public static void main(String args[]) {
/* Vector of initial capacity(size) of 2 */
Vector<String> vec = new Vector<String>(2);
/* Adding elements to a vector*/
vec.addElement("Apple");
vec.addElement("Orange");
vec.addElement("Mango");
vec.addElement("Fig");
/* check size and capacityIncrement*/
System.out.println("Size is: "+vec.size());
System.out.println("Default capacity increment is: "+vec.capacity());
vec.addElement("fruit1");
vec.addElement("fruit2");
vec.addElement("fruit3");
/*size and capacityIncrement after two insertions*/
System.out.println("Size after addition: "+vec.size());
System.out.println("Capacity after increment is: "+vec.capacity());
/*Display Vector elements*/
Enumeration en = vec.elements();
System.out.println("\nElements are:");
while(en.hasMoreElements())
System.out.print(en.nextElement() + " ");
}
}
hasilnya
import java.util.*;
public class VectorExample {
public static void main(String args[]) {
/* Vector of initial capacity(size) of 2 */
Vector<String> vec = new Vector<String>(2);
/* Adding elements to a vector*/
vec.addElement("Apple");
vec.addElement("Orange");
vec.addElement("Mango");
vec.addElement("Fig");
/* check size and capacityIncrement*/
System.out.println("Size is: "+vec.size());
System.out.println("Default capacity increment is: "+vec.capacity());
vec.addElement("fruit1");
vec.addElement("fruit2");
vec.addElement("fruit3");
/*size and capacityIncrement after two insertions*/
System.out.println("Size after addition: "+vec.size());
System.out.println("Capacity after increment is: "+vec.capacity());
/*Display Vector elements*/
Enumeration en = vec.elements();
System.out.println("\nElements are:");
while(en.hasMoreElements())
System.out.print(en.nextElement() + " ");
}
}
hasilnya
Menampilkan 100 angka prima
Kodenya:
class PrimeNumber100Demo
{
public static void main(String args[])
{
int n;
int status = 1;
int num = 3;
System.out.println("First 100 prime numbers are:");
System.out.println(2);
for ( int i = 2 ; i <=100 ; )
{
for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
{
if ( num%j == 0 )
{
status = 0;
break;
}
}
if ( status != 0 )
{
System.out.println(num);
i++;
}
status = 1;
num++;
}
}
}
Hasilnya:
Contoh hash
Kodenya
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
public class Hash {
public static void main(String args[]) {
/* This is how to declare HashMap */
HashMap<Integer, String> hmap = new HashMap<Integer, String>();
/*Adding elements to HashMap*/
hmap.put(12, "Chaitanya");
hmap.put(2, "Rahul");
hmap.put(7, "Singh");
hmap.put(49, "Ajeet");
hmap.put(3, "Anuj");
/* Display content using Iterator*/
Set set = hmap.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator.next();
System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
System.out.println(mentry.getValue());
}
/* Get values based on key*/
String var= hmap.get(2);
System.out.println("Value at index 2 is: "+var);
/* Remove values based on key*/
hmap.remove(3);
System.out.println("Map key and values after removal:");
Set set2 = hmap.entrySet();
Iterator iterator2 = set2.iterator();
while(iterator2.hasNext()) {
Map.Entry mentry2 = (Map.Entry)iterator2.next();
System.out.print("Key is: "+mentry2.getKey() + " & Value is: ");
System.out.println(mentry2.getValue());
}
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
public class Hash {
public static void main(String args[]) {
/* This is how to declare HashMap */
HashMap<Integer, String> hmap = new HashMap<Integer, String>();
/*Adding elements to HashMap*/
hmap.put(12, "Chaitanya");
hmap.put(2, "Rahul");
hmap.put(7, "Singh");
hmap.put(49, "Ajeet");
hmap.put(3, "Anuj");
/* Display content using Iterator*/
Set set = hmap.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator.next();
System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
System.out.println(mentry.getValue());
}
/* Get values based on key*/
String var= hmap.get(2);
System.out.println("Value at index 2 is: "+var);
/* Remove values based on key*/
hmap.remove(3);
System.out.println("Map key and values after removal:");
Set set2 = hmap.entrySet();
Iterator iterator2 = set2.iterator();
while(iterator2.hasNext()) {
Map.Entry mentry2 = (Map.Entry)iterator2.next();
System.out.print("Key is: "+mentry2.getKey() + " & Value is: ");
System.out.println(mentry2.getValue());
}
}
}
Hasilnya:
Contoh Tree
Kodenya:
import java.util.TreeMap;
import java.util.Set;
import java.util.Iterator;
import java.util.Map;
public class Tree {
public static void main(String args[]) {
/* This is how to declare TreeMap */
TreeMap<Integer, String> tmap =
new TreeMap<Integer, String>();
/*Adding elements to TreeMap*/
tmap.put(1, "Data1");
tmap.put(23, "Data2");
tmap.put(70, "Data3");
tmap.put(4, "Data4");
tmap.put(2, "Data5");
/* Display content using Iterator*/
Set set = tmap.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator.next();
System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
System.out.println(mentry.getValue());
}
}
}
hasilnya:
import java.util.TreeMap;
import java.util.Set;
import java.util.Iterator;
import java.util.Map;
public class Tree {
public static void main(String args[]) {
/* This is how to declare TreeMap */
TreeMap<Integer, String> tmap =
new TreeMap<Integer, String>();
/*Adding elements to TreeMap*/
tmap.put(1, "Data1");
tmap.put(23, "Data2");
tmap.put(70, "Data3");
tmap.put(4, "Data4");
tmap.put(2, "Data5");
/* Display content using Iterator*/
Set set = tmap.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator.next();
System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
System.out.println(mentry.getValue());
}
}
}
hasilnya:
Medapatkan Nilai Maksimum dari dua angka
Kodenya:
public class FindMaxOfTwoNumbersExample {
public static void main(String[] args) {
/*
* To find maximum of two int values, use
* static int max(int a, int b) method of Math class.
*/
System.out.println(Math.max(20,40));
/*
* To find minimum of two float values, use
* static float max(float f1, float f2) method of Math class.
*/
System.out.println(Math.max(324.34f,432.324f));
/*
* To find maximum of two double values, use
* static double max(double d2, double d2) method of Math class.
*/
System.out.println(Math.max(65.34,123.45));
/*
* To find maximum of two long values, use
* static long max(long l1, long l2) method of Math class.
*/
System.out.println(Math.max(435l,523l));
}
Hasilnya:
public class FindMaxOfTwoNumbersExample {
public static void main(String[] args) {
/*
* To find maximum of two int values, use
* static int max(int a, int b) method of Math class.
*/
System.out.println(Math.max(20,40));
/*
* To find minimum of two float values, use
* static float max(float f1, float f2) method of Math class.
*/
System.out.println(Math.max(324.34f,432.324f));
/*
* To find maximum of two double values, use
* static double max(double d2, double d2) method of Math class.
*/
System.out.println(Math.max(65.34,123.45));
/*
* To find maximum of two long values, use
* static long max(long l1, long l2) method of Math class.
*/
System.out.println(Math.max(435l,523l));
}
Hasilnya:
Find Floor Number
Kodenya
public class FindFloorValueExample {
public static void main(String[] args) {
/*
* To find a floor value, use
* static double floor(double d) method of Math class.
*
* It returns a largest integer which is not grater than the argument value.
*/
//Returns the same value
System.out.println(Math.floor(70));
//returns largest integer which is not less than 30.1, i.e. 30
System.out.println(Math.floor(30.1));
//returns largest integer which is not less than 15.5, i.e. 15
System.out.println(Math.floor(15.5));
//in this case it would be -40
System.out.println(Math.floor(-40));
//it returns -43.
System.out.println(Math.floor(-42.4));
//returns 0
System.out.println(Math.floor(0));
}
}
Hasilnya
public class FindFloorValueExample {
public static void main(String[] args) {
/*
* To find a floor value, use
* static double floor(double d) method of Math class.
*
* It returns a largest integer which is not grater than the argument value.
*/
//Returns the same value
System.out.println(Math.floor(70));
//returns largest integer which is not less than 30.1, i.e. 30
System.out.println(Math.floor(30.1));
//returns largest integer which is not less than 15.5, i.e. 15
System.out.println(Math.floor(15.5));
//in this case it would be -40
System.out.println(Math.floor(-40));
//it returns -43.
System.out.println(Math.floor(-42.4));
//returns 0
System.out.println(Math.floor(0));
}
}
Hasilnya
Contoh Do While Loop
Kodenya
class DoWhileLoopExample {
public static void main(String args[]){
int i=10;
do{
System.out.println(i);
i--;
}while(i>1);
}
}
Hasilnya:
class DoWhileLoopExample {
public static void main(String args[]){
int i=10;
do{
System.out.println(i);
i--;
}while(i>1);
}
}
Hasilnya:
Mendapatkan Input Data
Kodenya:
import java.util.Scanner;
class GetInputData
{
public static void main(String args[])
{
int num;
float fnum;
String str;
Scanner in = new Scanner(System.in);
//Get input String
System.out.println("Enter a string: ");
str = in.nextLine();
System.out.println("Input String is: "+str);
//Get input Integer
System.out.println("Enter an integer: ");
num = in.nextInt();
System.out.println("Input Integer is: "+num);
//Get input float number
System.out.println("Enter a float number: ");
fnum = in.nextFloat();
System.out.println("Input Float number is: "+fnum);
}
}
Hasilnya:
import java.util.Scanner;
class GetInputData
{
public static void main(String args[])
{
int num;
float fnum;
String str;
Scanner in = new Scanner(System.in);
//Get input String
System.out.println("Enter a string: ");
str = in.nextLine();
System.out.println("Input String is: "+str);
//Get input Integer
System.out.println("Enter an integer: ");
num = in.nextInt();
System.out.println("Input Integer is: "+num);
//Get input float number
System.out.println("Enter a float number: ");
fnum = in.nextFloat();
System.out.println("Input Float number is: "+fnum);
}
}
Hasilnya:
Mencari Angka Exponesial
Kodenya
public class FindExponentialNumberExample {
public static void main(String[] args)
{
/*
* To find exponential value of a number, use
* static double exp(double d) method of Java Math class.
*
* It returns e raised to argument value.
*/
System.out.println("Exponential of 2 is : " + Math.exp(2));
}
}
Hasilnya
public class FindExponentialNumberExample {
public static void main(String[] args)
{
/*
* To find exponential value of a number, use
* static double exp(double d) method of Java Math class.
*
* It returns e raised to argument value.
*/
System.out.println("Exponential of 2 is : " + Math.exp(2));
}
}
Hasilnya
Decimal Binary Stack
Kodenya
import java.util.*;
class DecimalBinaryStack
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
// Create Stack object
Stack<Integer> stack = new Stack<Integer>();
// User input
System.out.println("Enter decimal number: ");
int num = in.nextInt();
while (num != 0)
{
int d = num % 2;
stack.push(d);
num /= 2;
}
System.out.print("\nBinary representation is:");
while (!(stack.isEmpty() ))
{
System.out.print(stack.pop());
}
System.out.println();
}
}
Hasilnya
import java.util.*;
class DecimalBinaryStack
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
// Create Stack object
Stack<Integer> stack = new Stack<Integer>();
// User input
System.out.println("Enter decimal number: ");
int num = in.nextInt();
while (num != 0)
{
int d = num % 2;
stack.push(d);
num /= 2;
}
System.out.print("\nBinary representation is:");
while (!(stack.isEmpty() ))
{
System.out.print(stack.pop());
}
System.out.println();
}
}
Hasilnya
Subscribe to:
Posts (Atom)






















