A person keeps a log of daily spending. Write a program SpendingReport.java that
ID: 3857232 • Letter: A
Question
A person keeps a log of daily spending. Write a program SpendingReport.java that reports the statics of the spending for a week. Ask user to enter daily spending within the week. (Day 1, Monday, through day 7, Sunday). Save the information in an array. Calculate the total and the average spending for the week. Count the total days where spending is higher than the average. Print out average, total, number of days higher than the average and the day of the week with highest spending. Sample output: Enter spending day 1 ($): 25 Enter spending day 2 ($): 16.5 Enter spending day 3 ($): 23.5 Enter spending day 4 ($): 12 Enter spending day 5 ($): 0 Enter spending day 6 ($): 42 Enter spending day 7 ($): 31 Total spending: $ Average: $21.43/day Number of days above average spending: 4 Highest spending day: SaturdayExplanation / Answer
import java.util.Scanner;
public class SpendingReport {
public static void main(String[] args)
{
String[] days = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
double[] array = new double[7];//initializing an double array
Scanner sc = new Scanner(System.in);//initializing scanner
for(int i=0;i<7;i++)
{
System.out.println("Enter spending day "+(i+1)+" ($): ");
array[i]=sc.nextDouble();//Getting input from the user for spends
}
//initializing spending report class
SpendingReport sp = new SpendingReport();
//passing array to AverageofArray method and getting average
double average = sp.AverageofArray(array);
//passing array & average to spendAboveaverage method and getting daysAboveaverageSpend
int daysAboveaverageSpend = sp.spendAboveaverage(array, average);
//passing array to dayWithHighestSpend method and getting position
int position = sp.dayWithHighestSpend(array);
System.out.println("Total spending: $ Average: $"+average+"/day");
System.out.println("Number of days above average spending: "+daysAboveaverageSpend);
System.out.println("Highest Spending day: "+days[position]);
}
//method to calculate the average of values present in array
public double AverageofArray(double[] array)
{
double sum =0;
for(int i=0;i<array.length;i++)
{
sum = sum + array[i];
}
sum = sum/7;
return sum;
}
//method to check number of spends above average
public int spendAboveaverage(double[] array, double average)
{
int count =0;
for(int i=0;i<7;i++)
{
if(array[i]>average)
{
count++;
}
}
return count;
}
//method to check day with highest spend
public int dayWithHighestSpend(double[] array)
{
int position = 0;
double largest=0;
//loop to get the largest value
for(int i=1; i< array.length; i++)
{
if(array[i] > largest)
{
largest = array[i];
}
}
//loop to get the position of largest value
for(int i=1; i< array.length; i++)
{
if(largest == array[i])
{
position = i;
}
}
return position;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.