CSIS 10A Assignment 4 SOLUTIONS Read: Chapter 4 Choose and complete any 10 points from the problems below, which are all included in the download file on the website. Use BlueJ to complete the assignment, then export JAR file and upload to the server using your pass code. You may do more than 10 points of work but the max award will be 11 points. Part I: Exam Prep Exercises--7 points total To be completed in Exercises file Find the Error: 1. // This code contains ERRORS! // It adds two numbers entered by the user. int num1, num2; String input; char again; XXXXX should be char again = 'Y' so the while loop will be true Scanner keyboard = new Scanner(System.in); while (again == 'y' again == 'Y') XXXX needs a here System.out.print("Enter a number: "); num1 = keyboard.nextint(); System.out.print("Enter another number: "; num2 = keyboard.nextint(); System.out.println("Their sum is "+ (num1 + num2)); System.out.println("Do you want to do this again? "); keyboard.nextline(); // Consume remaining newline input = keyboard.nextline(); again = input.charat(0); XXXX needs a here
2. // This code contains ERRORS! int count = 1, total; XXXX total needs to be initialized to 0 while (count <= 100) XXXX needs a here total += count; XXXX needs count++; here XXXX needs a here System.out.print("The sum of the numbers 1-100 is "); System.out.println(total); Algorithm Workbench 1. Write a while loop that displays the following set of numbers: 20, 15, 10, 5, 0 int k = 20; while (k>=0) System.out.println(k + ", "); k -=5; // or, k = k 5; System.out.println(k ); // display last number without "," after it for (k = 0; k<=1000; k+=10) System.out.println(k + ", "); System.out.println(k ); // display last number without "," after it (should be 93.84) int a = 1, b = 30; double sum = 0.0; while ( a <=30 ) sum += (double)a/b; a++; b--;
for (count = 0; count < 50; count++) System.out.println("count is " + count); Part 2: Programming Challenges 3 points each Create a new class P1Guess and follow Challenge Lab 5 located here. import java.util.scanner; 1) P1Guess.java import java.util.random; //needed for Random class import java.util.scanner; //needed for Scanner class public class P1Guess public static void main(string [] args) //create Scanner object and Random class object Scanner keyboard = new Scanner (System.in); Random generator = new Random(); //declare variables int userguess, secret, count=0; //get a random secret number secret = generator.nextint(100); System.out.println("I've got a number 0 to 100."); //gets the user's guess and compares to secret do System.out.println("What's your guess?"); userguess = keyboard.nextint(); if (userguess == secret) System.out.println("Got it!"); else if (userguess > secret) System.out.println("Guess lower."); else System.out.println("Guess higher."); count++; while (userguess!= secret); System.out.println("That took you " + count + " tries.");
2) Create a new class called P2Hotel and solve the following: /** * Andrew Fiola * 2012.02.27 * * Calculates a hotel's total occupied and vacant rooms after taking user input * for each floor, then calculates the hotel's occupancy rate. */ import java.util.scanner; //get keyboard scanner public class P2Hotel public static void main(string[] args) //Variable declarations int totalfloors; //total number of floors in hotel int totalrooms = 0; //total number of rooms in hotel int roomsentered; //Temporary variable to store user input until it is validated int occupiedrooms = 0; //Number of occupied rooms in hotel Scanner keyboard = new Scanner(System.in); //Keyboard scanner //Prompt user for number of floors in hotel. // Will reject input if a number less than 1 is entered. System.out.println("\fHow many floors does the hotel have?"); totalfloors = keyboard.nextint(); while(totalfloors < 1) System.out.println("The hotel can't have less than one floor! Try again."); totalfloors = keyboard.nextint(); //Input loop. For each floor, gets total number of rooms and number occupied for(int currentfloor = 1; currentfloor <= totalfloors; currentfloor++) System.out.println("How many rooms are on floor " + currentfloor + "?" + " Please enter at least 10.");
roomsentered = keyboard.nextint(); while(roomsentered < 10) System.out.println("Invalid. Please enter at least 10."); roomsentered = keyboard.nextint(); totalrooms += roomsentered; System.out.println("How many rooms on this floor are occupied?"); roomsentered = keyboard.nextint(); while(roomsentered < 0) System.out.println("You can't have a negative number of occupied rooms!" + " Try again."); roomsentered = keyboard.nextint(); occupiedrooms += roomsentered; //Output results System.out.println("Results:"); System.out.println("The hotel has " + totalrooms + " rooms."); System.out.println("Of those, " + occupiedrooms + " are occupied and " + (totalrooms - occupiedrooms) + " are vacant."); System.out.println("The hotel's occupancy rate is " + (((double)occupiedrooms / totalrooms) * 100) + "%."); 3) Create a new class called P3UppercaseFile and solve the following: P3UppercaseFile.java import java.io.*; import java.util.scanner; public class P3UppercaseFile public static void main(string [] args)throws IOException Scanner keyboard = new Scanner (System.in); System.out.print("Enter the file to be read: "); String filename1 = keyboard.nextline(); File file1 = new File(filename1); Scanner inputfile = new Scanner(file1);
System.out.print("Enter the file to write: "); String filename2 = keyboard.nextline(); PrintWriter outputfile = new PrintWriter(filename2); while (inputfile.hasnext()) String lower = inputfile.nextline(); String upper = lower.touppercase(); outputfile.println(upper); //close the input file inputfile.close(); outputfile.close();