Professional Documents
Culture Documents
Định nghĩa
Code refactoring là hoạt động chỉnh sửa khiến source code dễ đọc hơn, được tổ chức khoa học hơn, và (có thể) có kiến trúc /
cấu trúc tốt hơn nhưng không làm thay đổi hành vi của hệ thống về mặt chức năng.
Yêu cầu:
- Tạo Project Họ và Tên của sinh vien (viết sát vào nhau không dấu, ví dụ nguyendangquanghuy, tạo package có tên
Refactoring, các class name là class nname của bài tập
Câu 1. Refactoring that codes
package REFACTOR;
public maxnumber() {
// TODO Auto-generated constructor stub
}
import java.util.Scanner;
class Instructor {
String Id; String
name; int
yearOfExp;
public Instructor() {
// TODO Auto-generated constructor stub
}
class Student {
String Id;
String name;
String ClassName;
public Student() {
// TODO Auto-generated constructor stub
}
package REFACTOR;
import java.util.Scanner;
public binary() {
// TODO Auto-generated constructor stub
}
public void FourNumber() { int Decimal = 0;
for (int i = 0; i < 2; i++) for (int
j = 0; j < 2; j++) for (int k = 0; k <
2; k++) for (int l = 0; l < 2;
l++) {
System.out.println("" + i + j
+ k + l + "\t" + Decimal);
Decimal++;
}
}
public static void main(String a[]) {
binary oc = new binary();
System.out.println("Binary\tDecimal");
oc.FourNumber();
}
}
package refactoring;
public OctanNumber() {
// TODO Auto-generated constructor stub
}
Equation.Quadratic(a, b, c);
Equation.Linear(a, b);
scanner.close();
}
public static void Linear(float a, float b) {
if (a == 0) {
if (b == 0)
System.out.println("Phương trình vô số nghiệm");
else
System.out.println("Phương trình vô nghiệm!");
} else {
System.out.println("Phương trình có một nghiệm: "
+ "x = " + (-b / a));
}
}
/**
* Giải phương trình bậc 2: ax2 + bx + c = 0
*
* @param a: hệ số bậc 2
* @param b: hệ số bậc 1
* @param c: số hạng tự do
* @param d: hệ số bậc 0
*/
public static void Quadratic(float a, float b, float c)
{
// kiểm tra các hệ số if
(a == 0) { if (b ==
0) { if (c == 0) {
System.out.println("Phương trình vô số
nghiệm");
} else
System.out.println("Phương trình vô
nghiệm!");
} else {
System.out.println("Phương trình có một
nghiệm: " + "x = " + (-c / b));
}
return;
}
// tính delta
float delta = b * b - 4 * a * c;
float x1; float
x2; // tính nghiệm
if (delta > 0) {
x1 = (float) ((-b + Math.sqrt(delta)) / (2 * a));
x2 = (float) ((-b - Math.sqrt(delta)) / (2 * a));
System.out.println("Phương trình có 2 nghiệm
là: " + "x1 = " + x1 + " và x2 = " + x2);
} else if (delta == 0) { x1 =
(-b / (2 * a));
System.out.println("Phương trình có nghiệm
kép: " + "x1 = x2 = " + x1);
} else {
System.out.println("Phương trình vô nghiệm!");
}
}
}
class dog {
String name; int
height; int
weight;
void bark() {
System.out.println("The dog barking...");
}
}
class Cat {
String name; int
height; int
weight;
void meow() {
System.out.println("The cat meowing...");
}
}
Câu 6.
Refactoring that codes (Duplicate code) ***
package REFACTOR;