REPORT LAB
REPORT LAB
Exercise 2.2.5: Write a program to calculate sum, difference, product, and quotient of 2 double
Result:
1. Enter the numbers:
2. Show results:
Exercise 2.2.6: Write a program to solve:
Source code:
import javax.swing.JOptionPane;
public class SolveEquation {
public static void main(String[] args) {
String strNotification;
while (true) {
strNotification = JOptionPane.showInputDialog("Enter type of equation (1
for first degree, 2 for system of first degree, 3 for second degree): ");
switch (strNotification) {
case "1":
double a = Double.parseDouble(JOptionPane.showInputDialog("Enter
a: "));
double b = Double.parseDouble(JOptionPane.showInputDialog("Enter
b: "));
firstDegreeEquation(a, b);
break;
case "2":
double a11 =
Double.parseDouble(JOptionPane.showInputDialog("Enter a11: "));
double a12 =
Double.parseDouble(JOptionPane.showInputDialog("Enter a12: "));
double a21 =
Double.parseDouble(JOptionPane.showInputDialog("Enter a21: "));
double a22 =
Double.parseDouble(JOptionPane.showInputDialog("Enter a22: "));
double b1 = Double.parseDouble(JOptionPane.showInputDialog("Enter
b1: "));
double b2 = Double.parseDouble(JOptionPane.showInputDialog("Enter
b2: "));
systemOfFirstDegreeEquation(a11, a12, b1, a21, a22, b2);
break;
case "3":
double a3 = Double.parseDouble(JOptionPane.showInputDialog("Enter
a: "));
double b3 = Double.parseDouble(JOptionPane.showInputDialog("Enter
b: "));
double c3 = Double.parseDouble(JOptionPane.showInputDialog("Enter
c: "));
secondDegreeEquation(a3, b3, c3);
break;
default:
JOptionPane.showMessageDialog(null, "Invalid input");
break;
}
}
}
static void firstDegreeEquation(double a, double b) {
String notification;
if (a == 0 && b == 0) {
notification = "Infinite solutions";
}
else if (a == 0) {
notification = "No solutions";
}
else {
notification = "One solution is " + (-b / a) ;
}
JOptionPane.showMessageDialog(null, notification);
System.exit(0);
}
static void systemOfFirstDegreeEquation(double a11, double a12, double b1,
double a21, double a22, double b2) {
String notification;
double D = a11 * a22 - a12 * a21;
double D1 = a22 * b1 - a12 * b2;
double D2 = a11 * b2 - a21 * b1;
if (D == 0 && D1 == 0 && D2 == 0) {
notification = "Infinite solutions";
}
else if (D != 0) {
notification = "Two solutions are " + (D1 / D) + " and " + (D2 / D);
}
else {
notification = "No solutions";
}
JOptionPane.showMessageDialog(null, notification);
System.exit(0);
}
static void secondDegreeEquation(double a, double b, double c) {
String notification;
double Delta = b * b - 4 * a * c;
if (Delta < 0) {
notification = "No solutions";
}
else if (Delta == 0) {
notification = "One solution is " + (-b / (2 * a));
}
else {
notification = "Two solutions are " + (-b + Math.sqrt(Delta)) / (2 * a) +
" and " + (-b - Math.sqrt(Delta)) / (2 * a);
}
JOptionPane.showMessageDialog(null, notification);
System.exit(0);
}
}
Result:
2. First-degree equation
3. System of first-degree equation:
4. Second-degree equation
Exercise 6.1: Write, compile and run the ChoosingOption program
Source code:
import javax.swing.JOptionPane;
public class ChoosingOption {
public static void main(String[] args) {
int option = JOptionPane.showConfirmDialog(null,
"Do you want to change to the first class ticket?");
JOptionPane.showMessageDialog(null, "You've chosen: "
+ (option == JOptionPane.YES_OPTION ? "Yes" : "No"));
System.exit(0);
}
}
Result:
2. Show result
Exercise 6.2: Write a program to input/output from keyboard
Source code:
import java.util.Scanner;
public class InputFromKeyboard {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Result:
Exercise 6.3: Write a program to display a triangle with a height of n stars (*), n is entered by users.
Source code:
import java.util.Scanner;
Result:
Exercise 6.4: Write a program to display the number of days of a month, which is entered by users
(both month and year). If it is an invalid month/year, ask the user to enter again.
Source code:
import java.util.Scanner;
Result:
Exercise 6.5: Write a Java program to sort a numeric array, and calculate the sum and average value of array
elements.
Source code:
import java.util.Scanner;
import java.util.Arrays;
public class SortArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements in array");
int n = sc.nextInt();
int sum = 0;
System.out.println("Enter the elements of array");
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
sum += arr[i];
}
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
System.out.println("The sum of array is " + sum);
System.out.println("The average value of array is " + sum / n);
}
}
Result:
Exercise 6.6: Write a Java program to add two matrices of the same size.
Source code:
import java.util.Scanner;
}
}
Result: