You are on page 1of 4

/* YOUR TASK IS TO COMPLETE GIVEN FUNCTIONS IN THIS FILE

(YOU CAN ADD NEW FUNCTIONS IF YOU SEE THEY ARE NECESSARY)
*/
import java.io.*;
import java.util.*;
public class MyClass {
//=================================================================
void f1() throws Exception {
String fname = "f1.txt";
File g123 = new File(fname);
if (g123.exists()) {
g123.delete();
}
RandomAccessFile f = new RandomAccessFile(fname, "rw"); // Create output
file
String s = Lib.readLineToStr("data.txt", 1); // Load data to a string s
Lib.dispStr(s, f); //Display the string s to the file f1.txt in first li
ne.
//-----------------------------------------------------------------------------------/* Do not edit or delete given statements before and after this area.
Your task is to insert statements here to process the given string s
according to the question in the exam paper. */
String u = "";
int end = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == 'B') {
end = i;
break;
}
}
for (int i = 0; i < s.length(); i++) {
Character c = s.charAt(i);
if(i<end){
c = 'y';
u = u+c;
}
else u=u+c;
}
s=u;
//-----------------------------------------------------------------------------------Lib.dispStr(s, f); //Display the string s to the file f1.txt in second l
ine.
f.close();
}
//=================================================================
void f2() throws Exception {
String fname = "f2.txt";
File g123 = new File(fname);

if (g123.exists()) {
g123.delete();// delete f2.txt if it already exists
}
RandomAccessFile f = new RandomAccessFile(fname, "rw"); // Create output
file f2.txt
String s = Lib.readLineToStr("data.txt", 2); // Load data to a string s
Lib.dispStr(s, f); //Display the string s to the file f1.txt in first li
ne.
String u = "";
//-----------------------------------------------------------------------------------/* Do not edit or delete given statements before and after this area.
Your task is to insert statements here to process the given string u
according to the question in the exam paper. */
String []s1=s.split(" ");
for(int i = 0;i<s1.length;i++){
if(s1[i].charAt(s1[i].length()-1)=='Y'){
u+= s1[i] + " ";
}
}
//-----------------------------------------------------------------------------------Lib.dispStr(u, f); //Display the string u to the file f1.txt in second
line.
f.close();
}
//=================================================================
void f3() throws Exception {
String fname = "f3.txt";
File g123 = new File(fname);
if (g123.exists()) {
g123.delete();// delete f3.txt if it already exists
}
//-----------------------------------------------------------------------------------/* Do not edit or delete given statements before and after this area.
Your task is to insert statements here
according to the question in the exam paper. */
String s1 = Lib.readLineToStr("data.txt", 4); //gan gia tri trong file v
ao String s1
String s2 = Lib.readLineToStr("data.txt", 5); //gan gia tri trong file v
ao String s2
String s = s1.trim() + " " + s2.trim();
//gop s1 vs s2 thanh s
String test = "";
String b = "";
String u = "";
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != ' ') {
test += s.charAt(i);
}
if (s.charAt(i) == ' ') {
list.add(test);
test = "";
}
}
list.add(test);

Check d = new Check();


for (int i = 0; i < list.size(); i++) {
if (d.CheckLetter(list.get(i)) == false) {
b += list.get(i) + " ";
}
}

String[] b1 = b.split(" ");


for (int i = 0; i < 3; i++) {
u += b1[i] + " ";
}
String[] kq = u.split(" ");
int min = Integer.parseInt(kq[0]);
for (int i = 0; i < kq.length; i++) {
int hientai = Integer.parseInt(kq[i]);
if(hientai < min)
min=hientai;
}
RandomAccessFile f123;
f123 = new RandomAccessFile(fname, "rw");
u = u + "\n" + min;
f123.writeBytes(s + "\r\n" + u);
f123.close();
//-----------------------------------------------------------------------------------}
class Check {
public boolean CheckLetter(String S) {
boolean check = true;//khai bao tra lai gia tri dung hoac sai
for (int i = 0; i < S.length(); i++) {
if (Character.isLetter(S.charAt(i))) {
check = true;
} else if (Character.isLetterOrDigit(S.charAt(i))) {
check = false;
} else {
check = false;
break;
}
}
return check;
}
}
//=================================================================
void f4() throws Exception {
String fname = "f4.txt";
File g123 = new File(fname);
if (g123.exists()) {
g123.delete();// delete f4.txt if it already exists
}
RandomAccessFile f = new RandomAccessFile(fname, "rw"); // Create output
file f4.txt

int[] a = Lib.readLineToIntArray("data.txt", 6); // Load data to the arr


ay a
int n = a.length;
int[] b = null;
//-----------------------------------------------------------------------------------/* You can insert statements in this area only.
The array a is already given, and the array b is declared.
Your task is to process the array b according to the question in the
exam paper. */
int[] c = a.clone();
Arrays.sort(c);
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 4; j++) {
if (c[i] < c[j]) {
int abc = c[i];
c[i] = c[j];
c[j] = abc;
}
}
}
for (int i = 0; i < c.length; i++) {
list.add(c[i]);
}
b= new int[4];
for(int i = 0; i<4;i++){
b[i] = list.get(i);
}

//-----------------------------------------------------------------------------------Lib.dispIntArray(a, f); // Display the array a to output file


Lib.dispIntArray(b, f); // Display the array b to output file
f.close();
}
}