CSIS 10A Assignment 4 SOLUTIONS Calculator.java Name: Naomi Lyles Approximate completion time: 20 minutes References: None Write a description of class Calculator here. @author (your name) @version (a version number or a date) import java.util.; public class Calculator public static void main(string []args) Create a Scanner object so we can get user input. Scanner keyboard; keyboard = new Scanner(System.in); Welcome the user System.out.println("Welcome to the calculator!"); System.out.println("What is your first number?"); Get a double value from the user double number1; number1 = keyboard.nextdouble(); Get another double value from the user System.out.println("What is your second number?"); double number2; number2 = keyboard.nextdouble(); Ask the user what arithmetic operation we are doing (store it as an int) System.out.println("What are we doing with these numbers?"); System.out.println("1) add"); System.out.println("2) subtract"); System.out.println("3) multiply"); System.out.println("4) divide"); int operation; operation = keyboard.nextint(); Calculate the answer if (operation == 1) double answer = number1 + number2; if (operation == 2) double answer = number1 - number2; if (operation == 3) double answer = number1 number2; double answer = number1 / number2; Print out the answer
Drills.java import java.util.scanner; Write a description of class Drills here. @author (your name) @version (a version number or a date) public class Drills public static void main (String [] args) System.out.println("\f"); flush the buffer (erase the screen) Scanner keyboard = new Scanner(System.in); int score; System.out.print("Enter a test score "); score = keyboard.nextint(); Ex A write a simple if statement that says "pass" if score is >= 65, otherwise it says nothing System.out.println("A) Conditional Execution"); if (score >= 65) System.out.println("pass"); / End Ex A Ex B Use an if- statement to choose one of the following: Show "pass" if score is>=65, otherwise show "fail" (use if-) Test your solution by entering different scores System.out.println("B) Alternative Execution"); if (score >= 65) System.out.println("pass"); System.out.println("fail"); / End Ex B Ex C Study the Lecture 4 slides and then write a nested if- statement that prints "A" if the variable score is >= 90, "B" if the variable score is >= 80, "C" if the variable score is >= 65, "D" if the variable score is >= 50, or "F" if none of the above are true System.out.println("C) Chained Conditionals"); if (score >= 90) System.out.println("A"); if (score >= 80) System.out.println("B"); if (score >= 65) System.out.println("C"); if (score >= 50) System.out.println("D");
System.out.println("F"); / End Ex C Ex D Write a mini program in which the user inputs an age and the output states whether or not the person is a teenager. Use nested conditionals -- first check if age is greater than 12 -- inside the code block for true, -- check if age is less than 20 Print "Hooray! A Teen!" or "sorry :(" depending on age Use the keyboard scanner we've already created to read age System.out.println("D) Nested Conditionals"); System.out.print(" What is your age?"); declare int age and read age from keyboard Scanner int age = keyboard.nextint(); write your nested if block here if (age > 12) if (age < 20) System.out.println("Hooray! A Teen!"); System.out.println("sorry :("); System.out.println("sorry :("); / End Ex D / Ex E write a void method, paycheck, below main that calculates and prints a worker's paycheck, given the number of hours, and pay rate similar to last weeks drill, but THIS TIME use an if statement to award overtime properly. paycheck( 40, 7.75); answer: $310 paycheck( 20, 9.25); answer: $185 paycheck( 50, 12.50); answer: $687.50 (the first 40 hours are paid at $12.50 the next 10 hours are at $12.501.5) paycheck( 45, 15.00); answer: $712.50 / End Ex E write your pay method here public static void paycheck(int hours, double rate) System.out.print("Hours: " + hours + " Rate: " + rate + " Paycheck: $");
double pay; if (hours <= 40) pay = hours rate; int overtimehours = hours - 40; pay = 40 rate + (overtimehours rate 1.5; System.out.println("$" + pay); System.out.format("%.02f%n", pay); Song.java Name: Naomi Lyles Approximate completion time: 15 minutes References: Lecture 4 Write a description of class Song here. @author (your name) @version (a version number or a date) public class Song public static void main(string [] args) System.out.print("\f"); beercount (99); public static void beercount(int beers) if (beers > 0) beerphrase(beers); System.out.print(" on the wall, "); beerphrase(beers); System.out.print(", ya' take one down, ya' pass it around, "); beerphrase(beers - 1); System.out.println(" on the wall."); beercount(beers - 1); System.out.println("No bottles of beer on the wall, no bottles of beer, ya can't take one down, ya' can't pass it around, 'cause there are no more bottles of beer on the wall!"); public static void beerphrase(int beers) System.out.print(beers + " bottles of beer"); BottlesOfBeer.java
Write a description of class BottlesOfBeer here. public class BottlesOfBeer public static String bottleorbottles(int n) return n == 1? "bottle" : "bottles"; public static void printbottles(int n) if (n > 0) System.out.printf("%d %s of beer on the wall, %d %s of beer, ya take one down, ya pass it around, %n",n, bottleorbottles(n), n, bottleorbottles(n)); System.out.printf("%d %s of beer on the wall.%n", n - 1, bottleorbottles(n - 1)); System.out.println("No bottles of beer on the wall, no bottles of beer, ya can t take one down, ya can t pass it"); System.out.println("around, cause there are no more bottles of beer on the wall!"); public static void beers(int n) if (n >= 0) printbottles(n); beers(n - 1); public static void main(string[] args) beers(99);
Challenge.java Name: Emma Stavropoulos Approximate completion time: 10 mins References: The online book Assignment 4 Challenge public class Challenge public static void main (String [] args) System.out.println("\f"); flush the buffer (erase the screen) verse(99); public static void bottles(int Nbottles) System.out.println(Nbottles + " bottles of beer on the wall,"); System.out.println(Nbottles + " bottles of beer, ya' take one down,"); System.out.println("ya' pass it around,"); System.out.println((Nbottles - 1) + " bottles of beer on the wall."); System.out.println("") public static void twobottles(int Nbottles) System.out.println(Nbottles + " bottles of beer on the wall,"); System.out.println(Nbottles + " bottles of beer, ya' take one down,"); System.out.println("ya' pass it around,"); System.out.println((Nbottles - 1) + " bottle of beer on the wall."); System.out.println(""); verse(nbottles-1); public static void onebottle(int Nbottles) System.out.println(Nbottles + " bottle of beer on the wall,"); System.out.println(Nbottles + " bottle of beer, ya' take one down,"); System.out.println("ya' pass it around,"); System.out.println("No bottles of beer on the wall."); System.out.println(""); verse(nbottles-1); public static void nobottles() System.out.println("No bottles of beer on the wall,"); System.out.println("no bottles of beer, ya' can't take one down,"); System.out.println("ya' can't pass it around, 'cause there are no more"); System.out.println("bottles of beer on the wall!"); public static void verse(int Nmain) if (Nmain == 0) nobottles(); if (Nmain == 2) twobottles(nmain); if (Nmain == 1) onebottle(nmain); bottles(nmain); verse(nmain - 1);
Song.java Write a description of class Song here. Rachel Weber September 13, 2014 public class Song public static void main(string [] args) Displays the lyrics for the song "99 Bottle of Beer on the Wall". bottlecountdown (99); public static void bottlecountdown (int bottles) if (bottles >= 1) Prints the first three parts of each of the song's lines. System.out.print (bottles + " bottles of beer on the wall, "); System.out.print (bottles + " bottles of beer,"); System.out.print ("ya take one down, ya pass it around,"); Calculates how many bottles are left. bottles = bottles - 1; Prints the last part stating how many bottles remain. System.out.println (bottles + "bottles of beer on the wall."); Restarts the cycle with one less bottle. bottlecountdown (bottles); Prints what is sung when no bottles of beer are left. System.out.print ("No bottles of beer on the wall,"); System.out.print ("no bottles of beer, "); System.out.print ("ya can t take one down, ya can t pass it around, "); System.out.println (" cause there are no more bottles of beer on the wall!");