Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This question deals with the simple java programming techniques and use of get-set method and arrays. Basically, this question is divided in three parts and

This question deals with the simple java programming techniques and use of get-set method and arrays. Basically, this question is divided in three parts and the main two class files we have to make are Expenses.java and expensesDemo.java. The question seems pretty big but the code is very simple just have to use arrays, correct programming techniques and please read the whole question first and the important points:-

Important points: -

1) Starting code is given for this project and so I had attached two files expenses.java and expensesDemo.java at last so modify that code or make a new code according to your preference.

2) Image for output for each part is also provided. So, please check it.

3) Please read whole question (all parts) first as in the last part you have to overwrite the second part, however if you directly know that last part you can jump over to that because at last I just need two file that is expenses.java and expensesArrayDemo.java that should satisfy all condition given in third (last) part.

4) MalonesCones.txt file is also attached with this question. So copy paste that content and make new.txt file as I cant attach .txt file here

5) Although the sample code is given to start but I am still attaching the image of question so if you want you can change that code too. image is as ( Sample.PNG and code is provided at end of this doc)

Part 1:

Use the two java files, Expenses.java and ExpensesDemo.java (copy and pasted at the end of question) . Import the two java files into your project and then:

a. Add the following data field to the Expenses class Month - the name of the month for the expenses [a string]

b. Add accessor and mutator methods for this new data field to the Expenses class

c. Add a constructor to the Expenses class that takes a single argument (the name of the month) and sets all other data fields to 0

d. In the expensesDemo source code add Java code to do the following:

i. Obtain the name of the month for which expenses will be entered; instantiate an Expenses object (using the constructor you created in part c) for the month; enter sales and expenses data for each week of the month; display the month name, balance, the total of income, total of expenditure, and Keleven from the month's expense object.

ii. Set up a loop so that multiple months data may be entered by the user (the following screen shot is an example execution of the program to assist your understanding of requirements). Note: for the purpose of this question there is no need to 'remember' all Expenses objects that are created, hence, an array is not needed.

THE SAMPLE OUTPUT FOR THIS CODE SHOULD BE LIKE:- OUTPUT 1.PNG

Expand image

PART 2:

User has realised that it would be easier for him to be able to enter the weekly sales and expenses figures for each month into a text file that your program could then process.

a) Modify your program (ExpensesDemo.java) to read the data from the file MalonesCones.txt( Given an the end of this program) instead of from the keyboard (ie, your program will no longer need to ask the user for the month name, number of weeks in the month or the weekly sales and expenses values since these are now in the text file) instantiating an Expenses object for each month that is read from the file. Note: the code in the Expenses class should not need to be modified. An example of the file MalonesCones.txt has been included in this week's practical zip file.

b) MalonesCones.txt contains data in the following format (suggestion: open the file in a text editor to confirm that you understand the structure and organisation of the data in the file):

c) Use the example input file to test your code. Make sure that your Java program checks that the file exists before attempting to read from it. If the file does not exist then handle this situation by informing the user that the file does not exist and then obtain a new file name/location from the user. You should also ensure that your code works correctly for different length files (ie, the input files used for marking your program may contain more or less lines of text than the example file provided).

d) The program will need to generate the on-screen report for Kevin as per the following example output:

THE OUTPUT SHOULD BE LIKE OUTPUT2.PNG

e) The program will need to generate a text file (expenseSummary.txt) containing the following information for each month that was processed from the MalonesCones.txt file:

Month Name

Month Balance

Total Sales

Total Expenses

keleven (User)

Each month's data is to be on a separate line with each data value separated by a comma. Hence, the expenseSummary.txt file should contain data in the following format:

February,-150.00,1125.00,1275.00,150.00

March,25.00,1325.00,1300.00,0.00

April,-256.80,1103.05,1359.85,256.80

This file format is commonly known as a CSV file (comma separated value).

PART 3:

Import a copy of the two java files, Expenses.java and ExpensesDemo.java that you developed in your project . You should also use the MalonesCones.txt file to test this program.

A second version of MalonesCones.txt (containing more months) is also provided ( Attached as Malones2 in this project) and do the following:-

a. Modify your solution to use an array of Expenses objects. That is, each month of data that is read from the file will be stored as an Expenses object in the array. Your array size should be 12 to allow for expenses for up to one year. You cannot assume that there will always be 12 months of values in the file (there may be more or less).

b. The program should create the on-screen report as per OUTPUT 2 but the report is to be generated by processing the array rather than processing the input file.

c. Add a new method to your program to calculate the average (mean) of monthly Kelevens (name of user) from the array of Expenses objects.

Display the average Keleven as shown in figure 1. (Attached as Figure 1.PNG)

The program should create the text file as per part e in part 2 but this file is to be generated by processing the array rather than processing the input file. Test the program with different sized files with different sets of numbers. Make sure your code works correctly if the file isn't found, if it exists but doesn't have any data in it, or, if the file has too many values to fit into the array. Your solution must incorporate appropriate methods utilizing appropriate parameter passing

ATTACHMENTS:

  • Sample PNG the question which I already solved but I am proving the question detail and the solution is also below which is Expenses.java and expensesDemo.java.

Expand image

THE TWO SAMPLE CODE :

1) expense.java :-

import java.lang.Math;

public class Expenses {

private double monthlyBalance = 0;

private double totalIncome = 0;

private double totalExpenditure = 0;

public void setMonthlyBalance(double monBalance) {

monthlyBalance = monBalance;

}

public double getMonthlyBalance() {

return monthlyBalance;

}

public double getTotalIncome() {

return totalIncome;

}

public double getTotalExpenditure() {

return totalExpenditure;

}

public void addAmount(double sales) {

setMonthlyBalance(monthlyBalance + sales);

}

public void subtractAmount(double expenditure) {

setMonthlyBalance(monthlyBalance - expenditure);

}

public void totalIncome(double sales) {

totalIncome += sales;

}

public void totalExpenditure(double expenditure) {

totalExpenditure += expenditure;

}

public double Keleven() {

if(monthlyBalance > 0)

return 0;

else

return Math.abs(monthlyBalance);

}

}

2) expensesdemo.java

import java.util.Scanner;

public class expensesDemo {

public static void main(String[] args) {

int n;

double sales, expenditure;

Scanner scan = new Scanner(System.in);

Expenses accountant = new Expenses();

System.out.print("How many weeks this month?t");

n = scan.nextInt();

System.out.println();

for(int i = 0; i < n; i++) {

System.out.print("Sales & Expenditure for week " + (i+1) + (": "));

sales = scan.nextDouble();

expenditure = scan.nextDouble();

accountant.addAmount(sales);

accountant.subtractAmount(expenditure);

accountant.totalIncome(sales);

accountant.totalExpenditure(expenditure);

}

System.out.println("nMonth Balance:t$" + accountant.getMonthlyBalance());

System.out.println("Total Sales:t$" + accountant.getTotalIncome());

System.out.println("Total Expenses:t$" + accountant.getTotalExpenditure());

System.out.println("Keleven:t$" + accountant.Keleven());

}

}

3) THE TXT FILE :- MALONESCONES.TXT

February 4

200 300

300 450

350 250

275 275

March 5

200 200

150 150

350 400

400 350

225 200

April 5

352.35 400.10

120.50 354.75

195 120.00

200.20 365

235 120

PLEASE LET ME KNOW IF YOU HAVE ANY MORE QUESTION AS MOSTLY EVERYTHING IS SPECIFIED. TRY TO BRING THE LAST PART OUTPUT AND I NEED TWO FILES EXPENSES.JAVA AND EXPENSESARRAYDEMO.JAVA

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago