Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.Scanner; class demo public static void main(String[l args) fina

ID: 3752672 • Letter: I

Question

import java.util.Scanner; class demo public static void main(String[l args) final double PI 3.14159; Scanner scan new Scanner (System.in); System.out.print("Enter the top-left coordinates for the rectangular portion: "); int x1 - scan.nextlnt(); int y1 scan.nextlnt) System.out.print("Enter the bottom-right coordinates for the rectangular portion: "); int x2 = scan.nextlnt(); int y2 - scan.nextlnt(); System.out.print("Enter the radius of the circular portion of the field: "); int radius scan.nextlnt) int side1 x2 - x1; int side2y1 - y2; int perimeter 2*side1 + 2*side2; double circumference - 2 * PI * radius; double fenceLength-perimeter 2 * radius 3.0/4 * circumference; System.out.println("You will need "+ fenceLength" meters of fence around this field."); int rectangleArea-side1 * side2; double circleArea -Pl * radius * radius; double area rectangleArea 3.0/4*circleArea; System.out.println("You have to cover" area + " square meters with grass."); double nbBags area /112; int nbBagsAslnt - (int) nbBags; nbBagsAslnt - nbBagsAslnt 1; System.out.println("You will need "nbBagsAslnt "bags of grass seeds for this."); int cost - nbBagsAslnt * 14; System.out.println("The total cost for seeds will be $" cost "before tax.");

Explanation / Answer

Program:

import java.io.*;
import java.util.*;
import java.text.DecimalFormat;

public class Solution {
public static void main(String[] args) {
//get input
Scanner in = new Scanner(System.in);
System.out.print("Enter the coordinates for the rectangular portion, as <X1,Y1>-<X2,Y2>:");
String points[] = in.nextLine().split("-");
points[0] = points[0].substring(1,points[0].length()-1);
points[1] = points[1].substring(1,points[1].length()-1);
String p1[] = points[0].split(",");
String p2[] = points[1].split(",");
int x1 = Integer.parseInt(p1[0]);
int y1 = Integer.parseInt(p1[1]);
int x2 = Integer.parseInt(p2[0]);
int y2 = Integer.parseInt(p2[1]);
  
int side1 = Math.abs(x2-x1);
int side2 = Math.abs(y2-y1);
  
//calculate random radius between 2 and side2/2
double r = Math.random();
int minR = 2;
int maxR = side2/2;
int radius = (int)(r*(maxR-minR)) + minR;
System.out.println(" The random radius is "+radius);
  
//decimal format
DecimalFormat df3 = new DecimalFormat("###.###"); //for area and perimeter, 3 decimal place
DecimalFormat df2 = new DecimalFormat("###.00"); //for cost, 2 decimal place
  
//perimeter
int perimeter = 2*side1 + 2*side2;
double circumference = 2*Math.PI*radius;
double fenceLength = perimeter - 2*radius + (3.0/4)*circumference;
System.out.println("You will need "+df3.format(fenceLength)+" meters of fense around this field.");
  
//area
int rectangleArea = side1*side2;
double circleArea = Math.PI*radius*radius;
double area = rectangleArea + (3.0/4)*circleArea;
System.out.println("You have to cover "+df3.format(area)+" square meters with grass.");
  
//bags
double nbBags = area/112;
int nbBagsAsInt = (int)Math.ceil(nbBags);
System.out.println("You will need "+nbBagsAsInt+" bags of grass seeds for this.");
  
//cost
int cost = nbBagsAsInt*14;
System.out.println("The total cost for seeds will be $"+df2.format(cost)+" before tax.");
}
  
}

Sample output: