Using Java
This program puts into practice the material from yourOne-Dimensional Array Basics worksheet. Use the main() method tocreate and populate the array with the 8 values from the salaryarray in the worksheet. To give you practice in the different waysto populate arrays, you will do this four different ways for thisproblem. Then you'll add methods to do a couple other tasks. Usethis data, in this order:Salary 33880.50 10700.25 40204.30 136900.80 33239.00 60774.30 150243.10 45000.00Subscript 0 1 2 3 4 5 6 7Ordinal 1st 2nd 3rd 4th 5th 6th 7th 8th
Create your main() method skeleton, then add this method aftermain() to display the array. You'll be using it several times:
public static void fvDisplayArray(double pdValues){ int iSub; int iNumElements; DecimalFormat dfMoney = NewDecimalFormat("$###,###.00");
iNumElements = pdValues.length; for (iSub = 0; iSub < iNumElements; iSub ++) { System.out.println("Ordinal value " + (iSub + 1) + "(subscript " + iSub + "): " +dfMoney.format(pdValues[iSub])); } return;}
The output should look like this:Ordinal value 1 (subscript 0): $33,880.50 Ordinal value 2 (subscript 1): $10,700.25Ordinal value 3 (subscript 2): $40,204.30…Ordinal value 8 (subscript 7): $45,000.00_____________________________________________________________________________________________For the first approach, use direct assignment statements topopulate the array. First, construct the array to store the eightsalary values. Then, add eight individual assignment statements topopulate the array. Add a statement to call fvDisplay() passing thename of the array as an argument to verify it works right.
On to the second approach. Comment out /*… */ out both the arrayconstructor and the eight assignment statements. This approach willuse "shortcut" assignment instead. Create a new constructor thatuses the "shortcut" method and assigns all eight values in the sameJava statement using a comma-separated list in curly braces asexplained in Notes 10 {…}. Add a statement to call fvDisplay()passing the name of the array as an argument to verify it worksright.
A word about shortcut notation: this approach is a common way toquickly test values in an app you're developing, but unless youhave a non-dynamic short list of values, you'll usually use anotherapproach in real-life apps such user input, file data, orprogram-generated values.
For the third approach, populate the array from user-suppliedconsole input. Comment out the "shortcut" assignment statement fromthe second approach, and uncomment the original arrayconstructor from the first: double dSals[8]; or whatever you namedit.
Now, in a loop, prompt the user for a value, capture the valueto an input-capture variable, and then store the value to the nextavailable array element. The style of loop is up to you. Your inputdialog should look like this (yellow highlight is what the usertypes in at runtime). To display the contents, add a statement tocall fvDisplay() passing the name of the array as an argument toverify it works right.
Salary 33880.50 10700.25 40204.30 136900.80 33239.00 60774.30 150243.10 45000.00Subscript 0 1 2 3 4 5 6 7Ordinal 1st 2nd 3rd 4th 5th 6th 7th 8th
Enter salary 1: 33880.50 Enter salary 2: 10700.25Enter salary 3: 40204.30…Enter salary 8: 45000.00
For the fourth and final approach, create an input file (.txt)with the same salary data. This time, read the data file with aScanner and the skills you just learned with file I/O and populatethe array from the data file. Comment out the unneeded code fromthe first three steps above.
This can get tricky. Since you're allocating 8 array elements inthe constructor, your data file must have exactly 8 items in it.Too many and you could go out of bounds for the array, too few andthe entire array won't be fully populated. Experiment with thesetwo scenarios as you work on the problem—you'll learn fromexperience how this works.
After you have your data file created, add the code to main() tocall a new method to read the data file and populate the array. Usea void() method and pass it two arguments: the array itself and theFully Qualified File Name (FQFN) to your .txt file. Remember tofollow method and parameter naming standards for thisclass.
Also in main() AFTER your new method call to populate the array,add another statement to execute the display array method passingthe name of the array to verify your method properly populated thearray.
This will give you an idea of how Java handles arrays when theyare received as parameters. Changes will propagate back to thecalling method's scope—unlike regular variables in a parameterlist. This is why you can pass an empty array to another method,populate it there, and then return to the calling method—only tofind that the changes "stuck." Arrays behave much like "passing byreference" in other languages.
Using Java This program puts into practice the material from your One-Dimensional Array Basics worksheet. Use the main()
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am