Lab1 Solution MathTrick.java /** * MathTrick Lab 1 * * @version 8/25/11 * Completion time: 10-15 minutes public class MathTrick public static void main(string [] args) int num = 34; //Get a positive integer int num2 = num * num; //Square it num2 = num + num2; //Add the original number num2 = num2 / num; //Divide by the original number num2 = num2 + 17; //Add 17 num2 = num2 - num; //Subtract the original number num2 = num2 / 6; //Divide by 6 System.out.println("Here is the final number, which will be three, unless you broke the universe:"); //Print the result /* Post Lab Assessment int a=4; int b=5; int c=6; a = a * 2 ; b = b + a ; c = b - a ; a = c + b ; // a 8 // b 13 // c 5 // a 18 Lab2 Solution CoinFlip.java //Date : 2/8/2011 //References: None public class CoinFlip public static void main(string [] args) //Flush the buffer System.out.println("\f"); //Get a random number double my1stnumber; my1stnumber = Math.random();
//Decide the outcome of a coin flip int score1; if (my1stnumber <.45) System.out.println("The first flip was heads"); score1 = 10; else if (my1stnumber <.90) System.out.println("The first flip was tails"); score1 = 5; else if (my1stnumber <.95) System.out.println("The first flip landed on the edge!"); System.out.println("You are one lucky dude."); score1 = 20; else System.out.println("The first flip fell off the table"); score1 = 1; System.out.println("Score for the first flip was: " + score1); //Get a second random number double my2ndnumber; my2ndnumber = Math.random(); //Decide the outcome of a second coin flip int score2; if (my2ndnumber <.45) System.out.println("The second flip was heads."); score2 = 10; else if (my2ndnumber <.90) System.out.println("The second flip was tails."); score2 = 5; else if (my2ndnumber <.95) System.out.println("The second flip landed on the edge!"); System.out.println("You are one lucky dude."); score2 = 20; else System.out.println("The second flip fell off the table."); score2 = 1; System.out.println("Score for the second flip was: " + score2); //Print out which flip had a higher score if (score1 > score2) System.out.println("The first flip had the higher score."); else if (score2 > score1) System.out.println("The second flip had the higher score."); else System.out.println("Both flips had the same score.");
int maxscore = Math.max(score1, score2); //Print out the maximum of the two flips if (score1!= score2) System.out.println("The higher score was " + maxscore); else System.out.println("And they are " + score1 + " each."); POSTLAB /* Post Lab Assessment. Type in your answers for these problems relating to the flowchart (shown below) 1) Follow the flowchart to determine the charge if the variable Age contains the value: Age: a) 5 b) 24 c) 53 d) 90 Charge: a) 7 b) 10 c) 10 d) 5 2) The following Java code attempts to reproduce the flow chart, but it contains some errors. (assume all variables are already declared and initialized). What will Charge now be for Age values: Age: a) 5 b) 24 c) 53 d) 90 Charge: a) 10 b) 10 c) 10 d) 5 if (Age < 65) Charge = 10; else if (Age < 16) Charge = 7; else Charge = 5; 3) Copy the Java statements above and paste below this line. Then correct the code so it matches the flowchart below. if (Age < 16) Charge = 7; else if (Age < 65) Charge = 10; else Charge = 5;
Lab3 Solution Calculator.java import java.util.*; public class Calculator public static void main (String [] args) //Flush the buffer System.out.println("\f"); //Create a Scanner object so we can get user input Scanner input = new Scanner(System.in); //Welcome the user System.out.println("Welcome to CSIS10A's calculator."); System.out.println("Please enter your first number."); //Get a double value from the user String value1 = input.nextline(); double number1 = Double.parseDouble(value1); //Get another double value from the user System.out.println("Please enter your second number."); String value2 = input.nextline(); double number2 = Double.parseDouble(value2); //Ask the user what arithmetic operation we are doing (store it as an int) System.out.println("What do you want to do with these numbers?"); System.out.println(" 1)Add"); //add the numbers you entered together System.out.println(" 2)Subtract"); //subtract them System.out.println(" 3)Multiply"); //multiply them System.out.println(" 4)Divide"); //divide them System.out.println("Please enter a number between 1,2,3 and 4 for the corresponding action"); String value3 = input.nextline(); int userchoice = Integer.parseInt(value3); double result; //Calculate and print out the answer if (userchoice == 1) result = number1 + number2; else if (userchoice == 2) result = number1 - number2; else if (userchoice == 3) result = number1 * number2; else if (userchoice == 4) result = number1 / number2; else // What if the users don't enter a number between 1,2,3 and 4 System.out.println("I am really sorry.");
System.out.println("The number you entered was not among all the choices."); System.out.println("Only 1,2,3 or 4 is accepted"); System.out.println("Please exit the program and run it again."); System.out.println("Thank you for using the calculator."); //End the program. POSTLAB /** MARK THE ERRORS! Find all 8 (or more?), if you dare! * * A program that reads an age that is typed in by the * user and insults them import java.util.*; public class AgeInsult public static void main(string args) XXXX need [] after String Scanner in = new Scanner(System.in); System.out.println("How old are you?"); int age = in.nextdouble(); XXXXX type mismatch, use in.nextint() //Exactly one of these 4 main blocks will be executed. if (age <= 3) System.out.println("Too young to know any better.") XXXXXXX need close curly else if (age < 10) Sytem.out.println(Not ready for prime time.); XXXX need 's' after 'y' in Sytem, and " " around text else if (age < 18) System.out.println("Grow up."); else (age >=18) XXXXXXX never put a condition after else. else means all the above conditions are false System.out.println("Get a job!"); //if statement without "else if" or even an "else". if (age > 120) System.out.println("Way too old! You are probably lying."); XXXXXX need a close curly for class AgeInsult