Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CIS611 
Individual Practice Programming Assignment: PA02 
Total Points:  20 
Methods, Sorting, and I/O Files (Input Error Validations): 
 
The purpose of this programming assignment is to: 
 Perform I/O File operations 
 Use predefined file classes to read and write data to files 
 Use sorting algorithms (methods) to sort data  
 Develop reusable code for modularity, readability, and maintainability 
 Define methods and invoke them 
 Use method overloading 
 I/O Validations  
 
General guidelines: 
Students are required to work on programming assignments on their own. Attempt to any form of 
plagiarism is severely monitored and condemned. It is highly encouraged to contact the IC to 
resolve any kind of doubt or issues related to the assignment. It is recommended that students 
follow the submission guidelines to make the grading process easier. The grading sheet is 
provided as well in advance. This helps to plan your submission and spend appropriate time in 
each section of the assignment. Good luck and happy coding!! 
 
Q1 (10 points): 
 
Write a Java program to analyze the annual budget of marketing costs of a company on two of its 
popular products. For each product, input the expenditure on the following items from the user: 
 
1. Website marketing & Search engine marketing 
2. Social media marketing 
3. Radio advertising 
4. Newspaper advertising 
 
You can create 2 arrays of suitable data-type, each of size 4, to hold the price for for each of the 
above items for each product. 
 
As per the design approach chosen by the student, if the student chooses to create an additional 
array, then it is left to the discretion of the student. The requirement can be met by creating two or 
more arrays, depending on the design approach chosen by the student.   
 
Your program should analyze and print the following results: 
1. Total costs spent on marketing per product and both the products put together 
2. Total costs spent on each expenditure for the 2 products put together 
3. Highest expenditure on each product  
4. Lowest expenditure on each product 
5. Highest expenditure on both the products put together 
6. Lowest expenditure on both the products put together 
7. Total percentage cost spent on each expenditure 
 
Name the Java class as CostAnalysis.java 
 
Use the following methods to break-down the functionalities and to make the programming task 
easier. It is your creativity to define the method signature and body and local and class variables 
or to add any additional methods as you think.  
 
1. totalCostPerProduct() 
2. totalCostPerExpenditure() 
3. highestCostCalculator() 
4. lowestCostCalculator() 
5. percentageCalculator() 
6. display() 
 
Tabulate the results as shown below. Use JOptionPane for input and output. Ensure no more than 
2 decimal values are used in the output. You can choose to use a single or two JOptionPane for 
displaying the output details.  
 
The program must handle invalid and required inputs properly. If the user provides an invalid 
input, the program must display an appropriate message and ask for that input to be re-entered. 
The program must handle any exceptions using try catch-block and display appropriate error 
messages. 
 
Sample output: (Here only 2 expenditure items are shown. You program should output for all 4 
items) 
 
The final display can be made in a single JOptionPane, or more than one JOptionPane, and it is 
left to the discretion of the student. 
 
Expenditure Item 
Name 
Product-1 Product-2 Total cost for each 
expenditure item 
Radio advertising $2500 $1400 $3900 (67.95%) 
Newspaper advertising $1250 $589.32 $1839.32 (32.05%) 
Total cost per product $3750 $1989.32 $5739.32 
 
 
 
 Product -1 Product -2  Product-1 & Product-2 
Combined 
Highest Expenditure 
Item 
Radio advertising Radio advertising Radio advertising 
Lowest Expenditure 
Item 
Newspaper advertising Newspaper 
advertising 
Newspaper advertising 
 
 
Q2 (10 points): 
 
Create a Java project that has two classes, the main entry Product (name it as Product.java) and 
Sort (name it as Sort.java) classes. The program reads data from a text file (products.txt), sort 
the data using the selection sort algorithm, and then store the sorted data in a different text file 
(name it as sortedProducts.txt). The data in the text file is originally sorted based on the product 
names; it needs to be sorted based on the product prices, and finally the sorted data is stored in a 
text file. 
 
The Product class has the following methods: 
- The main() method, which prompts the user to input the name of the input file (this may 
include the file path if the file is not stored in the same project folder), creates two arrays 
pName (String[]) and pPrice (double[]) of size 50, and then sequentially calls and passes file 
name (path) pName, pPrice to the static methods, readFromFile(),  sortArrays, and 
writeToFile() 
- readFromFile() is a static method that will read data from the text file “products.txt” 
enclosed with this document, and it stores the product names and product prices in the 
method parameters arrays pName and pPrice, respectively. After complete reading data 
from the file, the method should display a JOPtionPane message dialog of the array 
elements (product names and prices.) In your program, name the input file exactly as 
“products.txt” for consistency.  
- sortArrays() is a static method that passes the parameter arrays pName and pPrice to the 
static  selectionSort() method in the Sort class in order to sort both arrays based on the 
prices in the pPrice array, that means any change in the pPrice array will also results in a 
change in the pName array. You must not use the Array API class methods to sort the data, 
but write the code yourself.  
- writeToFile() is a static method that will write/store the sorted arrays data elements in the 
parameter list (pName and pPrice) into a file (line by line), the data should be stored in the 
“sortedProducts.txt” text file. After completing writing data to file, the method should 
display a JOPtionPane message dialog of the array elements (product names and prices), 
data should be sorted based on the product prices. Name the output file exactly as 
“sortedProducts.txt” for consistency. 
 
The Sort class has only one static selectionSort() method that receives two arrays in its 
parameter list (pName and pPrice) and sorts the arrays by using the selection sort algorithm. It 
sorts the pPrice array in ascending order, so that any change in pPrice array results in a change 
in the pName array, to keep the product name and price elements in both arrays in the same 
order (one way of doing it is having the same index value in array for each pName and pPrice 
array). You must not use the Array API class methods to sort the data but write the code 
yourself.  
 
The Lesson to learn: Arrays are used for formulating output by manipulating a collection of input 
data. Searching and sorting are popular array operations that are performed in most applications. 
Java provides a rich library to do array manipulations.  
 
Following are a few sample screenshots of Q2. There is more than one way to meet the 
programming requirement, and the screenshots shown below are not the only way to meet the 
requirements. Any deviation from the screenshots shown below is acceptable if they meet the 
requirements. It is preferred that the products.txt and the sortedProducts.txt files are stored within 
the project source code directory structure. 
 
 
 
The above screenshot shows the entering of the (input) file products.txt which is stored within the 
project source code (src) under package PA02. You may name the package differently, as you 
wish. 
 
 
 
The screenshot above shows a list of the products before sorting. 
 
 
 
 
The above screenshot shows a partial list of the products after they are sorted. 
 
Again, any deviation to the user interface is left to the discretion and creativity of the students, as 
long as it meets the requirements. 
 
Evaluation Criteria: 
- The programs must compile cleanly (no compile errors, but compile warnings are 
sometimes accepted) 
- The program should handle invalid input data and terminates gracefully  
- The programs should not crash while running, and it should terminate 
- All tasks (requirements) in this assignment must be completed in order to receive credit  
- The correct understanding and implementation (coding) of the requirements (programs 
should behave as anticipated): 
o The programs must terminate with proper/correct outputs 
o All the logical computations should be performed correctly   
 
Submission: (This is an individual Assignment!) 
 Copy the .java source files from the src folder in your workspace to another folder that 
should be named following the provided naming format in this course, then zip and upload the 
file under this assignment answer in Canvas. 
 File Name: FLLLLPA02.zip (F = first letter in your first name and LLLL = your last 
 name)          
 
  
 
Grading Rubric - PA02 
Student Name: ____________________________________________________ 
 
Question 1 
Requirements Any 
comment 
provided by 
grader 
Max 
Points 
Allowed 
Points 
Earned 
General Code Structure: 
Proper naming convention used for file (name the file as 
CostAnalysis.java) (0.25) 
 
Comments used in the code to explain the purpose of the code 
(0.25) 
 
Indentation of the code for better readability (0.25) 
 
Good choice of variable names (0.25) 
 1  
Input, Output, User Interface: 
 
Proper coding implementation of the logic to read the data (1) 
 
Proper coding implementation of displaying the expected output 
(1) 
 
Exception handling of the invalid input values. For example if 
no value is entered, or empty space is entered, or invalid data is 
entered, the program should not crash (2) 
 
Continue to accept use input when the user presses Yes (1) 
 5  
General Algorithm and Logic: 
 
Finding highest expenditure of individual and combined 
product. Please see “Highest Expenditure” row in the 
example table as a reference calculation (1)   
 
Finding lowest expenditure of individual and combined 
product. Please see “Lowest Expenditure” row in the 
example table as a reference calculation (1) 
  
Finding total expenditure of individual and combined 
product. Please see “Total cost per product” row in the 
 4  
example table as a reference calculation (1)  
 
Finding percentage of expenditure of combined product 
costs. Please see “Total cost per expenditure” column in the 
example table as a reference calculation. For example, 
67.95% is the expected answer for Radio Advertising and 
the value of 67.95% is calculated by 3900 (which is the cost 
for Radio Advertising) divided by 5739.32 (which is the 
total cost per product). Thus, 3900/5739.32 = 6795, which 
is 67.95%  (1)  
Total  10  
 
Question 2 
Requirements Any 
comment 
provided by 
grader 
Max 
Points 
Allowed 
Points 
Earned 
General Code Structure: 
Proper naming convention used for file (name them as 
Product.java and Sort.java) (0.25) 
 
Comments used in the code to explain the purpose of the code 
(0.25) 
 
Indentation of the code for better readability (0.25) 
 
Good choice of variable names (0.25) 
 1  
Input, Output, User Interface: 
Proper coding implementation of the logic to read the data from 
the text file products.txt (1.5) 
 
Proper coding implementation of writing the expected data to 
another text file called sortedProducts.txt (1.5) 
 
Exception handling of the invalid input values. For example, if 
no value is entered, or empty space is entered, invalid data is 
entered, or if file to read is not found, the program will not 
crash. the program should not crash (1) 
 
 4  
General Algorithm and Logic: 
 
Use of array of data type String to hold name (0.5) 
 
Use of array of data type double to hold price (0.5) 
 
Proper coding implementation of the methods readFromFile, 
 5  
sortArrays, selectionSort, writeToFile (4) 
Total  10  
Total ____/20