App Test Java (Complex, Segitiga, Suhu)
Yang Pertama Kita
- Buat Class Complex
public class Complex { private double real = 0; private double imaginer = 0; public Complex() { } public Complex(double real, double imaginer) { this.real = real; this.imaginer = imaginer; } public Complex(Complex other) { this.real = other.real; this.imaginer = other.imaginer; } public void setReal(double real) { this.real = real; } public void setImaginer(double imaginer) { this.imaginer = imaginer; } public double getReal() { return real; } public double getImaginer() { return imaginer; } public void add(Complex other) { real = this.real + other.real; imaginer = this.imaginer + other.imaginer; } public void sub(Complex other) { real = this.real - other.real; imaginer = this.imaginer - other.imaginer; } public void mul(Complex other) { double Reall = ((this.real * other.real) - (this.imaginer * other.imaginer)); double Imaginerr = ((this.real * other.imaginer) + (this.imaginer * other.real)); real = Reall; imaginer = Imaginerr; } public void div(Complex other) { double bagi = ( (other.real * other.real) + (other.imaginer * other.imaginer) ); if(bagi == 0) { real = Double.POSITIVE_INFINITY; imaginer = Double.POSITIVE_INFINITY; } else { double reall = ( (this.real * other.real) + (this.imaginer * other.imaginer) ) / bagi; double imaginerr = ( (other.real * this.imaginer) - (this.real * other.imaginer)) / bagi; real = reall; imaginer = imaginerr; } } public int compareTo(Complex other) { double magnitude1 = Math.sqrt(this.real * this.real + this.imaginer * this.imaginer); double magnitude2 = Math.sqrt(other.real * other.real + other.imaginer * other.imaginer); if (magnitude1 == magnitude2) { return 0; } else if(magnitude1 > magnitude2) { return 1; } else { return -1; } }
- Buat Class Suhu
public class Suhu { private double kelvin = 0.0; public Suhu (){ } public Suhu (double kel){ if (isValid(kel)) kelvin = kel; } private boolean isValid(double k){ if(k>=0) return true; else return false; } public double getKelvin(){ return kelvin; } public void setKelvin(double k){ if (isValid(k)) kelvin = k; } public double getCelcius(){ double c = kelvin - 273.15; return c; } public void setCelcius(double c){ if(isValid(c)) kelvin = c + 273.15; } public double getFahrenheit(){ double f = (kelvin * 9/5) - 459.67; return f; } public void setFahrenheit(double f){ if (isValid(f)) kelvin = (f + 459.67)* 5/9; } public double getReamur(){ return (kelvin - 273.15) * 4/5; } public void setReamur(double r){ if (isValid(r)) kelvin = r * 5/4 + 273.15; } public String getInfo(){ String i; double v = kelvin; if (isValid(v)) i = "Suhu{kelvin=" + kelvin+ "}"; else i = "Suhu{kelvin=" + kelvin+ "}"; return i; }
- Buat Class Segitiga
public class Segitiga { private double SisiA = 1.0; private double SisiB = 1.0; private double SisiC = 1.0; public Segitiga() { } public double getSisiA() { if (SisiA < 0 ) SisiA = 0; else if(SisiA == 0 || SisiA == 3) SisiA = 1.0; return SisiA; } public double getSisiB() { if (SisiB < 0) SisiB = 0; else if(SisiB == 0 || SisiB == 3) SisiB = 1.0; return SisiB; } public double getSisiC() { if (SisiC < 0) SisiC = 0; else if(SisiC == 0 || SisiC == 3) SisiC = 1.0; return SisiC; } public Segitiga(double a, double b, double c) { if (isValid (a,b,c)) { SisiA = a; SisiB = b; SisiC = c; } } public void setSisiA(double a) { if(a < 0) SisiA = 1.0; else SisiA = a; } public void setSisiB(double b) { if(b < 0) SisiB = 1.0; else SisiB = b; } public void setSisiC(double c) { if(c < 0) SisiC = 1.0; else SisiC = c; } public double getKeliling() { return (getSisiA() + getSisiB() + getSisiC()); } public double getLuas() { double s = (0.5 * (SisiA + SisiB + SisiC)); return (Math.sqrt(s * (s - SisiA)*(s - SisiB)*(s - SisiC) )); } public String getInfo() { return "Segitiga{sisiA="+SisiA+", sisiB="+SisiB+", sisiC="+SisiC+"}"; } public boolean isSamaSisi() { if (SisiA == SisiB && SisiA == SisiC ){ return true; } else{ return false; } } public boolean isSamaKaki() { if( SisiA == SisiB || SisiA == SisiC || SisiB == SisiC ) { return true; } else return false; } private boolean isValid(double a, double b, double c) { boolean test1 = false; boolean test2 = false; if((a > 0) && (b > 0) && (c > 0)){ test1 = true; } else if (a < 0){ SisiB = b; SisiC = c; } else if (b < 0){ SisiA = a; SisiC = c; } else if(c < 0){ SisiA = a; SisiB = b; } else return test1 = false; if (a > b && a > c && a < b +c) test2 = true; else if (b > a && b > c && b < a + c) test2 = true; else if (c > a && c > b && c < a + b) test2 = true; else if (a == b && a == c) test2 = true; else test2 = false; return test1 && test2; } public Segitiga(Segitiga other) { this(other.SisiA,other.SisiB,other.SisiC); } public boolean isKongruen(Segitiga other) { if ((this.SisiA == other.SisiA) && (this.SisiB == other.SisiB) && (this.SisiC == other.SisiC)) { return true; } else if((this.SisiA == other.SisiC) && (this.SisiB == other.SisiA) && (this.SisiC == other.SisiB)) { return true; } else if((this.SisiA == other.SisiC) && (this.SisiB == other.SisiB) && (this.SisiC == other.SisiA)) { return true; } else if((this.SisiA == other.SisiA) && (this.SisiB == other.SisiC) && (this.SisiC == other.SisiB)) { return true; } else{ return false; } }
Jika Ingin Mendownload Source Code Yang Lebih lengkap Bisa Langsung KLik Link Di Bawah iniDOWNLOAD SC
Untuk Membuat Class Test Nya Dapat Lihat Pada Artikel Berikut Ini