Buat project dengan nama project
OlahTitik
Buat kelas dengan nama Titik. berikut
source code dari kelas Titik.
/*
*
To change this template, choose Tools | Templates
*
and open the template in the editor.
*/
package olahtitik;
/**
*
*
@author Yudho */
public class Titik {
private double x;
private double y;
public Titik() {
}
public Titik(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public String getKoordinat() {//fungsi
untuk menampilkan koordinat
return "(" + x + "," + y +
")";
}
public static double hitJarak(Titik t1,
Titik t2) { //fungsi untuk mencari jarak 2 titik
double dx = t1.x - t2.x;
double dy = t1.y - t2.y;
return Math.sqrt(dx * dx + dy * dy);
}
public Titik(Titik t1, Titik t2)
{//construktor untuk mencari titik tengah
x
= (t1.x + t2.x) / 2;
y
= (t1.y + t2.y) / 2;
}
}
isi di mainnya dengan source code di
bawah ini.
/*
*
To change this template, choose Tools | Templates
*
and open the template in the editor.
*/
package olahtitik;
/**
*
*
@author Yudho
*/
public class Main {
/**
*
@param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Titik t1 = new Titik();//untuk membuat objek
t1
t1.setX(1); //memasukkan nilai untuk variabel
x
t1.setY(6); // memasukkan nilai untuk variabel
y
System.out.println("Menampilkan info
Kelas Titik t1 : ");
System.out.println(t1.getKoordinat());
Titik t2 = new Titik(10, 6);
System.out.println("Menampilkan info
Kelas Titik t2 : ");
System.out.println(t2.getKoordinat());
System.out.println("jarak dua titik
P: " + Titik.hitJarak(t1, t2));
Titik t3 = new Titik(t1, t2);
System.out.println("Titik Tengah :("
+ t3.getX() + "," + t3.getY() + ")");
}
}
0 komentar:
Posting Komentar