Program Sederhana dalam Java


Berikut adalah beberapa codingan mengenai program sederhana dalam Java :

Menghitung Nilai Akhir Sekolah

import java.util.Scanner;
public class NilaiAkhir {
static double na,nUTS,nUAS,nTugas,Nilai;
static Scanner s;
public static void main(String[] args){
s = new Scanner(System.in);

System.out.println("Masukkan Nilai UAS :");
nUAS = s.nextInt();

System.out.println("Masukkan Nilai UTS :");
nUTS = s.nextInt();
System.out.println("Masukkan Nilai Tugas :");
nTugas = s.nextInt();
na = (nUAS*0.35)+(nUTS*0.3)+(nTugas*0.35);

System.out.println("Nilai Akhir = " +String.format("%.2f",na));
Nilai=na;
System.out.println("Nilai Huruf = " +getHuruf(Nilai));
}
static char getHuruf(double Nilai){
char huruf;
if (NilaiAkhir.Nilai<50)
huruf='E';
else if (NilaiAkhir.Nilai<60)
huruf='D';
else if (NilaiAkhir.Nilai<70)
huruf='C';
else if(NilaiAkhir.Nilai<85)
huruf='B';
else
huruf='A';
return huruf;
}
}
Luas Segitiga

import java.util.Scanner;
public class LuasSegitiga {
static double alas;
static double tinggi;
static double Luas;
static Scanner s;
public static void main(String[] args){
s = new Scanner(System.in);

System.out.println("Masukkan Alas :");
alas = s.nextInt();

System.out.println("Masukkan Tinggi :");
tinggi = s.nextInt();

Luas = (alas*tinggi)/2;

System.out.println("("+alas+"*"+tinggi+")/2= "+Luas);
}
}
Operator Kondisi

import java.util.Scanner;
public class OprKondisi{
public static void main(String[] x){
String keterangan;
Scanner s= new Scanner(System.in);
int nilai;
System.out.print("Masukkan nilai:");
nilai=s.nextInt();
keterangan=(nilai>70)?"Lulus":"Gagal";
System.out.println(keterangan);
}
}
Array

import java.util.ArrayList;
import java.util.List;
public class ProgramArray{
private List < Integer > arr = new ArrayList < Integer >();
private int jumlah = 0 ;
private int terbesar ;
private double rata ;
public void input ( int value ) {
this . arr . add (value);
}
public int hitung () {
int i ;
for (i= 0 ; i< this . arr . size (); i++) {
this . jumlah += this . arr . get (i);
}
return this . jumlah ;
}
public int cariTerbesar () {
int i ;
this . terbesar = this . arr . get ( 0 );
for (i= 0 ; i< this . arr . size (); i++) {
if ( this . arr . get (i) > this . terbesar ) {
this . terbesar = this . arr . get (i);
}
}
return this . terbesar ;
}
public double average () {
this . rata = this . jumlah / this . arr . size ();
return this . rata ;
}
public void print () {
System . out . println ( this . arr . size ());
}
}
Program Hitung Luas dengan Case 

import java.util.Scanner;
public class ProgramHL{
double panjang,luas,jari,lebar,alas,tinggi;
double phi = 3.14;
Scanner masuk = new Scanner(System.in);
public static void main(String[]args){
ProgramHL object = new ProgramHL();
object.program();
}
public void program(){
System.out.println("=========================");
System.out.println(" Program Menghitung Luas ");
System.out.println("1. Persegi Panjang");
System.out.println("2. Segitiga");
System.out.println("3. Lingkaran");
System.out.println("4. Keluar");
System.out.println("=========================");
System.out.println("Masukkan Pilihan :");
int pilihan = this.masuk.nextInt();

switch(pilihan){
case 1: persegipanjang();
break;
case 2: segitiga();
break;
case 3: lingkaran();
break;
case 4: keluar();
break;
default:System.out.println("Pilihan Yang Anda Masukkan Salah");
program();
}
}
void persegipanjang(){
System.out.println("Masukkan Lebar :");
lebar = masuk.nextDouble();
System.out.println("Masukkan Panjang :");
panjang = masuk.nextDouble();
luas = (lebar*panjang);
System.out.println(+lebar+"*"+panjang+" = "+luas);
}
void segitiga(){
System.out.println("Masukkan Alas :");
alas = masuk.nextInt();

System.out.println("Masukkan Tinggi :");
tinggi = masuk.nextInt();

luas = (alas*tinggi)/2;

System.out.println("("+alas+"*"+tinggi+")/2= "+luas);
}
void lingkaran(){
System.out.println("Masukkan Jari-jari Lingkara :");
jari = masuk.nextInt();

luas = (jari*jari)*phi;

System.out.println("("+jari+"*"+jari+")*phi = "+luas);
}
void keluar(){
System.out.println("Terima Kasih");
System.exit(4);
}
}
If

import java.util.Scanner;
public class TesIf{
public static void main(String[]x){
Scanner s= new Scanner(System.in);
int nilai;
System.out.print("Masukkan nilai:");
nilai = s.nextInt();

if(nilai>70)
System.out.println("Lulus");
else
System.out.println("Gagal");
}
}
If Else

import java.util.Scanner;
public class TestIfElseIf{
public static void main(String[]x){
Scanner s= new Scanner(System.in);
int nilai;
System.out.print("Masukkan nilai:");
nilai = s.nextInt();
System.out.println(getHuruf(nilai));
}
static char getHuruf(int nilai){
char huruf;
if (nilai<50)
huruf='E';
else if (nilai<60)
huruf='D';
else if (nilai<70)
huruf='C';
else if (nilai<85)
huruf='B';
else
huruf='A';
return huruf;
}
}

Comments