I have this lab I\'ve been trying to work on but I can\'t figure out how to comp
ID: 3824861 • Letter: I
Question
I have this lab I've been trying to work on but I can't figure out how to complete it. Here are all the resources provided by the instructor.
LAB07, Array of Student Class objects – Bubble Sort Points: 25
OBJECTIVE
• To demonstrate the concept of sorting
• To demonstrate the Bubble sort
• To demonstrate sorting objects
REQUIRED
A report of your work, in a flat pocket folder, in the following order:
1. Grade form Lab07GradeForm.doc
2. This program description
3. The source listing of the Student class Student.java
4. Problem Analysis with Input, Output, Pseudocode (word processed)
5. Data dictionary (included as comments in Java program
6. The listing of the source program Lab07.java
7. Listing of the input file Lab07StudentFile.txt
8. Listing of the student name file Lab07Names.txt
8. Listing of the expected results (already created) Lab07ExpectedResults.xlsx
9. Listing of the output file Lab07Report.txt
SPECIFICATIONS
Start with Lab06 and rename it as Lab07
There were two parts to Lab06:
Part1 – create an array of objects and then place values into the objects in the array
Part2 – retrieve the objects from the array and print a report
Lab07 will insert a step in between –- Sorting the array of objects
Create a Bubble Sort method and pass the array to it. Using the Bubble sort, sort the array in descending order, by average Since arrays are referenced when passed to a method, there is no need to return the array.
In order for this to work properly: Part1 must create the complete array and nothing else Then the array is sorted Part2 retrieves objects from the array and creates the report, with the student name. Therefore, the name search must be after the sort.
The report should look the same as Lab06 except it will be in descending order by average.
INPUT
File: Student file Lab07StudentFile.txt
Record: Student record
Field Data Type
Student id# 4 numbers (ex. 1234)
Ten test scores integers (valid numbers are 0 -100)
INPUT
File: Student name file Lab07Names.txt
Record: Student name record
Field Data Type
Student id# 4 numbers (ex. 1234)
Student name String
OUTPUT
File: Grade Report file Lab07Report.txt
Record:
Student Grade Report, sorted by Avg
ID# Name /----------------TEST Scores---------------/ Total Adj Total Avg
xxxx xxxxxxxxxxxxxxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxxx xxxx xxx
xxxx xxxxxxxxxxxxxxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxxx xxxx xxx
xxxx xxxxxxxxxxxxxxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxxx xxxx xxx
Total students = xx
Here's the link to the completed lab06 which it builds upon.--> https://pastebin.com/4cxF0YGb
Here's the link to the completed Student Class file.--> https://pastebin.com/DvZvKq2M
Explanation / Answer
TheMainmodule declares the arrays and variables to be used in the program, displays a welcome message, and calls its submodules. Remember: The arrays Numbers,
Names, and Pricesstore the data from the "pricelist"file. The arrays OrderNums
andOrderAmtshold the part numbers and quantities input by the user. ListCount
is the number of records in the "pricelist"file and OrderCountis the number of
parts ordered by the user. The pseudocode for this module is as follows:
1 Main
2 Declare Numbers[100], OrderNums[50]As Integer
3 Declare OrderAmts[50]As Integer
4 Declare Names[100]As String
5 Declare Prices[100]As Float
6 Declare ListCount,OrderCountAs Integer
7 Declare CustomerAs String
8 Display a welcome message
9 Call Input_Data module
10 Call Sort_Parts_Order module
11 Call Print_Invoice module
12 End Program
Input_DataModule
All this module does is to call its two submodules as follows:
Call Load_Price_List module
Call Input_Parts_Order module
Load_Price_ListModule
This module reads the data in the sequential file "pricelist"and assigns it to three
parallel arrays—Numbers, Names, and Prices. While reading the file, we use the
counter named ListCountto count the number of records it contains. The counter
not only serves as a subscript for the arrays, but also gives the number of elements
Figure 10.6Hierarchy chart for the Invoice Preparation program
Main
Module
Input
Data
Sort
Parts
Order
Load
Price List
Input
Parts Order
Search
for Part
Number
Print
Invoice
10.5 Focus on Problem Solving: The Invoice Preparation Program 629
in each array and can be used in the test condition of loops that process these arrays.
The pseudocode for this module is as follows:
1 Open "pricelist" For Input As DataFile
2 Set ListCount= 0
3 While NOT EOF(DataFile)
4 ReadDataFile,Numbers[ListCount],Names[ListCount],
Prices[ListCount]
5 Set ListCount= ListCount+ 1
6 End While
7 Close DataFile
Input_Parts_OrderModule
The customer places the order in this module. The module inputs the data for the
parts order, which consists of the customer’s name and the part numbers and quantities wanted. The part number for each part ordered is put into one array named
OrderNumsand the quantity the customer wants is input into a parallel array named
OrderAmts. A counter (OrderCount) acts as a subscript for the two arrays and also
determines the number of items in the order. At the end, the counter contains the
number of elements in the arrays, but as you know, the highest subscript is actually
OrderCount 1.
1 Declare Num,AmtAs Integer
2 Write "Enter the customer's name: "
3 Input Customer
4 Set OrderCount= 0
5 Write "Enter part number, quantity desired:"
6 Write "Enter 0, 0 when done."
7 Input Num,Amt
8 While Num!= 0
9 Set OrderNums[OrderCount]= Num
10 Set OrderAmts[OrderCount]= Amt
11 Set OrderCount= OrderCount+ 1
12 Write "Enter part number, quantity desired. Enter 0, 0
when done."
13 Input Num,Amt
14 End While
Sort_Parts_OrderModule
This module uses the bubble sort method (see Chapter 8) to sort the list of ordered
part numbers (the array OrderNums) in ascending order. It also puts the parallel array,
OrderAmts, into the same order as OrderNumsso that the two arrays remain parallel.
The pseudocode for this module is as follows:
1 Declare Flag,K,TempName,TempAmtAs Integer
2 Set Flag= 0
3 While Flag== 0
4 Set Flag= 1
5 For (K= 0; K<= OrderCount 2; K++)
630 Chapter 10 Sequential Data Files
6 If OrderNums[K]> OrderNums[K+1]Then
7 Set TempNum= OrderNums[K]
8 Set OrderNums[K]= OrderNums[K+1]
9 Set OrderNums[K+1]= TempNum
10 Set TempAmt= OrderAmts[K]
11 Set OrderAmts[K]= OrderAmts[K+1]
12 Set OrderAmts[K+1]= TempAmt
13 Set Flag= 0
14 End If
15 End For
16 End While
Print_InvoiceModule
This module displays the invoice. (See Figure 10.5 for a sample.) This task entails
displaying a title, the customer’s name, the headings, the list of all parts ordered,
and the total amount due. For each part ordered, we must search the price list for
the corresponding name and price, and do one of the following:
If the item is found, the quantity ordered, part number, part name, unit
price, and total cost of that item are displayed on one line of the invoice.
If the item is not found, the part number and an appropriate message is displayed on that line of the invoice.
The same loop that displays the above information also sums the total cost of the
parts ordered by summing the ItemCostvalues so that the amount due can be displayed at the bottom of the invoice. The pseudocode for this module is as follows:
1 Declare AmountDue,ItemCostAs Float
2 Declare K,Found,IndexAs Integer
3 Set AmountDue= 0
4 // Display a title for the invoice
5 Write "Customer: " + Customer
6 Write "Quantity Part Number Part Name Unit Price
Item Price"
7 For (K= 0; K< OrderCount; K++)
8 // Search the parts list for OrderNums[K]:
9 Call Search_for_Part_Number module
10 /* Found = 1 indicates that search is successful, Index is the
subscript of item found */
11 If Found== 1 Then
12 Set ItemCost= OrderAmts[K]* Prices[Index]
13 Write OrderAmts[K]+ " " + OrderNums[K]+ " " +
Names[Index]+ " " + Prices[Index]+ " " + ItemCost
14 Set AmountDue= AmountDue+ ItemCost
15 Else
16 Write OrderNums[K]+ " Invalid Part Number"
17 End If
18 End For
19 Write "TOTAL DUE ... "+ AmountDue
10.5 Focus on Problem Solving: The Invoice Preparation Program 631
Recall that the symbol indicates a tab stop, and is used here to show that the
output must be formatted to line up in columns.
Search_for_Part_NumberModule
This submodule performs a serial search (see Chapter 8) of the array Numbersfor
the part number with subscript K, which is stored as OrderNums[K]. If this number is
found, it sets the variable Indexequal to the current subscript and sets the variable
Foundequal to 1; otherwise, there is no change to Found. The pseudocode for this
module is as follows:
1 Set Index= 0
2 Set Found= 0
3 While (Found== 0) AND (Index<= ListCount 1)
4 If Numbers[Index]== OrderNums[K]Then
5 Set Found= 1
6 Set Index= Index+ 1
7 End If
8 End While
Program Code
The program code is now written using the design as a guide. At this stage, header
comments and step comments are inserted into each module, providing internal
documentation for the program. The following are several points concerning the
coding that are specific to this program:
The welcome message and the invoice should be displayed on a blank
screen. Recall that this is accomplished by using the programming language’s clear screen statement.
To produce a professional invoice, similar to the one shown in Figure 10.5,
we must format the output to ensure that the data in the invoice lines up in
columns and that the dollar amounts align on their decimal points. This can
be accomplished using the programming language’s special print formatting
statements.
Program Test
This program can be adequately tested by creating a data file, "pricelist", which
contains the following records:
13254,"Handle",15.65
14153,"Wheel (6 in.)",5.95
14233,"Blade (20 in.)",12.95
14528,"Engine (260 cc)",97.50
14978,"Carburetor",43.00
15251,"Starter (recoil)",24.80
15560,"Adjusting knob",0.95
16195,"Rear skirt",14.95
16345,"Grass bag",12.95
16577,"Axle (small)",7.50