Write a java console application for a lawn-mowing service for N lawns. N should
ID: 3919123 • Letter: W
Question
Write a java console application for a lawn-mowing service for N lawns. N should be declared as a constant and it should be equal to the largest digit of your student ID number (e.g. if your ID number is S143261 then N should be equal to 6 and you can declare it as final int N=6). The weekly fee for mowing a lawn is shown below in Table 1. Table 1. Weekly Fee for Lawn Mowing Lawn Area Weekly Fee ($) Lawn under 500 square meter 40 500 square meter or more but under 900 square meter 60 900 square meter or more but under 1500 square meter 75 1500 square meter or over 100 The application should ask the user to enter the area for each lawn (total N lawns) and calculate the weekly fee for lawn mowing. The application should display the fee for each lawn as shown in the example below. At the end of the Nth lawn, the details such as total weekly fee, lowest weekly fee, highest weekly fee, and lawn number with highest monthly fee should be displayed. The lowest fee is calculated by finding the lowest value from weekly fee for N lawns. The highest monthly fee is calculated by finding the highest value from weekly fee for N lawns. The average lawn moving fee is calculated by dividing the total weekly fee with the total number of lawns. The monthly fee is calculated by multiplying the weekly fee by 4. Example for N=4 Enter the area for lawn 1: 450 The weekly fee for lawn 1 is $40 Enter the area for lawn 2: 600 The weekly fee for lawn 2 is $60 Enter the area for lawn 3: 1200 The weekly fee for lawn 3 is $75 Enter the area for lawn 4: 3000 The weekly fee for lawn 4 is $100 --------------------------------------------------Report----------------------------------------------------- Total weekly lawn mowing fee: $275 Lowest weekly lawn mowing fee: $40 Highest weekly lawn mowing fee: $100 Average weekly lawn mowing fee: $68.75 Lawn number with highest monthly fee: 4 ------------------------------------------------------------------------------------------------------------------- Your application is to follow the same format for input and output as in the example above, but with customised welcome and exit messages. The application is to use the following classes. public class Lawn { public Lawn() { // constructor } public double lawnMowingFee(double area) { // code to calculate lawn mowing fee goes here } public int lawnNumber(double fee) { // code to determine lawn number with highest monthly fee } } public class LawnTest { public static void main(String[] args) { // local variable declarations go here // display welcome message // loop to input area, calculate and display fee // generate and display report // display exit message } }
Explanation / Answer
ScreenShot
-------------------------------------------------------------------------
Program
/* This program is to calculate lawn moving charge
* Use a class lawn to create lawn moving charges of give area
* use variable N as the count of lawns to determine charges*/
//Packages for input read and sort
import java.util.Arrays;
import java.util.Scanner;
//Lawn class create each lawn and calculate fee
class Lawn {
//Member variables
double area;
double fee;
// constructor
public Lawn() {
area=0.0;
fee=0.0;
}
// code to calculate lawn mowing fee goes here
public double lawnMowingFee(double area) {
if(area<500) {
fee=40;
}
else if(area>=500 && area<900) {
fee=60;
}
else if(area>=900&& area<1500) {
fee=75;
}
else if(area>=1500) {
fee=100;
}
return fee;
}
}
//Test class
public class LawnTest {
public static void main(String[] args) {
// local variable declarations go here
final int N = 4;
double area=0.0,total=0.0,fee=0.0,highest=0.0;
int lawnNumber=0;
double[] feeArray=new double[N];
Scanner sc=new Scanner(System.in);
// display welcome message
System.out.println("**********Welcome To Lawn Mowing Fee Calculation Report**********");
// loop to input area, calculate and display fee
for(int i=0;i<N;i++) {
System.out.print("Enter the area for lawn "+(i+1)+": ");
area=sc.nextDouble();
Lawn l=new Lawn();
fee=l.lawnMowingFee(area);
total+=fee;
feeArray[i]=fee;
System.out.println("The weekly fee for lawn "+(i+1)+" is $"+fee);
}
//Find highest lawn number
for(int i=0;i<N;i++) {
if(feeArray[i]>highest) {
highest=feeArray[i];
lawnNumber=i+1;
}
}
// generate and display report // display exit message
System.out.println("-----------------Report-------------------");
System.out.println("Total weekly lawn mowing fee: $"+total);
Arrays.sort(feeArray);
System.out.println("Lowest weekly lawn mowing fee: $"+feeArray[0]);
System.out.println("Highest weekly lawn mowing fee: $"+feeArray[N-1]);
System.out.println("Average weekly lawn mowing fee: $"+(total/N));
System.out.println("Lawn number with highest monthly fee: "+lawnNumber);
System.out.println("-----------------Thank You--------------------");
}
}
------------------------------------------------------------------
Output
**********Welcome To Lawn Mowing Fee Calculation Report**********
Enter the area for lawn 1: 450
The weekly fee for lawn 1 is $40.0
Enter the area for lawn 2: 600
The weekly fee for lawn 2 is $60.0
Enter the area for lawn 3: 1200
The weekly fee for lawn 3 is $75.0
Enter the area for lawn 4: 3000
The weekly fee for lawn 4 is $100.0
-----------------Report-------------------
Total weekly lawn mowing fee: $275.0
Lowest weekly lawn mowing fee: $40.0
Highest weekly lawn mowing fee: $100.0
Average weekly lawn mowing fee: $68.75
Lawn number with highest monthly fee: 4
-----------------Thank You--------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.