0% found this document useful (0 votes)
4 views5 pages

Java

The document contains multiple Java programming exercises focusing on different concepts such as object-oriented programming, data input/output, multithreading, and stream operations. Each exercise includes a main class with methods to perform specific tasks like calculating GPA, counting pass/fail results, finding min/max values from binary files, summing squares of an array using threads, and checking conditions on a list of scores. The code snippets demonstrate practical implementations of these concepts in Java.

Uploaded by

nhuquynh.ngocqp
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Java

The document contains multiple Java programming exercises focusing on different concepts such as object-oriented programming, data input/output, multithreading, and stream operations. Each exercise includes a main class with methods to perform specific tasks like calculating GPA, counting pass/fail results, finding min/max values from binary files, summing squares of an array using threads, and checking conditions on a list of scores. The code snippets demonstrate practical implementations of these concepts in Java.

Uploaded by

nhuquynh.ngocqp
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Đề 1:

Câu 1:
import [Link].*;

class Subject {
private String code;
private int credit;
private double score;

Subject(String code, int credit, double score) {


[Link] = code; [Link] = credit; [Link] = score;
}

String getCode() { return code; }


int getCredit() { return credit; }
double getScore() { return score; }

double gradePoint() {
if (score >= 8.5) {
return 4.0;
}
else if (score >= 7.0) {
return 3.0;
}
else if (score >= 5.5) {
return 2.0;
}
else if (score >= 4.0) {
return 1.0;
}
else {
return 0.0;
}
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
// TODO
ArrayList<Subject> list = new ArrayList<>();

for(int i=0; i< n; i++) {


String code = [Link]();
int credit = [Link]();
double score = [Link]();

[Link](new Subject(code, credit, score));


}

double totalPoint = 0;
int totalCredit = 0;

for (Subject s: list) {


double temp = [Link]();
[Link]("%s %.1f\n", [Link](), temp);
totalPoint += [Link]() * temp;
totalCredit += [Link]();
}
double gpa = totalPoint/totalCredit;
[Link]("GPA %.2f", gpa);
}
}
Câu 2:
import [Link].*;

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
HashMap<String, Integer> cnt = new HashMap<>();
[Link]("PASS", 0);
[Link]("FAIL", 0);
// đếm

for(int i = 0; i< n; i++) {


String ten = [Link]();
double diem = [Link]();

if (diem >= 5.0) {


[Link]("PASS", [Link]("PASS") + 1);
}
else {
[Link]("FAIL", [Link]("FAIL") + 1);
}
}

[Link]("PASS " + [Link]("PASS"));


[Link]("FAIL " + [Link]("FAIL"));
}
}
Câu 3:
import [Link].*;
import [Link].*;

public class Main {


public static void main(String[] args) throws Exception {
Scanner sc = new Scanner([Link]);
int n = [Link]();

// Ghi nhị phân


DataOutputStream dos = new DataOutputStream(new FileOutputStream("[Link]"));

for(int i = 0; i<n; i++) {


int x = [Link]();
[Link](x);
}
[Link]();
// Đọc lại, tìm min/max
DataInputStream dis = new DataInputStream(new FileInputStream("[Link]"));

int min = Integer.MAX_VALUE;


int max = Integer.MIN_VALUE;

try {
while (true) {
int x = [Link]();
if (x < min) {
min = x;
}
if (x > max) {
max = x;
}
}
} catch (EOFException e) { }
[Link]();
[Link]("min: " + min);
[Link]("max: " + max);
}
}
Câu 4:
import [Link].*;

class SquareThread extends Thread {

int[] arr;
int start;
int end;
long sum = 0;

SquareThread(int[] arr, int start, int end) {


[Link] = arr;
[Link] = start;
[Link] = end;
}

@Override
public void run() {

for (int i = start; i < end; i++) {


sum += (long) arr[i] * arr[i];
}
}
}

public class Main {


public static void main(String[] args) throws Exception {

Scanner sc = new Scanner([Link]);

int N = [Link]();

int[] arr = new int[N];

for (int i = 0; i < N; i++) {


arr[i] = [Link]();
}

int K = [Link]();
SquareThread[] threads = new SquareThread[K];

int chunk = N / K;

for (int i = 0; i < K; i++) {

int start = i * chunk;

int end;

if (i == K - 1) {
end = N;
} else {
end = start + chunk;
}

threads[i] = new SquareThread(arr, start, end);

threads[i].start();
}

long total = 0;

for (int i = 0; i < K; i++) {

threads[i].join();

total += threads[i].sum;
}

[Link](total);
}
}
Câu 5:
import [Link].*;
import [Link].*;

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

int n = [Link]();

List<Double> list = new ArrayList<>();

for (int i = 0; i < n; i++) {


[Link]([Link]());
}

// anyMatch / allMatch / noneMatch


boolean any = [Link]().anyMatch(s -> s >= 9);

boolean all = [Link]().allMatch(s -> s >= 5);

boolean none = [Link]().noneMatch(s -> s < 0 || s > 10);


[Link]("ANY " + any);

[Link]("ALL " + all);

[Link]("NONE " + none);


}
}

You might also like