JAVA Problems Selection 2
JAVA Problems Selection 2
JAVA Problems Selection 2
Ali Alsaffar 7
3 Selection Problems
1. Write a program to find the ideal weight for a given height. The height must be given in centimeters between
140 cm and 240 cm. The ideal weight is the height minus 100. Before calculating the height check that the input
is valid?
2. ACE balloon shop sells balloons to customers at 10 fils each. If the customer buys 500 − 1000 balloons, the price
is 8 fils each. If the customer buys over 1000 balloons, the price is 6 fils each. Write a program that reads the
number of balloons to purchase and prints the amount of the bill?
3. A part time employee is paid BD. 2/000 per hour for the first 20 hours in a week. Additional hours are paid
BD. 2/500 per hour. In addition the company deducts 5% of the total weekly income for health insurance. Find
how much (s)he will be paid for any given number of hours?
4. Write a Java program that asks the user to input the weight of a canary bird in grams. The program should
determine and display a message the category of the bird’s weight according to the following table:
Weight Category
Between 50 Grams and 100 Grams (inclusive) Healthy
Under 50 but above 20 OR above 100 but under 130 Critical
All other ranges Endangered
5. A valid mobile number must consists of 8 digits. Write a program that reads a mobile number of type long and
verify that it’s exactly 8-digits. If the mobile number is not exactly 8 digits, then display an invalid message and
quit the program. Otherwise, your program will print the provider name according to the following table.
(a) Base price of the pasta is BD. 2.5 for chicken, BD. 2.5 for Meat, and BD. 2.1 for vegetables.
(b) Each appetizer is an additional BD. 1.2
Sample Input/Output
Enter pasta type: C for chicken, M for Meat, V for Vegetables:
C
Enter number of appetizers:
2
Net price is BD. 4.9
10. Write a program that reads two points on a line and another two points on another line. Your program should
determine whether these two lines are parallel or they intersect. If they intersect, you need further to state
whether they are perpendicular or not.
Two lines are parallel if their slopes are equal, otherwise they intersect. Two intersected lines are perpendicular
if the product of their slopes is -1. Use the formula below to compute the slopes of the two lines.
y2 − y1
Slope of (x1 , y1 ) and (x2 , y2 ) is .
x2 − x1
11. Write a Java program that asks the user to enter two float numbers and a character which represents a code
for an arithmetic operation. The program performs arithmetic operations (addition, subtraction, multiplication,
and division) on those two numbers depending on the value of code as follows:
You must use the switch statement to determine the arithmetic operation. If the code character that is entered
by the user is not one of those specified above, print a message saying “invalid code”.
12. A wholesale department is offering reduced prices on large orders of kitchen utensils. Write a Java program to
calculate and display the net price of an order based onthree user inputs: Price (float), Quantity (int) and
Payment method (char: A for cash in advance and D for cash on delivery.) The net price of the order is
calculated as follows:
Net price = (Price - Discount1 - Discount2) × Quantity
Discount1 = 4% of Price for Quantities of more than or equal to 1000, otherwise 2% of Price.
Discount2 = 5% of Price for cash in advance payment and no discount for cash on delivery.
Sample Input/Output
Enter Price, Quantity and Payment Method (A or D)
10 2500 A
13. The “Burger and Fries” restaurant is running a promotion in which delivery orders receive a discount while
dine-in orders receive no discount. Write a program that displays the discount percentage. The program should
first ask the user for two values: order type (delivery or dine-in), and the number of burgers to be ordered.
The program will then determine the discount as follows:
(a) Dine-in orders receive no discount.
(b) Delivery orders of up to two burgers receive 25% discount.
(c) Delivery orders of larger than two burgers receive a 40% discount.
The program will select from the above which discount percentage to display.
14. A triangle is a geometric shape in which no side can be larger than the sum of the other two sides. Write a Java
program that:
Dr. Ali Alsaffar 9
• Prompts the user to enter from the keyboard 3 values of type float and each value must be larger than zero:
x1, x2, and x3.
• Prints a message indicating ”invalid entered values” if any of the entered values is NOT larger than zero.
• Checks whether the entered values x1, x2, and x3 form a valid rectangle or not, and prints a proper message.