To conserve space, I wrote all the programming challenges in a single file. The purpose of writing was not focused on error handling.
import java.util.Scanner; public class Chapter3 { private static Scanner keyboard = new Scanner(System.in); private static void challenge1() { System.out.println("Enter a whole number within the range 1 - 10, inclusive."); int number = keyboard.nextInt(); switch(number) { case 1: System.out.println("I"); break; case 2: System.out.println("II"); break; case 3: System.out.println("III"); break; case 4: System.out.println("IV"); break; case 5: System.out.println("V"); break; case 6: System.out.println("VI"); break; case 7: System.out.println("VII"); break; case 8: System.out.println("VIII"); break; case 9: System.out.println("IX"); break; case 10: System.out.println("X"); break; default: System.out.println("You didn't enter a whole number silly goose!"); } } private static void challenge2() { System.out.println("Enter a date like so 6/10/60 so that the month times the day is equal to the year."); String[] date = keyboard.nextLine().split("/"); if (Integer.parseInt(date[0]) * Integer.parseInt(date[1]) == Integer.parseInt(date[2])) { System.out.println("MAGIC!"); } else { System.out.println("Not magic."); } } private static void challenge3() { System.out.println("Enter your weight in lbs and height in inches."); String[] weightHeight = keyboard.nextLine().split("\\s"); double bmi = Integer.parseInt(weightHeight[0]) * 703/ Math.pow(Double.parseDouble(weightHeight[1]), 2); if (bmi < 18.5) { System.out.println("You're underweight."); } else if (bmi >= 18.5 && bmi <= 25) { System.out.println("Your weight is optimal for your height and lifestyle."); } else if (bmi > 25){ System.out.println("You're overweight."); } else { System.out.println("Unable to determine your BMI."); } } private static void challenge4() { System.out.println("Enter 3 test score grades."); String[] grades = keyboard.nextLine().split("\\s"); double grade1 = Double.parseDouble(grades[0]); double grade2 = Double.parseDouble(grades[1]); double grade3 = Double.parseDouble(grades[2]); double average = (grade1 + grade2 + grade3)/grades.length; if (average >= 90 && average <= 100) { System.out.println("average letter grade is A."); } else if (average >= 80 && average <= 89) { System.out.println("average letter grade is B."); } else if (average >= 70 && average <= 79) { System.out.println("average letter grade is C."); } else if (average >= 60 && average <= 69) { System.out.println("average letter grade is D."); } else if (average <59){ System.out.println("average letter grade is F."); } else { System.out.println("unable to determine average letter grade."); } } private static void challenge5() { System.out.println("Enter an objects mass in kilograms."); double mass = Double.parseDouble(keyboard.nextLine()); double weight = mass * 9.8; // N if (weight < 10) { System.out.println("item is too light."); } else if (weight > 1000) { System.out.println("item is too heavy."); } else { System.out.println(weight); } } private static void challenge6() { System.out.println("Enter a number of seconds."); double numberOfSeconds = Double.parseDouble(keyboard.nextLine()); if (numberOfSeconds >= 60 && numberOfSeconds < 3600) { System.out.println(numberOfSeconds/60 + " minutes."); } else if (numberOfSeconds >= 3600 && numberOfSeconds < 86400) { System.out.println(numberOfSeconds/3600 + " hours."); } else { System.out.println(numberOfSeconds/86400 + " days."); } } private static void challenge7() { System.out.println("Enter three names."); String[] names = keyboard.nextLine().split("\\s"); String[] ascendingNames = new String[names.length]; for (int i = 0; i < names.length; i++) { for (int j = 0; j < names.length; j++) { if (names[i].charAt(0) < names[j].charAt(0)) { ascendingNames[i] = names[i]; } } } for (String name : ascendingNames) { System.out.print(name + " "); } } private static void challenge8() { double packagePrice = 99; System.out.println("Enter the amount of software packages you wish to purchase."); int numberOfPackages = keyboard.nextInt(); double quantityDiscountPercentage = 0; double discount = 0; double total = 0; if (numberOfPackages >= 10 && numberOfPackages <= 19) { quantityDiscountPercentage = 0.2; discount = packagePrice * quantityDiscountPercentage; total = packagePrice - discount; System.out.println(total); } else if (numberOfPackages >= 20 && numberOfPackages <= 49) { quantityDiscountPercentage = 0.3; discount = packagePrice * quantityDiscountPercentage; total = packagePrice - discount; System.out.println(total); } else if (numberOfPackages >= 50 && numberOfPackages <=99) { quantityDiscountPercentage = 0.4; discount = packagePrice * quantityDiscountPercentage; total = packagePrice - discount; System.out.println(total); } else if (numberOfPackages >= 100) { quantityDiscountPercentage = 0.5; discount = packagePrice * quantityDiscountPercentage; total = packagePrice - discount; System.out.println(total); } else { System.out.println("Unable to determine the quantity discount."); } } private static void challenge9() { System.out.println("Enter the weight of your package."); double weight = keyboard.nextDouble(); if (weight <= 2) { System.out.println("shipping charge: $1.10"); } else if (weight > 2 && weight < 6) { System.out.println("shipping charge: $2.20"); } else if (weight > 6 && weight < 10) { System.out.println("shipping charge: $3.70"); } else if (weight > 10) { System.out.println("shipping charge: $3.80"); } else { System.out.println("unable to determine shipping charge."); } } private static void challenge10() { System.out.println("Enter the number of calories and fat grams in a food item."); String[] caloriesAndFat = keyboard.nextLine().split("\\s"); double caloriesFromFat = Double.parseDouble(caloriesAndFat[1]) * 9; double fatPercentage = caloriesFromFat * Double.parseDouble(caloriesAndFat[0]); boolean lowFat = fatPercentage < 0.3; System.out.println(fatPercentage * 100 + "% fat."); if (lowFat) { System.out.println("LOW FAT"); } } private static void challenge11() { System.out.println("Enter 3 runner names and their time in minutes it took each of them to finish the race."); String[] namesAndTimes = keyboard.nextLine().split("\\s"); String[] names = {namesAndTimes[0], namesAndTimes[2], namesAndTimes[4]}; double[] times = {Double.parseDouble(namesAndTimes[1]), Double.parseDouble(namesAndTimes[3]), Double.parseDouble(namesAndTimes[5])}; for (int i = 0; i < times.length; i++) { for (int j = 0; j < times.length; j++) { String tempName = ""; double tempTime = 0; if (times[i] < times[j]) { tempName = names[i]; tempTime = times[i]; names[i] = names[j]; times[i] = times[j]; names[j] = tempName; times[j] = tempTime; } } } int timesIndex = 0; for(String name : names) { System.out.println(name + " " + times[timesIndex]); timesIndex++; } } private void challenge12() { int airSpeedPerSecond = 1100; // feet per second int waterSpeedPerSecond = 4900; int steelSpeedPerSecond = 16400; System.out.println("Enter the medium (air,water,steel) and the distance the sound wave will travel."); String[] mediumAndDistance = keyboard.nextLine().split("\\s"); String medium = mediumAndDistance[0]; double distance = Double.parseDouble(mediumAndDistance[1]); double time = 0; switch(medium) { case "air": time = distance / airSpeedPerSecond; System.out.println(time); break; case "water": time = distance / waterSpeedPerSecond; System.out.println(time); break; case "steel": time = distance / steelSpeedPerSecond; System.out.println(time); break; default: System.out.println("Unable to determine the time."); break; } } private static void challenge13() { System.out.println("Enter the letter of your package and the number of hours used."); String[] letterAndHours = keyboard.nextLine().split("\\s"); String letter = letterAndHours[0]; double hours = Double.parseDouble(letterAndHours[1]); double pricePerMonth; double pricePerHour; double total; switch(letter) { case "A": pricePerMonth = 9.95; // for 10 hours pricePerHour = 2; // after 10 hours if (hours > 10) { double extraHours = hours - 10; total = pricePerMonth + extraHours * pricePerHour; } else { total = pricePerMonth; } System.out.println(total); challenge14("A", total, hours); break; case "B": pricePerMonth = 13.95; // for 20 hours pricePerHour = 1; // after 20 hours if (hours > 20) { double extraHours = hours - 10; total = pricePerMonth + extraHours * pricePerHour; } else { total = pricePerMonth; } System.out.println(total); challenge14("B", total, hours); break; case "C": pricePerMonth = 19.95; // unlimited access total = pricePerMonth; System.out.println(total); break; default: System.out.println("Unable to determine total."); break; } } private static void challenge14(String packageLetter, double total, double hours) { double couldHaveSavedWithB; double bPackagePrice; double couldHaveSavedWithC; double bPackagePricePerMonth = 13.95; // per month for 20 hours double costPerExtraHour = 1; // per hour double cPackagePrice = 19.95; // per month switch(packageLetter) { case "A": // B package if (hours > 20) { double extraHours = hours - 20; bPackagePrice = costPerExtraHour * extraHours + bPackagePricePerMonth; } else { bPackagePrice = bPackagePricePerMonth; } if (total > bPackagePrice) { couldHaveSavedWithB = total - bPackagePrice; System.out.println("Could have saved " + couldHaveSavedWithB); } else { System.out.println("This package seems to be a good fit."); } // C package if (total > cPackagePrice) { couldHaveSavedWithC = total - cPackagePrice; System.out.println("Could have saved " + couldHaveSavedWithC); } break; case "B": // C package if (total > cPackagePrice) { couldHaveSavedWithC = total - cPackagePrice; System.out.println("Could have saved " + couldHaveSavedWithC); } else { System.out.println("This package seems to be a good fit."); } break; default: System.out.println("Huh?"); break; } } private static void challenge15() { double monthlyFee = 10; double checkFee; double total; System.out.println("Enter the number of checks written for the month."); int numberOfChecks = keyboard.nextInt(); if (numberOfChecks > 0 && numberOfChecks < 20) { checkFee = 0.1; total = monthlyFee + numberOfChecks * checkFee; } else if (numberOfChecks >= 20 && numberOfChecks <= 39) { checkFee = 0.08; total = monthlyFee + numberOfChecks * checkFee; } else if (numberOfChecks >= 40 && numberOfChecks <= 59) { checkFee = 0.06; total = monthlyFee + numberOfChecks * checkFee; } else if (numberOfChecks >= 60) { checkFee = 0.04; total = monthlyFee + numberOfChecks * checkFee; } else { total = monthlyFee; } System.out.println(total); } private static void challenge16() { System.out.println("Enter the number of books you purchased this month."); int numberOfBooks = keyboard.nextInt(); int numberOfPoints = 0; // default if purchased books is 0 if (numberOfBooks == 1) { numberOfPoints = 5; } else if (numberOfBooks == 2) { numberOfPoints = 15; } else if (numberOfBooks == 3) { numberOfPoints = 30; } else if (numberOfBooks >= 4) { numberOfPoints = 60; } System.out.println(numberOfPoints); } public static void main(String[] args) { // insert desired method calls } }
















