Question: // ALL USING JAVA package QUESTION1; import java.io.*; /** * Write a program that uses the buffered classes to write out - your name -

 // ALL USING JAVA package QUESTION1; import java.io.*; /**  *  Write a program that uses the buffered classes to write out  - your name  - your favorite color  - this class's code (111)   to a new file, with one item per line.   Now, read in all of the data, line by line, and display it.   *  */  public class Question_1_Write_Name_Color_Class_Code { public static void main(String[] args) { new Question_1_Write_Name_Color_Class_Code().fileIO(); } public void fileIO() { String filename = "data.txt"; String name = ""; //TODO fill in your name  String favoriteColor = ""; //TODO fill in your favorite color  int classCode = 2545; writeToFile(filename, name, favoriteColor, classCode); printDataFromFile(filename); } public void writeToFile(String filename, String name, String favoriteColor, int classCode) { // TODO write the three pieces of information to the file given by filename.  // TODO Add try-catch blocks to this method. This method should NOT declare that it throws IOException or any other file exceptions.   } public void printDataFromFile(String filename) { // TODO read in the data from the file, and print it.  // TODO Add try-catch blocks to this method. This method should NOT declare that it throws IOException or any other file exceptions.   } } 

======================================================

package QUESTION2; /**  *  Start with your recycling truck program from last time.  Add code so that it writes the data as a report to a file.  The report file should look like this example.  Note that it should use "crate" for 1 crate, "crates" otherwise.    House 0 recycled 1 crate  House 1 recycled 2 crates  House 2 recycled 1 crate  House 3 recycled 2 crates  House 4 recycled 3 crates  House 5 recycled 3 crates  House 6 recycled 2 crates  House 7 recycled 1 crate  Total crates recycled: 15    Add try-catch blocks for IOException. The try-with-resources version is recommended.   */ public class Question_2_Write_Recycling_Report { public String filename = "recycling_report.txt"; // TODO you can add your methods from the recycling truck program you wrote last lab    public static void main(String[] args) { new Question_2_Write_Recycling_Report().recycling(); } public void recycling() { int numberOfHouses = 8; int[] cratesPerHouse = getRecyclingPerHouse(numberOfHouses); int totalCrates = calculateTotal(cratesPerHouse); int maxCrates = calculateMax(cratesPerHouse); int minCrates = calculateMin(cratesPerHouse); int houseWithMostRecycling = calculateHouseWithMostRecycling(cratesPerHouse); System.out.println("Total crates from all houses = " + totalCrates); System.out.println("Max crates at any house = " + maxCrates); System.out.println("Min crates at any house = " + minCrates); System.out.println("House with most recycling = " + houseWithMostRecycling); writeReport(cratesPerHouse, totalCrates, filename); } // Ask user for number of crates for each house. Store in array and return this array.  public int[] getRecyclingPerHouse(int houses) { // TODO ask user for input.  return null; // Replace with your code  } //Add up all of the numbers in the array and return that  public int calculateTotal(int[] cratesPerHouse) { // TODO calculate and return the total.  return 0; // Replace with your code  } //Which is the largest number in the array?  public int calculateMax(int[] cratesPerHouse) { // TODO identify the largest number in the array.  return 0; // Replace with your code  } //Which is the smallest number in the array?  public int calculateMin(int[] cratesPerHouse) { // TODO identify the smallest number  return 0; // Replace with your code  } //Use the array to figure out which house number - or array element number - has the most recycling  public int calculateHouseWithMostRecycling(int[] cratesPerHouse) { // TODO which house had the most recycling? If more than one house  // had the max number, return either house number.   return 0; // Replace with your code  } public void writeReport(int[] cratesPerHouse, int totalCrates, String filename) { // TODO write report to file with the name in the filename variable.   /* Your lines should look exactly like this   House 0 recycled 1 crate  House 1 recycled 2 crates   and the last in the file should look like this   Total crates recycled: 15   The grader is looking for this exact format.   */   // TODO you could write a new method to generate one line, e.g. "House 0 recycled 1 crate" to write to the file   } } 

===================================================================

package QUESTION3; import java.util.ArrayList; /**   There is a file called recycling-report-main-street.txt in the root  directory of this project.   This file contains data for a much longer street. Again, the house numbers correspond to array or ArrayList indexes.  Read it into your program, analyze the data, and then display the numbers of the house(s) that recycled the most crates?   Make sure you use try-catch blocks for IOException.   */  public class Question_3_Read_Recycling_Report { String filename = "recycling-report-main-street.txt"; public static void main(String[] args) { new Question_3_Read_Recycling_Report().recyclingReport(); } public void recyclingReport(){ // Read the file into   ArrayList lines = readLinesFromRecyclingDataFile(filename); ArrayList crateQuantities = extractCrateQuantityData(lines); int max = calculateMax(crateQuantities); ArrayList housesWithMax = copyIndexesToNewArrayList(crateQuantities, max); System.out.println("The maximum number of crates is " + max); System.out.println("The houses with this max number of crates is " + housesWithMax); } public ArrayList readLinesFromRecyclingDataFile(String filename) { // TODO read the lines of the file, given by filename, into an ArrayList.  // Don't modify the lines. Each element in the ArrayList is one line from the file.  // Return the ArrayList.   return null; } public ArrayList extractCrateQuantityData(ArrayList lines) { //TODO extract the number of crates from each String,  // convert to an integer, and save each integer in a new ArrayList   return null; } public int calculateMax(ArrayList crates) { // TODO calculate the largest value in the crates ArrayList, and return it  // Example: if the ArrayList contains [ 9, 3, 2, 5, 9, 0 ], you should return 9.   // Make this method general, so it works for any set of numbers.  // If the ArrayList contains [-9, -4, -2, -10] this function should return -2.   return 0; } public ArrayList copyIndexesToNewArrayList(ArrayList quantities, int value) { //TODO copy the indexes of the houses with this maximum number into a new ArrayList and return this.   // This should be a generic method to copy all of the indexes of an ArrayList  // where the element holds a certain value; into a new ArrayList.  // Exmple: if the ArrayList contains [ 9, 3, 2, 5, 9, 0 ]  // The maximum values, 9, is found at index 0 and 5  // So return a new ArrayList [ 0, 5 ]   return null; } } 

================================================

package QUESTION4; /**  *  * Read the following, and answer the questions by filling in the Strings in the code.   Java doesn't make you deal with possible NullPointerException, or ArrayIndexOutOfBoundsException in your code.  If these exceptions happen, and are not caught, your program crashes.  NullPointerException, ArrayIndexOutOfBoundsException, and several other exceptions, are called unchecked exceptions.   It's possible to add try-catch blocks for NullPointerException, and other unchecked exceptions.  You can also declare that a method throws NullPointerException, or any other unchecked exception   But, Java insists that you deal with IOException  which is a checked exception - in some way.  The compiler checks that you have indeed done something about code that can throw IOException.  You either have to surround your file IO code with a try-catch block, or declare that the method throws IOException.  If your method throws IOException, then a method that calls this method also has to add a try-catch block, or declare that it also throws IOException.   There are other checked exceptions, for example SQLException, which you'll see when we work with databases.   Java's decision to implement two types of exceptions (checked and unchecked) is unusual.  Other languages like C#, Python, JavaScript... have exceptions, but it's always up to you how to deal with them.  You are never required to add try-catch blocks or declare that a method throws a particular exception;  the responsibility is left to the programmer to implement whatever exception/error handling mechanism is appropriate.   Questions:   Considering many other languages don't require you to deal with exceptions,  think about why Java does make you deal with at least some.   1. List at least 1 benefit of checked exceptions  2. List at least 2 negative consequences of checked exceptions  3. What is your opinion on Java's decision to use checked exceptions?   Usually, it's better to anticipate and try to prevent errors. Almost all unchecked exceptions can be  prevented (in theory) by careful programming. And many checked exceptions can also be prevented with careful coding.   It's more common for programmers to try to prevent unchecked exceptions (e.g. NullPointerException)  than to write a try-catch block for code that may throw that type of exception.   4. For unchecked exceptions, why is it usually better to anticipate and avoid errors when possible,  instead of using try-catch blocks? At least 2 reasons.   Questions on this subject are common in Java job interviews!    */ public class Question_4_Exception_Handling_Questions { // Questions on checked and unchecked exceptions   String q1_pros_of_checked_exceptions = ""; //TODO Fill this in with your answer  String q2_cons_of_checked_exceptions = ""; //TODO Fill this in with your answer, at least 2 reasons  String q3_your_opinion_on_java_having_checked_and_unchecked_exceptions = ""; // TODO fill this in with your opinion   // Why is it usually better to anticipate and try to prevent errors, vs. letting exceptions be thrown and then catch them?   String[] q4_why_is_it_better_to_anticipate_and_prevent_errors = { "", "" }; // TODO fill this in. At least 2 reasons.  } 

================================================================================================

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!