Template World
Template World
Hà Nội, 9/2024
20225731 Lê Khánh Linh744530 - IT3103 – Kỳ 2024.1
Nội dung
BÁO CÁO THỰC HÀNH LAP 1...................................................................................................3
The Very First Java Programs..........................................................................................3
2.2.1 Write, compile the first Java application:............................................................3
2.2.2 Write, compile the first dialog Java program:.....................................................3
2.2.3 Write, compile the first input dialog Java application:........................................4
2.2.4 Write, compile, and run the following example:.................................................4
20225731 Lê Khánh Linh744530 - IT3103 – Kỳ 2024.1
import javax.swing.JOptionPane;
public class ShowTwoNumbers {
public static void main(String[] args) {
String strnum1, strnum2;
String strNotification = "Le Khanh Linh 20225731 - You've just entered: ";
strnum1 = JOptionPane.showInputDialog(null, "Le Khanh Linh 20225731 - Please
input the first number:",
"Le Khanh Linh 20225731 - Input the first number",
JOptionPane.INFORMATION_MESSAGE);
strNotification += strnum1 + " and ";
strnum2 = JOptionPane.showInputDialog(null, " Le Khanh Linh 20225731 - Please
input the second number:",
"Le Khanh Linh 20225731 - Input the second number",
JOptionPane.INFORMATION_MESSAGE);
strNotification += strnum2;
JOptionPane.showMessageDialog(null, strNotification, "Le Khanh Linh 20225731 -
Show two numbers",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
20225731 Lê Khánh Linh744530 - IT3103 – Kỳ 2024.1
2.2.5 Write a program to calculate sum, difference, product, and
quotient of 2 double numbers which are entered by users.
import javax.swing.JOptionPane;
public class ArithmeticOperations {
public static void main(String[] args) {
String strNum1 = JOptionPane.showInputDialog("Enter the first number:");
String strNum2 = JOptionPane.showInputDialog("Enter the second number:");
double num1 = Double.parseDouble(strNum1);
double num2 = Double.parseDouble(strNum2);
double sum = num1 + num2;
double difference = num1 - num2;
double product = num1 * num2;
StringBuilder result = new StringBuilder();
result.append("Sum: ").append(sum).append("\n");
result.append("Difference: ").append(difference).append("\n");
result.append("Product: ").append(product).append("\n");
if (num2 != 0) {
double quotient = num1 / num2;
result.append("Quotient: ").append(quotient);
} else {
result.append("Division by zero is not allowed.");
}
JOptionPane.showMessageDialog(null, result.toString(), "Arithmetic Results",
JOptionPane.INFORMATION_MESSAGE);
}
}
20225731 Lê Khánh Linh744530 - IT3103 – Kỳ 2024.1
switch (choice) {
case "1":
solveFirstDegreeOneVariable();
break;
case "2":
solveSystemOfFirstDegreeTwoVariables();
break;
case "3":
solveSecondDegreeOneVariable();
break;
default:
JOptionPane.showMessageDialog(null, "Invalid choice.");
}
}
if (a == 0) {
JOptionPane.showMessageDialog(null, "Coefficient a cannot be zero for a
first-degree equation.");
} else {
double x = -b / a;
JOptionPane.showMessageDialog(null, "The solution is: x = " + x);
}
}
if (D == 0) {
if (D1 == 0 && D2 == 0) {
JOptionPane.showMessageDialog(null, "The system has infinitely many
solutions.");
} else {
JOptionPane.showMessageDialog(null, "The system has no solution.");
}
} else {
double x1 = D1 / D;
double x2 = D2 / D;
JOptionPane.showMessageDialog(null, "The solution is: x1 = " + x1 + ", x2 =
" + x2);
}
}
if (a == 0) {
JOptionPane.showMessageDialog(null, "Coefficient a cannot be zero for a
second-degree equation.");
return;
}
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
20225731 Lê Khánh Linh744530 - IT3103 – Kỳ 2024.1
JOptionPane.showMessageDialog(null, "The equation has two distinct real
roots: x1 = " + root1 + ", x2 = " + root2);
} else if (discriminant == 0) {
double root = -b / (2 * a);
JOptionPane.showMessageDialog(null, "The equation has a double root: x = "
+ root);
} else {
JOptionPane.showMessageDialog(null, "The equation has no real roots.");
}
}
}