Hello, I need help with a problem. I will post a program that has introduced us
ID: 3853865 • Letter: H
Question
Hello, I need help with a problem. I will post a program that has introduced us to UMF and using a tester for our code. This code is called Rectangle and my professor would like use to do a similiar code but instead doing it on a Circle. I will show the UMF for the rectangle and then the code, and what i need is a code for a circle, which i will post the UMF diagram for the circle as well at the bottom. Thank you! (circle that grows in size to a certain size, and then shrinks back.)
Example (Rectangle):
public class Rectangle
{
private double width;
private double height;
public Rectangle()
{
width = 1;
height = 1;
}
public Rectangle(double w, double h)
{
width = w;
height = h;
}
//mutators or setters
public void setWidth(double newWidth)
{
width = newWidth;
}
public void setHeight(double newHeight)
{
height = newHeight;
}
//accessor or getters
public double getWidth()
{
return width;
}
import java.util.*;
public class RectangleDriver
{
public static void main(String[] args)
{
Rectangle r2 = new Rectangle(2,3);
//double area = r2.getArea();
//Rectangle r3 = new Rectangle(6,7);
//boolean result = r2.equals(r3);
//System.out.println(r);
Rectangle[] r = new Rectangle[2];
fillArray(r);
printArray(r);
}
public static void printArray(Rectangle[] r)
{
for (int i = 0; i < r.length; i++)
{
System.out.println(r[i].toString());
}
}
public static void fillArray(Rectangle[] recs)
{
Scanner kb = new Scanner(System.in);
for (int i = 0; i < recs.length; i++)
{
System.out.println("Enter the width: ");
double w = kb.nextDouble();
System.out.println("Enter the height: ");
double h = kb.nextDouble();
Rectangle r = new Rectangle(w,h);
recs[i] = r;
}
}
}
-------------------------------------------------------------------------------------
WHAT I NEED (CIRCLE):
-width: Double - height: double + Rectangle 0 +Rectangle (double w, double h) +get width 0 double +get height 0 double +set width 0 double w +set height O: doubleh ToString QiString + get Area 0 double + get perimeter 0: doubleExplanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Circle
{
private double radius;
public Circle()
{
radius=1.0;
}
public Circle(double r)
{
radius=r;
}
public double getter()
{
return radius;
}
public void setter(double r)
{
radius=r;
}
public double getArea()
{
return 3.14*radius*radius;
}
public String toString()
{
return "Area= "+getArea();
}
public double getCircumference()
{
return 2*3.14*radius;
}
public double getDiameter()
{
return 2*radius;
}
public void expand(double amount)//Function which increases the radius of circle by the given amount
{
radius+=amount;
}
public void shrink(double amount)//Function which decreases the radius of circle by the given amount
{
radius-=amount;
}
public static void main (String[] args) throws java.lang.Exception
{
Circle ob1=new Circle(6);
System.out.println("Area of circle with radius "+ob1.getter()+" is "+ob1);
ob1.expand(2);
System.out.println("After expanding the radius of circle to "+ob1.getter()+" , "+ob1);
ob1.shrink(2);
System.out.println("After shrinking the radius of circle to "+ob1.getter()+" , "+ob1);
System.out.println("Circumference of circle with radius "+ob1.getter()+" is "+ob1.getCircumference());
System.out.println("Diameter of circle with radius "+ob1.getter()+" is "+ob1.getDiameter());
}
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.