Problem Set 1: Basic Skills To complete this set of problems you must have gone through the following lessons: - Class Preparation
- Using BlueJ
- Printing
- Variables
- String
- char
- int
- double
- boolean
- User Input
- Random Numbers
- Arithmetic
Problem 1 | Write a program that has three character (char) variables declared. The first variable should be assigned the letter 'H', the second variable the letter 'I' and the third variable the character '!'. Have the computer print the following output. | Output Example
| The first character is H The second character is I The third character is! Together they spell HI! |
Problem 2 | Write a program that allows the user to enter two numbers. The output produced should be as shown below. | Output Example
| Enter a value for X: 5 Enter a value for Y: 8 X * Y = 40 X + Y = 13 |
Problem 3 | Enter a program asks the user to enter his/her name and favorite animal. | Output Example
| Enter your first name: Jim Enter your second name: Smith Enter your favorite animal: dog
Hi Jim. My last name is also Smith! I think a dog is a great animal. |
Problem 4 | Write a program that allows the user to enter the price of a loaf of bread and the number of loaves that will be purchased. The output produced should be as shown below. (Hint: use double type variables) | Output Example
| Enter a price (in pennies) for the loaf of bread: 89 Enter the number of loaves to buy: 4
The total comes to $3.56 |
Problem 5 | Write a program that allows the user to enter your weight in pounds and your height in inches. The output produced should be as shown below. | Output Example
| Enter your weight in pounds: 150 Enter your height in inches: 75
You weigh 2.00 pounds per inch! |
Problem 6 | A piece of pizza normally contains 375 calories. A person jogging 1 mile uses about 100 calories. Write a program that asks the user to enter the number of pieces of pizza that were eaten. The computer will calculate the number of miles the pizza eater needs to run to burn off the calories. (Hint: miles = number of pieces * 3.75) | Output Example
| Enter the number of pieces of pizza eaten: 4 You will have to run 15 miles to burn that off. |
Problem 7 | Write a program that asks for the measurements of a room and then calculates the volume. (Volume = length * width * height) | Output Example
| Enter the room's height (in feet): 10 Enter the room's width (in feet): 12 Enter the room's length (in feet): 8
The volume of the room is 960 cubic feet. |
Problem 8 | The area of a triangle can be found by multiplying 1/2 base times the height. Write a program that allows the user to enter the base and height of a triangle and then displays the area. | Output Example
| Enter the base (inches): 20 Enter the height (inches): 10
The triangle's area is 100 square inches. |
Problem 9 | Given the assumption that you sleep a healthy 8 hours a night, have the computer print the number of hours of your life that you have spent sleeping. Use 365 for the number of days in a year and 30 for the number of days in a month. Hint: days alive = (this year - year born)*365 + (this month - month born)*30 + (this day - day born) | Output Example
| Enter the day you were born: 1 Enter the month you were born: 1 Enter the year you were born: 1990
Enter today's day: 3 Enter today's month: 1 Enter today's year: 1990 You have slept 2 days for a total of 16 hours. |
Problem 10 | The price of stocks is commonly given to the nearest eighth; e.g. 77 3/8, or 23 1/2. Write a program that determines the value of a stock holding by reading in the number of shares held, the whole portion and the numerator and denominator of the fractional portion of the price per share. This example represents 20 shares each valued at 5 3/8. (Hint: 107.50 = (20*5) + (20*3/8)) | Output Example
| Enter the number of shares: 20 Enter the whole portion of the share's value: 5 Enter the numerator: 3 Enter the denominator: 8
The value of your stock is $107.50 |
Problem 11 | Suppose that electricity costs 3.25 cents per kilowatt hour, and that a surcharge of 10% is added to the bill. A city utility tax of 3% is also levied; however no tax is put on the surcharge, and no surcharge is put on the tax. Write a program that computes an electric bill. | Output Example
| Enter the number of kilowatt hours: 300 Basic Electric bill: $9.75 10% Surcharge: $0.98 3% Utility Tax: $0.29 Total due: $11.02 |
Problem 12 | Write a program that asks for two numbers to be entered. Have the computer divide the first number by the second number. The computer output should be as shown below. (Hint: use integer (int) variables) | Output Example
| Enter a number: 22 Enter another number (smaller than the first number): 5
22 divided by 5 is 4 with a remainder of 2 |
Problem 13 | A flower company wants a program to calculate how many boxes it will take to ship its flower pot orders. You have been hired to write a program that calculates the number of boxes based on the following information. A large box can hold 4 pots and a small box can hold 1 pot. The output should be as shown below. (Hint: use integer (int) variables) | Output Example
| Enter the number of flower pots ordered: 27 You will need 6 large boxes and 3 small boxes. |
Problem 14 | The company in the previous problem has a new set of boxes and needs you to modify the program you wrote. The new box sizes are as follows: a large box holds 8 pots, a medium box holds 4 pots and a small box holds 1. The output should be as shown below. | Output Example
| Enter the number of flower pots ordered: 71 You will need 8 large boxes, 1 medium box and 3 small boxes. |
Problem 15 | Write a program that returns values from 1 to 15, 15 to 20 and 75 to 76 selected randomly by the computer. (remember, the numbers are selected randomly so your output will not likely be the same as the sample below) | Output Example
| The number selected from 1 to 15 is 12 The number selected from 15 to 20 is 16 The number selected from 75 to 76 is 76 |
Problem 16 | Write a program that randomly selects 2 numbers from 1 to 20. Have the computer produce the following output using the two numbers selected. (remember, the numbers are selected randomly so your output will not likely be the same as the sample below) | Output Example
| The numbers selected are: 14 and 19 14 + 19 = 33 14 - 19 = -5 14 * 19 = 266 14 / 19 = 0 with a remainder of 14 |
|