Programming Assignment #9-CarSales Write a Carsales class (Carsales java file) t
ID: 3826711 • Letter: P
Question
Programming Assignment #9-CarSales Write a Carsales class (Carsales java file) that: has 2 attributes: o sales. (private) which is an amay ofintegers. Each element in the array represents the number of sales in a given month of the year. Jan index 0. Dec index 11 year, (public or private) which is the year of the monthly sales. has a Carsales constructor that takes 2 inputs and initializes the instance variables: o the year o an array of integers as input and initializing the sales instance array variable. has a calculateTotalCarsales method with no input to calculate and return the total number of cars sold in year. has a calculateMonthMostSales method with no input to calculate and return the month with most cars sold. has a calculateMonth LeastSales method with no input to calculate and return the month with fewest cars sold. has a calculateAveragesales method with no input to calculate and retum the average cars sold for year. has a getsalesForMonth method with month as in to put return the number of cars sold for month. month is input, output is the number ofcars sold for the specified month. Create a demo program, CarsalesDemo, will Call CarSales constructor to create saleAgentl object for 2016. Pass in the year, 2016, and the agentisales array This will initialize the agentisales object with the year and array of sales for the year. Call CarSales constructor to create saleAgent2 object for 2016. Pass in the year, 2016, and the agentisales array. This will initialize the agent2Sales object with the year and array of sales for the year. Here are the sales for each agent: Year Jan Feb Mar Apr May Jun Jul Aug Sep oet Nov Dec 2016 24 68 Print the month with the most cars sold for salesAgent1 and sales Agent2. Print the month with the fewest cars sold for salesAgentl and salesAgent2 Print the total number of cars sold for the year for sales Agent1 and salesAgent2Explanation / Answer
CarSales.java
public class CarSales {
private int[] sales = new int[12];
private int year;
public CarSales(int year, int[] sales)
{
this.year = year;
this.sales = sales;
}
public int calculateTotalCarSales()
{
int total = 0;
for(int i = 0; i < sales.length; i++)
{
total += sales[i];
}
return total;
}
public int caculateMonthMostSales()
{
int most = 0;
int mostSale = sales[most];
for(int i = 1; i < sales.length; i++)
{
if(mostSale < sales[i])
{
most = i;
mostSale = sales[i];
}
}
return most;
}
public int caculateMonthLeastSales()
{
int least = 0;
int leastSale = sales[least];
for(int i = 1; i < sales.length; i++)
{
if(leastSale > sales[i])
{
least = i;
leastSale = sales[i];
}
}
return least;
}
public double calculateAverageSales()
{
return ((double)calculateTotalCarSales())/sales.length;
}
public int getsalesForMonth(int month)
{
return sales[month];
}
public int getYear()
{
return year;
}
}
CarSalesDemo.java
public class CarSalesDemo {
public static void main(String[] args)
{
int[] agent1Sales = {0,40,43,60,70,80,75,65,45,77,65,90};
int[] agent2Sales = {24,68,23,90,56,94,56,17,92,86,63,78};
CarSales saleAgent1 = new CarSales(2016, agent1Sales);
CarSales saleAgent2 = new CarSales(2016, agent2Sales);
System.out.println("SalesAgent1 total car sales for " + saleAgent1.getYear() + " is " + saleAgent1.calculateTotalCarSales());
System.out.println("SalesAgent2 total car sales for " + saleAgent2.getYear() + " is " + saleAgent2.calculateTotalCarSales());
System.out.println("SalesAgent1 average car sales for " + saleAgent1.getYear() + " is " + saleAgent1.calculateTotalCarSales());
System.out.println("SalesAgent2 average car sales for " + saleAgent2.getYear() + " is " + saleAgent2.calculateTotalCarSales());
System.out.println("SalesAgent1 month with the highest sales " + saleAgent1.getYear() + " is month "
+ (saleAgent1.caculateMonthMostSales() + 1) + " with " + saleAgent1.getsalesForMonth(saleAgent1.caculateMonthMostSales()) + " cars");
System.out.println("SalesAgent2 month with the highest sales " + saleAgent2.getYear() + " is month "
+ (saleAgent2.caculateMonthMostSales() + 1) + " with " + saleAgent2.getsalesForMonth(saleAgent2.caculateMonthMostSales()) + " cars");
System.out.println("SalesAgent1 month with the lowest sales " + saleAgent1.getYear() + " is month "
+ (saleAgent1.caculateMonthLeastSales() + 1) + " with " + saleAgent1.getsalesForMonth(saleAgent1.caculateMonthMostSales()) + " cars");
System.out.println("SalesAgent2 month with the lowest sales " + saleAgent2.getYear() + " is month "
+ (saleAgent2.caculateMonthLeastSales() + 1) + " with " + saleAgent2.getsalesForMonth(saleAgent2.caculateMonthMostSales()) + " cars");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.