My program is below and it works great but I need my output to match this Test R
ID: 3631110 • Letter: M
Question
My program is below and it works great but I need my output to match this Test Run:Test Run Output
This program will calculate the fine your city may pay based on carbon foot print
The fine index is as follows:
If Average Carbon foot print(CFP) per month is <= 1 no fine
if CFP>1 and CFP<= 3 Fine = $1000000.0
if CFP>3 and CFP<= 5 Fine = $2000000.0
if CFP>5 and CFP<= 7 Fine = $3000000.0
if CFP> 7 Fine = $4500000.0
Enter the value of Carbon Foot Print[whole number only] or enter -1 to exit.
3
Enter the value of Carbon Foot Print [whole number only] or enter -1 to exit.
2
Enter the value of Carbon Foot Print [whole number only] or enter -1 to exit.
1
Enter the value of Carbon Foot Print [whole number only] or enter -1 to exit.
3
Enter the value of Carbon Foot Print [whole number only] or enter -1 to exit.
5
Enter the value of Carbon Foot Print [whole number only] or enter -1 to exit.
-1
Sum of all Carbon foot print values is = 14
The total number of readings is = 5
The real average carbon footprint = 2.8
Possibly to benefit you, the rounded down averaged value of carbon footprint = 2
Now we will compute your fine based on rounded average value and fine table.
Your fine is $1000000.0
End of program
Basically I don't know how to add in the "Sum of all Carbon foot print values", the "Total number of readings", " The real average carbon footprint", and "The rounded down averaged value of carbon footprint" and be able to print those outputs.
Here is what I have so far:
import java.util.Scanner;
public class CarbonFootprint
{
public static void main(String args[])
{
//scanner object for reading input
Scanner scan = new Scanner(System.in);
int counter = 0;//to hold number values entered
double sum = 0;//to compute sum
double value;//to read value
double average = 0;//calculate average
int roundedAverage;//to store rounded average
double fine;//to store calculated fine
System.out.println("This program will calculate the fine your city may pay based on carbon foot print");
System.out.println("The fine index is as follows:");
System.out.println("If Average Carbon foot print(CFP) per month is <= 1 no fine");
System.out.println("if CFP>1 and CFP<= 3 Fine = $1000000.0");
System.out.println("if CFP>3 and CFP<= 5 Fine = $2000000.0");
System.out.println("if CFP>5 and CFP<= 7 Fine = $3000000.0");
System.out.println("if CFP>7 Fine = $4500000.0");
//prompting user for input
System.out.println("Enter carbon footprint values (to quit -1)");
//reading value
value = scan.nextDouble();
//until the value is negative
while(value>=0)
{
sum +=value;
counter ++;
//continue reading
value=scan.nextDouble();
}
//calculating average
average = sum/counter;
//round the average
roundedAverage = (int)Math.floor(average);
//calculating fine using if else
if(roundedAverage<=1)
fine = 0;
else if(roundedAverage>1&&roundedAverage<=3)
fine=1000000;
else if(roundedAverage>3&&roundedAverage<=5)
fine=2000000;
else if(roundedAverage>5&&roundedAverage<=7)
fine=3000000;
else
fine = 4500000;
//displaying fine amount
System.out.println("The City has to pay as fine : $"+fine);
}//end of main
}//end of class
Explanation / Answer
import java.util.Scanner;
public class CarbonFootprint
{
public static void main(String args[])
{
//scanner object for reading input
Scanner scan = new Scanner(System.in);
int counter = 0;//to hold number values entered
double sum = 0;//to compute sum
double value;//to read value
double average = 0;//calculate average
int roundedAverage;//to store rounded average
double fine;//to store calculated fine
System.out.println("This program will calculate the fine your city may pay based on carbon foot print");
System.out.println("The fine index is as follows:");
System.out.println("If Average Carbon foot print(CFP) per month is <= 1 no fine");
System.out.println("if CFP>1 and CFP<= 3 Fine = $1000000.0");
System.out.println("if CFP>3 and CFP<= 5 Fine = $2000000.0");
System.out.println("if CFP>5 and CFP<= 7 Fine = $3000000.0");
System.out.println("if CFP>7 Fine = $4500000.0");
// loop for input
//until the value is negative
while(true)
{
//prompting user for input
System.out.println("Enter the value of Carbon Foot Print [whole number only] or enter -1 to exit.");
//reading value
value = scan.nextDouble();
// check if user enters -1
if(value < 0)
break;
sum +=value;
counter ++;
//continue reading
}
//calculating average
average = sum/counter;
//round the average
roundedAverage = (int)Math.floor(average);
//calculating fine using if else
if(roundedAverage<=1)
fine = 0;
else if(roundedAverage>1&&roundedAverage<=3)
fine=1000000;
else if(roundedAverage>3&&roundedAverage<=5)
fine=2000000;
else if(roundedAverage>5&&roundedAverage<=7)
fine=3000000;
else
fine = 4500000;
// output
System.out.println("Sum of all Carbon foot print values is = "+sum);
System.out.println("The total number of readings is = "+counter);
System.out.println("The real average carbon footprint = "+average);
System.out.println("Possibly to benefit you, the rounded down averaged value of carbon footprint = "+roundedAverage);
System.out.println("Now we will compute your fine based on rounded average value and fine table.");
System.out.println("Your fine is $"+String.format("%.2f", fine));
System.out.println("End of program");
}//end of main
}//end of class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.