Program Exercises
Program Exercises
JAVA
Write a program that computes and display the momentum and kinetic energy of a moving
object with a given mass and velocity which are inputted by the user via the keyboard.
Filename: MP2.JAVA
Write a program that compute and display the sum of all integers from 1 to a given integer limit,
say N.
Filename: MP3.JAVA
Write a program that allows the user to enter the two perpendicular side lengths of a
perpendicular triangle, compute and display the hypotenuse and area of the triangle.
Filename: MP4.JAVA
Write a program that accept from the user through the keyboard the time in seconds, compute
and display the time in hours, minutes and seconds
Filename: MP5.JAVA
Write a program that reads the following information and prints a payroll statement:
Write this program in two versions: (a) Use dialog boxes to obtain input and display output; (b)
Use console input and output. A sample run of the console input and output is shown below.
Filename: MP6.JAVA
Write a program that classifies a given amount of money into smaller monetary units. The
program lets the user enter an amount value representing a total in pesos and cents, and outputs a
report listing the monetary equivalent in 5 pesos, 50 cents, 25 cents, 10 cents, and 1 cent. Write
this program in two versions: (a) Use dialog boxes to obtain input and display output; (b) Use
console input and out.
Filename: MP7.JAVA
Create a Java program for a first grader to practice subtraction. The program randomly generates
two single-digit integers number1 and number2 with number1 > number2 and displays a
question such as "What is 9-2?" to the student, as shown in Figure (a). After the student types
the answer in the input dialog box, the program displays a message dialog box to indicate
whether the answer is correct, as shown in Figure (b).
The program generates a subtraction question and grades the student's answer.
Filename: MP8.JAVA
Modify the MP7.java program for a first grader. This time the program should output how many
correct and wrong answers were made by the student. It will show the grade in terms of
percentage (assuming the number of items is 10, each questions weights 10 points) and the
program recommends the student to move to the next level or review back the teacher’s lecture
on subtraction though an appropriate message dialog boxes.
Filename: MP09.JAVA
Write a program that reads three edges for a triangle and determines whether the input is valid.
The input is valid if the sum of any two edges is greater than the third edge. For example, if your
input for three edges is 1, 2, 1, the output should be:
Can edges 1, 2, and 1 form a triangle? false
Filename: MP10.JAVA
(Finding the number of days in a month) Write a program that prompts the user to enter the
month and year, and displays the number of days in the month. For example, if the user entered
month 2 and year 2000, the program should display that February 2000 has 29 days. If the user
entered month 3 and year 2005, the program should display that March 2005 has 31 days.
Filename: MP11.JAVA
Write a program that finds the smallest among the five integers inputted by the users.
Filename: MP12.JAVA
Write a program that computes the grade of a student using the formula:
Grade = 40% major exam + 30% ave of 2 long exams + 30% ave of 3 short quizzes
Display the average of 2 long exams, average of 3 quizzes and the grade of the student. Display
also a message whether the student passed or failed. Passing is 75%.
Filename: MP13.JAVA
Write a program that asks the user for the hours worked for the week and the hourly rate. The
basic salary is computed as:
Salary = hours worked * hourly rate
Bonuses are given:
Filename: MP14.JAVA
XYZ company gives year-end bonus to its employees based on their number of years of service
and their salary, using the following:
Filename: MP15.JAVA
Write a program that accepts five numbers from the user and displays the highest and lowest
number. Assume that there are no duplicate values.
Filename: MP16.JAVA
Write a program segment that will ask the user if he wants to compute the perimeter of the area
of rectangle. If the perimeter is wanted, ask the measure of the three sides and compute for the
perimeter. If the area is wanted, ask for the measures of the base and height and compute for the
area. Display the computed value.
Filename: MP17.JAVA
Write a program that will display “IT'S COLD!” if the temperature is less than 20, “IT'S HOT” if
the temperature is greater than 30, “COOL CLIMATE!” otherwise.
Filename: MP18.JAVA
Write a program that gives a discount of 100 pesos to a customer if the shirt bought is XL and
the price is greater than 500 pesos; a discount of 50 pesos if the shirt bought is L and the price is
greater than 400.
Filename: MP19.JAVA
Write a program to determine the equivalent grade of each student in class as follows:
a. Read in the student's name, midterm grade, minor B and final exam ratings.
b. Determine the final grade of the student by the formula:
final grade=0.30 of midterm grade + 0.10 of minor B + 0.60 final exam.
c. Determine the equivalent grade for the numerical value obtained by the following grading
marks:
98-100 = 1.00
95-97 =1.25
92-94 =1.5
89-91 =1.75
86-88 =2.0
83-85 =2.25
80-82 =2.50
77-79 =2.75
74-76 =3.00
71-73 =3.25
68-70 =3.5
64-67 =3.75
60-63 = 4.0
below 60=5.0
Filename: MP21.JAVA
You were hired by PAGCOR as part of programming team in-charge of automating its BINGO
game. Your task is to write a program that will accept an integer whose value is 1 to 75.
Thereafter, your program should print the letter that corresponds to that number. That is, the
numbers from 1 to 15 correspond to 'B', 16 to 30 correspond to 'I', 31 to 45 correspond to 'N', 46
to 60 correspond to 'G' and 61 to 75 correspond to 'O'.
Filename: MP22.JAVA
You were hired as a programmer by TNP (Telepono ng Pilipino) a phone company. TNP
employs a metering scheme in computing the bill. The metering scheme is as follows: Calls
made on a weekday between 6:00 AM to 6:00 PM are charged at 2.50 pesos per minute.
Charges made at other times during a weekday are charged a discounted 2.00 pesos per minutes.
Calls made anytime on a weekend are charged a weekend rate of 1.50 per minute. Your job is to
write a program that will ask the user to enter the following information:
Filename: MP23.JAVA
Write a program which quantifies numbers. Read an integer x and test it, producing the
following output:
x greater than or equal to 1000 Display the message “hugely positive”
x from 999 to 100 (including 100) Display the message “very positively”
x between 100 and 0 Display the message “positive”
x exactly 0 Display the message “zero”
x between 0 and -100 Display the message “negative”
x from -100 to -999 (including -100) Display the message “very negatively”
x less than or equal to -1000 Display the message “hugely negative”
Thus -10 would print the message “negative”. -100 “very negative” and 458 “very positive”.
Filename:MP24.JAVA
Write a program which draws an ‘X’ of user specified height. If the user typed 7, the following
series of ‘*’ characters would be drawn (without the columns and the numbers).
Write a program that would compute the following AREA, RADIUS, and VOL into a single
program called CIRC.JAVA which accepts four options. The option ‘A’ or ‘a’ calculates the
area of a circle of a circle(prompting for the radius), the option ‘C’ or ‘c’ calculates the
circumference of a circle(prompting for the radius), the option ‘V’ or ‘v’ calculates the volume
of a cylinder (prompting for the radius and the height) while the option ‘Q’ or ‘q’ quits the
program. The program should loop until the quit option is chosen.
Filename: MP26.JAVA
Write a program that will ask the user to enter a number n and display all the numbers from 1 to
n on a single line.
Filename: MP27.JAVA
Write a program that will ask the user to enter a number n and display the sum of all numbers
from 1 to n.
Filename: MP28.JAVA
Write a program that asks the user to enter a number n and display the first n even numbers:
Example: if n=5, the first 5 even numbers are 2, 4, 6, 8, 10.
Filename: MP29.JAVA
Write a program that asks the user to enter a number n and display the first n odd numbers:
Example: if n=5, the first 5 odd numbers are 1, 3, 5, 7, 9
Filename: MP30.JAVA
Write a program containing a function that takes a 5-digit integer and returns the number with its
digit reversed. For example, if the number is 12345, the function should return 54321.
Filename:SET_A.JAVA
Write a Java program that will be used to handle Automated Teller Machine Transactions.
Such transaction includes; (1) balance inquiry, (2) deposit, (3) withdrawal and (4) exit.
Aside from these, the user should be presented a menu showing these transactions as possible
options.
Additional Requirements:
Sample Dialogs:
The system will continue asking for input/s until the user declines to. Display the computed area.
Ensure that the program is fault tolerant. Use Switch statements and goto and label functions.
SAMPLE OUTPUT
AREA CALCULATOR
FIGURE CODE
Square S
Circle C
Triangle T
Parallelogram P
Trapezoid R
Ellipse E
Sector O
Enter Code: T
Enter side a, b, and h separated by commas: 20,13,8
HOT COLD
COFFEE Php 12.00 Php 14.00
MILK Php 10.50 Php 12.50
TEA Php 11.75 Php 13.75
CHOCOLATE Php 14.25 Php 16.25
The system will continue asking for orders until the user declines to order. Display the complete
order detail and the total amount tendered by the customer. Ensure that the program is fault
tolerant. Use Switch and if statements and goto and label functions.
SAMPLE OUTPUT
VENDING MACHINE
PRICE LIST
HOT(1) COLD(2)
COFFEE (C) Php 12.00 Php 14.00
MILK (M) Php 10.50 Php 12.50
TEA (T) Php 11.75 Php 13.75
CHOCOLATE (O) Php 14.25 Php 16.25
INSTRUCTION
Input the item code followed by a dash then mode of service (Hot or Cold).
The machine only accepts 20, 50, and 100 Bills.
SELECTION
Item Selected: M-2
Cash Amount: 100
ORDER DETAILS
Order: Cold Milk
Total Amount: Php 12.50
Cash Amount: Php 100.00
Change: Php 87.50