Repository navigation
Expand file tree
/
Copy pathBai1.java
More file actions
84 lines (70 loc) · 2.28 KB
/
Copy pathBai1.java
File metadata and controls
84 lines (70 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package Chuong3;
import java.util.Scanner;
// Bài 1: Lớp DIEM
class Diem {
private float x;
private float y;
// Hàm thiết lập không tham số
public Diem() {
x = 0;
y = 0;
}
// Hàm thiết lập có tham số
public Diem(float x, float y) {
this.x = x;
this.y = y;
}
// get / set
public float getX() { return x; }
public void setX(float x) { this.x = x; }
public float getY() { return y; }
public void setY(float y) { this.y = y; }
// Nhập số thực rồi kiểm tra SAU khi ép sang float: số quá lớn thành vô cực thì nhập lại
static float nhapFloat(Scanner sc, String nhan) {
while (true) {
float v = (float) NhapLieu.soThuc(sc, nhan);
if (Float.isFinite(v)) return v;
System.out.println(" Gia tri vuot pham vi float, nhap lai!");
}
}
public void nhap(Scanner sc) {
x = nhapFloat(sc, "Nhap x: ");
y = nhapFloat(sc, "Nhap y: ");
}
public void xuat() {
System.out.println("(" + x + ", " + y + ")");
}
// Di chuyển điểm: x = x + x', y = y + y'
// Nếu tọa độ mới vượt phạm vi float thì không di chuyển và trả về false
public boolean diChuyen(float dx, float dy) {
float xMoi = x + dx;
float yMoi = y + dy;
if (!Float.isFinite(xMoi) || !Float.isFinite(yMoi)) return false;
x = xMoi;
y = yMoi;
return true;
}
}
public class Bai1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Diem a = new Diem(); // không tham số
Diem b = new Diem(3, 4); // có tham số
System.out.println("Nhap diem A:");
a.nhap(sc);
System.out.print("Diem A: ");
a.xuat();
System.out.print("Diem B: ");
b.xuat();
System.out.println("Nhap x' va y' de di chuyen A:");
float dx = Diem.nhapFloat(sc, " x' = ");
float dy = Diem.nhapFloat(sc, " y' = ");
if (a.diChuyen(dx, dy)) {
System.out.print("A sau khi di chuyen: ");
a.xuat();
} else {
System.out.println("Toa do moi vuot pham vi float, A giu nguyen.");
}
sc.close();
}
}