Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java Excersize

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Variables and Data Type

Exercise 1: Write a Java program to display Hello World on


the screen.

Exercise 2:Write a Java program to display the asterisk


pattern as shown below (Without using loop):

*****

*****

*****

*****

Exercise 3: Write a Java program to declare two integer variables,


one float variable, and one string variable and assign 10, 12.5,
and "Java programming" to them respectively. Then display their
values on the screen.

Exercise 4: Write a Java program by using BufferedReader


class to prompt a user to input his/her name and then
the output will be shown as an example below:

Hello Dara!
JAVA ARITHMETIC OPERATORS
Exercise 1: Write Java program to allow the user to input two
integer values and then the program prints the results of
adding, subtracting, multiplying, and dividing among the two
values.
See the example below:

Enter value a:30

Enter value b:10

The result of adding is 40.

The result of subtracting is 20;

The result of multiplying is 300.

The result of dividing is 3.

Exercise 2: Write Java program to generate a random number


between 1 to 6.

To generate a random number, you can use the Random class of java.util

package. You may use the abs() method of Math class to make sure you can

get only a positive number.


COMPOUND OPERATORS

Exercise 1: Write Java program to allow the user to input two float values and

then the program adds the two values together. The result will be assigned to

the first variable.

Enter value a:12.5

The value of a before adding is 12.5.

Enter value b:34.9

The value of a after adding is 47.4.

Exercise 2: Write Java program to allow the user to input the amount of

deposit, yearly interest rate (percentage), and income tax(percentage). Then


the program will calculate the amount of interest that the person earns in the

year. See the example output below:

The amount of deposit: 1000

Yearly interest rate: 7.5%

Income tax rate: 4%

The amount of interest earned in the year:72.0


JAVA IF ELSE

Exercise 1: Write Java program to allow the user to input his/her age. Then

the program will show if the person is eligible to vote. A person who is eligible

to vote must be older than or equal to 18 years old.

Enter your age: 18

You are eligible to vote.

Exercise 2: Write a Java program to determine whether an input number is an

even number.

IF ELSE AND LOGICAL OPERATORS


Exercise 1: Write a Java program that determines a student’s
grade.
The program will read three types of scores (quiz, mid-term, and final scores)
and determine the grade based on the following rules:
-if the average score >=90% =>grade=A
-if the average score >= 70% and <90% => grade=B
-if the average score>=50% and <70% =>grade=C
-if the average score<50% =>grade=F

See the example output below:

Quiz score: 80
Mid-term score: 68

Final score: 90

Your grade is B.

Exercise 2: Write a Java program to calculate the revenue


from a sale based on the unit price and quantity of a product
input by the user.
The discount rate is 10% for the quantity purchased between 100 and 120
units, and 15% for the quantity purchased greater than 120 units. If the
quantity purchased is less than 100 units, the discount rate is 0%. See the
example output as shown below:

Enter unit price: 25

Enter quantity: 110

The revenue from sale: 2475.0$

After discount: 275.0$(10.0%)

SWITCH CASE

Exercise 1: Write a Java program to detect key presses.


If the user pressed number keys( from 0 to 9), the program will tell the number
that is pressed, otherwise, program will show "Not allowed".

Exercise 2: Write a Java program that allows the user to


choose the correct answer of a question.
See the example below:
What is the correct way to declare a variable to store an integer value in Java?
a. int 1x=10;
b. int x=10;
c. float x=10.0f;
d. string x="10";
Enter your choice: c
Invalid choice

a. int 1x=10;
b. int x=10;
c. float x=10.0f;
d. string x="10";
Enter your choice: b
Congratulations!

LOOPS IN JAVA
Exercise 1: Write a Java program by using two for loops to
produce the output shown below:

*******

******
*****

****

***

**

Exercise 2: Write a Java program by using three for loops to


print the following pattern:

1******

12*****

123****

1234***

12345**

123456*

1234567

WHILE LOOP
Exercise 1: Write Java program to prompt the user to choose
the correct answer from a list of answer choices of a
question.
The user can choose to continue answering the question or stop answering it.
See the example below:

What is the command keyword to exit a loop in Java?

a. int

b. continue

c. break

d. exit

Enter your choice: b

Incorrect!

Again? press y to continue:

Enter your choice: c

Congratulations!

Exercise 2:Write Java program to print the table of characters


that are equivalent to the Ascii codes from 1 to 122.
The program will print the 10 characters per line.
DO WHILE LOOP

Exercise 1: By using do while loop, write Java program to


prompt the user to choose the correct answer from a list of
answer choices of a question.
The user can choose to continue answering the question or stop answering it.
See the example below:

What is the command keyword to exit a loop in Java?

a. int

b. continue

c. break

d. exit
Enter your choice: b

Incorrect!

Again? press y to continue:

Enter your choice: c

Congratulations!

Exercise 2: By using do while loop, write Java program to


print the table of characters that are equivalent to the Ascii
codes from 1 to 122.
The program will print the 10 characters per line.

You might also like