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 [Link];
public class TwoNumberComputation {
public static void main(String[] args) {
String strNum1, strNum2;
String strNotification = "You have just entered: ";
strNum1 = [Link]("Enter first number: ");
strNum2 = [Link]("Enter second number: ");
double num1 = [Link](strNum1);
double num2 = [Link](strNum2);
[Link](null, "Sum of " + num1 + " and " + num2 + " is
" + (num1 + num2));
[Link](null, "Difference of " + num1 + " and " + num2
+ " is " + (num1 - num2));
[Link](null, "Product of " + num1 + " and " + num2 + "
is " + (num1 * num2));
[Link](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 [Link];
public class SolveEquation {
public static void main(String[] args) {
String strNotification;
while (true) {
strNotification = [Link]("Enter type of equation (1
for first degree, 2 for system of first degree, 3 for second degree): ");
switch (strNotification) {
case "1":
double a = [Link]([Link]("Enter
a: "));
double b = [Link]([Link]("Enter
b: "));
firstDegreeEquation(a, b);
break;
case "2":
double a11 =
[Link]([Link]("Enter a11: "));
double a12 =
[Link]([Link]("Enter a12: "));
double a21 =
[Link]([Link]("Enter a21: "));
double a22 =
[Link]([Link]("Enter a22: "));
double b1 = [Link]([Link]("Enter
b1: "));
double b2 = [Link]([Link]("Enter
b2: "));
systemOfFirstDegreeEquation(a11, a12, b1, a21, a22, b2);
break;
case "3":
double a3 = [Link]([Link]("Enter
a: "));
double b3 = [Link]([Link]("Enter
b: "));
double c3 = [Link]([Link]("Enter
c: "));
secondDegreeEquation(a3, b3, c3);
break;
default:
[Link](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) ;
}
[Link](null, notification);
[Link](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";
}
[Link](null, notification);
[Link](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 + [Link](Delta)) / (2 * a) +
" and " + (-b - [Link](Delta)) / (2 * a);
}
[Link](null, notification);
[Link](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 [Link];
public class ChoosingOption {
public static void main(String[] args) {
int option = [Link](null,
"Do you want to change to the first class ticket?");
[Link](null, "You've chosen: "
+ (option == JOptionPane.YES_OPTION ? "Yes" : "No"));
[Link](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 [Link];
public class InputFromKeyboard {
public static void main(String[] args) {
Scanner keyboard = new Scanner([Link]);
[Link]("What's your name?");
String strName = [Link]();
[Link]("How old are you?");
int iAge = [Link]();
[Link]("How tall are you (m)?");
double dHeight = [Link]();
[Link]("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 [Link];
public class DrawingTriangle {
public static void main(String[] args) {
Scanner keyboard = new Scanner([Link]);
[Link]("Enter height:");
int n = [Link]();
for (int i = 1; i <=n ; i++) {
for (int j = n - i; j > 0; j--){
[Link](" ");
}
for (int j = 1; j <= 2 * i - 1; j++){
[Link]("*");
}
[Link]();
}
}
}
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 [Link];
public class DisplayDaysOfMonth {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
while(true){
[Link]("Enter a month: ");
String month = [Link]();
[Link]("Enter a year: ");
String year = [Link]();
int monthNum = getMonth(month);
int yearNum = getYear(year);
if (isValidDate(monthNum, yearNum)){
[Link]("The number of days in month " + monthNum + " year
" + yearNum + " is " + getDays(monthNum, yearNum));
break;
} else {
[Link]("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 = [Link]();
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 ([Link]("\\d+")) return [Link](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 [Link];
import [Link];
public class SortArray {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of elements in array");
int n = [Link]();
int sum = 0;
[Link]("Enter the elements of array");
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
sum += arr[i];
}
[Link](arr);
[Link]([Link](arr));
[Link]("The sum of array is " + sum);
[Link]("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 [Link];
public class AddMatrix {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows: ");
int n = [Link]();
[Link]("Enter the number of columns: ");
int m = [Link]();
int[][] a = new int[n][m];
int[][] b = new int[n][m];
int[][] sum = new int[n][m];
[Link]("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] = [Link]();
}
}
[Link]("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] = [Link]();
}
}
[Link]("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];
[Link](sum[i][j] + " ");
}
[Link]();
}
}
}
Result: