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:
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: