Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

public class MaxVariablesDemo {

public static void main(String args[]) {

// integer
byte largestByte = Byte.MAX_VALUE;
short largestShort = Short.MAX_VALUE;
int largestInteger = Integer.MAX_VALUE;
long largestLong = Long.MAX_VALUE;

// real numbers / float dan double


float largestFloat = Float.MAX_VALUE;
double largestDouble = Double.MAX_VALUE;

// tipe primitive
char aChar = 'S';
boolean aBoolean = true;

// tampilkan semua
System.out.println("The largest byte value is "
+ largestByte);
System.out.println("The largest short value is "
+ largestShort);
System.out.println("The largest integer value is "
+ largestInteger);
System.out.println("The largest long value is "
+ largestLong);

System.out.println("The largest float value is "


+ largestFloat);
System.out.println("The largest double value is "
+ largestDouble);

if (Character.isUpperCase(aChar)) {
System.out.println("The character " + aChar
+ " is upper case.");
} else {
System.out.println("The character " + aChar
+ " is lower case.");
}
System.out.println("The value of aBoolean is " + aBoolean);
}
}

class TestAritmetika {
public static void main (String[] args) {
short x = 6;
int y = 4;
float a = 12.5f;
float b = 7f;

System.out.println(x = + x + , y = + y);
System.out.println(x + y = + (x + y));
System.out.println(x - y = + (x - y));
System.out.println(x * y = + (x * y));
System.out.println(x / y = + (x / y));
System.out.println(x % y = + (x % y));

System.out.println(a = + a + , b = + b);
System.out.println(a / b = + (a / b));
}
}

class conditional {
public static void main (String args[ ] ) {
int x = 0;
boolean isEven = false;
System.out.println(x = + x);
x = isEven ? 4 : 7;
System.out.println(x = + x);
isEven = true;
x = isEven ? 4 : 7;
System.out.println(x = + x);
}
}

class bacaHuruf {
public static void main(String args[]) {
char input = (char) -1;
try {
input = (char) System.in.read();
}
catch (Exception e) {System.out.println(error + e);}
}
}

class bacaData1 {
public static void main (String args[]) {
StringBuffer s = new StringBuffer();
char c;
try {
while ((c = (char) System.in.read() ) != \n ) {
s.append(c);
}
}
catch (Exception e) {
System.out.println(Error : + e.toString());
}
System.out.println(s);
}
}

class bacaData2 {
public static void main (String args[]) {
byte buf[] = new byte[80];
try {
System.in.read(buf);
}
catch (Exception e) {
System.out.println(Error : + e.toString());
}
String s = new String(buf, 0);
System.out.println(s);
}
}

class bacaData3 {
public static void main (String args[]) {
byte buf[] = new byte[10];
try {
System.in.read(buf, 0, 10);
}
catch (Exception e) {
System.out.println(Error : + e.toString());
}
String s = new String(buf, 0);
System.out.println(s);
}
}

class in_out {
public static void main(String args[])
{
DataInputStream in = new DataInputStream(System.in);
char c;
int angka=0;
char huruf=' ';
String kalimat=" ";

/* menerima input String lalu dirubah ke Integer */


try {
System.out.print("Enter a number : ");
kalimat = in.readLine();
angka = Integer.parseInt(kalimat);
}
catch(IOException e){ System.out.println(e); }

/* menerima karakter dari input-stream */


try {
System.out.print("Enter a character : ");
kalimat = in.readLine();
huruf = kalimat.charAt(0);
}
catch(IOException e) { System.out.println(e); }

/* menerima String dari input-stream */


try {
System.out.print("Enter some words : ");
kalimat = in.readLine();
}
catch(IOException e) { System.out.println(e); }

/* hasil input di tampilkan */


System.out.println("Here are what you type in : ");
System.out.println(angka);
System.out.println(huruf);
System.out.println(kalimat);
}
}

class ContohArgumen {
public static void main(String args[]) {
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
int hasil = i*j;
System.out.println(Hasil kali = + hasil);
}
}

//Contoh2.1
importjava.io.*;
publicclassInterest{
/*
Classinimengimplementasikansuatuprogram
yangmenghitungjumlahbungadariuang
yangdisimpandibankdenganbunga7%setahun.
*/
publicstaticvoidmain(String[]args){
/*Deklarasivariable.*/
doubleprincipal=0;//Uangsimpanan.
doublerate;//Bungapertahun.
doubleinterest;//Jumlahbunga.
DataInputStreamin=newDataInputStream(System.in);
/*Bacadatamodal*/
try{
System.out.print(MasukkanJumlahSimpanan:);
principal=Double.valueOf(in.readLine());
}
catch(IOExceptionerr){}
rate=0.07;
interest=principal*rate;//hitungjumlahbunga.
principal=principal+interest;
/*Hasilnyaditampilkan.*/
System.out.print("Bungayangdiperoleh:Rp.");
System.out.println(interest);
System.out.print("Nilaiinvestasisetelahsatutahun:Rp.");
System.out.println(principal);
}//endofmain()
}
/* contoh 2.2 *
* @(#)ExtrakDigit.java
* @author Suarga
* @version 1.00 2007/6/10
*/
import java.io.*;
public class ExtrakDigit {

// memisahkan digit dari suatu angka (maksimum 3 digit


public static void main(String[] args){
// Deklarasi Variabel
int angka, d1, d2, d3, sisa;
DataInputStream in = new DataInputStream(System.in);
// nilai awal
angka = 0;
// menerima nilai angka
try {
System.out.print("Masukkan angka maximum 3 digit :");
angka = Integer.parseInt(in.readLine());
}
catch (IOException err) {}
// memisahkan digit
d1 = angka/100;
sisa = angka%100;
d2 = sisa/10;
d3 = sisa%10;
// menampilkan hasilnya
System.out.println("Digit pertama : " + d1);
System.out.println("Digit kedua : " + d2);
System.out.println("Digit ketiga : " + d3);
}
}

You might also like