Exercises and Assignment – FIles and a few Arrays
Exercise代做 Review the topics of the flow of control and files Examine the fundamentals of arrays declaration and syntax
Objectives
 Review the topics of the flow of control and files
 Examine the fundamentals of arrays declaration and syntax
 Practise the techniques of manipulating arrays and array elements
Exercises – SelfReview Exercise代做
Complete the following questions and exercises from the text for personal review:
From Chapter 7 Review Questions (JavaFoundations):
SR 7.1 to SR 7.8 – these are fundamental topics of arrays
EX 7.1, EX 7.3 (the most common mistake when attempting to process all the elements in an array)
EX 7.6 – assume array created with an initialiser list: String[] names = {“Bob”, “Alice”, …};
PP 7.1 – similar in idea to PP 7.3 (except using specific numbers 150 instead of ranges)
PP 7.5 – load the double input values in the array with an initialiser list, write methods to calculate the mean() and sd() of
the array (included as an exercise below)
Exercises – Lab & Homework Exercise代做

Declare and initialise an array values[] with the following values,
int[] numbers = new int[] { 72, 15, 0, 2, 100, 5, 11 };
Determine and display the minimum and maximum values in the array, and ds
Hints:
– start by assuming both the minimum and maximum have the value in the first array element numbers[0]
– loop through the remaining elements: for ( i=1; i<numbers.length; i++ )
and within the loop use the common approach to determine min & max of the values, by comparing minimum and maximum with numbers[i]

[from EX 7.6]
Write an application that contains the loops that display the values stored in the following array names[],
String[] names = new String[] { "Bob","Alice","Zait","Sar","Kimberly" };
Loops to display the array names as,
a.forward, with each name on its own line, in the same order the strings are defined in the array
b.forward as well, but with each name displayed in UPPERCASE: names[i].toUpperCase()
c.in reverse order, with all names on the same line, but each name separated by a space 2
3.Note: This question does not require using arrays…actually, using arrays make the solution much more difficult. Exercise代做
Write an application to create a simple shopping receipt. Purchases records are read from a data file, and output sent to
report file for a formatted receipt. The input & output file names are provided by the user.
Two (2) Parts to this question.
Part A:
With a text editor (Geany, Notepad, TextEdit), create a text file with purchase data (purchases.txt) – see example.
The purchase data consisting of records that have an item name (a string, with no spaces in the item name) and the price in cents (as an integer in cents, to avoid precision errors).
o formatting (x/100) is required on the output, to show the prices, subtotal, tax, and total in dollars $#.##
The number of purchase records is unknown (could be 1 or 100, or more!), so the application must keep reading records, and processing the data until, there is no more data (looping until .hasNext() is false)
For example,
consider the purchase data in purchases.txt, in the format: item price(in cents) Exercise代做
breadsliced 250 milk_(2l) 479 pepsi_(2l) 220 ivory_soap 499 large_eggs 289
the output “printed” receipt file (assume Tax applies to ALL items at 14% of the subtotal; ignore rules of when tax is applied or not, and the splitting Canadian GST & PST).
Java SuperStore Sat Mar 14 19:57:35 PDT 2020 BREADSLCED 2.50 MILK_(2L) 4.79 PEPSI_(2L) 2.20 IVORY_SOAP 4.99 LARGE_EGGS 2.89  Subtotal 17.37 Tax 2.43 Total 19.80
Hints: Exercise代做
in the processing loop, sum the subtotal of the item prices (as a normal running total (sum) )
after reading all the data (after the loop), calculate the tax as: subtotal * 0.14
and the overall total is calculated as: subtotal + tax
recall from the first exercises, to get the current date:
import java.util.Date;
and,
String todayStr = “” + new Date(); // put formatted date in todayStr
create the purchases input file in the same location (folder) as the program: purchases.txt, items.data, or whatever
a creative approach is necessary to format the output and make sure purchase records line up; using .printf()
o for example:
receiptFile.printf ("%15s %6.2f", itemName, (price/100.0) );
which formats the itemName in a string (%s) of width 15 leftjustified, and the price/100.0 in floatingpoint (%f) of width 6 with 2 decimal places3
Part B: Exercise代做
Enhance the receipt, by informing the user which item is the most expensive (maximum price).
Hints:
determining the max price is simply performing the “maximum calculation” logic as each record is read from the file, but within the IF to determine the new maximum price, perform two operations: 1) the new maximum price as the current price AND the item name (so setting both max_price and max_name)
display the max_name and max_price after the subtotal, tax, and total
For example, displaying the maximum price below the Total,
Subtotal 17.37 Tax 2.43 Total 19.80 Most expensive IVORY_SOAP 4.994
 “Fibonacci Sequence” (details on the Fibonacci Number: https://en.wikipedia.org/wiki/Fibonacci_number )
The Fibonacci Sequence is important in mathematics, computing science, and the natural sciences (biology)—simply, it is stunningly cool! Example below showing first 21 values of the sequence (F0 … F20).
The basic formula to calculate any value (Fn) of the Fibonacci Sequence,
F0 = 0, F1 = 1 — starting values of the Fibonacci Sequence
Fn = Fn1 + Fn2 , where n >= 2
For example to calculate F5: F5 = F4 + F3
Write an application FibCalculator,
declare an int array called fib[] of size 21 (the indexes are 0 to 20)
assign the starting values in the first two (2) elements of the array as: fib[0]=0; fib[1]=1;
loop FOR the remaining element indexes in the array ( i=2; i<fib.length; i++ ), and calculate the Fibonacci values as per the formula above: fib[i] = fib[i1] + fib[i2];
write another FOR loop to display a table of the populated Fibonacci array, showing,
n  Fib(n)  0  0 1  1 2  1 3  2 4  3 5  5 6  8 ... 19  4181 20  6765
INTERESTING: Increase the array size in the declaration to 50, and run the program. The Fibonacci Sequence grows very quickly and exceeds the int datatype. Change the array to long[] and array size to 100. Now try double[] and size 1500.
CHALLENGING ARRAY QUESTION Exercise代做
 [from PP 7.5] “Mean and [Sample] Standard Deviation” – See problem description in Chapter 7 Review Questions
This problem may look complex due to the mathematical formulae described, but it really isn’t.
For example, to sum all elements in a double array called data[], where n = data.length (length of the array), and described mathematically as (“mean” is another word for average),
Written in Java as,
sumOfDiff = 0.0; // sum of differences, squared for (int i=0; i<data.length; i++) { sumOfDiff = sumOfDiff + Math.pow( (data[i]mean), 2); } sampleSD = Math.sqrt( sumOfDiff / (data.length1) );
TASK: Using the above, calculate the mean and sample standard deviation of the values in the arrays (to 3 decimal places).
A) double[] data = new double[] { 0.0, 1.0 }; mean = 0.500, stan.dev. = 0.707
B) double[] data = new double[] { 10.0, 10.0, 10.0, 25.0, 25.0, 25.0 }; mean = 17.500, stan.dev. = 8.216
C) double[] data = new double[] { 11.0, 2.55, 29.82, 35.0, 6.42, 50.0 }; mean = 22.465, stan.dev. = 18.734
Hints: Exercise代做
break up the problem into pieces, with each calculating a specific part of the whole formula
Math class methods are required, in particular: .pow(), .sqrt()
the array of double values is processed to perform the calculations:
o summation the values (using a FOR loop)
o calculate the “mean” (another word for “average”)
o sum the “sum of differences” squared: (xi – mean)2 (using a FOR loop)
o calculate the “sample” standard deviation: sqrt( sum of differences / n1 )
Check your results: https://www.calculator.net/standarddeviationcalculator.html (and check Sample).6
Assignment
Complete and submit your solutions for the following,
 Fibonacci Calculator – capturing the output of running for n = 50 – a single run
 Shopping Receipt – there is no important console output, but submit the purchase data input files and receipt output files
 Mean and Standard Deviation – capturing the output of running with the three (3) data sets – 3 runs
Follow the necessary requirements (as per the previous assignment), Exercise代做
 proper Header Comment in source code files (see example in Assignment 1 and 2)
 sufficient code commenting, providing variables descriptions, and useful descriptions for important & key sections
 an Output Capture Document, showing the testing of the applications (named something such as Assignment 6b Output),
o presenting content in an organised manner, with program output formatted with a proper font
o save the output in a formatted document file, so that you can use different fonts and formatting
o in Windows: Microsoft Word or Wordpadl in MacOS: Microsoft Word or TextEdit
valid document formats are .docx (Word) or .rtf (Wordpad, TextEdit) – not .pdf
Before the due date & time, on our course Moodle page, attach the following as part of your assignment submission,
source code, the.java files (but not the .class files)
a single, combined output capture document, the .docx or .rtf file
And when you have added the files, and doublechecked all required are attached, and ensure to click [Submit] to get marked.