Write a simple version of MovingAverage Your program will prompt and input for a
ID: 3771655 • Letter: W
Question
Write a simple version of MovingAverage
Your program will prompt and input for a sequence of monthly profits. Then it will output information about the profits. Develop it incrementally: first code the printing of the profits. Then continue to add features.
Suggestion: I have provided code for some (6) appropriate methods . Look at what is available, and as you write your program look for opportunities to use these methods . Then put this relevant method into your class , and call it as needed. As you type a method in, you should reason through how it works.
An example run. Output of averages should be rounded to the nearest whole value .
Enter the number of months, and then enter those profits (in whole thousands of $)
8 13 -1 5 4 2 8 14 12
==============================
Profits were 13, -1, 5, 4, 2, 8, 14, 12
Average Profit = 7
Minimum Profit = -1
Maximum Profit = 14
Most consecutive profit declines = 2
3-month averages were 6, 3, 4, 5, 8, 11
Average 3-month-avg = 6
Minimum 3-month-avg = 3
Maximum 3-month-avg = 11
Most consecutive 3-month-avg declines = 1
============================================================
/** returns d rounded to the nearest integer */
public static int round(double d){
int ret = (int ) (Math.abs(d)+0.5);
if(d<0) ret = -ret;
return ret;
}
/** The values in b are printed as a comma separated list */
public static void printlnA( int [] b ){
for(int i=0; i<b.length-1; i++) System.out .print(b[i]+", ");
System.out .println(b[b.length-1]); //the last one, with a newline instead of a comma
}
/** returns the rounded average value in b. precondition: b has length>0 */
public static int avg(int [] b){
int sum = 0;
for(int i=0; i<b.length; i++) sum = sum + b[i];
return round( 1.0*sum /b.length );
}
/** returns the maximum value in b. precondition: b has length>0 */
public static int max(int [] b){
int max = b[0];
for(int i=0; i<b.length; i++){
if(b[i]>max) max = b[i];
}
return max;
}
/** returns the minimum value in b. precondition: b has length>0 */
public static int min(int [] b){
// you can do this one
}
/** returns the maximum number of consecutive decreases in b. */
public static int maxDec(int [] b){
int maxD = 0, curD = 0; //max count, and the current count
for(int i=1; i<b.length; i++){ //i=1 is the first one to consider
if( b[i]<b[i-1] ){
curD = curD + 1;
maxD = Math.max(maxD,curD);
}else{
curD = 0;
}
}
return maxD;
}
Explanation / Answer
This code is excuted in Netbeans IDE:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package movingaverage;
import java.util.Scanner;
/**
*
* @author Lucky
*/
public class MovingAverage {
//public int[] b= new int[14];
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number of months : ");
int ArraySize = sc.nextInt();
int[] b = new int[ArraySize];
System.out.println("Enter the profits:");
for(int i = 0; i < ArraySize; i++) {
b[i] = sc.nextInt();
//sum = sum + marks[i];
}
//calling println
System.out.println(""+"========================================");
System.out.println(""+"Profits were:");printlnA(b);
System.out.println(""+"Average profit:"+avg(b));
System.out.println(""+"Minimum profit:"+min(b));
System.out.println(""+"Maximum profit:"+max(b));
System.out.println(""+"Most consecutive profit declines:"+maxDec(b));
}
public static void printlnA( int [] b ){
for(int i=0; i<b.length-1; i++) System.out .print(b[i]+", ");
System.out .println(b[b.length-1]); //the last one, with a newline instead of a comma
}
/** returns d rounded to the nearest integer */
public static int round(double d){
int ret = (int ) (Math.abs(d)+0.5);
if(d<0) ret = -ret;
return ret;
}
/** returns the rounded average value in b. precondition: b has length>0 */
public static int avg(int [] b){
int sum = 0;
for(int i=0; i<b.length; i++) sum = sum + b[i];
return round( 1.0*sum /b.length );
}
/** returns the maximum value in b. precondition: b has length>0 */
public static int max(int [] b){
int max = b[0];
for(int i=0; i<b.length; i++){
if(b[i]>max) max = b[i];
}
return max;
}
/** returns the minimum value in b. precondition: b has length>0 */
public static int min(int [] b){
// you can do this one
int min = b[0];
for(int i=0; i<b.length; i++){
if(b[i]<min) min = b[i];
}
return min;
}
/** returns the maximum number of consecutive decreases in b. */
public static int maxDec(int [] b){
int maxD = 0, curD = 0; //max count, and the current count
for(int i=1; i<b.length; i++){ //i=1 is the first one to consider
if( b[i]<b[i-1] ){
curD = curD + 1;
maxD = Math.max(maxD,curD);
}else{
curD = 0;
}
}
return maxD;
}
}
OUT PUT:
run:
Enter Number of months :
5
Enter the profits:
14 -12 5 6 1
========================================
Profits were:
14, -12, 5, 6, 1
Average profit:3
Minimum profit:-12
Maximum profit:14
Most consecutive profit declines:1
BUILD SUCCESSFUL (total time: 21 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.