Please help in Java: You need to use a Circle (for the flower bed) and a Rectang
ID: 3782119 • Letter: P
Question
Please help in Java:
You need to use a Circle (for the flower bed) and a Rectangle (for the yard).
This program calculates the cost of putting a fence
all the way around a rectangular yard AND putting sod everywhere in the yard
EXCEPT for a circular flower bed.
Input consists of the length and width of the yard and the radius of the
flower bed (all double and all in feet).
The cost of fencing (a constant) is $2.50 per foot and the cost of sod is
$1.50 per square foot (another constant).
Fencing can only be purchased in one foot sections,
and sod can only be purchased by the square foot. (Remember to use ceil)
If you use this input:
length 6.2, width 3.4, radius 1.2
Then cost of fence is $50, cost of sod is $25.5, total cost is $75.5
This is what I have so far:
public static class Rectangle{
private double length;
private double width;
public void setLength(double l){
length=l;
}
public void setWidth(double w){
width=w;
}
public double areaRectangle(){
return length*width;
}
}
public static class Circle{
private double radius;
public Circle(){
radius=0.0;
}
public Circle(double r){
radius=r;
}
public void setRadius(double r){
radius=r;
}
public double areaCircle(){
return Math.PI*radius*radius;
}
}
public static void main(String[] args) {
//declare Scanner
Scanner scnr =new Scanner (System.in);
//prompt user for length,width,radius
System.out.println("Please enter the length of the yard: ");
double length = scnr.nextDouble();
System.out.println("Please enter the width of the yard: ");
double width = scnr.nextDouble();
System.out.println("Please enter the radius of the flower bed: ");
double radius = scnr.nextDouble();
//System.out.println(calc(length,width,radius));
}
public static double calc (int a, int b, int c, double areaCircle, double areaRectangle){
double fCost = 2.50;
double sCost = 1.50;
/*declare pi
//double pi = 3.14159;
//calc area of circle and rectangle
double area = a * b;
double areaofCircle = Math.pow(c, 2) * pi;*/
//calc fence cost
fCost = Math.ceil(fCost * ( 2 * a) + (2 * b));
//calc sod cost
sCost = Math.ceil(sCost * (areaRectangle - areaCircle));
//calc total cost
double tCost = sCost + fCost;
//results
//System.out.println("The cost of the fence is: " + fCost);
//System.out.println("The cost of sod is: " + sCost);
//System.out.println("The total cost is: " + tCost);
return tCost;
}
}
Thanks!!
Explanation / Answer
Rectangle.java
public class Rectangle {
//Declaring variables
private double length,width;
//Parameterized constructor
public Rectangle(double length, double width) {
super();
this.length = length;
this.width = width;
}
//Getters and setters
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
//This method will calculate the perimeter of the Rectangle and return it.
public double getPerimeter()
{
return Math.ceil(2*(length+width));
}
//This method will calculate the area of the Rectangle and return it.
public double getArea()
{
return length*width;
}
}
_____________________
Circle.java
public class Circle {
//Declaring variable
private double radius;
//Parameterized constructor
public Circle(double radius) {
super();
this.radius = radius;
}
//Setters and getters
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//This method will calculate the area of the Circle and return it
public double getArea()
{
return Math.PI*radius*radius;
}
}
_____________________
RectangleCircularBed.java
import java.util.Scanner;
public class RectangleCircularBed {
public static void main(String[] args) {
//Declaring constants
final double fence_cost_perfoot=2.50,sod_cost_perfoot=1.50;
//Declaring variables
double length,width,radius,cost_of_fence,cost_of_sod,total_cost;
double perimeter_of_rect,area_of_rect,area_of_circle,area_of_rect_except_circleBed;
//Scanner class object is used to read the inouts entered by the user
Scanner sc=new Scanner(System.in);
//Getting the length entered by the user
System.out.print("Enter the length(in feet) :");
length=sc.nextDouble();
//Getting the width entered by the user
System.out.print("Enter the width(in feet) :");
width=sc.nextDouble();
Rectangle r=new Rectangle(length, width);
//Getting the radius entered by the user
System.out.print("Enter the radius(in feet) :");
radius=sc.nextDouble();
Circle c=new Circle(radius);
//Calculating the perimeter of the rectangle
perimeter_of_rect=r.getPerimeter();
//Calculating the cost of the fencing of the rectangle
cost_of_fence=perimeter_of_rect*fence_cost_perfoot;
//Displaying the cost of fencing of the rectangle
System.out.println("Cost of fence is $"+cost_of_fence);
//Calculating the area of the circle
area_of_circle=c.getArea();
//Calculating the area of the rectangle
area_of_rect=r.getArea();
//Calculating the area of the rectangle except the circular bed
area_of_rect_except_circleBed=Math.ceil(area_of_rect-area_of_circle);
//calculating the cost of the sod
cost_of_sod=area_of_rect_except_circleBed*sod_cost_perfoot;
//Displaying the cost of the sod
System.out.println("Cost of sod is $"+cost_of_sod);
//calculating the total cost
total_cost=cost_of_fence+cost_of_sod;
//Displaying the total cost
System.out.println("Total cost is $"+total_cost);
}
}
_______________________
Output:
Enter the length(in feet) :6.2
Enter the width(in feet) :3.4
Enter the radius(in feet) :1.2
Cost of fence is $50.0
Cost of sod is $25.5
Total cost is $75.5
___________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.