0% found this document useful (0 votes)
15 views32 pages

Oop Lab 1 Report

This lab report includes various Java programs demonstrating basic object-oriented programming concepts. Programs include 'HelloWorld', dialog boxes, arithmetic operations, solving equations, and handling user input. The report also contains exercises for sorting arrays and adding matrices.

Uploaded by

Đạt Tiến
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views32 pages

Oop Lab 1 Report

This lab report includes various Java programs demonstrating basic object-oriented programming concepts. Programs include 'HelloWorld', dialog boxes, arithmetic operations, solving equations, and handling user input. The report also contains exercises for sorting arrays and adding matrices.

Uploaded by

Đạt Tiến
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

LAB REPORT

IT3100E-750861-Object-Oriented
Programming lab
Full name Phan Đăng Đạt
Student ID 20235908

I,First Program
2.2.1,Write the ”HelloWorld” Program
Code:
package lab_01;

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Xin chao \n cac ban!");
System.out.println("Hello \t world!");
}
}
2.2.2,Write the “FirstDialog” program
Code:
package lab_01;
import javax.swing.JOptionPane;

public class FirstDialog {


public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Hello world! How are
you?");
System.exit(0);
}
}
2.2.3,Write the “HelloNameDialog”
program
Code:
package lab_01;
import javax.swing.JOptionPane;

public class HelloNameDialog {


public static void main(String[] args) {
String result;
result = JOptionPane.showInputDialog("Please enter your
name:");
JOptionPane.showMessageDialog(null, "Hi " + result + "!");
System.exit(0);
}
}
2.2.4,Write the “ShowTwoNumber”
Program
Code:
package lab_01;
import javax.swing.JOptionPane;

public class ShowTwoNumber {


public static void main(String[] args) {
String strNum1, strNum2;
String strNotification = "You've just entered: ";

strNum1 = JOptionPane.showInputDialog(null,
"Please input the first number: ", "Input the first number",
JOptionPane.INFORMATION_MESSAGE);
strNotification += strNum1 + " and ";

strNum2 = JOptionPane.showInputDialog(null,
"Please input the second number: ", "Input the second
number",
JOptionPane.INFORMATION_MESSAGE);

strNotification += strNum2;

JOptionPane.showMessageDialog(null, strNotification,
"Show two numbers",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
2.2.5, Write a program to calculate sum,
difference, product, and quotient of 2
double numbers which are entered by
users.
Code:
package lab_01;
import java.util.Scanner;

public class Example_2_2_5 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input the double value from user
System.out.print("Input the value of number 1: ");
String strNum1 = scanner.nextLine();
double num1 = Double.parseDouble(strNum1);
System.out.print("Input the value of number 2: ");
String strNum2 = scanner.nextLine();
double num2 = Double.parseDouble(strNum2);
// Calculate
double sum = num1 + num2;
double difference = num1 - num2;
double product = num1 * num2;
// Check if divide 0
double quotient = (num2 != 0) ? (num1 / num2) : Double.NaN;
// Solution
System.out.println("Sum of the two number: " + sum);
System.out.println("Difference of the two number: " +
difference);
System.out.println("Product of the two number: " + product);
if (num2 != 0) {
System.out.println("Quotient of the two number: " +
quotient);
} else {
System.out.println("Quotient of the two number: Can't divide
0.");
}
scanner.close();
}
}
Case 1:Input 10 and 5

Case 2:Input 10 and 0


2,2,6:
Q1:Write a program to solve:The first-degree
equation with one variable
Code:
package lab_01;
import java.util.Scanner;

public class Example_2_2_6_1 {


public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
//Input the value of the two number
System.out.println("Input the value of a:");
double a=scanner.nextDouble();
System.out.println("Input the value of b:");
double b=scanner.nextDouble();
//Check if a=0=>The equation is not valid and otherwise
opposite
if(a==0)
{
System.out.println("The equation is not solution");
}
else
{
double x=-b/a;
System.out.println("Solution of the equation:"+x);
}
scanner.close();
}
}

Case 1:a=10,b=5
Case 2:a=0,b=5
Q2:Write a program to solve:The system of
first-degree equations with two variable
Code:
package lab_01;
import java.util.Scanner;

public class Example_2_2_6_2 {


public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
//Input the value
System.out.println("Input the value of a11:");
double a11=scanner.nextDouble();
System.out.println("Input the value of a12:");
double a12=scanner.nextDouble();
System.out.println("Input the value of b1:");
double b1 =scanner.nextDouble();
System.out.println("Input the value of a21:");
double a21=scanner.nextDouble();
System.out.println("Input the value of a22:");
double a22=scanner.nextDouble();
System.out.println("Input the value of b2:");
double b2=scanner.nextDouble();
//Calculate determinant
double D=a11*a22-a21*a12;
double D1= b1 *a22-b2*a12;
double D2=a11*b2-a21*b1;
//Solution of the equation
if (D==0)
{
if (D1==0 && D2==0)
{
System.out.println("The equation has infinite solutions");
}
else
{
System.out.println("The equation is not solution");
}
}
else
{
double x1=D1/D,x2=D2/D;
if (x2==-0.0)
{
x2=0.0;
}
System.out.println("The solution of the equation is : x1=
"+x1+" and x2="+x2);
}
scanner.close();
}
}

Case 1:x+2y=5 and 2x+3y=7(has a solution)

Case 2:x+2y=5 and x+2y=9(no solution)


Case 3:x+2y=2 and 2x+4y=4(infinite
solution)
Q3:Writte a program to solve the two-degree
equation with one variable
Code:
package lab_01;
import java.util.Scanner;

public class Example_2_2_6_3 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//Input the value
System.out.println("Input the value of a:");
double a = scanner.nextDouble();
System.out.println("Input the value of b:");
double b=scanner.nextDouble();
System.out.println("Input the value of c:");
double c=scanner.nextDouble();
//Check a==0
if (a == 0) {
if (b==0 && c==0)
{
System.out.println("The equation has infinite solution");
}
else if (b==0 && c!=0)
{
System.out.println("The equation has not solution");
}
else
{
double x=-c/b;
System.out.println("The equation has solution x="+x);
}
}
else
{
//calculate delta
double delta=b*b-4*a*c;
//solution
if (delta>0)
{
double x1=(-b+Math.sqrt(delta))/(2*a);
double x2=(-b-Math.sqrt(delta))/(2*a);
System.out.println("The equation has 2 solution :x1=
"+x1+" and "+"x2="+x2);
}
else if(delta==0)
{
double x=-b/(2*a);
System.out.println("The equation has solution : x="+x);
}
else
{
System.out.println("The equation has not solution");
}
}
}
}

Case 1:a=0,b=0,c=0(Infinite solution)


Case 2:a=0,b=0,c=1(not solution)

Case 3:a=0,b=2,c=3(Has 1 root)


Case 4:a=1,b=2,c=-3(has 2 root)

Case 5:a=1,b=2,c=3(no solution)


Case 6:a=1,b=-4,c=4(has 1 root)

II,EXERCISE
Ex6.1:Write “ChoosingOption” Program
Code:
package lab_01;
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);
}
}

Case 1:Choose “Yes”


Case 2:Choose ”No”

Ex6.2:Write a program for input/output


from keyboard
Code:
package lab_01;
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();

//similar to other data types


//nextByte(), nextShort(), nextLong()
//nextFloat(), nextBoolean()

System.out.println("Mrs/Ms. " + strName + ", " + iAge + "


years old. "
+ "Your height is " + dHeight + ".");
}
}

Ex6.3,Write a program to display a


triangle with a height of n star(*),n is
entered by user.
Code:
package lab_01;
import java.util.Scanner;

public class DisplayTriangle {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Enter the height of the triangle from the user
System.out.print("Enter the height of the triangle: ");
int n = scanner.nextInt();
// Draw an isosceles triangle
for (int i = 1; i <= n; i++) {
// Print spaces for alignment
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
// Print stars to form the triangle
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}
Ex6.4,Write a program to display the
number of day of a month,which is
entered bys users(both month and
year).If it is an invalid month/year,ask
the user enter again.
Code:
package lab_01;
import java.util.Scanner;

public class DayofMonth {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String monthInput;
int month = -1, year;
while (true) {
// Prompt user for month input
System.out.print("Enter month (name, abbreviation, or
number): ");
monthInput = scanner.next();
System.out.print("Enter year: ");
year = scanner.nextInt();
// Convert month input to number
monthInput = monthInput.toLowerCase();
switch (monthInput) {
case "january": case "jan.": case "jan": case "1": month =
1; break;
case "february": case "feb.": case "feb": case "2": month
= 2; break;
case "march": case "mar.": case "mar": case "3": month =
3; break;
case "april": case "apr.": case "apr": case "4": month = 4;
break;
case "may": case "5": month = 5; break;
case "june": case "jun.": case "jun": case "6": month = 6;
break;
case "july": case "jul.": case "jul": case "7": month = 7;
break;
case "august": case "aug.": case "aug": case "8": month =
8; break;
case "september": case "sept.": case "sep": case "9":
month = 9; break;
case "october": case "oct.": case "oct": case "10": month
= 10; break;
case "november": case "nov.": case "nov": case "11":
month = 11; break;
case "december": case "dec.": case "dec": case "12":
month = 12; break;
default:
System.out.println("Invalid month input. Please enter
again.");
continue;
}

if (year < 1) {
System.out.println("Invalid year. Please enter again.");
} else {
break;
}
}
int days;
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days = 31;
break;
case 4: case 6: case 9: case 11:
days = 30;
break;
case 2:
// Check for leap year
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 ==
0)) {
days = 29;
} else {
days = 28;
}
break;
default:
days = 0; // This should never be reached due to
validation
}

System.out.println("Number of days in month " + month + " of


year " + year + " is: " + days);
scanner.close();
}
}

Case 1:month:oct,year:-100
Case 2:month:-oct,year:100

Case 3:month:oct,year:100
Case 4:month:Oct,year:100

Case 5:month:Oct.,year:100
Case 6:month:10,year:100
Ex6.5,Write a Java program to sort a
numeric array and calculate the sum and
average value of array elements
Code:
package lab_01;
import java.util.Arrays;
import java.util.Scanner;

public class Array {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt user for array size
System.out.print("Enter the number of eleme5nts in the array:
");
int n = scanner.nextInt();
// Initialize array
int[] arr = new int[n];
// Input array elements
System.out.println("Enter " + n + " numbers:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
// Sort the array
Arrays.sort(arr);
// Calculate sum and average
int sum = 0;
for (int num : arr) {
sum += num;
}
double average =(double) sum / n;
// Display results
System.out.println("Sorted Array:"+Arrays.toString(arr));
System.out.println("Sum of the element array: " + sum);
System.out.println("Average of the element array: " +
average);
scanner.close();
}
}

Ex6.6,Write a Java program to add two


matrices of the same size
Code:
package lab_01;
import java.util.Scanner;

public class Matrices {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input matrix dimensions
System.out.print("Enter the number of rows: ");
int row = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int col = scanner.nextInt();
int[][] matrix1 = new int[row][col];
int[][] matrix2 = new int[row][col];
int[][] sum = new int[row][col];
// Input first matrix
System.out.println("Enter elements of first matrix:");
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
matrix1[i][j] = scanner.nextInt();
}
}
// Input second matrix
System.out.println("Enter elements of second matrix:");
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
matrix2[i][j] = scanner.nextInt();
}
}
// Compute sum of matrices
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
// Print the result
System.out.println("Sum of matrices:");
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
scanner.close();
}
}

You might also like