JAVA
EXERCISE - CONTROL FLOW STATEMENTS
SAMPLE 1: Write a Java program to input an
integer from keyboard. Loop this process until the input integer is a prime.
SOLUTION:
Create Project Baitap3Demo, names Main class as Main, choose Finish.
Create a new class, named BaitapMau:
Click button
Open BaitapMau.java,
create default constructor BaitapMau(),
and add following code:
The content of function PrimeCheck(int) as follows:
Open Main.java,
create an instance of the class BaitapMau:
Press F6 to run project.
SAMPLE 2: Write a class to help a shop for
product description. The shop's product includes information: productId, productName, productPrice.
Create 3 instance of the built class and display to the screen.
SOLUTION:
-
Create a class named Product:
-
Class Product will have content as follows:

- Open Main.java, create 3 instances and run project with F6.


- Open Main.java, create 3 instances and run project with F6.

EXERCISES:
- Write a
program to solve equation Ax2 + Bx + C = 0. A, B, C are entered
from the keyboard.
- Write a
program to find a number abc which sastify the formula: abc = a3
+ b3 + c3.
- Write a
program to find the greatest common divisor (ước số chung lớn nhất -
USCLN) of two number A and B. A, B are entered from the keyboard. Suggest:
Loop the process A - B (if A>B) or B - A (if B > A) until A = B.
- Create a
class Student, to descript a group of students which have information as:
Full name, student id, GPA. Create 3 instance of the class Student and
display the information to the screen.
No comments
Post a Comment