This commit is contained in:
louiscklaw
2025-02-01 01:58:47 +08:00
parent b3da7aaef5
commit 04dbefcbaf
1259 changed files with 280657 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
import java.util.Scanner;
public class Ex1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double r, l;
System.out.print("Enter radius: ");
r = input.nextDouble();
System.out.print("Enter length: ");
l = input.nextDouble();
System.out.printf("volume = %.2f\n", (r*r*Math.PI*l) );
}
}

View File

@@ -0,0 +1,16 @@
import java.util.Scanner;
public class Ex3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double a, b, c;
System.out.print("Please enter a: ");
a = input.nextDouble();
System.out.print("Please enter b: ");
b = input.nextDouble();
System.out.print("Please enter c: ");
c = input.nextDouble();
System.out.printf("x1 = %.2f, x2 = %.2f\n", ((-b+Math.sqrt(b*b - 4*a*c))/(2*a)), ((-b-Math.sqrt(b*b-4*a*c))/(2*a)));
}
}

View File

@@ -0,0 +1,33 @@
# Lab 4 Simple I/O
## Topic 2.1-2.3: Variables, Data Types, Operators
### Intended Learning Outcomes
Upon completion of this tutorial/lab, you should be able to:
- Use console screen to input/output data.
- Use dialog box and message box to input/output data.
#### Exercise 1 Programming Exercise
PROBLEM: Calculate the volume of a cylinder (with console I/O)
Write a Java program that computes the volume ($v$) of a cylinder. The program should use Scanner class to read the radius ($r$) and the length ($l$), and compute the volume ($v$) with the formula:$ v = r^2\pi l$.
#### Exercise 2 Programming Exercise
PROBLEM: Calculate the volume of a cylinder (with dialog and message boxes)
Rewrite the program in Exercise 1 using dialog and message boxes in the `JOptionPane` class as the input/output method.
#### Exercise 3 Programming Exercise
PROBLEM: Find the root(s) of a quadratic equation
Write a Java program to calculate the two roots of a quadratic equation (ax2 + bx + c = 0) by using the formulae:
```math
x1 = \frac{-b+\sqrt{b^2-4ac}}{2a}, x2=\frac{-b-\sqrt{b^2-4ac}}{2a}
```
You can use `Math.sqrt(x)` method to calcuate $\sqrt{x}$ For example, the statement
```java
System.out.println(Math.sqrt(10));
```
prints the square root of 10. You are free to use either console I/O or `JOptionPane` classes.