Professional Documents
Culture Documents
(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);