Thursday, November 26, 2015

Program-Program Java



1. 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!!");
        }
        }
         }

2. 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");
        }
    }
}


3. 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");
        }
    }
}

4. konversi fahrenheit

public class konversifahrenheit  {
public static void main(String[] args) {
                                double celcius = 10;
                 double fahrenheit = (celcius*9)/5+32;
System.out.println("Jika "+celcius+" Celcius, maka suhunya dalam fahrenheit yaitu "+fahrenheit+" Fahrenheit");
}
}




5. Menghitung Jajar Genjang


 public class LuasJajarGenjang {
public static void main(String[] args) {
                                            int alas, tinggi, hasil;
                 alas = 8;
                 tinggi = 10;
                 hasil = alas*tinggi;
                 System.out.println("Luas Jajargenjang adalah "+hasil);
}
}                 
}
 

No comments:

Post a Comment