Friday, May 24, 2013

Lab VII (Kelas Esensial) - Yacoba Febriana Helweldery - H11112277


1.       Syntax:
package lab7;
import java.io.*;
public class Tugas1 {
    public static void main(String args[])throws IOException{
           
        // membuat file dan menulis dalam suatu file      
        Writer x;
        String text = "Silahkan menulis kalimat yang Anda mau.";
        File a = new File("E:/Yacoba.txt");
        x = new BufferedWriter(new FileWriter(a));
        x.write(text);
        x.close();      
    }
}

Hasil Program:

2.       Syntax:
package lab7;
import java.io.*;
public class Tugas2 {
    public static void main (String [] args) throws IOException {
        FileInputStream fis;
        FileOutputStream fos;
        int data;
       
        Writer x;
        String text = "Tugas kedua";
        File a = new File("E:\\Yacoba.txt");       
        x = new BufferedWriter(new FileWriter(a));
        x.write(text);
        x.close();       
       
        // membuka file
        try {
            fis = new FileInputStream("E:\\Yacoba.txt");
        }catch(FileNotFoundException fnfe){
                        System.out.println("Data Tidak Ditemukan");
                        return; // keluar method membuka file
        }
       
        // membuka file copy
        try{
            fos = new FileOutputStream("E:\\Yacoba-Copy.txt");
        }catch (FileNotFoundException fnfe) {
            System.out.println("Tidak Ditemukan data Copynya");
            return;
        }
       
        //membaca file
        //dan memasukkannya ke dalam file copy-an
        try {
            while ((data = fis.read()) != -1){
                         fos.write(data);}
        }catch (IOException ioe){
            System.out.println(ioe.getMessage());
        }
    }
}

Hasil Program:

3. A. Syntax:
package lab7;
import java.io.*;
public class Tugas3a {
    public static void main (String [] args){
        Runtime program = Runtime.getRuntime();
        Process game;
       
        try{
            game = program.exec("D:/MY GAME/Parampaa.exe");
        }catch(Exception e){
            System.out.println("Terjadi kesalahan");
        }
    }
}

Hasil Program:


B. Syntax:
package lab7;
import java.lang.*;
import java.util.*;
import java.io.*;
public class Tugas3b {
    public static String ReadString(){
        try{
            InputStreamReader input = new InputStreamReader(System.in);
            BufferedReader reader = new BufferedReader(input);
            return reader.readLine();
        }catch (Exception e){
            e.printStackTrace();
            return "";}
    }
   
    public static int ReadInteger(){
        try{
            InputStreamReader input = new InputStreamReader(System.in);
            BufferedReader reader = new BufferedReader(input);
            return Integer.parseInt(reader.readLine());
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }
   
    public static String InfixToPostfixConvert(String infixBuffer){
        int priority = 0;
        String postfixBuffer = ""; 
        Stack s1 = new Stack();
       
        for (int i = 0; i < infixBuffer.length(); i++){
            char ch = infixBuffer.charAt(i);
            if (ch == '+' || ch == '-' || ch == '*' || ch == '/'){
            // check the precedence
                if (s1.size() <= 0)
                    s1.push(ch);
                else{
                    Character chTop = (Character) s1.peek();
                    if (chTop == '*' || chTop == '/')
                        priority = 1;
                    else
                        priority = 0;
                    if (priority == 1){
                        if (ch == '+' || ch == '-'){
                            postfixBuffer += s1.pop();
                            i--;
                        }
                        else{
                            // Same
                            postfixBuffer += s1.pop();
                            i--;
                        }
                    }
                    else{
                        if (ch == '+' || ch == '-'){
                            postfixBuffer += s1.pop();
                            s1.push(ch);
                        }
                        else
                            s1.push(ch);
                    }
                }
            }
            else{
                postfixBuffer += ch;
            }
        }
       
        int len = s1.size();
        for (int j = 0; j < len; j++)
            postfixBuffer += s1.pop();
        return postfixBuffer;
    }
   
    public static void main(String[] args){
        String infixBuffer = "";
        String postfixBuffer = "";
       
        if(args.length == 1){
            infixBuffer = args[0];
            postfixBuffer = InfixToPostfixConvert(infixBuffer);
            System.out.println("InFix  : " + infixBuffer);
            System.out.println("PostFix: " + postfixBuffer);
            System.out.println();
        }
        else{
            infixBuffer = "a+b*c";
            postfixBuffer = InfixToPostfixConvert(infixBuffer);
            System.out.println("InFix  : " + infixBuffer);
            System.out.println("PostFix: " + postfixBuffer);
            System.out.println();        

            infixBuffer = "a+b*c/d-e";
            postfixBuffer = InfixToPostfixConvert(infixBuffer);
            System.out.println("InFix  : " + infixBuffer);
            System.out.println("PostFix: " + postfixBuffer);
            System.out.println();              

            infixBuffer = "a+b*c/d-e+f*h/i+j-k";
            postfixBuffer = InfixToPostfixConvert(infixBuffer);
            System.out.println("InFix  : " + infixBuffer);
            System.out.println("PostFix: " + postfixBuffer);
            System.out.println();
        }
    }
}
   

Hasil Program:

Sunday, May 5, 2013

H11112277-03-PEMOGRAMAN


1.       Syntaks:
package tugaspakarmin;
import java.util.Scanner;
public class scanner1 {
    public static void main(String[] args) {
        java.util.Scanner Tugas1 = new java.util.Scanner (System.in);
        System.out.println("Menghitung Volume Bangun Ruang Balok");
        System.out.println("");
       
        System.out.println("Masukkan nilai p = ");
        int p = Tugas1.nextInt();
       
        System.out.println("Masukkan nilai l = ");
        int l = Tugas1.nextInt();
       
        System.out.println("Masukkan nilai t = ");
        int t = Tugas1.nextInt();  
   
        System.out.println("");
        System.out.println("Volume balok = " + (p*l*t));
       
    }
}

Program:



2.       Syntaks:
package tugaspakarmin;
import java.io.*;
public class buffered {
    public static void main (String[] args){
        BufferedReader dataIn = new BufferedReader (new InputStreamReader (System.in));
       
        String name = "";
       
        System.out.println("Masukkan nama Anda = ");
        try{
            name = dataIn.readLine();
        }
        catch (IOException e){
            System.out.println("Error!");
        }
        System.out.println("\nHallo " +name+ "!\nSelamat Datang\nTuhan Memberkati (>_<)");
    }
}

Program:

3.       Syntaks:
package tugaspakarmin;

public class generik {
    private A data;
    public generik (A data) {
        this.data = data;
    }
    public A getData() {
        return data;
    }
}

class GenSample {
    public String method(String input) {  
        String data1 = input;
        generik basicGeneric = new generik <>(data1);
        String data2 = basicGeneric.getData();
        return data2;
    }
   
    public Integer method(int input) {
        Integer data1 = new Integer(input);
        generik basicGeneric = new generik <>(data1);
        Integer data2 = basicGeneric.getData();
        return data2;
    }
   
    public static void main(String args[]) {
        GenSample sample = new GenSample();
        System.out.println(sample.method("Some generic data"));
        System.out.println(sample.method(1234));
}
}

Program:

H11112277-04-PEMOGRAMAN



1.       Syntaks:
package tugaspakarmin;
import java.util.logging.*;
public class konkurensi implements Runnable {

    //
    public static int countwriter = 0;
    public static int countreader = 0;
    //
    public static final int READER = 0;
    public static final int WRITER = 1;
    //
    private int type;
    private int id;
    private String strid;

    public konkurensi (int type) {
        this.type = type;
        this.strid = (type == READER) ? ("Reader " + (this.id = ++countreader)) : ("Writer " + (this.id = ++countwriter));
    }

    public static void main(String[] argv) {
        (new Thread(new konkurensi(READER))).start();
        (new Thread(new konkurensi(READER))).start();
        (new Thread(new konkurensi(WRITER))).start();
    }

    public void run() {
        if (type == READER) {
            read();
        } else if (type == WRITER) {
            write();
        }
    }

    //*********************************************************
    // Reader
    //*********************************************************
    /**
     *
     */
    public void read() {
        System.out.println(strid + ": start!");
        while (true) {
            System.out.println(strid + ": reading!");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(konkurensi.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    //*********************************************************
    //*********************************************************
    // Writer
    //*********************************************************

    public void write() {
        System.out.println(strid + ": start!");
        while (true) {
            System.out.println(strid + ": writing!");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(konkurensi.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    //*********************************************************
}

2.       Program :