lab4
unknown
java
a year ago
6.2 kB
12
Indexable
package q1;
class PhepToan {
private int a;
private int b;
private String info;
// Constructor
public PhepToan(int a, int b) {
this.a = a;
this.b = b;
this.info = "";
}
// Getters and Setters for a, b, and info
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
// add() method
public void add() {
int result = a + b;
this.info = a + " + " + b + " = " + result;
}
// sub() method
public void sub() {
int result = a - b;
this.info = a + " - " + b + " = " + result;
}
// mul() method
public void mul() {
int result = a * b;
this.info = a + " * " + b + " = " + result;
}
// div() method
public void div() {
if (b == 0) {
this.info = "divide by Zero";
} else {
int result = a / b;
this.info = a + " / " + b + " = " + result;
}
}
}
package q1;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Q1 {
//Change the name of input and output file based on practical paper
String inputFile = "input.txt";
String outputFile = "output.txt";
//--VARIABLES - @STUDENT: DECLARE YOUR VARIABLES HERE:
int n;
String result = "";
ArrayList<String> operations = new ArrayList<>();
PhepToan pt;
//--FIXED PART - DO NOT EDIT ANY THINGS HERE--
//--START FIXED PART--------------------------
String fi, fo;
/**
* Set input and output file for automatic rating
* @param args path of input file and path of output file
*/
public void setFile (String[] args){
fi = args.length>=2? args[0]: inputFile;
fo = args.length>=2? args[1]: outputFile;
}
/**
* Reads data from input file
*/
public void read(){
try (Scanner sc = new Scanner(new File(fi))){
//--END FIXED PART----------------------------
//INPUT - @STUDENT: ADD YOUR CODE FOR INPUT HERE:
// Đọc số lượng phép toán từ dòng đầu tiên của file
n = Integer.parseInt(sc.nextLine());
// Đọc dòng tiếp theo chứa hai số a và b, sau đó chia nhỏ thành mảng a,b
String[] ab = sc.nextLine().split(" ");
// Chuyển đổi giá trị a và b từ chuỗi sang kiểu số nguyên
int a = Integer.parseInt(ab[0]);
int b = Integer.parseInt(ab[1]);
// Tạo một đối tượng PhepToan mới với hai số a và b
pt = new PhepToan(a, b);
// Lặp qua số lượng phép toán n
for (int i = 0; i < n; i++) {
// Kiểm tra xem còn dòng nào trong file không
if (sc.hasNextLine()) {
// Nếu có, đọc dòng tiếp theo và thêm phép toán vào danh sách operations
operations.add(sc.nextLine());
} else {
// Nếu không đủ phép toán trong file, in thông báo lỗi và thoát
System.out.println("Not enough operations in the file.");
return; // Kết thúc phương thức nếu không đủ phép toán
}
}
//--FIXED PART - DO NOT EDIT ANY THINGS HERE--
//--START FIXED PART--------------------------
sc.close();
}catch(FileNotFoundException ex){
System.out.println("Input Exception # " + ex);
}
}
//--END FIXED PART----------------------------
//ALGORITHM - @STUDENT: ADD YOUROWN METHODS HERE (IF NEED):
//--FIXED PART - DO NOT EDIT ANY THINGS HERE--
//--START FIXED PART--------------------------
/**
* Main algorithm
*/
public void solve(){
//--END FIXED PART----------------------------
//ALGORITHM - @STUDENT: ADD YOUR CODE FOR OUTPUT HERE:
// Kiểm tra loại phép toán và thực hiện phép toán tương ứng
for (String operation : operations) {
switch (operation) {
case "add":
pt.add();
break;
case "sub":
pt.sub();
break;
case "mul":
pt.mul();
break;
case "div":
pt.div();
break;
default:
break;
}
result += pt.getInfo() + "\n";
}
//--FIXED PART - DO NOT EDIT ANY THINGS HERE--
//--START FIXED PART--------------------------
}
/**
* Write result into output file
*/
public void printResult(){
try{
FileWriter fw = new FileWriter(fo);
//--END FIXED PART----------------------------
//OUTPUT - @STUDENT: ADD YOUR CODE FOR OUTPUT HERE:
fw.write(result);
//--FIXED PART - DO NOT EDIT ANY THINGS HERE--
//--START FIXED PART--------------------------
fw.flush();
fw.close();
}catch (IOException ex){
System.out.println("Output Exception # " + ex);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Q1 q = new Q1();
q.setFile(args);
q.read();
q.solve();
q.printResult();
}
//--END FIXED PART----------------------------
}
Editor is loading...
Leave a Comment