-- Java Coding Program Sederhana --
Berikut ini adalah Coding Sederhana Pada Program Java.
membaca input dari file
import java.io.*;
import java.util.*;
public class Main {
public static void main (String[] args)throws FileNotFoundException {
try {
double n1, n2, n3;
String n;
Scanner fileInput = new Scanner(new FileReader("nilai.txt"));
PrintWriter outFile = new PrintWriter("hasil.out");
System.out.println("mulai scanner file");
try {
while (fileInput.hasNext()) {
String line = fileInput.nextLine();
Scanner lineScan = new Scanner(line);
lineScan.useDelimiter(";");
n = lineScan.next();
n1 = lineScan.nextDouble();
n2 = lineScan.nextDouble();
n3 = lineScan.nextDouble();
double average=(n1+n2+n3)/3;
outFile.println(n+";"+average);
} } catch (Exception e) {
System.out.println("scan file error");
}
fileInput.close();
outFile.close();
System.out.println("berhasil scanner file, hasil output akan dicetak di hasil.txt");
} catch (FileNotFoundException fnfex) {
System.out.println("File does not exist!!");
}
}
}
matriks
orthogonal
import java.util.Scanner;
public class array
{
public static void main(String[] args) {
//deklarasi variabel array dan penciptaan
Double array[][]=new Double [2][2];
Scanner scan=new Scanner(System.in);
System.out.println("Masukkan nilai matriks baris 1 kolom 1: ");
array[0][0]=scan.nextDouble();
System.out.println("Masukkan nilai matriks baris 1 kolom 2: ");
array[0][1]=scan.nextDouble();
System.out.println("Masukkan nilai matriks baris 2 kolom 1: ");
array[1][0]=scan.nextDouble();
System.out.println("Masukkan nilai matriks baris 2 kolom 2: ");
array[1][1]=scan.nextDouble();
System.out.println("Matriks input");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.print(array[i][j]+" ");
}
System.out.println();
}
Double transpose[][]={{array[0][0],array[1][0]},{array[0][1],array[1][1]}};
Double det=(array[0][0]*array[1][1])-(array[0][1]*array[1][0]);
Double invers[][]={{array[1][1]/det,array[0][1]/det*-1},{array[1][0]/det*-1,array[0][0]}};
if(transpose[0][0]==invers[0][0] && transpose[0][1]==invers[0][1] && transpose[1][0]==invers[1][0] && transpose[1][1]==invers[1][1])
{
System.out.println("Matriks ini orthogonal!");
}
else
{
System.out.println("Matriks ini bukan matriks orthogonal");
}
}
}
kalkulator
sederhana (handling exception)
import java.util.*;
public class kalkulator
{
// method operasi kali
public int kali(int a, int b){
int c= a*b;
return c;
}
// method operasi bagi
public float bagi(float a, float b){
float c= a/b;
return c;
}
//method operasi pangkat
public int pangkat(int a, int b){
int c=(int) Math.pow(a,b);
return c;
}
// method untuk input angka
public int inputAngka(){
String a;
int a2=0;
try {
Scanner sc = new Scanner(System.in);
a=sc.nextLine();
a2=Integer.parseInt(a);
}
catch (InputMismatchException e) {
System.err.println("Error: " + e);
}
return a2;
}
// method pemilihan menu dan pemanggilan method2 sesuai pilihan
public void pilihmenu(int pil){
int a=0;
int b=0;
float c=0;
switch(pil){
case 1 :
try
{
Scanner scan=new Scanner(System.in);
int array[]= new int [10];
int awal=0, tot;
for(int i=0;i<100;i++)
{
System.out.println("Masukkan angka ke-"+(i+1));
array[i]=scan.nextInt();
tot=awal+array[i];
awal=tot;
if(tot==-1 || i==9)
{
System.out.println("Total = "+tot);
break;
}
else if(tot>9999 || tot<-9999)
{
throw new IllegalArgumentException();
}
}
}
catch (InputMismatchException err)
{
System.err.println("ERORR INPUT");
}
catch (IllegalArgumentException err)
{
System.err.println("ERROR, jumlah digit tidak boleh lebih dari 5");
}
break;
case 2:
try
{
System.out.println("[Perkalian]");
System.out.print("angka 1: ");
a=this.inputAngka();
System.out.print("angka 2: ");
b=this.inputAngka();
c=this.kali(a,b);
System.out.println("------------ *");
System.out.println("hasil : "+c);
System.out.println();
if (c<-9999 || c>9999)
{
throw new IllegalArgumentException();
}
}
catch (NumberFormatException err)
{
System.err.println("Bukan Angka");
}
catch (IllegalArgumentException err)
{
System.err.println("ERROR, jumlah digit tidak boleh lebih dari 5");
}
break;
case 3 :
try
{
System.out.println("[Pembagian]");
System.out.print("angka 1: ");
a=this.inputAngka();
System.out.print("angka 2: ");
b=this.inputAngka();
c=this.bagi(a,b);
System.out.println("------------ /");
System.out.println("hasil : "+c);
System.out.println();
if (c<-9999 || c>9999)
{
throw new IllegalArgumentException();
}
}
catch (ArithmeticException err)
{
System.err.println("ERROR");
}
catch (NumberFormatException err)
{
System.err.println("Bukan Angka");
}
catch (IllegalArgumentException err)
{
System.err.println("ERROR, jumlah digit tidak boleh lebih dari 5");
break;
}
break;
case 4:
try
{
System.out.println("[Pangkat]");
System.out.print("angka 1: ");
a=this.inputAngka();
System.out.print("angka 2: ");
b=this.inputAngka();
c=this.pangkat(a,b);
System.out.println("------------ /");
System.out.println("hasil : "+c);
if (c<-9999 || c>9999)
{
throw new IllegalArgumentException();
}
}
catch (NumberFormatException err)
{
System.err.println("Bukan Angka");
}
catch (IllegalArgumentException err)
{
System.err.println("ERROR, jumlah digit tidak boleh lebih dari 5");
break;
}
break;
case 5:
System.out.println();
System.out.println("Anda Keluar");
break;
default:
System.out.println("-----------");
System.out.println("Salah Pilih");
System.out.println("-----------");
}
}
public static void main(String[] args)
{
int pil;
kalkulator kal= new kalkulator();
do {
System.out.println();
System.out.println("------------------");
System.out.println("MENU:");
System.out.println("1 Penjumlahan");
System.out.println("2 Perkalian");
System.out.println("3 Pembagian");
System.out.println("4 Pemangkatan");
System.out.println("5 Keluar");
System.out.println("------------------");
System.out.print("Masukkan Pilihan : ");
pil=kal.inputAngka();
kal.pilihmenu(pil);
} while (pil!=5);
}
}
Lihat Pula Program Program Sederhana dengan C++ yang telah saya posting di blog ini
kodingnya keren, saya coba langsung.
ReplyDeletekunjungi balik gan http://arekgadget.com/
superr
ReplyDeleteMy blog
mantap koding nya
ReplyDeleteDistributor Pabrik Jual Karet Gelang Jakarta Murah di Jakarta
mantab banget gan
ReplyDeletealat cuci ultrasonic