0% found this document useful (0 votes)
12 views20 pages

Report Lab01

The document is a lab report for an Object-Oriented Programming course, authored by Nguyễn Thùy Linh. It includes source code for various mathematical calculations such as addition, subtraction, multiplication, and division of two numbers, as well as solutions for linear equations, systems of linear equations, and quadratic equations. Additionally, it discusses user input handling and matrix addition operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views20 pages

Report Lab01

The document is a lab report for an Object-Oriented Programming course, authored by Nguyễn Thùy Linh. It includes source code for various mathematical calculations such as addition, subtraction, multiplication, and division of two numbers, as well as solutions for linear equations, systems of linear equations, and quadratic equations. Additionally, it discusses user input handling and matrix addition operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

IT3100E LAB

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;

public class CalculateTwoNumbers {


public static void main(String[] args) {
String strNum1, strNum2;
double num1, num2;
strNum1 = JOptionPane.showInputDialog(null,
"Please input the first number:", "Input the first number",
JOptionPane.INFORMATION_MESSAGE);
strNum2 = JOptionPane.showInputDialog(null,
"Please input the second number:", "Input the second number",
JOptionPane.INFORMATION_MESSAGE);
num1 = Double.parseDouble(strNum1);
num2 = Double.parseDouble(strNum2);
double sum = num1 + num2;

Nguyễn Thùy Linh – 20235965


IT3100E LAB

double difference = num1 - num2;


double product = num1 * num2;
String quotient;
if (num2 != 0) {
quotient = String.valueOf(num1 / num2);
} else {
quotient = "Cannot divide by zero";
}
String result = "Calculation results:\n\n" +
num1 + " + " + num2 + " = " + sum + "\n" +
num1 + " - " + num2 + " = " + difference + "\n" +
num1 + " * " + num2 + " = " + product + "\n" +
num1 + " / " + num2 + " = " + quotient;
JOptionPane.showMessageDialog(null, result,
"Result", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}
}

2.2.6
a. Picture
1. Linear equation:

2. Linear system equations:

Nguyễn Thùy Linh – 20235965


IT3100E LAB

3.

3. Second degree equation:

Nguyễn Thùy Linh – 20235965


IT3100E LAB

B. Full source code:


package LAB01;
import javax.swing.JOptionPane;
import java.util.Scanner;
public class Calnum {
private static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {

String[] options = {"Solve linear equation", "Solve system of linear


equation", "Solve Quadratic equation"};
int choice = JOptionPane.showOptionDialog(null,
"Choose type of equation",
"Solve equation",
JOptionPane.DEFAULT_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, options, options[0]);

switch(choice) {
case 0:
solveLinearEqu();
break;
case 1:
solveLinearSys();
break;
case 2:
solveSecondDegree();

Nguyễn Thùy Linh – 20235965


IT3100E LAB

break;
default:
System.exit(0);
}

scanner.close();
}

private static void solveLinearEqu() {


System.out.print("Enter coefficient A (Ax+B=0): ");
double a = scanner.nextDouble();

System.out.print("Enter coefficient B (Ax+B=0): ");


double b = scanner.nextDouble();

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

Nguyễn Thùy Linh – 20235965


IT3100E LAB

}
else {
if(D1==0 && D2==0) {
System.out.println("The system has infinite solutions");
}
else {
System.out.println("The system has no solution");
}
}
}

private static void solveSecondDegree() {


System.out.print("Enter coefficient A (Ax²+Bx+C=0): ");
double a = scanner.nextDouble();
System.out.print("Enter coefficient B (Ax²+Bx+C=0): ");
double b = scanner.nextDouble();
System.out.print("Enter coefficient C (Ax²+Bx+C=0): ");
double c = scanner.nextDouble();
double delta = b*b - 4*a*c;
if(delta<0) {
System.out.println("The equation has no real solution");
}
else if(delta==0) {
double sol = -b/(2*a);
System.out.println("The equation has a double solution: x = " +
sol);
}
else {
double x1 = (-b-Math.sqrt(delta))/(2*a);
double x2 = (-b+Math.sqrt(delta))/(2*a);
System.out.println("The equation has two distinct solutions:");
System.out.println("x1 = " + x1);
System.out.println("x2 = " + x2);
}
}
}

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

Nguyễn Thùy Linh – 20235965


IT3100E LAB

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;

public class ChoosingOption2 {

public static void main(String[] args) {


// TODO Auto-generated method stub
Object[] options = {"I do", "I don't"};

int option = JOptionPane.showOptionDialog(


null,
"Do you want to change the first class ticket?",
"Ticket Change",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,null, options,
options[0]
);

a. Picture:

b. Source code:
package ex6;

Nguyễn Thùy Linh – 20235965


IT3100E LAB

import javax.swing.JOptionPane;

public class ChoosingOption {


public static void main(String[] args) {
int option = JOptionPane.showConfirmDialog(null,"Do you
want to change the first class ticket?");
JOptionPane.showMessageDialog(null, "You've chosen:"+
(option==JOptionPane.YES_OPTION?"Yes":"No")) ;
System.exit(0);
}

6.2
a. Picture

b. Souce code:
package ex6;
import java.util.Scanner;
public class InputFromKeyboard {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
System.out.println("What's your name?");

Nguyễn Thùy Linh – 20235965


IT3100E LAB

String strname = keyboard.nextLine();


System.out.println("How old are you?");
int iAge = keyboard.nextInt();
System.out.println("How tall are you?");
double dHeight = keyboard.nextDouble();
System.out.println("Ms/Mr" + " " + strname + ", " + iAge + "
years old."+" Your height is " + dHeight +" cm.");

6.3
a. Picture:

b. Source code:
package ex6;
import java.util.Scanner;

Nguyễn Thùy Linh – 20235965


IT3100E LAB

public class DrawTriangle {


public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner scanner = new Scanner(System.in);


System.out.print("Enter the height of the triangle: ");
int height = scanner.nextInt();
for (int i = 1; i <= height; i++) {
for (int j = 1; j <= height - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}

6.4
a. Picture:

Nguyễn Thùy Linh – 20235965


IT3100E LAB

Nguyễn Thùy Linh – 20235965


IT3100E LAB

1. Case 1: Full name:

2. Case 2: Abbreviations:

3. Case 3: 3 letters:

4. Case 4: Number:

Nguyễn Thùy Linh – 20235965


IT3100E LAB

5. Wrong input:

b. Source code:
package ex6;
import java.util.Scanner;

public class DaysInMonth {


public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
boolean validInput = false;
while (!validInput) {
System.out.print("Enter month (full name, abbreviation, 3
letters, or number): ");
String monthInput = scanner.nextLine().trim();
System.out.print("Enter year (full 4 digits): ");
String yearInput = scanner.nextLine().trim();
int year = 0;
boolean validYear = true;
for (int i = 0; i < yearInput.length(); i++) {
if (!Character.isDigit(yearInput.charAt(i))) {
validYear = false;
break;
}
}
if (validYear && yearInput.length() > 0) {
year = Integer.parseInt(yearInput);
if (year < 0) {
validYear = false;
}
} else {

Nguyễn Thùy Linh – 20235965


IT3100E LAB

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;

int days = getDaysInMonth(month, year);


String monthName = getMonthName(month);

System.out.println(monthName + " " + year + " has " + days


+ " days.");
}

scanner.close();
}

private static int getMonthNumber(String monthInput) {


if (monthInput.length() == 1 || monthInput.length() == 2) {
for (int i = 0; i < monthInput.length(); i++) {
if (!Character.isDigit(monthInput.charAt(i))) {
return -1;
}
}
int monthNum = Integer.parseInt(monthInput);
if (monthNum >= 1 && monthNum <= 12) {
return monthNum;
}
return -1;
}

String monthLower = monthInput.toLowerCase();

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;

Nguyễn Thùy Linh – 20235965


IT3100E LAB

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

private static String getMonthName(int month) {


switch (month) {
case 1: return "January";
case 2: return "February";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";
default: return "Unknown";
}
}

private static int getDaysInMonth(int month, int year) {


switch (month) {
case 4: case 6: case 9: case 11:
return 30;
case 2:
return isLeapYear(year) ? 29 : 28;
default:
return 31;
}
}

Nguyễn Thùy Linh – 20235965


IT3100E LAB

private static boolean isLeapYear(int year) {


return (year % 4 == 0 && year % 100 != 0) || (year % 400 ==
0);
}
}

6.5:
a. Picture:

Nguyễn Thùy Linh – 20235965


IT3100E LAB

Nguyễn Thùy Linh – 20235965


IT3100E LAB

c. Source code:
package ex6;
import java.util.Scanner;

public class MatrixAddition {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Do you want to enter matrices (1) or use
constants (2)? ");
int choice = scanner.nextInt();
int rows, cols;
int[][] matrix1, matrix2, resultMatrix;
if (choice == 1) {
System.out.print("Enter number of rows: ");
rows = scanner.nextInt();
System.out.print("Enter number of columns: ");
cols = scanner.nextInt();
matrix1 = new int[rows][cols];
matrix2 = new int[rows][cols];
System.out.println("Enter elements for first matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {

Nguyễn Thùy Linh – 20235965


IT3100E LAB

System.out.print("Enter element at position [" + i


+ "][" + j + "]: ");
matrix1[i][j] = scanner.nextInt();
}
}
System.out.println("Enter elements for second matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Enter element at position [" + i
+ "][" + j + "]: ");
matrix2[i][j] = scanner.nextInt();
}
}
} else {
rows = 2;
cols = 3;

matrix1 = new int[][]{{1, 2, 3}, {4, 5, 6}};


matrix2 = new int[][]{{7, 8, 9}, {10, 11, 12}};

System.out.println("Using constant matrices:");


System.out.println("Matrix 1:");
printMatrix(matrix1);

System.out.println("Matrix 2:");
printMatrix(matrix2);
}

resultMatrix = new int[rows][cols];

for (int i = 0; i < rows; i++) {


for (int j = 0; j < cols; j++) {
resultMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

System.out.println("Result after addition:");


printMatrix(resultMatrix);

scanner.close();
}

private static void printMatrix(int[][] matrix) {


for (int[] row : matrix) {
for (int element : row) {
System.out.print(element + "\t");
}
System.out.println();
}
}

Nguyễn Thùy Linh – 20235965


IT3100E LAB

d.

Nguyễn Thùy Linh – 20235965

You might also like