package week_5; import java.util.LinkedList; import java.util.NoSuchElementException; /** * This program has two example methods. Both throw exceptions when you run the code. Fix by adding a try-catch...

don't change the form or lines or words of the codes.


package week_5; import java.util.LinkedList; import java.util.NoSuchElementException; /** * This program has two example methods. Both throw exceptions when you run the code. Fix by adding a try-catch block in each method. Don't modify any of the existing code. Here, you will be adding new code. For the printLanguageList method, add try-catch statements. In the catch block, print a message that the LinkedList is empty. You don't need to do anything else, just let the method return. For the wordCount method, add try-catch statements so the NullPointerException thrown when the list is empty, is caught. In the catch block, return 0. So if the sentence is null, your method will return 0. */ public class Question_5_Add_Exception_Handling { public static void main(String[] args) { Question_5_Add_Exception_Handling q5 = new Question_5_Add_Exception_Handling(); // Test the printLanguageList() method q5.printLanguageList(); // Test the wordCount() method String sentence1 = "This is an example sentence."; String sentence2 = null; int words1 = q5.wordCount(sentence1); int words2 = q5.wordCount(sentence2); System.out.println(sentence1 + " has this many words: " + words1); System.out.println(sentence2 + " has this many words: " + words2); } /* Adds some example programming languages to a LinkedList, and then prints them in reverse order. */ //Start of printLanguageList. Don't change or move this comment. The Autograder needs it. public void printLanguageList() { // TODO stop this code crashing by adding exception handling to the method. // Don't modify any of the code or change what is printed. // Add a try-catch block to catch the NoSuchElementException that's thrown. // Your code should still print all the languages, but not crash. LinkedList languages = new LinkedList(); languages.push("Python"); languages.push("Swift"); languages.push("C#"); while (true) { String oneLanguage = languages.pop(); System.out.println(oneLanguage); } } //End of printLanguageList. Don't change or move this comment. The Autograder needs it. /* A very simple word count function. This function should return the number of words in a string. For this program, each word is assumed to be separated by a single space character. If the String sentence is null, this method should return 0. Counting words in real-world applications can be a much trickier problem, with various special cases to consider. For example, is "sugar-free" one word, or two? How many words in "D. B. Cooper" ? */ //Start of wordCount. Don't change or move this comment. The Autograder needs it. public int wordCount(String sentence) { // TODO This code throws a NullPointerException if the String sentence is null. // Add a try-catch block to catch the NullPointerException, so and return 0 if the String sentance is null. // Don't modify any of the code that's here already. String[] words = sentence.split(" "); return words.length; } //End of wordCount. Don't change or move this comment. The Autograder needs it. } package week_5; /** * * Write a program that creates a sales report for a coffee shop. The coffee shop will use this at the end of every day to calculate sales, expenses, and profit. The coffee shop sells 12 different drinks. The name of each drink, the price the shop charges the customer, and how much it costs to make each drink, are saved in the file coffee_price_data.txt. It's in the root directory of this project. The data is in the format name;cost to make;price charged As in this example, Cappuccino;1.56;3.50 So the cappuccino drink costs the coffee shop $1.56 to make, and they charge the customer $3.50. The file coffee_sales_data.txt contains the sales data for one day. This file is in the format name;number sold As in this example, Cappuccino;100 The coffee shop sold 100 cappuccino drinks. Your program should read this data from coffee_price_data.txt, and coffee_sales_data.txt, and store it all in some kind of data structure. You should deal with any file-related exceptions properly. Once you have gathered all the data, generate a report that will be written out to a new file called daily_sales_report.txt. For each drink, record the number of drinks sold, the total that it cost to make the total quantity of those drinks (expenses), and the total amount (revenue) spent by customers on that drink. So, for example, if the coffee shop sold 100 cappuccinos today, you'll write a line that says Cappuccino: Sold 100, Expenses $150.60, Revenue $350.00, Profit $190.40 You may wish to use this String formatting template, "%s: Sold %d, Expenses $%.2f, Revenue $%.2f, Profit $%.2f" And a similar line for each of the drinks. The autograder is looking for this exact format. At the bottom of the file, write the total expenses, total revenue, and total profit for all drinks, for example, like this, All Drinks: Sold 1000, Expenses $1000, Revenue $2500, Profit $1500 You should use try-with-resources exception handling for both file reading, and file writing. Use methods to organize your code. The autograder will call the salesReport() method, and will examine the output file your program creates. The instructor will assess the quality of your code and solution. You should probably write some extra helper methods for the subtasks of this problem. Optional: write unit tests for your helper methods. Ask if you need help. Test and comment your code. */ public class Question_7_Coffee_Shop { static String reportLineTemplate = "%s: Sold %d, Expenses $%.2f, Revenue $%.2f, Profit $%.2f"; public static void main(String[] args) { Question_7_Coffee_Shop q7 = new Question_7_Coffee_Shop(); q7.salesReport(); } String price_data_file = "coffee_price_data.txt"; String sales_data_file = "coffee_sales_data.txt"; String output_report_file = "daily_sales_report.txt"; public void salesReport() { // Suggested outline of program. You can use any data structure you like. // You may need to combine more than one type of data structure. // readCoffeeDataFiles and writeReportFile may get quite long and complex. So, // it is likely you will need to add some more helper methods that readCoffeeDataFiles // and writeReportFile will call. // Replace 'Object' with the data type of the data structure you use. Object allDrinkData = readCoffeeDataFiles(price_data_file, sales_data_file); // TODO replace Object with the type of your data structure writeReportFile(allDrinkData, output_report_file); } // Don't change the name, arguments, or purpose of this method. You should change the return type. // The test expects it to be in your code, and have the behavior defined in the requirements. public Object readCoffeeDataFiles(String dataFile, String salesFile) { // TODO read
Oct 31, 2019
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here