Report Lab01
Report Lab01
REPORT LAB
Object –oriented Programming
LAB 01
Full name: Nguyễn Thùy Linh
Student ID: 20235965
2.2.5
a. Code picture
b. Source code
package LAB01;
import javax.swing.JOptionPane;
System.exit(0);
}
}
2.2.6
a. Picture
1. Linear equation:
3.
switch(choice) {
case 0:
solveLinearEqu();
break;
case 1:
solveLinearSys();
break;
case 2:
solveSecondDegree();
break;
default:
System.exit(0);
}
scanner.close();
}
if(a==0) {
if(b==0) {
System.out.println("The equation has infinite solutions");
}
else {
System.out.println("The equation has no solution");
}
}
else {
double res = -b/a;
System.out.println("Solution is: x = " + res);
}
}
private static void solveLinearSys() {
System.out.print("Enter a11: ");
double a11 = scanner.nextDouble();
System.out.print("Enter a12: ");
double a12 = scanner.nextDouble();
System.out.print("Enter b1: ");
double b1 = scanner.nextDouble();
System.out.print("Enter a21: ");
double a21 = scanner.nextDouble();
System.out.print("Enter a22: ");
double a22 = scanner.nextDouble();
System.out.print("Enter b2: ");
double b2 = scanner.nextDouble();
double D = a11 * a22 - a21 * a12;
double D1 = b1 * a22 - b2 * a12;
double D2 = a11 * b2 - a21 * b1;
if(D!=0) {
double x1 = D1/D;
double x2 = D2/D;
System.out.println("The system has a unique solution:");
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2);
}
else {
if(D1==0 && D2==0) {
System.out.println("The system has infinite solutions");
}
else {
System.out.println("The system has no solution");
}
}
}
6.1:
1. What happens if users choose "Cancel"?
When a user clicks "Cancel", the showConfirmDialog() method returns
JOptionPane.CANCEL_OPTION (which has a value of 2).
However, in the current conditional statement: "You've chosen:" +
(option==JOptionPane.YES_OPTION?"Yes":"No")
Only two cases are handled: “Yes" or “No”. So when the user selects "Cancel", the program
will still display "You've chosen: No".
2. How to customize the options to users:
package ex6;
import javax.swing.JOptionPane;
a. Picture:
b. Source code:
package ex6;
import javax.swing.JOptionPane;
6.2
a. Picture
b. Souce code:
package ex6;
import java.util.Scanner;
public class InputFromKeyboard {
6.3
a. Picture:
b. Source code:
package ex6;
import java.util.Scanner;
6.4
a. Picture:
2. Case 2: Abbreviations:
3. Case 3: 3 letters:
4. Case 4: Number:
5. Wrong input:
b. Source code:
package ex6;
import java.util.Scanner;
validYear = false;
}
if (!validYear) {
System.out.println("Invalid year. Please enter a non-
negative number.");
continue;
}
int month = getMonthNumber(monthInput);
if (month == -1) {
System.out.println("Invalid month. Please try
again.");
continue;
}
validInput = true;
scanner.close();
}
if (monthLower.equals("january") || monthLower.equals("jan.")
|| monthLower.equals("jan")) return 1;
if (monthLower.equals("february") || monthLower.equals("feb.")
|| monthLower.equals("feb")) return 2;
if (monthLower.equals("march") || monthLower.equals("mar.") ||
monthLower.equals("mar")) return 3;
if (monthLower.equals("april") || monthLower.equals("apr.") ||
monthLower.equals("apr")) return 4;
if (monthLower.equals("may") || monthLower.equals("may."))
return 5;
if (monthLower.equals("june") || monthLower.equals("jun.") ||
monthLower.equals("jun")) return 6;
if (monthLower.equals("july") || monthLower.equals("jul.") ||
monthLower.equals("jul")) return 7;
if (monthLower.equals("august") || monthLower.equals("aug.")
|| monthLower.equals("aug")) return 8;
if (monthLower.equals("september") ||
monthLower.equals("sept.") || monthLower.equals("sep.") ||
monthLower.equals("sep")) return 9;
if (monthLower.equals("october") || monthLower.equals("oct.")
|| monthLower.equals("oct")) return 10;
if (monthLower.equals("november") || monthLower.equals("nov.")
|| monthLower.equals("nov")) return 11;
if (monthLower.equals("december") || monthLower.equals("dec.")
|| monthLower.equals("dec")) return 12;
return -1;
}
6.5:
a. Picture:
c. Source code:
package ex6;
import java.util.Scanner;
System.out.println("Matrix 2:");
printMatrix(matrix2);
}
scanner.close();
}
d.