Sunday, 28 July 2019

Program Exception Java STMIK AKAKOM YOGYAKARTA


PERTEMUAN KE –12
Exception  

pratik 1

import java.util.Scanner;
public class TestError {
   public static void main(String[] arg){
   Scanner al = new Scanner(System.in);
   try {
   System.out.print("Please enter the miles you have driven : ");
   double miles = al.nextDouble();
   System.out.print("Please enter the gallon of gas you have used : ");
     double gallon = al.nextDouble();

  double mpg = miles/gallon;
  System.out.println("Your car's mpg is " + mpg + ".");
  }
  catch (Exception e){
    System.out.println("You must enter a number.");
    }
    }
  }

pratik 2

import java.util.Scanner;
public class TestError2 {
   public static void main(String[] arg){
       Scanner al = new Scanner(System.in);
       try {
   System.out.print("Please enter the miles you have driven : ");
   double miles = al.nextDouble();
   System.out.print("Please enter the gallon of gas you have used : ");
   double gallon = al.nextDouble();

   if (gallon ==0)
   throw new  ArithmeticException();
           double mpg = miles/gallon;
           System.out.println("Your car's mpg is " + mpg + ".");
      }
      catch (Exception e){
System.out.println("You must enter a number.");
System.out.println(e.getClass());
      }
}
}



pratik 3

import java.util.Scanner;
public class TestError3 {
   public static void main(String[] arg){
       Scanner al = new Scanner(System.in);
       try {
   System.out.print("Please enter the miles you have driven : ");
   double miles = al.nextDouble();
   System.out.print("Please enter the gallon of gas you have used : ");
   double gallon = al.nextDouble();

   if (gallon ==0)
   throw new  ArithmeticException();
   double mpg = miles/gallon;
   System.out.println("Your car's mpg is " + mpg + ".");
      }catch (ArithmeticException e){
          System.out.println("You can not divide by zero");
      }catch (Exception e){
          System.out.println("You must enter a number.");
      }
}
}

 

pratik 4

import java.util.Scanner;
public class TestError5 {
   public static void main(String[] arg){
   Scanner reader = new Scanner(System.in);
   try {
   System.out.print("Please enter the miles you have driven : ");
   double miles = reader.nextDouble();
   System.out.print("Please enter the gallon of gas you have used : ");
   double gallon = reader.nextDouble();
       if (gallon ==0)
           throw new  DivideByZeroException1();
       if (gallon<0 || miles <0)
           throw new  NegativeNumberException1();

       double mpg = miles/gallon;
       System.out.println("Your car's mpg is " + mpg + ".");
      }catch (DivideByZeroException1 e){
          System.out.println("You can not divide by zero");
      }catch (NegativeNumberException1 e){
          System.out.println("You can not use negatif numbers");
      }catch (Exception e){
          System.out.println("You must enter a number.");
          System.out.println(e.getClass());
      }
}
}
class DivideByZeroException1 extends Exception{
   DivideByZeroException1(){
   }DivideByZeroException1(String message){
   super(message);
   }
}class NegativeNumberException1 extends Exception{
   NegativeNumberException1(){
   }
   NegativeNumberException1(String message){
   super(message);
   }
}

pratik 5

import java.util.Scanner;
public class TestError6 {
   public static void main(String[] arg){
   Scanner reader = new Scanner(System.in);
   try {
   System.out.print("Please enter the miles you have driven : ");
   double miles = reader.nextDouble();
       System.out.print("Please enter the gallon of gas you have used : ");
       double gallon = reader.nextDouble();

       if (gallon ==0)
           throw new  DivideByZeroException1();

       if (gallon<0 || miles <0)
           throw new  NegativeNumberException1();

       double mpg = miles/gallon;
       System.out.println("Your car's mpg is " + mpg + ".");
      }catch (DivideByZeroException1 e){
          System.out.println("You can not divide by zero");
      }catch (NegativeNumberException1 e){
          System.out.println("You can not use negatif numbers");
      } catch (Exception e){
          System.out.println("You must enter a number.");
          System.out.println(e.getClass());
      }finally {
          System.out.println("Thank you for using the MPG System");
      }
      System.out.println("Program ends");
}
}

Program Searching pada Java STMIK AKAKOM YOGYAKARTA

SEARCHING
PERTEMUAN KE -07

1. buble sort

code: 
public class BinarSearch{
public static void main(String[]arg){
int batasAtas,batasBawah,index,cari,posisiSekarang;
int[]array = new int[5];
array[0] = 25;
array[1] = 30;
array[2] = 40;
array[3] = 45;
array[4] = 50;
batasAtas = array.length-1;
batasBawah = 0;
for(index=0;index<array.length;index++){
System.out.print(array[index]+ " ");
}
System.out.println("");
cari = 30;
boolean belumKetemu = true;
while(belumKetemu){
posisiSekarang = (batasAtas + batasBawah)/2;
if(array[posisiSekarang]== cari){
belumKetemu = false;
System.out.println("ditemukan " + cari);
}else if(batasBawah > batasAtas){
System.out.println("ditemukan " + cari);
break;
}else{
if(array[posisiSekarang]< cari){
batasBawah = posisiSekarang +1;
}else{
batasAtas = posisiSekarang = 1;
}
}
}
}
}

output

2. Selecetion Sort

import java.util.Scanner;
public class selection_sort1
{
static int [] x=new int [100];
static int i,j,index,large,n;

public void input()
{
Scanner MyScanner=new Scanner(System.in);
System.out.print("Masukkan berapa banyak data yang ingin dimasukkan = ");
n=MyScanner.nextInt();
for(i=0;i<n;i++)
{
System.out.print("Masukkan data "+(i+1)+" = ");
x[i]=MyScanner.nextInt();
}
}
public void sort()
{
for(i=n-1;i>0;i--)
{
large=x[0];
index=0;
for(j=1;j<=i;j++)
{
if(x[j]>large)
{
large=x[j];
index=j;
}
}
x[index]=x[i];
x[i]=large;
}
System.out.println();
}
public static void main(String[]args)
{
selection_sort1 ok=new selection_sort1();
ok.input();
ok.sort();
System.out.print("Data yang sudah urut = ");
for(i=0;i<n;i++)
{
System.out.print(x[i]+" ");
}
System.out.println();
System.out.println();
}

}
output


Program Mengecek GenapGanjil dengan Java STMIK AKAKOM YOGYAKARTA

Cek Genap Ganjil

import java.util.Scanner;
public class ProyekCekGenap1{
static boolean cekGenap(int a){
boolean status;
if(a%2==0){
status = true;
}else{
status = false;
} return status;
} public static void main(String[]arg){
Scanner al = new Scanner(System.in);
int x;
boolean genap;
System.out.print("Masukkan bilangan : ");
x = al.nextInt();
genap = cekGenap(x);
if(genap){
System.out.println("Bilangan yang dicetak ternyata genap");
}else{
System.out.println("Bilangan yang dicetak ternyata ganjil");
}
}
}

output

fungsiIterasi

import java.util.Scanner;
public class ProyekInterasiFungsi{
static void cetakUlang( int Ulang){
for(int i =0;i<Ulang;i++){
System.out.println("Cetak ke " + (i+1));
}
}
public static void main(String[]arg){
int Ulang;
Scanner al = new Scanner(System.in);
System.out.print("Akan dicetak berapa kali : ? ");
Ulang = al.nextInt();
cetakUlang(Ulang);
}
}

output

Studi Kasus laporan Algoritma dan pemograman STMIK AKAKOM YOGYAKARTA

STUDI KASUS
PERTEMUAN 14


pratik 1

code :
import java.util.Scanner;
public class Praktik1 {
    public static void main(String arg[]){
        int max=0;
        int jum=0;
        double rata=0;
        Scanner x=new Scanner(System.in);
        int array[]=new int[10];
        for(int i=0;i<array.length;i++){
            System.out.print("masukan data ke-"+(i+1)+":");
            array[i]=x.nextInt();
            for(int j=0;j<array.length;j++){
                if(array[i]>max){
                    max=array[i];
                }
            }
            jum += array[i];
        }
        rata=jum/array.length;
        System.out.println("Bilangan terbesarnya adalah "+max);
        System.out.println("Rata-rata dari sepuluh bilangan tersebut adalah "+rata);
    }
}
output

pratik  2
code :


import java.util.Scanner;
public class Praktik2 {
    public static int sum(int a,int b){
        return a+b;
    }
    public static int min(int a,int b){
        return a-b;
    }
    public static int kali(int a,int b){
        return a*b;
    }
    public static double bagi(double a,double b){
        return a/b;
    }
    public static void main(String arg[]){
        int x,y;
        Scanner dix=new Scanner(System.in);
        System.out.print("masukan nilai pertama : ");
        x=dix.nextInt();
        System.out.print("masukan nilai kedua   : ");
        y=dix.nextInt();
        System.out.println("Sum        :"+sum(x,y));
        System.out.println("Difference :"+min(x,y));
        System.out.println("Product    :"+kali(x,y));
        System.out.println("Quontient  :"+bagi(x,y));
    }
}

output


pratik 3
code :
import java.util.Scanner;
public class Praktik3 {
     public static void main(String[] args) {
        int x = 0;
        Scanner in = new Scanner(System.in);
        System.out.print("masukan tinggi : ");
        try {
            x = in.nextInt();
        } catch (Exception e) {
        }
        x = x + 2;
        for (int a = 1; a < x; a += 2) {
            for (int b = 0; b < 4 - a / 2; b++) {
                System.out.print(" ");
            }
            for (int b = 0; b < a; b++) {
                System.out.print("*");
            }
            System.out.print("\n");
        }
        for (int a = x; a > 0; a -= 2) {
            for (int b = 0; b < 4 - a / 2; b++) {
                System.out.print(" ");
            }
            for (int b = 0; b < a; b++) {
                System.out.print("*");
            }
            System.out.print("\n");
        }
    }
}

output


import java.util.Scanner;
public class Praktik4 {
    public static void main(String[] args) {
        Scanner dix = new Scanner(System.in);
        System.out.print("Masukan nilai dari x : ");
        int x = dix.nextInt();
        System.out.print("Masukan nilai dari y : ");
        int y = dix.nextInt();
        while (y != 0) {
            int y1 = y;
            y = x % y;
            System.out.println("y = x%y = " + x + " % " + y1 + " = " + y);
            x = y1;
            System.out.println("x = " + x);
        }
    }
}
OUTPUT:





PRAKTIK 5
import java.io.*;
import java.util.Scanner;
public class Praktik5 {
    public static void main(String[] args) throws IOException {

        Scanner input = new Scanner(System.in);
        StringBuffer sb = new StringBuffer();
        System.out.print("masukan nama file : ");
        File d = new File(input.next());
        if (d.createNewFile() == true) {
            System.out.println("File telah dibuat");
            System.out.print("masukan isi File : ");
            BufferedWriter isi = new BufferedWriter(new FileWriter(d));
            String s = input.next();
            sb.append(s);
            isi.write((sb.reverse()).toString());
            sb.delete(0, sb.length());
            isi.close();
        } else {
            System.out.println("File sudah ada");
        }
        BufferedReader b = new BufferedReader(new FileReader(d));
        String s = b.readLine();
        sb.append(s);
        System.out.println("isi file dibalik: " + sb);
    }
}
OUTPUT:

 

Program Exception Java STMIK AKAKOM YOGYAKARTA

PERTEMUAN KE – 12 Exception   pratik 1 import java.util.Scanner; public class TestError {    public static void main(String[] ...