0% found this document useful (0 votes)
5 views

REPORT LAB

The document contains a series of Java programming exercises completed by a student, including calculations for sums, differences, products, and quotients of two numbers, solving various types of equations, and handling user inputs. Each exercise includes source code, expected results, and descriptions of the functionality implemented. The exercises cover basic programming concepts such as user input, conditional statements, loops, and array manipulations.

Uploaded by

dimansego213
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

REPORT LAB

The document contains a series of Java programming exercises completed by a student, including calculations for sums, differences, products, and quotients of two numbers, solving various types of equations, and handling user inputs. Each exercise includes source code, expected results, and descriptions of the functionality implemented. The exercises cover basic programming concepts such as user input, conditional statements, loops, and array manipulations.

Uploaded by

dimansego213
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

REPORT LAB 01

Full Name: Nguyễn Việt Anh


Student ID: 20235894

IDE used : IntelliJ IDEA

Exercise 2.2.5: Write a program to calculate sum, difference, product, and quotient of 2 double

numbers which are entered by the user.


Source code:
import javax.swing.JOptionPane;
public class TwoNumberComputation {
public static void main(String[] args) {
String strNum1, strNum2;
String strNotification = "You have just entered: ";
strNum1 = JOptionPane.showInputDialog("Enter first number: ");
strNum2 = JOptionPane.showInputDialog("Enter second number: ");
double num1 = Double.parseDouble(strNum1);
double num2 = Double.parseDouble(strNum2);
JOptionPane.showMessageDialog(null, "Sum of " + num1 + " and " + num2 + " is
" + (num1 + num2));
JOptionPane.showMessageDialog(null, "Difference of " + num1 + " and " + num2
+ " is " + (num1 - num2));
JOptionPane.showMessageDialog(null, "Product of " + num1 + " and " + num2 + "
is " + (num1 * num2));
JOptionPane.showMessageDialog(null, "Quotient of " + num1 + " and " + num2 +
" is " + (num1 / num2));;
}
}

Result:
1. Enter the numbers:
2. Show results:
Exercise 2.2.6: Write a program to solve:

- The first-degree equation (linear equation) with one variable

- The system of first-degree equations (linear system) with two variables

- The second-degree equation with one variable

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:

1. Enter type of equation

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:

1. Output the dialog to choose ( this case choose yes)

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);

System.out.println("What's your name?");


String strName = keyboard.nextLine();
System.out.println("How old are you?");
int iAge = keyboard.nextInt();
System.out.println("How tall are you (m)?");
double dHeight = keyboard.nextDouble();
System.out.println("Mrs/Ms. " + strName + ", " + iAge + " years old. "
+ "Your height is " + dHeight + " .");
}
}

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;

public class DrawingTriangle {


public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter height:");
int n = keyboard.nextInt();
for (int i = 1; i <=n ; i++) {
for (int j = n - i; j > 0; j--){
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++){
System.out.print("*");
}
System.out.println();
}
}
}

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;

public class DisplayDaysOfMonth {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(true){
System.out.println("Enter a month: ");
String month = input.nextLine();
System.out.println("Enter a year: ");
String year = input.nextLine();
int monthNum = getMonth(month);
int yearNum = getYear(year);
if (isValidDate(monthNum, yearNum)){
System.out.println("The number of days in month " + monthNum + " year
" + yearNum + " is " + getDays(monthNum, yearNum));
break;
} else {
System.out.println("Invalid date, please try again");
}
}
}
public static boolean isLeapYear(int year){
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
public static int getMonth(String month){
month = month.toLowerCase();
switch(month){
case "january": case "jan.": case "jan": case "1": return 1;
case "february": case "feb.": case "feb": case "2": return 2;
case "march": case "mar.": case "mar": case "3": return 3;
case "april": case "apr.": case "apr": case "4": return 4;
case "may": case "5": return 5;
case "june": case "jun.": case "jun": case "6": return 6;
case "july": case "jul.": case "jul": case "7": return 7;
case "august": case "aug.": case "aug": case "8": return 8;
case "september": case "sep.": case "sep": case "9": return 9;
case "october": case "oct.": case "oct": case "10": return 10;
case "november": case "nov.": case "nov": case "11": return 11;
case "december": case "dec.": case "dec": case "12": return 12;
default: return -1;
}
}
public static int getYear(String year){
if (year.matches("\\d+")) return Integer.parseInt(year);
else return -1;
}
public static int getDays(int month, int year){
switch(month){
case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31;
case 4: case 6: case 9: case 11: return 30;
case 2: return isLeapYear(year) ? 29 : 28;
}
return 0;
}
public static boolean isValidDate(int month, int year){
if (year < 0) return false;
if (month == -1) return false;
return true;
}
}

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;

public class AddMatrix {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int n = sc.nextInt();
System.out.println("Enter the number of columns: ");
int m = sc.nextInt();
int[][] a = new int[n][m];
int[][] b = new int[n][m];
int[][] sum = new int[n][m];
System.out.println("Enter the elements of the first matrix 1: ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = sc.nextInt();
}
}
System.out.println("Enter the elements of the second matrix 2: ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
b[i][j] = sc.nextInt();
}
}
System.out.println("The sum of the two matrices is: ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
sum[i][j] = a[i][j] + b[i][j];
System.out.print(sum[i][j] + " ");
}
System.out.println();
}

}
}
Result:

You might also like