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 - Variables, Input, & Math


Exercises

Variable Reassignment & Arithmetic (Math)

Difficulty: Low

Title: Variable Reassignment & Arithmetic (Math)

Description:
Create a Java program to apply variable reassignment and arithmetic operators.

  • Choose the correct data type: int, double
  • Create a variable name (your choice): int myNumber;
  • Use variable reassignment and/or arithmetic operators: myNumber = myNumber + 1;
  • Use print statements to output the value of the variable: System.out.println(myNumber);
What is the value of each of the following expressions:
  • Create and initialize a variable with the value of 5 and print out that value.
  • Change the variable value above to 10 and print out the new value.
  • Create and initialize a variable with the value of 6.4567 and print out that value.
  • Change the variable value above to 9.12345 and print out the new value.
  • Create a variable and solve 5 + 7 - 3 and print out the value.
  • Create a variable and solve 10 * 2 - 3 and print out the value.
  • Create a variable and solve 10 * (2 - 3) and print out the value.
  • Create a variable and solve 8 - 3 * 2 and print out the value.

Output:
5
10
6.4567
9.12345
9
17
-10
2


Object Height

Difficulty: Low

Title: Object Height

Description:
The height of an object at any given time dropped from a starting height of 100 meters is given by the equation:

h = 100 – 4.9 * t * t
NOTE: 't' is the time in seconds

Create an ObjectHeight application that prompts the user for a time less than 4.5 seconds and then displays the height of the object at that time. The application output should look like:

Output:
Enter a time: 2
The height of the object is: 80.4 meters


Enter a time: 4
The height of the object is: 21.6 meters


Height

Difficulty: Low

Title: Height

Description:
Create a Height application that prompts the user for a person's height in inches and coverts the input to feet and inches, for example:

NOTE: Use the Scanner class method of nextInt() to obtain a value from the user. See page 81 in the textbook.

NOTE: There are 12 inches in every foot.

Output:
Enter your height in inches: 68
You are 5 feet and 8 inches tall


Simple Interest

Difficulty: Low

Title: Simple Interest

Description:
There are two kinds of interest. Simple and compound. With simple interest, the amount of the deposit remains the same, and the amount of interest is paid at the end of a time interval. For example, if $1,000 is deposited for 7 years at an interest rate of 6% per year, $60 will be deposited at the end of each year.

  • The value of the amount after the term is calculated using the formula:
Amount = Principal * (1 + years * interest rate)

Create a SimpleInterest application that prompts the user for the principal, number of years, and interest rate. Then calculates the amount of interest.

NOTE: The output should use the NumberFormat class for the Simple Interest amount outputted (i.e. $6,500 in the example below).

Output:
Enter the principal: 5000
Enter the number of years: 5
Enter the interest rate: .06
The value after the term is: $6,500.00


Pizza Cost

Difficulty: Low

Title: Pizza Cost

Description:
The cost of making one pizza at a local shop is as follows:

  • Labor cost is $1.25 per pizza, regardless of size
  • Rent cost is $1.50 per pizza, regardless of size
  • Material cost is $0.10 x size x size
  • The state tax rate is 6%
Create a PizzaCost application that prompts the user for the size of a pizza and then displays the cost of making the pizza before and after taxes.
  • Labor, rent, taxes, and material costs must be assigned to variables.
NOTE: Use NumberFormat.getCurrencyInstance to format the output for the cost.

Output:
Enter the diameter of the pizza: 10
The cost of making the pizza is $12.75 before taxes and $13.51 after taxes.


Enter the diameter of the pizza: 12
The cost of making the pizza is: $17.15 before taxes and $18.18


Project

Difficulty: Medium

Title: Project

Description:
Create a Project application to help analyze the time taken for a Java project.

The application should prompt you for the time spent designing, coding, debugging, and testing, and then displays a table showing the percentage of time taken for each part.

NOTE: Use the getPercentInstance() format to format the percentage for each part of the project.

Output:
Enter the number of minutes spent on each of the following project tasks:
Designing: 120
Coding: 240
Debugging: 30
Testing: 30

Task       % Time
Designing:     29%
Coding:       57%
Debugging:    7%
Testing:       7%


Gallon Converter

Difficulty: Medium

Title: Gallon Converter

Description:
Create a GallonConverter application that prompts the user for the number of gallons and then displays the number of quarts, pints, cups, and tablespoons in that number of gallons.

  • In a gallon, there are four quarts.
  • In a quart, there are two pints
  • In a pint, there are two cups
  • In a cup there are sixteen tablespoons
NOTE: Use double as your variable types.

Output:
Enter the number of gallons: 4
In 4.0 gallons there are:
16.0 quarts
32.0 pints
64.0 quarts
1024.0 tablespoons


Steps Per Day

Difficulty: Medium

Title: Steps Per Day

Description:
Write a program that:

  • Prompts the user for the number of steps they walk for four days in a row.
  • Next have the program calculate the total number of steps, the average number of the steps, and convert the steps to miles as well.

NOTE: Use the Scanner class method of nextInt() to obtain a value from the user.

NOTE: Use NumberFormat.getIntegerInstance to format the output for the steps output.

NOTE: Getting the wrong answer? Check your formula for order of operations.

For this program, let's say 2,000 steps are in a mile.

Output:
Enter number of steps for Day 1: 17000
Enter number of steps for Day 2: 12000
Enter number of steps for Day 3: 10000
Enter number of steps for Day 4: 5000

Total number of steps: 44,000
Total number of miles: 22.0
Average steps per day: 11,000
Average miles per day: 5.5


Days

Difficulty: Medium

Title: Days

Description:
Create a Days application that prompts the user for a number of Days. Next have the computer compute and print out the following:

NOTE: Use the Scanner class method of nextInt() to obtain a value from the user.

NOTE: Use NumberFormat.getIntegerInstance to format the output for hours, minutes, and seconds. S

  • number of weeks
  • number of hours
  • number of minutes
  • number of seconds

Output:
Enter number of Days: 9
Number of weeks and days: 1 week(s) and 2 day(s)
Number of hours: 216
Number of minutes: 12,960
Number of Seconds: 777,600


Change

Difficulty: Medium

Title: Change

Description:
Create a Change application that prompts the user for an amount of change in cents and then displays the minimum number of coins necessary to make the change (i.e. start with determining how many quarters, then dimes, etc.). The change can be made of quarters, dimes, nickels, and pennies.

NOTE: The amount of change is reduced each time you determine the number of quarters, then dimes, and then nickels.

Output:
Enter the change in cents: 212
The minimum number of coins is:
Quarters: 8
Dimes: 1
Nickels: 0
Pennies: 2


Digits

Difficulty: Medium

Title: Digits

Description:
Create a Digits application that will show the hundreds-place, tens-place, and ones-place.

HINT: The way I solved this problem was using a mixture of integer division, subtraction at times, and mod division. I started first with the hundreds spot and division, then ten's spot with some subtraction and division, and then one's spot using modulus.

Output:
Enter a three-digit number: 258
The hundreds place is: 2
The tens place is: 5
The ones place is: 8


Election

Difficulty: High

Title: Election

Description:
Create an Election application that will prompt the user for the number of Votes for two candidates - Awbrey and Martinez. After prompting for the number of votes from each state, calculate the percentage of votes for both candidates and display the results.

Output:
Please enter the results for Awbrey:
Maryland: 645612
Delaware: 304134
Virginia: 734564

Please enter the results for Martinez:
Maryland: 587541
Delaware: 254871
Virginia: 633006

The results of a primary election between two candidates in three stare are:
   Awbrey Martinez
Maryland: 645612 587541
Delaware: 304134 254871
Virginia: 734564 633006

Candidate Votes Percentage
Awbrey: 1684310 53%
Martinez: 1475418 47%

TOTAL VOTES: 3159728


Time Conversion

Difficulty: High

Title: Time Conversion

Description:
Create a TimeConversion application that prompts the user for a time in minutes and then displays the time in hours and minutes.

Be sure to consider times when the number of minutes left over is less than 10 (for example 184 minutes should produce 3:04 in the hour:minute format). To do this use the same formula / strategy you used to figure out the Digits.

NOTE: I do not want to see anything fancy with conditionals or sub-string, etc.

Output:
Enter the time in minutes: 184
The time is: 3:04


Enter the time in minutes: 135
The time is: 2:15



Computer Science