Jumat, 24 Oktober 2014

Tugas Algoritma Pemrograman 3

Pembahasan Codingan Program Rumus ABC 


import java.util.Scanner; 

public class SquareEquationToy{
private double a;
private double b;
private double c;
private double x1;
private double x2;
private boolean fValid = false;
public SquareEquationToy(double a, double b, double c){
this.a = a;
this.b = b;
this.c = c;
calcX();
}
private double calcDiscriminant(){
return b*b - 4*a*c;
}
private void calcX(){
double d = calcDiscriminant();
if (d > 0) {
x1 = (-b + Math.sqrt(d))/(2*a);
x2 = (-b - Math.sqrt(d))/(2*a);
fValid = true;
}
}
public boolean isValid(){
return fValid;
}
public double getA(){
return a;
}
public double getB(){
return b;
}
public double getC(){
return c;
}
public double getX1(){
return x1;
}
public double getX2(){
return x2;
}
static void test()
{
Scanner userinput = new Scanner(System.in);
System.out.print("Silahkan masukan Nilai ke 1 = ");
double x = userinput.nextDouble();
System.out.println();
System.out.print("Silahkan masukan Nilai ke 2 = ");
double y = userinput.nextDouble();
System.out.println();
System.out.print("Silahkan Masukan Nilai ke 3 = ");
double z = userinput.nextDouble();
System.out.println();
SquareEquationToy s = new SquareEquationToy(x,y,z);
System.out.println("Nilai Diskriminan adalah = " + s.calcDiscriminant ());
if (s.isValid()){
System.out.println("Persamaan "+s.getA()+"X^2 "+s.getB()+"X "+s.getC() + " mempunyai akar "+s.getX1()+"dan"+s.getX2());
}
else {
System.out.println("Persamaan "+s.getA()+"X^2 "+s.getB()+"X "+s.getC() + " Tidak mempunyai akar-akar real");
}
}
public static void main (String[]args){
System.out.println ("PROGRAM Rumus ABC");
System.out.println ("------------------------------------------------------------------------------");
test();
}
}

 

 Output kodingan pada hal 145 

Pembahasan : 

Untuk package yang digunakan yaitu import java.util.Scanner lalu disetiap string masukan, beri code [tipe var] [var] = userinput.nextDouble();  perubahan statement untuk lebih mudah dimengerti. Dalam kodingan ini menunjukkan bahwa akar-akar persamaan kuadrat dengan tiga buah variable yaitu A,B dan C dan 2 koefisien yaitu x1 dan x2. rumus yang digunakan adalah  d = b*b - 4*a*c yaitu rumus mencari Diskriminan. Dengan menginput 3 data dengan nilai ini akan menghasilkan seperti pada gambar yaitu “tidak mempunyai akar-akar riil". Dengan menggunakan program ini, bisa diubah input nilai tersebut sehingga dapat memeriksa nilai tersebut.

 

Jumat, 17 Oktober 2014

Tugas Algoritma Pemrograman 3



Pembahasan Coding Java (Hal. 54-56) 

Program :
/*

* MathExploreToy.java
* Created on October 14, 2014, 18.31
*/
public class MathExploreToy {
 
/** Creates a new instance of MathExploreToy */
 
public MathExploreToy() {
 
}
 
 
static void test() {

testE();
   
testPI();
   
testAbs();
 
}
 

static void bettertest(){
   
testIntAbs();
 
}
 
 
static void testE() {
   
System.out.println("Testing untuk Math.E");
   
System.out.println("Math.E = "+Math.E);
   
System.out.println(" ");
 
}
 
 
static void testPI() {
   
System.out.println("Testing untuk Math.PI");
   
System.out.println("Math.PI = "+Math.PI);
   
System.out.println(" ");
 
}
 
 
static void testAbs(){
   
System.out.println("Testing untuk Math.Abs() ");
   
System.out.println("Math.abs(-23.7) = "+Math.abs(-23.7));
   
System.out.println("Math.abs(0.0) = "+Math.abs(0.0));
  
System.out.println("Math.abs(23.7) = "+Math.abs(23.7));
   
System.out.println("Double.MIN_VALUE = "+Double.MIN_VALUE*(-1));
   
System.out.println("Math.abs(Double.MIN_VALUE) = "+Math.abs(Double.MIN_VALUE));
   
System.out.println("Math.abs(-Double.MIN_VALUE) = "+Math.abs(-Double.MIN_VALUE));
   
System.out.println("Double.MAX_VALUE = "+Double.MAX_VALUE);
   
System.out.println("Math.abs(Double.MAX_VALUE) = "+Math.abs(Double.MAX_VALUE));
   
System.out.println("Math.abs(-Double.MAX_VALUE) = "+Math.abs(-Double.MAX_VALUE));
   
System.out.println(" ");
 
}
 
 
static void testIntAbs(){
   
System.out.println("Cara pengujian yang lebih baik mendukung otomatisasi!");
   
System.out.println("Testing untuk Math.abs() bagi bilangan bulat");
   
if (Math.abs(-23) != 23)
    System.out.println("Math(-23) :: Error! Hasil harus 23");
   
else System.out.println("Math(-23) :: Correct!");
   
   
if (Math.abs(0) != 0) System.out.println("Math(0) :: Error! hasil harus 0");
  
else System.out.println("Math(0) :: Correct");
   
   
if (Math.abs(23) != 23) System.out.println("Math(23) :: Error! Hasil harus 23");
   
else System.out.println("Math(23) :: Correct!");
   
   
System.out.println("Integer.MIN_VALUE = "+Integer.MIN_VALUE);
   
System.out.println("Math.abs(Integer.MIN_VALUE) = "+Math.abs(Integer.MIN_VALUE));
  
System.out.println("Integer.MAX_VALUE = "+Integer.MAX_VALUE);
   
System.out.println("Math.abs(Integer.MAX_VALUE) = "+Math.abs(Integer.MAX_VALUE));
   
   
if (Math.abs(Integer.MIN_VALUE) != (-Integer.MIN_VALUE))
   
System.out.println("Math(Integer.MIN_VALUE) :: Error! ");
   
else System.out.println("Math(Integer.MIN_VALUE) :: Correct!");
   
   
if (Math.abs(Integer.MIN_VALUE) < 0)
   
System.out.println("Math(Integer.MIN_VALUE) :: Error! Harus > 0");
   
else System.out.println("Math(Integer.MIN_VALUE) :: Correct!");
   
   
if (Math.abs(Integer.MAX_VALUE) != Integer.MAX_VALUE)
   
System.out.println("Math(Integer.MAX_VALUE) :: Error! ");
   
else System.out.println("Math(Integer.MAX_VALUE) :: Correct!");
   
   
System.out.println("\n\nPerhatian!");
   
System.out.println("Pengujian menunjukkan hasil yang tidak sesuai dengan matematika!!!\n"+
"Pada kondisi batas sangat sulit diprediksi, untuk itu kita harus mengacu dokumentasi bahasa\n"+
"Pengujian otomatis kadang tidak dapat menemukannya secara benar karena dibatasi juga\n"+
"oleh kemampuan bahasa itu sendiri bila kesalahan adalah pada spesifikasi bahasa itu sendiri!\n"+
"Pengujian otomatis akan benar ditangan yang ahli, pengujian dapat ditambah sbb:\n"+
"Berdasarkan kenyataan bahwa hasil fungsi abs tidak boleh kurang dari nol");
   
System.out.println("\n"+"Di Java, Math.abs(Integer.MIN_VALUE) justru menghasilkan bilangan negatif/ \n"+
"Melanggar rumus matematika dimana hasil fungsi absolut selalu bernilai positif.\n"+
"Hasil ini dapat ditelusuri karena representasi int adalah two's complement\n"+
"Versi yang dipakai penulis :: JDK 1.8");
 
}
 
 
static void testAcos(){
   
System.out.println("Math.acos(0.9) = "+Math.acos(0.9));
 
}
 
 
static void testCeil(){
 
}
 
 
static void testCos(){
 
}
 
 
static void testExp(){
 
}
 
 
static void testFloor(){
 
}
static void testLog(){
 
}
 
 
static void testMax(){
 
}
 
 
static void testMin(){
 
}
 
 
static void testPow(){

}
 
 
static void testSin(){
 
}
 
 
static void testSqrt(){
 
}
 
 
static void testTan(){
 
}
 
 
public static void main (String[] args){
   
test();
   
bettertest();
 
}

}


Output MathExploreToy 

Pembahasan :
Pada codingan program ini menunjukkan suatu pengujian terhadap Math.E; Math.PI; dan Math.Abs. pada petunjuk yang sudah ditentukan bahwa pengjian terhadap Math.E; Math.PI dan Math.E, nilai Integer yang dihasilkan harus menghasilkan bilagan positif, bilangan bulat dan atau tidak boleh kurang dari 0(nol). Syarat pengujian Math.Abs, jika mengandung bilangan bulat maka outputnya adalah ‘Correct’.  Namun pada pengujian Math.Abs menunjukkan ada nilai Integer yang tidak merupakan bilangan bulat. Sehingga akan terjadi ‘Error’ yang melanggar rumus Matematika dimana hasil fungsi absolut selalu bernilai positif dan bilangan bulat. Meskipun demikian, pada pengujian ini menunjukkan suatu ‘Error’ karena dapat ditelusuri representatif int ini adalah two’s complement. Versi Java yang dipakai penulis adalah JDK 1.8.