You are on page 1of 14

BÀI THỰC HÀNH REFACTORING

Đị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 class maxnumber {

public maxnumber() {
// TODO Auto-generated constructor stub
}

public int max2(int a, int b) {


if (a >= b)
return a;
else
return b;
}

public int max3(int a, int b, int c) {


if (a >= b)
if (a >= c)
return a;
else
return c;
else if (b >= c)
return b;
else
return c;
}
}
Câu 2. Refactoring that codes (Deeply nested loop)
package REFACTOR;

public class Hexa {

public void FourNumber() {


char[] d = new char[] { 48, 49, 50, 51, 52, 53,
54, 55, 56, 57, 65, 66, 67, 68, 69, 70 };
int Decimal = 0;
for (int i = 0; i < 16; i++)
for (int j = 0; j < 16; j++)
for (int k = 0; k < 16; k++)
for (int l = 0; l < 16; l++) {
System.out.println("" + d[i]
+ d[j] + d[k] + d[l] + "\t" + Decimal);
Decimal++;
}
}

public static void main(String a[]) {


Hexa oc = new Hexa();
System.out.println("Hexa\tDecimal");
oc.FourNumber();
}

Câu 3. Refactoring that codes (too many parameter)


public class student { public void CreateNewUser(int
id, String name, String username, String password,
String capchar)
{
System.out.println(id + " " + name);
}
}
Câu 4. Refactoring that codes (Rooting too long)
package REFACTOR;

import java.util.Scanner;

public class array1 {


/**
* main
*
* @param args
*/
public static void main(String[] args) {
System.out.println("Đề bài: Viết chương trình Java
nhập một mảng số nguyên a0, a1, a2, …, an-1. Hãy sắp xếp
mảng theo thứ tự tăng dần");
Scanner scanner = new Scanner(System.in);
System.out.print("Nhập số phần tử của mảng: ");
int n = scanner.nextInt();
// khởi tạo arr int[] arr =
new int[n];
System.out.print("Nhập các phần tử của mảng:
\n");
for (int i = 0; i < n; i++) {
System.out.printf("a[%d] = ", i);
arr[i] = scanner.nextInt();
}
// sắp xếp dãy số theo thứ tự tăng dần
int temp = arr[0];
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
System.out.println("Dãy số được sắp xếp tăng dần:
");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
Câu 5. Refactoring that codes (Inheritance)
package REFACTOR;

class Instructor {
String Id; String
name; int
yearOfExp;

public Instructor() {
// TODO Auto-generated constructor stub
}

public int getSalary() {


return 1;
}
}

class Student {
String Id;
String name;
String ClassName;

public Student() {
// TODO Auto-generated constructor stub
}

public int getScholarship() {


return 1;
}
}

Câu 6. Refactoring that codes (Duplicate code) ***


package REFACTOR;
public class Sort
{ int a; int b;
int c;
public void ascSort() {
if (a > b) {
int temp = a;
a = b; b =
temp;
}
if (a > c) {
int temp = a;
a = c;
c = temp;
}
if (b > c) {
int temp = b;
b = c; c =
temp;
}
}
}

Câu 1. Refactoring that codes (Duplicate code)

package REFACTOR;

import java.util.Scanner;

public class print {


public static void printStart() {
System.out.println("*****************");
System.out.println("******START******");
System.out.println("*****************");
}

public static void printEnd() {


System.out.println("*****************");
System.out.println("*******END*******");
System.out.println("*****************");
}

public static void main(String[] args) {


// int i;
printStart();
System.out.println("THE MAIN FUNCTION");
printEnd();
//Close the variable
}
}

Câu 2. Refactoring that codes (Deeply nested loop)


package REFACTOR;

public class binary {

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();
}
}

Câu 3. Refactoring that codes (too many parameter)


package REFACTOR;

public class paint {

// print the result in location(x,y) with the color(red


blue,green)
public void paintResult(int x, int y, byte red, byte
blue, byte green) {
// do not need to rewrite this code
System.out.println(x + ", " + y);
System.out.println(red + ", " + blue + ", " + green);
}
}
Câu 4. Refactoring that codes (Rooting too long)
public class array { public static void
main(String[] args) {
// Khai báo các biến cần thiết
int n;
int[] soNguyen;
Scanner sc = new Scanner(System.in);
// Nhập dữ liệu
System.out.println(
"Đề bài: Viết chương trình cho phép
nhập vào n, sau đó nhập vào n phần tử số nguyên.\r\nSắp
xếp các phần tử trong mảng theo thứ tự ngược lại.");
System.out.println("Nhập vào n:");
n = sc.nextInt(); soNguyen
= new int[n]; for (int i = 0; i <
n; i++) {
System.out.println("Nhập vào số nguyên:");
soNguyen[i] = sc.nextInt();
}
// In ra mảng ban đầu
System.out.println("Mảng trước khi đảo ngược: ");
for (int i = 0; i < n; i++)
System.out.print(soNguyen[i] + " ");
// Đảo ngược mảng for (int i =
0; i < n / 2; i++) {
int empty; empty =
soNguyen[i]; soNguyen[i] = soNguyen[n
- i - 1];
soNguyen[n - i - 1] = empty;
}
// In ra mảng sau khi đảo ngược
System.out.println("\nMảng sau khi đảo ngược: ");
for (int i = 0; i < n; i++)
System.out.print(soNguyen[i] + " ");
}
}

Câu 5. Refactoring that codes (Inheritance)


package REFACTOR;
class
Coder
{
String Name;
String ID; int
level;
public void computeSalary() {
System.out.println("The salary of Coder");
}
}
class
Manager
{
String Name;
String ID;
public int computeSalary() {
return 20000000;
}
}

Câu 6. Refactoring that codes (Duplicate code) ***


package REFACTOR;

public class Sort {


int a;
int b;
int c;
public void descSort() {
if (a < b) {
int temp = a;
a = b;
b = temp;
}
if (a < c) {
int temp = a;
a = c;
c = temp;
}
if (b < c) {
int temp = b;
b = c;
c = temp;
}
}
}

Câu 1. Refactoring that codes (Duplicate code)


package REFACTOR;

public class circle {


double r;

public double Area() {


return r * r * 3.14;
}

public double Primeter() {


return 2 * 3.14 * r;
}
}

Câu 2. Refactoring that codes (Deeply nested loop)

package refactoring;

public class OctanNumber {

public OctanNumber() {
// TODO Auto-generated constructor stub
}

public void FourNumber() { int Decimal = 0;


for (int i = 0; i < 8; i++) for (int j = 0; j
< 8; j++) for (int k = 0; k < 8; k++)
for (int l = 0; l < 8; l++) {
System.out.println("" + i + j + k
+ l + "\t" + Decimal);
Decimal++;
}
}
public static void main(String a[]) {
OctanNumber oc = new OctanNumber();
System.out.println("Octan\tDecimal");
oc.FourNumber();
}
}

Câu 3. Refactoring that codes (too many parameter)


package REFACTOR;

public class student {

public void display(int id, String name, int classID,


String birthday, String address, String phone) {
System.out.println(id + ", " + name + ", " +
classID + ", " + birthday + ", " + address + ", " +
phone);
}
}
Câu 4. Refactoring that codes (routing too long and duplicate code)
import java.util.Scanner;

public class Equation {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Nhập hệ số bậc 2, a = ");
float a = scanner.nextFloat();
System.out.print("Nhập hệ số bậc 1, b = ");
float b = scanner.nextFloat();
System.out.print("Nhập hằng số tự do, c = ");
float c = scanner.nextFloat();

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!");
}
}
}

Câu 5. Refactoring that codes (Inheritance)


package REFACTOR;

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;

public class Sort {


int a; int
b; int c;
public void ascSort() {
if (a > b) {
int temp = a;
a = b;
b = temp;
}
if (a > c) {
int temp = a;
a = c;
c = temp;
}
if (b > c) {
int temp = b;
b = c; c =
temp;
}
}
}

You might also like