Tuesday, January 26, 2016

Java awt Exercise 3: CONTROL FLOW STATEMENTS

JAVA EXERCISE - CONTROL FLOW STATEMENTS SAMPLE 1: Write a Java program to input an integer from keyboard. Loop this process until the inp... thumbnail 1 summary
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:

-          Create the content for Product as follows:



-          Press Alt + Insert, choose Constructor, and press Enter.

-          In Generate Constructor window, choose 3 fields and press Generate.

-          Press Alt + Insert, choose toString().., and press Enter. Click the button Generate.

-          Class Product will have content as follows:



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



EXERCISES:
  1. Write a program to solve equation Ax2 + Bx + C = 0. A, B, C are entered from the keyboard.
  2. Write a program to find a number abc which sastify the formula: abc = a3 + b3 + c3.
  3. 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.
  4. 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