You are on page 1of 6

Đề thi Lập trình Java Kỳ 2 năm học 2021-2022 (Đề 4).

Thời gian làm bài 60 phút


Họ tên:……………………………Lớp:……………………………MSSV:……………………...
Dán code và kết quả lên trên file này, lưu bài với định dạng MSSV_HọTên.pdf và nộp bài thông qua MS Teams

1. Hãy viết chương trình nhập vào 1 chuỗi ký tự và thực hiện các yêu cầu sau
(3đ): Yêu cầu:
a. Kiểm tra chuỗi đó có phải là số nguyên hay không.
b. Nếu là số nguyên thì hoán đổi vị trí các ký tự đó để được 1 số chia hết
cho 45, in ra số đó. Nếu không thể hoán đổi để được 1 số chia hết cho 45
thì in ra -1.
i. Ví du:
1. Input: 9576
2. Output 9675
# Dán code vào bên dưới
package GiuaKy;

import java.util.Scanner;

public class Cau1 {

public static void main(String[] args) {


String s ;
Scanner scanner = new Scanner(System.in);

System.out.println("Nhập chuỗi: ");


s = scanner.nextLine();
if(isInteger(s)) {
System.out.println("Đây là số nguyên");
}
else {
System.out.println("Đây không phải là số nguyên");
}

StringBuilder sb = new StringBuilder(s);


sb.reverse();
String text = sb.toString();
System.out.println("Reversed string: " + text);
}

public static boolean isInteger(String s) {


if(s == null || s.equals("")) {
System.out.println("Không được để trống");
return false;
}

try {
int iVal = Integer.parseInt(s);
return true;
}
catch(NumberFormatException e) {
System.out.println("không thể phân tích chuỗi thành số");
}
return false;
}

# Dán kết quả thực thi vào bên dưới:


Nhập chuỗi:
1212334
Đây là số nguyên
Reversed string: 4332121

2. Hãy viết 1 interface với tên là MonHoc với 1 phương thức trừu tượng (3đ):
double diem(double BT, double GK, double CK)
Sau đó xây dựng 2 lớp kế thừa từ MonHoc như yêu cầu sau:
a. Lớp MonKyThuatLapTrinh có công thức tính điểm là
Diem=BT*0.2+GK*0.3+CK*0.5
b. Lớp MonLapTrinhJava có công thức tính điểm là
Diem=BT*0.2+GK*0.2+CK*0.6
c. Xây dựng hàm main chứa 2 thực thể được sinh ra từ 2 lớp trên rồi nhập
và in kết quả tính toán của hàm diem.

# Dán code vào bên dưới


package GiuaKy;

import java.util.Scanner;

interface MonHoc
{

public double diem(double BT, double GK, double CK);


}

class MonKyThuatLapTrinh implements MonHoc


{
double BT;
double GK;
double CK;
public double diem(double BT, double GK, double CK)
{
return BT * 0.2 + GK * 0.3 + CK*0.5;
}
}

class MonLapTrinhJava implements MonHoc


{
double BT;
double GK;
double CK;
public double diem(double BT, double GK, double CK)
{
return BT * 0.2 + GK * 0.2 + CK*0.6;
}
}

public class Cau2 {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
MonKyThuatLapTrinh a = new MonKyThuatLapTrinh();
MonLapTrinhJava b = new MonLapTrinhJava();

System.out.print("Diem Bai Tap MonKyThuatLapTrinh: ");


a.BT = sc.nextDouble();
System.out.print("Diem Giua Ky MonKyThuatLapTrinh: ");
a.GK = sc.nextDouble();
System.out.print("Diem Cuoi Ky MonKyThuatLapTrinh: ");
a.CK = sc.nextDouble();
System.out.print("Diem Bai Tap MonLapTrinhJava: ");
b.BT = sc.nextDouble();
System.out.print("Diem Giua Ky MonLapTrinhJava: ");
b.GK = sc.nextDouble();
System.out.print("Diem Cuoi Ky MonLapTrinhJava: ");
b.CK = sc.nextDouble();

System.out.println("Diem MonKyThuatLapTrinh: " + a.diem(a.BT, a.GK,


a.CK));
System.out.println("Diem MonLapTrinhJava: " + b.diem(b.BT, b.GK,
b.CK));

}
}

# Dán kết quả thực thi vào bên dưới:


Diem Bai Tap MonKyThuatLapTrinh: 1
Diem Giua Ky MonKyThuatLapTrinh: 2
Diem Cuoi Ky MonKyThuatLapTrinh: 3
Diem Bai Tap MonLapTrinhJava: 1
Diem Giua Ky MonLapTrinhJava: 2
Diem Cuoi Ky MonLapTrinhJava: 3
Diem MonKyThuatLapTrinh: 2.3
Diem MonLapTrinhJava: 2.4

3. Xây dựng giao diện sau (4đ):


Mỗi ô nhỏ là 1 nút bấm. Khi bấm nút bất kỳ thì hiện ra 1 JFrame hiển thị dòng
chữ “Hello Calendar”.

# Dán code vào bên dưới

package GiuaKy;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.LabelView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class bai3 extends JFrame{

public static void main(String[] args) {


new bai3();
}
public bai3() {
this.setTitle("My Calender");
this.setSize(300,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setLayout(new BorderLayout());

JPanel p = new JPanel();


JPanel q = new JPanel();
p.setLayout(new GridLayout(2,2));
q.setLayout(new GridLayout(2,2));
JButton month[] = new JButton[6];
String bname[]
={"1.january","2.february","3.march","4.april","5.may","6.june"} ;
for(int i=0;i<6;i++) {
month[i] = new JButton(bname[i]);
p.add(month[i]);
}
for(int i=0;i<6;i++) {
month[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null, "Hello calender",
"calender", JOptionPane.INFORMATION_MESSAGE);
}
});
}
for (int i=0;i<3;i++) {
p.add(month[i]);
}
for (int i=3;i<6;i++) {
q.add(month[i]);
}
p.add(q);
this.add(p,BorderLayout.CENTER);

this.setVisible(true);
}
}

# Dán hình ảnh thực thi vào bên dưới:

You might also like