Ayo Belajar Java lagi

Relasi Demo menggunakan input dari keyboard :
import java.util.Scanner;
public class Tugas2
{      
       public static void main(String args[])
       {
              Scanner input = new Scanner(System.in);
              int i;
              int j;
              int k;

              System.out.print( " Masukkan nilai i : ");
              i = input.nextInt();
              System.out.print( " Masukkan nilai j : ");
              j = input.nextInt();
              System.out.print( " Masukkan nilai k : ");
              k = input.nextInt();

              System.out.println("Variabel ...");
              System.out.println(" i = "+i);
              System.out.println(" j = "+j);
              System.out.println(" k = "+k);
      
              //bigger than
              System.out.println("Bigger than ...");
              System.out.println(" i>j = "+(i>j));//false
              System.out.println(" j>i = "+(j>i));//true
              System.out.println(" k>j = "+(k>j));//false

              //Bigger or equals
              System.out.println("Bigger or equals ...");
              System.out.println(" i>=j = "+(i>=j));//false
              System.out.println(" j>=i = "+(j>=i));//true
              System.out.println(" k>=j = "+(k>=j));//true

              //less than
              System.out.println("Less than .. ");
              System.out.println(" i<j = "+(i<j));//true
              System.out.println(" j<i = "+(j<i));//false
              System.out.println(" k<j = "+(k<j));//false
      
              //smaller or equals
              System.out.println("Smaller or equals ... ");
              System.out.println(" i<=j = "+(i<=j));//true
              System.out.println(" j<=i = "+(j<=i));//false
              System.out.println(" k<=j = "+(k<=j));//true
      
              //equal
              System.out.println("equal ... ");
              System.out.println(" i==j = "+(i==j));//false
              System.out.println(" k==j = "+(k==j));//true

              //not same as
              System.out.println("not same as ... ");
              System.out.println(" i!=j = "+(i!=j));//true
              System.out.println(" k!=j = "+(k!=j));//false
       }
}

Analisis Program
Program Tugas2 pada dasarnya sama dengan program Relsai Demo pada program III - 6, hanya yang membedakannya  pada variable pembanding, untuk Relasi Demo variable i, j, and k telah diinisialisasikan terlebih dahulu dengan nilai yang tetap, sedangkan pada Tugas2 variable i, j, dan k diinput memalui keyboard sesuai dengan keinginan user.
3. Program SumAverage
import java.util.Scanner;
public class SumAverage
{
       public static void main(String args[]){
              Scanner input = new Scanner(System.in);
              int x;
              int y;
              int z;
              int sum;
              float average;

              System.out.print("\n Program Penghitung Jumlah dan Rata-Rata\n ");        
              System.out.print("\n Masukan Nilai ke-1 : ");
              x = input.nextInt();

              System.out.print(" Masukan Nilai ke-2 : ");
              y = input.nextInt();

              System.out.print(" Masukan Nilai ke-3 : ");
              z = input.nextInt();

              sum = x+y+z;
              System.out.printf("\n Hasil Penjumlahan "+x+" + "+y+" + "+z+"\t= " +sum);

              average = (sum/3);
              System.out.println("\n Hasil Rata - Rata "+sum+" / 3\t= "+average);
       }
}

Analisis Program
Program SumAverage adalah program yang memiliki dua fungsi, yaitu penjumlahan dan rata-rata . Dengan menggunakan 3 variable yang diinput dari keyboard oleh user , program ini menjumlahkan dan menghitung rata-ratanya.
4. Program menggunakan BufferReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Tugas4
{
       public static void main(String[] args)
       {
              BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
             
              String name="";
              System.out.print("\n Please Enter Your Name\t\t: ");
              try
                     {
                     name = dataIn.readLine();
                     }
              catch( IOException e )
                     {
                     System.out.println("Error!");
                     }
      
              String hobby="";
              System.out.print(" Please Enter Your Hobby\t: ");
              try
                     {     
                     hobby = dataIn.readLine();
                     }
                     catch( IOException e ){
                     System.out.println("Error!");
                     }

              System.out.println("\n So your hobby is "+hobby+". Nice hobby Mr. "+name+" !");
       }
}

Analisis Program

Program Tugas4 tersebut sama dengan Program GetinputFromKeyboard2, perbedaannya hanya terdapat pada penggunaan JOptionPane. Nama dan hobby diinput oleh user dari keyboard, kemudian ditampilkan menjani satu kalimat.

0 Response to "Ayo Belajar Java lagi"

Post a Comment