CS49J

Remember not to create packages. Use the default package

14A

You are given a text file containing the contents of a shopping cart at a grocery store. The records consists of a item name (which can be any number of words) a price, and a quantity separated by one or more white spaces. The file has this format:

item  price  quantity
raman 0.50 5
whole wheat bread 2.48 1

You are to complete the class called GroceryCart.

which will read the items and then print: the name, the price, the quantity, and the extended price. Extended price is the quantity * the price. Use printf (page 530 section 11.2.9) to format the output so that it is displayed in columns. The column with the name will be 20 characters wide an left justified. Each of the numbers will 8 characters wide and right justified. Price and extended price should have 2 decimal places. After all items are processed, print the word Total:, a space, a $ and then the total value of all the items in the cart (with 2 decimal places). It will look like this:

raman                   0.33       5    1.65
whole wheat bread 2.48 1 2.48
edamame 1.85 2 3.70
corn tortillas 0.75 3 2.25
apples 1.00 2 2.00
salmon 11.90 1 11.90
paneer 3.29 1 3.29
tofu 1.75 2 3.50
dried chilies 1.05 5 5.25
Total: $36.02

Here is a file that will be used by Codecheck. Copy it into your project.

Declare that the main method throws an exception when the file is not found.

Here is a starter. Do not change the file name or remove the //SUB

public class GroceryCart
{
   public static void main(String[] args) throws FileNotFoundException
   {
      String fileName = "items.txt"; //SUB "items2.txt"
      
      //your code here
   }
}


Codecheck URL

14B

Here is a text file containing the information about the salaries of the top 10 highest paid CEOs.

Ticker	Company	CEO Name	Year	Compensation ($)
VRX Valeant Pharmaceutical International J. Michael Pearson 2015 $143,077,442
MASI Masimo Corporation Joe Kiani 2015 $119,222,614
SCTY SolarCity Corporation Lyndon R. Rive 2015 $77,318,016
GBL Gamco Investors Mario J. Gabelli 2015 $75,018,176
PANW Palo Alto Networks Mark D. Mclaughlin 2015 $66,606,716
CBS CBS Corporation Leslie Moonves 2015 $56,773,822
VIAB Viacom Philippe P. Dauman 2015 $54,154,312
LNG Cheniere Energy Charif Souki 2015 $54,041,015
ORCL Oracle Corporation Mark V. Hurd 2015 $53,245,128
REGN Regeneron Pharmaceuticals Leonard S. Schleifer 2015 $47,462,526

Write a program CEOSalaries to print a list which contains the ticker symbol, colon space, the name of the CEO, space, and the salary printed with a $ and comma separators. Use printf. The first record looks like this

VRX: J. Michael Pearson $143,077,442

After all the records are printed, print: "Average salary: " and then the average salary with a $ and comma separators and 2 decimal places.

Note: You need to remove the $ and commas before using Integer.parseInt to convert a strings to an int.

The file name will be provided in the command line arguments. This will tell you how to use command line arguments in Eclipse.

Catch the exception if the file does not exist and print this message: No such file: filename

To process data in a file, you have to exam the file to determine the format of the data. In this case, the name of the CEO can be either two or three words. Company names also varies in length. If the CEO name has three parts, one will be an initial, (like J. in the first entry). The initial may be either the first or the second name. Notice that the last name is the third name from the end

Codecheck URL

14C

The United States Social Security Administration keeps lists of the most popular baby names by year and by decade. We have two list:, one from the 1900 to 1909 and one 100 years later from the 2000 to 2009. Each contains the top names for the decade. We want to know what names from the 1900s were still popular in the 2000s.

Write an application called BabyNames1900sAnd2000s to print two columns in alphabetical order. The first column will contain all the boys names for 1900s that were still popular in 2000s. The second column will contain all the girls names for 1900s that were still popular in 2000s. Make the columns 30 characters wide and left justified.

You can get the files from the following URLs

Here is the file format

	Males	Females
Rank Name Number Name Number
1 John 84,591 Mary 161,508
2 William 69,321 Helen 69,429
3 James 62,170 Margaret 57,921
4 George 43,589 Anna 54,918
5 Charles 36,186 Ruth 51,011

Do not use try / catch here. Just declare that the method throws an exception

For full points, design the program so you only read each file one time.

Codecheck URL