0% found this document useful (0 votes)
14 views9 pages

Template World

Uploaded by

Kỳ Nguyễ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)
14 views9 pages

Template World

Uploaded by

Kỳ Nguyễ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/ 9

ĐẠI HỌC BÁCH KHOA HÀ NỘI

TRƯỜNG CÔNG NGHỆ THÔNG TIN VÀ TRUYỀN


THÔNG

BÁO CÁO THỰC HÀNH


IT1130-744360-2024.1

BÀI THỰC HÀNH 1

Họ tên sinh viên: Lê Khánh Linh


Mã số sinh viên: 20225731
Giáo viên hướng dẫn: Lê Thị Hoa

Hà Nội, 9/2024
20225731 Lê Khánh Linh744530 - IT3103 – Kỳ 2024.1

Nội dung
BÁO CÁO THỰC HÀNH LAP 1...................................................................................................3
The Very First Java Programs..........................................................................................3
2.2.1 Write, compile the first Java application:............................................................3
2.2.2 Write, compile the first dialog Java program:.....................................................3
2.2.3 Write, compile the first input dialog Java application:........................................4
2.2.4 Write, compile, and run the following example:.................................................4
20225731 Lê Khánh Linh744530 - IT3103 – Kỳ 2024.1

BÁO CÁO THỰC HÀNH LAP 1

The Very First Java Programs


2.2.1 Write, compile the first Java application:
public class HelloWorld{
public static void main(String args[]){
System.out.println("Xin chao \n cac ban!");
System.out.println("Hello \t world!");
}
}
Kết quả:

2.2.2 Write, compile the first dialog Java program:


import javax.swing.JOptionPane;
public class FirstDialog{
public static void main(String args[]){
JOptionPane.showMessageDialog(null,"Le Khanh Linh – 20225731- Hello world! How
are you?");
System.exit(0);
}
}
20225731 Lê Khánh Linh744530 - IT3103 – Kỳ 2024.1
2.2.3 Write, compile the first input dialog Java application:
import javax.swing.JOptionPane;
public class HelloNameDialog{
public static void main(String args[]){
String result;
result=JOptionPane.showInputDialog("Le Khanh Linh 20225731 - Please enter your
name:");
JOptionPane.showMessageDialog(null,"Toi la Le Khanh Linh 20225731. Hi
"+result+"!");
System.exit(0);
}
}

2.2.4 Write, compile, and run the following example:


20225731 Lê Khánh Linh744530 - IT3103 – Kỳ 2024.1

import javax.swing.JOptionPane;
public class ShowTwoNumbers {
public static void main(String[] args) {
String strnum1, strnum2;
String strNotification = "Le Khanh Linh 20225731 - You've just entered: ";
strnum1 = JOptionPane.showInputDialog(null, "Le Khanh Linh 20225731 - Please
input the first number:",
"Le Khanh Linh 20225731 - Input the first number",
JOptionPane.INFORMATION_MESSAGE);
strNotification += strnum1 + " and ";
strnum2 = JOptionPane.showInputDialog(null, " Le Khanh Linh 20225731 - Please
input the second number:",
"Le Khanh Linh 20225731 - Input the second number",
JOptionPane.INFORMATION_MESSAGE);
strNotification += strnum2;
JOptionPane.showMessageDialog(null, strNotification, "Le Khanh Linh 20225731 -
Show two numbers",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
20225731 Lê Khánh Linh744530 - IT3103 – Kỳ 2024.1
2.2.5 Write a program to calculate sum, difference, product, and
quotient of 2 double numbers which are entered by users.
import javax.swing.JOptionPane;
public class ArithmeticOperations {
public static void main(String[] args) {
String strNum1 = JOptionPane.showInputDialog("Enter the first number:");
String strNum2 = JOptionPane.showInputDialog("Enter the second number:");
double num1 = Double.parseDouble(strNum1);
double num2 = Double.parseDouble(strNum2);
double sum = num1 + num2;
double difference = num1 - num2;
double product = num1 * num2;
StringBuilder result = new StringBuilder();
result.append("Sum: ").append(sum).append("\n");
result.append("Difference: ").append(difference).append("\n");
result.append("Product: ").append(product).append("\n");
if (num2 != 0) {
double quotient = num1 / num2;
result.append("Quotient: ").append(quotient);
} else {
result.append("Division by zero is not allowed.");
}
JOptionPane.showMessageDialog(null, result.toString(), "Arithmetic Results",
JOptionPane.INFORMATION_MESSAGE);
}
}
20225731 Lê Khánh Linh744530 - IT3103 – Kỳ 2024.1

2.2.6 Write a program to solve:


import javax.swing.JOptionPane;

public class EquationSolver {


public static void main(String[] args) {
String choice = JOptionPane.showInputDialog("Choose equation type to solve:\n1.
First-degree equation (1 variable)\n2. System of first-degree equations (2 variables)\
n3. Second-degree equation (1 variable)");

switch (choice) {
case "1":
solveFirstDegreeOneVariable();
break;
case "2":
solveSystemOfFirstDegreeTwoVariables();
break;
case "3":
solveSecondDegreeOneVariable();
break;
default:
JOptionPane.showMessageDialog(null, "Invalid choice.");
}
}

public static void solveFirstDegreeOneVariable() {


double a = Double.parseDouble(JOptionPane.showInputDialog("Enter coefficient
a:"));
20225731 Lê Khánh Linh744530 - IT3103 – Kỳ 2024.1
double b = Double.parseDouble(JOptionPane.showInputDialog("Enter constant
b:"));

if (a == 0) {
JOptionPane.showMessageDialog(null, "Coefficient a cannot be zero for a
first-degree equation.");
} else {
double x = -b / a;
JOptionPane.showMessageDialog(null, "The solution is: x = " + x);
}
}

public static void solveSystemOfFirstDegreeTwoVariables() {


double a11 = Double.parseDouble(JOptionPane.showInputDialog("Enter coefficient
a11:"));
double a12 = Double.parseDouble(JOptionPane.showInputDialog("Enter coefficient
a12:"));
double b1 = Double.parseDouble(JOptionPane.showInputDialog("Enter constant
b1:"));

double a21 = Double.parseDouble(JOptionPane.showInputDialog("Enter coefficient


a21:"));
double a22 = Double.parseDouble(JOptionPane.showInputDialog("Enter coefficient
a22:"));
double b2 = Double.parseDouble(JOptionPane.showInputDialog("Enter constant
b2:"));

double D = a11 * a22 - a21 * a12;


double D1 = b1 * a22 - b2 * a12;
double D2 = a11 * b2 - a21 * b1;

if (D == 0) {
if (D1 == 0 && D2 == 0) {
JOptionPane.showMessageDialog(null, "The system has infinitely many
solutions.");
} else {
JOptionPane.showMessageDialog(null, "The system has no solution.");
}
} else {
double x1 = D1 / D;
double x2 = D2 / D;
JOptionPane.showMessageDialog(null, "The solution is: x1 = " + x1 + ", x2 =
" + x2);
}
}

public static void solveSecondDegreeOneVariable() {


double a = Double.parseDouble(JOptionPane.showInputDialog("Enter coefficient
a:"));
double b = Double.parseDouble(JOptionPane.showInputDialog("Enter coefficient
b:"));
double c = Double.parseDouble(JOptionPane.showInputDialog("Enter constant
c:"));

if (a == 0) {
JOptionPane.showMessageDialog(null, "Coefficient a cannot be zero for a
second-degree equation.");
return;
}

double discriminant = b * b - 4 * a * c;

if (discriminant > 0) {
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
20225731 Lê Khánh Linh744530 - IT3103 – Kỳ 2024.1
JOptionPane.showMessageDialog(null, "The equation has two distinct real
roots: x1 = " + root1 + ", x2 = " + root2);
} else if (discriminant == 0) {
double root = -b / (2 * a);
JOptionPane.showMessageDialog(null, "The equation has a double root: x = "
+ root);
} else {
JOptionPane.showMessageDialog(null, "The equation has no real roots.");
}
}
}

You might also like