1. What is the numeric value of each of the following expressions as evaluated by Java?

    a. 4 + 6 * 2 16 b. 10 / 5 + 8 10 c. 12 / 4 + 16 / 2 11 d. 17 / 2 8 e. 22 / 5 4 f. 39 / 10 3 g. 19 % (2 + 3) 4 h. 3 + 4 * 20 / 3 29 i. 36 % (6 + 2) 4 j. 8 % 2 * 0 0
  2. What is the value of each of the following Boolean expressions?

    a. 15 < 13 false b. 8 <= (2 + 6) true c. 15 == 15 true d. 3 >= 3 true e. 4 * 2 == 2 * 4 true f. 5 < 8 – 3 false g. 7 != 7 false h. 8 != (2 + 5) true i. 10 – 20 == -10 true j. 3 + 2 * 6 == 30 false
  3. Choose the best data type for each of the following so that any reasonable value is accommodated but no memory storage is wasted. Give an example of a typical value that would be held by the variable, and explain why you chose the type you did.

    a. The number of siblings you have: For most people, the value is no more than 10 or 12, so the number would easily fit in a byte. If you count half siblings, you might need a short because an Emperor in Morocco in the early 1700s had more than 800 children. b. Your final grade in this class: The answer depends on the grading system in your school. If the grade is a letter, such as A, then you would use a char. If the letter grade includes + or -, such as A+, then you need a String. If the value is a percentage, such as 88, then a byte would be sufficient. c. The population of Earth: The population of earth is over 7 billion, so you need a long. d. The population of a U.S. county: The largest county in the U.S. is Los Angeles county in California. The population is about 10 million, so you would use an int. e. The number of passengers on a bus: A byte would be sufficient for almost all busses. f. One player’s score in a Scrabble game: The highest score ever achieved was about 800, so you could use a short. g. One team’s score in a Major League Baseball game: The highest score in history is 29 runs,so a byte is more than sufficient. h. The year an historical event occurred: If you use only years since year 0, then a short is sufficient. If you use negative numbers for years B.C., and you include the entire history of the earth, you need a long because the earth is believed to be more than 4 billion years old. If you want to include “A.D.” or “B.C.” with dates, then you need a String. i. The number of legs on an animal: If you interpret animal to mean mammal, then you can use a byte. Some centipedes have 200 legs however, so you would need a short. j. The price of an automobile: If you assume that the price is in whole dollars, then an int is the best choice.
  4. Write a program that declares a named constant to hold the number of quarts in a gallon (4). Also declare a variable to represent the number of quarts needed for a painting job, and assign an appropriate value—for example, 18. Compute and display the number of gallons and quarts needed for the job. Display explanatory text with the values—for example, A job that needs 18 quarts requires 4 gallons plus 2 quarts. Save the program as QuartsToGallons.java.

    // QuartsToGallons.java public class QuartsToGallons { public static void main(String[] args) { final int QUARTS_IN_GALLON = 4; int quartsNeeded = 18; int gallonsNeeded; int extraQuartsNeeded; gallonsNeeded = quartsNeeded / QUARTS_IN_GALLON; extraQuartsNeeded = quartsNeeded % QUARTS_IN_GALLON; System.out.println("A job that needs " + quartsNeeded + " quarts requires " + gallonsNeeded + " gallons plus " + extraQuartsNeeded + " quarts."); } }
  5. Convert the QuartsToGallons program to an interactive application. Instead of assigning a value to the number of quarts, accept the value from the user as input. Save the revised program as QuartsToGallonsInteractive.java.

    // QuartsToGallonsInteractive.java import java.util.Scanner; public class QuartsToGallonsInteractive { public static void main(String[] args) { final int QUARTS_IN_GALLON = 4; int quartsNeeded = 18; int gallonsNeeded; int extraQuartsNeeded; Scanner input = new Scanner(System.in); System.out.print("Enter quarts needed >> "); quartsNeeded = input.nextInt(); gallonsNeeded = quartsNeeded / QUARTS_IN_GALLON; extraQuartsNeeded = quartsNeeded % QUARTS_IN_GALLON; System.out.println("A job that needs " + quartsNeeded + " quarts requires " + gallonsNeeded + " gallons plus " + extraQuartsNeeded + " quarts."); } }
  6. Write a program that declares named constants to represent the number of inches, feet, and yards in a mile. Also declare a variable to represent a number of miles and assign a value to it. Compute and display, with explanatory text, the value in inches, feet, and yards. Save the program as MileConversions.java.

    public class MileConversions { public static void main(String[] args) { final double INCHES_IN_MILE = 63360; final double FEET_IN_MILE = 5280; final double YARDS_IN_MILE = 1760; double miles = 4; double in, ft, yds; in = miles * INCHES_IN_MILE; ft = miles * FEET_IN_MILE; yds = miles * YARDS_IN_MILE; System.out.println(miles + " miles is " + in + " inches, or " + ft + " feet, or " + yds + " yards"); } }
  7. Convert the MileConversions program to an interactive application. Instead of assigning a value to the miles variable, accept it from the user as input. Save the revised program as MileConversionsInteractive.java.

    import java.util.Scanner; public class MileConversionsInteractive { public static void main(String[] args) { final double INCHES_IN_MILE = 63360; final double FEET_IN_MILE = 5280; final double YARDS_IN_MILE = 1760; double miles = 4; double in, ft, yds; Scanner input = new Scanner(System.in); System.out.print("Enter miles >> "); miles = input.nextDouble(); in = miles * INCHES_IN_MILE; ft = miles * FEET_IN_MILE; yds = miles * YARDS_IN_MILE; System.out.println(miles + " miles is " + in + " inches, or " + ft + " feet, or " + yds + " yards"); } }
  8. Write a program that declares a variable named inches, which holds a length in inches, and assign a value. Display the value in feet and inches; for example, 86 inches becomes 7 feet and 2 inches. Be sure to use a named constant where appropriate. Save the program as InchesToFeet.java.

    public class InchesToFeet { public static void main(String[] args) { final int INCHES_IN_FOOT = 12; int inches = 86; int feet; int inchesLeft; feet = inches / INCHES_IN_FOOT; inchesLeft = inches % INCHES_IN_FOOT; System.out.println(inches + " inches is " + feet + " feet and " + inchesLeft + " inches"); } }
  9. Write an interactive version of the InchesToFeet class that accepts the inches value from a user. Save the class as InchesToFeetInteractive.java.

    import java.util.Scanner; public class InchesToFeetInteractive { public static void main(String[] args) { final int INCHES_IN_FOOT = 12; int inches; int feet; int inchesLeft; Scanner input = new Scanner(System.in); System.out.print("Enter inches >> "); inches = input.nextInt(); feet = inches / INCHES_IN_FOOT; inchesLeft = inches % INCHES_IN_FOOT; System.out.println(inches + " inches is " + feet + " feet and " + inchesLeft + " inches"); } }
  10. Write a program that declares variables to hold your three initials. Display the three initials with a period following each one, as in J.M.F. Save the program as Initials.java.

    public class Initials { public static void main(String[] args) { char init1 = 'J'; char init2 = 'M'; char init3 = 'F'; System.out.println(init1 + "." + init2 + "." + init3 + "."); } }
  11. WMeadowdale Dairy Farm sells organic brown eggs to local customers. It charges $3.25 for a dozen eggs, or 45 cents for individual eggs that are not part of a dozen. Write a program that prompts a user for the number of eggs in the order and then display the amount owed with a full explanation. For example, typical output might be, You ordered 27 eggs. That’s 2 dozen at $3.25 per dozen and 3 loose eggs at 45 cents each for a total of $7.85. Save the program as Eggs.java.

    import java.util.Scanner; public class Eggs { public static void main(String[] args) { final int DOZEN = 12; final double PRICE_PER_DOZEN = 3.25; final double PRICE_PER_EGG = 0.45; int eggs; int dozens; int leftOver; double total; Scanner input = new Scanner(System.in); System.out.print("Enter eggs needed >> "); eggs = input.nextInt(); dozens = eggs / DOZEN; leftOver = eggs % DOZEN; total = dozens * PRICE_PER_DOZEN + leftOver * PRICE_PER_EGG; System.out.println("You ordered " + eggs + " eggs. That's " + dozens + " dozen at $" + PRICE_PER_DOZEN + " per dozen and " + leftOver + " loose eggs at " + (int) (PRICE_PER_EGG * 100) + " cents each for a total of $" + total); } }
  12. The Huntington Boys and Girls Club is conducting a fundraiser by selling chili dinners to go. The price is $7 for an adult meal and $4 for a child’s meal. Write a program that accepts the number of each type of meal ordered and display the total money collected for adult meals, children’s meals, and all meals. Save the program as ChiliToGo.java.

    import java.util.Scanner; public class ChiliToGo { public static void main(String[] args) { final double ADULT_PRICE = 7; final double CHILD_PRICE = 4; int adultMeals; int childMeals; double totalAdult, totalChild, grandTotal; Scanner input = new Scanner(System.in); System.out.print("Enter number of adult meals ordered >> "); adultMeals = input.nextInt(); System.out.print("Enter number of child meals ordered >> "); childMeals = input.nextInt(); totalAdult = adultMeals * ADULT_PRICE; totalChild = childMeals * CHILD_PRICE; grandTotal = totalAdult + totalChild; System.out.println(adultMeals + " adult meals were ordered at " + ADULT_PRICE + " each."); System.out.println(" Total is " + totalAdult); System.out.println(childMeals + " child meals were ordered at " + CHILD_PRICE + " each."); System.out.println(" Total is " + totalChild); System.out.println("Grand total for all meals is " + grandTotal); } }
  13. In the ChiliToGo program in Exercise 12, the costs to produce an adult meal and a child’s meal are $4.35 and $3.10, respectively. Modify the ChiliToGo program to display the total profit for each type of meal as well as the grand total profit. Save the program as ChiliToGoProfit.java.

    import java.util.Scanner; public class ChiliToGoProfit { public static void main(String[] args) { final double ADULT_PRICE = 7; final double CHILD_PRICE = 4; final double ADULT_COST = 4.35; final double CHILD_COST = 3.10; int adultMeals; int childMeals; double totalAdult, totalChild, grandTotal; double profitAdult, profitChild, totalProfit; Scanner input = new Scanner(System.in); System.out.print("Enter number of adult meals ordered >> "); adultMeals = input.nextInt(); System.out.print("Enter number of child meals ordered >> "); childMeals = input.nextInt(); totalAdult = adultMeals * ADULT_PRICE; totalChild = childMeals * CHILD_PRICE; grandTotal = totalAdult + totalChild; profitAdult = totalAdult - (adultMeals * ADULT_COST); profitChild = totalChild - (childMeals * CHILD_COST); totalProfit = profitAdult + profitChild; System.out.println(adultMeals + " adult meals were ordered at " + ADULT_PRICE + " each."); System.out.println(" Total is " + totalAdult); System.out.println(childMeals + " child meals were ordered at " + CHILD_PRICE + " each."); System.out.println(" Total is " + totalChild); System.out.println("Grand total for all meals is " + grandTotal); System.out.println("Profits:"); System.out.println(" Adult profit is " + profitAdult); System.out.println(" Child profit is " + profitChild); System.out.println("Total profit is " + totalProfit); } }
  14. Write a program that calculates and displays the conversion of an entered number of dollars into currency denominations—20s, 10s, 5s, and 1s. Save the program as Dollars.java.

    import java.util.Scanner; public class Dollars { public static void main(String[] args) { int dollars; int twenties, tens, fives, ones, amount; final int TWENTIES = 20; final int TENS = 10; final int FIVES = 5; Scanner input = new Scanner(System.in); System.out.print("Enter a number of dollars "); dollars = input.nextInt(); twenties = dollars / TWENTIES; amount = dollars - (twenties * TWENTIES); tens = amount / TENS; amount = amount - (tens * TENS); fives = amount / FIVES; amount = amount - (fives * FIVES); ones = amount; System.out.println("$" + dollars + " converted is " + twenties + " $20s, " + tens + " $10s, " + fives + " $5s, and " + ones + " $1s"); } }
  15. Write a program that accepts a number of minutes and converts it both to hours and days. For example, 6,000 minutes equals 100 hours and equals 4.167 days. Save the program as MinutesConversion.java.

    import java.util.Scanner; public class MinutesConversion { public static void main(String[] args) { int minutes; double hours; double days; double MINS_IN_HR = 60; double HRS_IN_DAY = 24; Scanner input = new Scanner(System.in); System.out.print("Enter minutes >> "); minutes = input.nextInt(); hours = minutes / MINS_IN_HR; days = hours / HRS_IN_DAY; System.out.println(minutes + " minutes is " + hours + " hours or " + days + " days."); } }
  16. Write a program that accepts the names of three political parties and the number of votes each received in the last mayoral election. Display the percentage of the vote each party received. Save the program as ElectionStatistics.java.

    import java.util.Scanner; public class ElectionStatistics { public static void main(String[] args) { int vote1, vote2, vote3; String party1, party2, party3; int total; double pct1, pct2, pct3; Scanner input = new Scanner(System.in); System.out.print("Enter name for first party >> "); party1 = input.nextLine(); System.out.print("Enter votes received >> "); vote1 = input.nextInt(); input.nextLine(); System.out.print("Enter name for second party >> "); party2 = input.nextLine(); System.out.print("Enter votes received >> "); vote2 = input.nextInt(); input.nextLine(); System.out.print("Enter name for third party >> "); party3 = input.nextLine(); System.out.print("Enter votes received >> "); vote3 = input.nextInt(); input.nextLine(); total = vote1 + vote2 + vote3; pct1 = (double) vote1 / total * 100; pct2 = (double) vote2 / total * 100; pct3 = (double) vote3 / total * 100; System.out.println("The " + party1 + " party got " + pct1 + " percent of the vote"); System.out.println("The " + party2 + " party got " + pct2 + " percent of the vote"); System.out.println("The " + party3 + " party got " + pct3 + " percent of the vote"); } }