CompSci
  • Home
  •  Unit 1
    Output Exercises
     Unit 2
    Variables Math Modules Exercises
     Unit 3
    Conditionals Exercises
     Unit 4
    Iteration / Loops Exercises
     Unit 5
    Strings Exercises
     Unit 6
    Lists Exercises
     Unit 7
    Functions Exercises
  •  Unit 1
    Java Introduction Java Syntax Errors & Coding Conventions Format Method Exercises
     Unit 2
    Variable Introduction Math / Arithmetic Operators Obtaining a Value Formatting Numeric Output Concatenation & Constants Division & Type Casting Exercises
     Unit 3
    Conditionals Exercises
     Unit 4
    while Loops for Loops Exercises
     Unit 5
    Strings Exercises
     Unit 6
    Methods Method Parameters Method Return Types Exercises
  • Muddy City
  • Github Classroom
  •  Assignments
    Add an Assignment Edit Assignment Search Program Code
     Forum
    Ask a Question Search the Forum
     New Account
    New Account


Account Mgmt
Reset Password

Java - Conditionals


Exercises

Car Recall (SWITCH Statements)

Difficulty: Low

Title: Car Recall (SWITCH Statements)

Description:
NOTE: You have to write the code using a SWITCH statement for this program. No IF statements.

An auto company produced some models of cars that may be difficult to drive because the car wheels are not exactly round. Cars with model numbers 119, 179, 189 through 195, 221, and 780 have been found to have this defect. In addition only the model number of 179 also needs a new bumper.

  • Create a CarRecall application that prompts a customer for the model number of their car to find out if it is defective.
  • Displays “Your car is not defective.” when the user typed a model number without a defect.
  • Otherwise, the message “Your car is defective. It must be repaired.” should be displayed.
  • Model number 179 also needs a special prompt to inform: "Your car is defective and needs a new bumper. It must be repaired."

Output:
Enter the car's model number: 191
Your car is defective. It must be repaired.


Enter the car's model number: 2
Your car is not defective.
Enter the car's model number: 179
Your car is defective and needs a new bumper. It must be repaired.


Printing

Difficulty: Low

Title: Printing

Description:
Printing prices are typically based on the number of copies to be printed.

  • 0 - 99 $0.30 per copy
  • 100 - 499 $0.28 per copy
  • 500 - 749 $0.27 per copy
  • 750 - 1000 $0.26 per copy
  • over 1000 $0.25 per copy
Create a Printing application that prompts the user for the number of copies to print and then displays the price per copy and the total price for the job.

NOTE: The output must be formatted using the currency / money format.

Output:
Enter the number of copies to be printed: 1001
Price per copy is: $0.25
Total cost is: $250.25


MyPow

Difficulty: Low

Title: MyPow

Description:
The MyPow application should prompt the user for two numbers and then display the result from the formula and, for comparison, show the same result using the Math pow() method.

  1. Create a MyPow application that uses the formula: e(Y * log(X)) to calculate XY.
  2. Use the same numbers inputted and use the Math.pow() method and display the output.
NOTE: e is short for exponent (exp(double num)) and log is logarithms (log(double num)) (see above readings for more information on how to apply these Math methods.

Output:
Enter a value for X: 7
Enter a value for Y: 5
The result from using the formula is: 16806.9 The result from using the Math pow() method is: 16807


Car Payment

Difficulty: Medium

Title: Car Payment

Description:
Create a CarPayment application that calculates a monthly car payment after prompting the user for the principal owing (P), the interest rate (r) and the number of monthly payments (m). The monthly car payment is calculated using the formula:

P(r /12) / (1-(1+r/12)-m)

NOTE: In the formula above that is a negative sign in front of the m, which makes it -m.
HINT: To solve don't forget about order of operations and the Math methods that you can use.
NOTE: The output should be formatted using the money / currency format.

Output:
Principal: 20000
Interest Rate: .07
Number of monthly payments: 48
The monthly payment is $470.92


Package Check

Difficulty: Medium

Title: Package Check

Description:
Your shipping company is going international and you need to rewrite your company's software to use kilograms and cubic meters.

Your international delivery service does not accept packages heavier than 27 kilograms or larger than 0.1 cubic meters (100,000 cubic centimeters). Create a PackageCheck application that prompts the user for the weight of a package and its dimensions in centimeters (length, width, and height), and then displays an appropriate message if the package does not meet the requirements.

length(cm) × width(cm) × height(cm) = cubic centimeters

To calculate the cubic meters:

  • length in cm / 100 = length in meters
  • width in cm / 100 = width in meters
  • height in cm / 100 = height in meters
  • length in meters x width in meters x height in meters = cubic meters
Messages should include:
  • Ready to ship
  • Too heavy
  • Too large
  • Too heavy and too large

Output:
Enter package weight in kilograms: 32
Enter package length in centimeters: 10
Enter package width in centimeters: 25
Enter package height in centimeters: 38
Too heavy


Eggs

Difficulty: Medium

Title: Eggs

Description:
A wholesale egg company bases their prices on the number of individual eggs purchased:

  • 0 up to but not including 4 dozen: $0.50 per dozen
  • 4 up to but not including 6 dozen: $0.45 per dozen
  • 6 up to but not including 11 dozen: $0.40 per dozen
  • 11 or more dozen: $0.35 per dozen
NOTE: Extra eggs are priced at 1/12 the per dozen price.
NOTE: The output must be formatted using the currency / money format.

Output:
Enter the number of eggs purchased: 18
The bill is equal to: $0.75


Enter the number of eggs purchased: 62
The bill is equal to: $2.32
Enter the number of eggs purchased: 127
The bill is equal to: $4.23
Enter the number of eggs purchased: 152
The bill is equal to: $4.43


Volume

Difficulty: High

Title: Volume

Description:
The volume of objects are calculated differently depending on the shape of the object.

  • Begin your program by entering a number for the shape to calculate (1 - Rectangular Prism, 2 - Sphere, or 3 - cube).
  • Based upon the shape chosen prompt display the choice and prompt the user for the information needed to calculate that shapes volume.
Rectangular Prism
The volume of a rectangular prism is calculated using the formula:
V = lwh

Sphere
The volume of a sphere is calculated using the formula:
πd3 / 6

NOTE: Use the Math,PI and Math.pow methods.
NOTE: The formula being used, uses the diameter.

Cube
The volume of a cube is calculated using the formula:
V = s3

Prompt the user for the length of each side of a cube. The application should then display the volume of the cube.

Output:
1 - Rectangular Prism
2 - Sphere
3 - Cube
Choose from the above choices: 1
Length: 3
Width: 4
Height: 5
The volume is: 60.0


1 - Rectangular Prism
2 - Sphere
3 - Cube
Choose from the above choices: 2
Diameter: 5
The volume is: 65.44984694978736
1 - Rectangular Prism
2 - Sphere
3 - Cube
Choose from the above choices: 3
Side: 5
The volume is: 125.0


Bacteria Growth

Difficulty: High

Title: Bacteria Growth

Description:
The formula y = nekt can be used for estimating growth where:

  • y is the final amount
  • n is the initial amount
  • k is a constant
  • t is the time

HINT: If you look at the formula above, you should notice that 'e' is not a variable. So look above at the Math methods in the Reading section and find out what it stands for in the formula.

For example, this formula could be used for estimating population growth in a region or for estimating cell growth in a lab experiment. Create a BacteriaGrowth application that calculates how many bacteria will be present based on this formula.
  • The application should prompt the user initial bacteria, the constant k, and the time.

Output:
Enter initial bacteria amount: 5
Enter a constant value for k: .8
Enter the growth time period in hours: 8
3009.225189360411 bacteria will be present after 8.0 hours.


Decay

Difficulty: High

Title: Decay

Description:
The formula used in the BacteriaGrowth problem can also be used in decay problems. In decay problems, k is negative.

Create an application that allows the user to select from the following options:

  • Calculate the final amount: ne–kt
  • Calculate the initial amount: y / e–kt
  • Calculate the constant (called the half-life): (log (y/n)) / t
NOTE: 'e' represents Math.exp

The application should prompt the user to select one of the three choices and based on the selected option prompts the user to enter the appropriate known information.

For example, a radioactive mass of 200 grams will reduce to 100 grams in 10 years. Based on this information, the half-life is calculated to be –0.06931.

Here are what the variables in the formulas below stand for:
  • y is the final amount
  • n is the initial amount
  • k is a constant
  • t is the time

Output:
NOTE: I did not enter a negative number for "k", because I hard-coded the value to be negative in my formula.

1. Final Amount
2. Initial Amount
3. Constant (half-life)
Find: 1
Enter initial value (n): 200
Enter the constant (k): .0691
Enter the decay time (t): 10
The final amount answer: 100.00471816729655


1. Final Amount
2. Initial Amount
3. Constant (half-life)
Find: 2
Enter the final amount value (y): 100
Enter the constant (k): .0691
Enter the decay time (t): 10
The initial amount answer: 200.0
1. Final Amount
2. Initial Amount
3. Constant (half-life)
Find: 3
Enter the final amount value (y): 100
Enter initial value (n): 200
Enter the decay time (t): 10
The constant (half-life) answer: -0.06931471805599453



Computer Science