Math from class isn't carrying in to object - java

I have to make a program that has the Circle class which calculates the area and circumference of a circle by accepting a parameter for r from a new circle object in main. I fed it the radius value of 14. This is what it should output:
***** Circle *****
radius 14.0
area 615.75164
circumference 87.96452
But instead I'm getting this
***** Circle *****
radius 14.0
area 0.0
circumference 0.0
Why is this happening and how can I fix it? I was given the overall shell of the code to make work in java and I have to keep it for the most part how it was presented to me, so while I appreciate the intention of giving recommendations on better ways to structure things and make this program in general, the teacher wants me to do it this way. The only thing I'm not sure of is what to do with setRadius(), and whether or not I should have declared r at the top of the class.
package shapedemo;
class Circle {
private double radius;
private double circumference;
private double area;
private double r;
// constructors
public Circle(){
radius = 1;
}
public Circle(double r) {
radius = r;
}
// setters and getters
public void setRadius(double radius){
this.radius = radius;
}
public double getRadius(){
return radius;
}
// other methods
public double calcArea() {
area = Math.PI * r * r;
return area;
}
public double calcCircumference(){
circumference = Math.PI * 2 * r;
return circumference;
}
// display method
public void display() {
/*blank line
***** Circle *****
radius nnnn.nn
area nnnn.nn
circumference nnnn.nn
blank line
*/
System.out.println("\n***** Circle *****\nradius " + radius + "\narea " + area + "\ncircumference " + circumference + "\n");
}
}
public class ShapeDemo {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Circle circle1 = new Circle(14);
circle1.display();
}
}

Your area and circumference variables are only being set if you call the appropriate calculation methods (calcArea and calcCircumference). You're not calling them, so they have the default values of 0.
You could fix this in your main method:
public static void main(String[] args) {
Circle circle = new Circle(14);
circle.calcArea();
circle.calcCircumference();
circle.display();
}
... although you'd also have to fix your methods to use radius instead of r, as you're never setting r.
Personally I think it would be better to either move the logic into your constructor or calculate it on demand with methods rather than having fields at all for them.
First approach, in the constructor:
final class Circle {
private final double radius;
private final double circumference;
private final double area;
public Circle() {
this(1); // Delegate to other constructor
}
public Circle(double r) {
radius = r;
area = Math.PI * radius * radius;
circumference = Math.PI * 2 * radius;
}
public void display() {
System.out.println(
"\n***** Circle *****" +
"\nradius " + radius +
"\narea " + area +
"\ncircumference " + circumference + "\n");
}
}
Second approach, making methods compute the values:
final class Circle {
private final double radius;
public Circle() {
this(1); // Delegate to other constructor
}
public Circle(double r) {
radius = r;
}
public double getRadius() {
return radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getCircumference() {
return Math.PI * 2 * radius;
}
public void display() {
System.out.println(
"\n***** Circle *****" +
"\nradius " + radius +
"\narea " + getArea() +
"\ncircumference " + getCircumference() + "\n");
}
}
In both cases, I've made this an immutable type - while you can keep it mutable, in the first case that would mean recomputing the area and circumference. In that case you'd probably want your constructor to just call setRadius() and make that method do all the computations.
For example:
final class Circle {
private double radius;
private double circumference;
private double area;
public Circle() {
this(1); // Delegate to other constructor
}
public Circle(double r) {
setRadius(r);
}
public void setRadius(double r) {
radius = r;
area = Math.PI * radius * radius;
circumference = Math.PI * 2 * radius;
}
public double getRadius() {
return radius;
}
public double getArea() {
return area;
}
public double getCircumference() {
return circumference;
}
public void display() {
System.out.println(
"\n***** Circle *****" +
"\nradius " + radius +
"\narea " + area +
"\ncircumference " + circumference + "\n");
}
}

You never call to calcArea() and calcCircumference(), so circumference and area have the intial 0 value.
Circle circle1 = new Circle(14);
circle1.calcArea();
circle1.calcCircumference();
circle1.display();
Or change your constructor to
public Circle(double r) {
radius = r;
calcArea();
alcCircumference();
}

You're not calling the calcArea and calcCircumference so they retain the default value 0.0d
this should be your public static void main()
public static void main(String[] args) {
Circle circle1 = new Circle(14);
circle1.calcArea();
circle1.calcCircumference();
circle1.display();
}
Also, change your return type of calcArea() and calcCircumference() to void

Related

Size comparisons (circumference/square perimeter) etc

3 diffrent classes 1 for handling Circle isntances ,1 for Square instances and the 3rd for comparrisons between them(main) . In the main function i find the circle (between c1..c4) and square (between s1...s5) and print the biggest circumference and area of them respectively.[so circle-circle and square-square comparison]
!!!! NOTE : Only the ones with the biggers radius or sides have the biggest circumference or area , so i only use r and a for comparisons.i dont know if its possible to return this if i use the area/circumference method(no , cause then i will only handle numbers ?).Correct me please.
Now i want to print the characteristics(x,y,r/a) of the geometric shape (circle/square) with the biggest perimeter. How can i do this ? Where do i compare?New class?[square-circle comparison]
public class Circle {
public double x,y,r;
public double circumference() {
return 2*(3.14)*r;
}
public double area() {
return 3.14*r*r;
}
public Circle bigger(Circle c){
if(c.r>r) return c; else return this;
}
public Circle(double x, double y, double r) {
this.x=x;
this.y=y;
this.r=r;
}
}
public class Square {
public double x,y,a;
public double perimeter() {
return 4*a;
}
public double area() {
return a*a;
}
public Square bigger(Square s){
if(s.a>a) return s; else return this;
}
public Square(double x, double y, double a) {
this.x=x;
this.y=y;
this.a=a;
}
}
public class CircleAndSquareTest {
public static void main(String[] args) {
Circle c1 = new Circle(0.0,0.0,1.0);
Circle c2 = new Circle(1.0,0.0,2.0);
Circle c3 = new Circle(0.0,2.0,4.0);
Circle c4 = new Circle(1.0,3.0,1.0);
Circle cb = c1.bigger(c2).bigger(c3).bigger(c4);
System.out.println("The circle with the biggest circumference has:\n");
System.out.println("x-axis value: " + cb.x + " y-axis value: " + cb.y + " radius: " + cb.r+"\n");
Square s1 = new Square(0.0,0.0,1.0);
Square s2 = new Square(0.0,0.0,1.0);
Square s3 = new Square(0.0,0.0,5.0);
Square s4 = new Square(4.0,2.0,2.0);
Square s5 = new Square(0.0,0.0,1.0);
Square sb = s1.bigger(s2).bigger(s3).bigger(s4).bigger(s5);
System.out.println("The square with the biggest area has:\n");
System.out.println("x-axis value: " + sb.x + " y-axis value: " +
sb.y + " side: " + sb.a);
}
}
Here's how to do it using Comparators and the Collections class to find the max value. This is untested but it should do what you want. Note I'm using static inner classes here but they can be standard classes defined in their own file if needs be - this is just for the purpose of creating a quick answer.
public interface Shape {
double getPerimeter();
double getArea();
}
public static class PerimeterComparator implements Comparator<Shape> {
#Override
public int compare(Shape a, Shape b) {
return Double.compare(a.getPerimeter(), b.getPerimeter());
}
}
public static class AreaComparator implements Comparator<Shape> {
#Override
public int compare(Shape a, Shape b) {
return Double.compare(a.getArea(), b.getArea());
}
}
public static class Circle implements Shape {
private final double x, y, r;
#Override
public double getPerimeter() {
return 2 * (3.14) * r;
}
#Override
public double getArea() {
return 3.14 * r * r;
}
public Circle(double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getR() {
return r;
}
}
public static class Square implements Shape{
private final double x, y, a;
#Override
public double getPerimeter() {
return 4 * a;
}
#Override
public double getArea() {
return a * a;
}
public Square(double x, double y, double a) {
this.x = x;
this.y = y;
this.a = a;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getA() {
return a;
}
}
public static void main(String[] args) {
List<Shape> shapes = new ArrayList<>();
List<Circle> circles = new ArrayList<>();
circles.add(new Circle(0.0,0.0,1.0));
circles.add(new Circle(1.0,0.0,2.0));
circles.add(new Circle(0.0,2.0,4.0));
circles.add(new Circle(1.0,3.0,1.0));
Circle largestCircle = Collections.max(circles, new PerimeterComparator());
System.out.println("The circle with the biggest circumference has:\n");
System.out.println("x-axis value: " + largestCircle.getX() + " y-axis value: " + largestCircle.getY() + " radius: " + largestCircle.getPerimeter() +"\n");
List<Square> squares = new ArrayList<>();
squares.add(new Square(0.0,0.0,1.0));
squares.add(new Square(0.0,0.0,1.0));
squares.add(new Square(0.0,0.0,5.0));
squares.add(new Square(4.0,2.0,2.0));
squares.add(new Square(0.0,0.0,1.0));
Square largestSquare = Collections.max(squares, new PerimeterComparator());
System.out.println("The square with the biggest area has:\n");
System.out.println("x-axis value: " + largestSquare.getX() + " y-axis value: " + largestSquare.getY() + " side: " + largestSquare.getA());
shapes.addAll(circles);
shapes.addAll(squares);
Shape largestPerimeter = Collections.max(shapes, new PerimeterComparator());
Shape largestArea = Collections.max(shapes, new AreaComparator());
System.out.printf("\nThe shape with the biggest perimeter is a %s and has has: a perimeter of: %f\n", largestPerimeter.getClass().getSimpleName(), largestPerimeter.getPerimeter());
System.out.printf("The shape with the biggest area is a %s and has has: an area of: %f\n", largestArea.getClass().getSimpleName(), largestArea.getArea());
}
Start by declaring a base interface, maybe called Shape that defines a method getPerimeterLength() for example.
Have all your shape classes implement that interface, and the corresponding method(s).
Now, a Square is also a Shape, and so is a Circle. Then you could put all these objects into an array of Shape. You iterate that array, and identify that entry with the maximum perimeter length. Then you simply call toString() on that object. Because you also overwrite the toString() method in all your classes to print the (different!) details each class has internally.

Can't display the proper value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have these classes:
interface Shape
{
public double getPerimeter();
public double getArea();
public void getDetails();
}
and
class Circle implements Shape
{
private double PI = 3.14;
private int radius;
double perimeter, area;
//constructor
public Circle(int radius)
{
perimeter = 2.0 * (PI * radius);
area = PI * (radius * radius);
}
public int getRadius()
{
return radius;
}
public double getPerimeter()
{
return perimeter;
}
public double getArea()
{
return area;
}
public void getDetails()
{
System.out.println("Shape Type: Circle " + "\n"
+ "Radius: " + getRadius() + "\n"
+ "Perimeter: " + getPerimeter() + "\n"
+ "Area: " + getArea() + "\n" );
}
}
and finally
public class TestShape
{
public static void main(String args[])
{
for(int i = 0; i < args.length; i++)
{
try
{
Integer.parseInt(args[i]);
}
catch(NumberFormatException nfe)
{
System.err.print("wrong");
}
catch(IllegalArgumentException iae)
{
System.err.print("wrong");
}
if(args.length==1)
{
i = Integer.parseInt(args[0]);
Circle r1 = new Circle(i);
r1.getDetails();
}
}
}
}
We were assigned to input 1,2 or 3 numbers on the command line of the terminal and the output would display what kind of shape it is depending on the array size. I managed to get the proper parameter and area when I input a number but the radius keeps displaying 0 instead of the actual number I inputted.
This is what the output looks like
So what do you guys think?
You are not assigning the radius in the constructor of Circle...
public Circle(int radius)
{
perimeter = 2.0 * (PI * radius);
area = PI * (radius * radius);
this.radius = radius // this statement is missing
}
Therefore, circle.getRadius() always returns the uninitialized default int value 0.
In your constructor:
public Circle(int radius)
{
perimeter = 2.0 * (PI * radius);
area = PI * (radius * radius);
}
radius refers to the parameter radius, not the radius field declared in Circle. You need to make them the same by assigning the radius parameter to the radius field. To refer to the radius field, use this.radius.
this.radius = radius;
Because you did not do this in your code, the radius returned by getRadius is 0, which is the default value of an unassigned int.
Instance variable are assigned values by default if not initialized. In case of int it's 0 by default. So to assign the value to your instance variable radius you need to add this line in your constructor.
this.radius = radius
We are using this because your instance variable and local variable are having the same name.

Program print keeps print 0.0 for Cylinder area, volume, range + inheritance java

I have tried this a couple of different ways. It is required I use the inheritance to extend these classes. Every time I run the program it just out puts 0.0 for volume and area. Radius displays correctly. Output at the bottom.
public class Base_HW04Q1
{
public double pi = 3.14, l, radius, height, area, volume;
public static class RoundShape extends Base_HW04Q1 {
public RoundShape(double radius) {
this.radius = radius;
}
public double calcArea () {
area = (radius * radius) * pi;
return area;
}
public String toString() {
return "A Round Shape of radius: " + radius + ", area " + area + ".";
}
}
public static class Cylinder extends Base_HW04Q1
{
public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
public double calcArea() {
l = Math.sqrt((radius * radius) + (height * height));
area = 2 * pi * radius * height + 2 * pi * l;
return area;
}
public double calcVolume() {
volume = pi * (radius * radius) * height;
return volume;
}
public String toString() {
return "A Cylinder of radius: " + radius + ", area " + area + " and a volume of " + volume;
}
}
public static class Cone extends Base_HW04Q1 //TODO: This line is almost, but not quite, complete.
{
public Cone(double radius, double height) {
this.radius = radius;
this.height = height;
}
public double calcArea() {
l = Math.sqrt((radius * radius) + (height * height));
area = (pi * radius * l) + (pi * radius * radius);
return area;
}
public double calcVolume() {
volume = 0.333 * pi * radius * radius * height;
return volume;
}
public String toString() {
return "A Cone of radius: " + radius + ", area " + area + " and a volume of " + volume;
}
}
public static void main(String[] args)
{
//object creation
Cylinder Cylinder1 = new Cylinder(30, 10);
Cone Cone1 = new Cone(10, 20);
RoundShape RoundShape1 = new RoundShape(50);
//print for objects
System.out.println(Cylinder1);
System.out.println(RoundShape1);
System.out.println(Cone1);
}
}
Output:
A Cylinder of radius: 30.0, area 0.0 and a volume of 0.0 A Round Shape
of radius: 50.0, area 0.0. A Cone of radius: 10.0, area 0.0 and a
volume of 0.0
Your toString() never calls the methods that do the calculations and instead prints the default 0.0 field values. You will run this risk if toString() is ever called before the calcXxxx() methods are called, i.e., before the calculated fields have been given a decent value. The best solution is to prevent this problem from happening in the first place by entirely getting rid of fields for calculated values, for instance area and volume. Instead within toString(), call the methods to get these values.
e.g.,
public double pi = 3.14, l, radius, height; // , area, volume;
public static class RoundShape extends Base_HW04Q1 {
public RoundShape(double radius) {
this.radius = radius;
}
public double calcArea () {
return (radius * radius) * pi;
// return area;
}
public String toString() {
return "A Round Shape of radius: " + radius + ", area " + calcArea() + ".";
}
}
It's because you only instantiate the object and entered to constructor but not in the other method.
Cylinder Cylinder1 = new Cylinder(30, 10);
Cone Cone1 = new Cone(10, 20);
RoundShape RoundShape1 = new RoundShape(50);
no one called in these methods
public double calcArea() {
l = Math.sqrt((radius * radius) + (height * height));
area = (pi * radius * l) + (pi * radius * radius);
return area;
}
public double calcVolume() {
volume = 0.333 * pi * radius * radius * height;
return volume;
}
public String toString() {
return "A Cone of radius: " + radius + ", area " + area + " and a volume of " + volume;
}
and the rest methods. Call them from constructor or from main if you want to calculate through method like:
public static void main(String[] args)
{
//object creation
Cylinder Cylinder1 = new Cylinder(30, 10);
Cone Cone1 = new Cone(10, 20);
RoundShape RoundShape1 = new RoundShape(50);
double roundArea = RoundShape1.calcArea();//then use this
string roundMessage = RoundShape1.toString();//and this whatever you want.
//do it in others too
//print for objects
System.out.println(Cylinder1);
System.out.println(RoundShape1);
System.out.println(Cone1);
}

Encapsulation with Java

I'm trying to implement encapsulation in a program as part of some coursework however I've run into an error which I just can't seem to be able to fix with my limited knowledge which isn't helped by my Teacher/Lecturer who is very good at what he does however doesn't do very well when it actually comes to communicating the information, because of this could someone help me fix the error which is presented from the following program and explain to me why it's not working as intended.
class TwoDShapeEncap{
double width, height;
//Width
void setWidth(double w){
width = w;
}
double getWidth(){
return width;
}
//Height
void setHeight(double h){
height = h;
}
double getHeight(){
return height;
}
}
class Triangle extends TwoDShapeEncap{
String type;
private double sideA, sideB, sideC, adjacent, opposite;
//Side A
void setsideA(double a){
sideA = a;
}
double getsideA(){
return sideA;
}
//Side B
void setsideB(double b){
sideB = b;
}
double getsideB(){
return sideB;
}
//Side C
void setsideC(double c){
sideC = c;
}
double getsideC(){
return sideC;
}
//Adjacent
void setadjacent(double a){
adjacent = a;
}
double getadjacent(){
return adjacent;
}
//Opposite
void setopposite(double o){
width = o;
}
double getopposite(){
return opposite;
}
double getPerimeter(){
if(getsideB() == 0.0 && getsideC() == 0.0){
type = "equilateral";
return getsideA() * 3;
}
else if (getsideC() == 0.0){
type = "isosceles";
return getsideA() + getsideB() * 2;
}
else{
type = "scalene";
return getsideA() + getsideB() + getsideC();
}
}
//*******************************************************************************************
//* Paste the perimeter() and hypotenuse() methods from your previous class into this class *
//*******************************************************************************************
//***************************************
//* add an area method()into this class *
//***************************************
double area(double a, double b){
getWidth();
getHeight();
return (getWidth() * getHeight()/2);
}
}
class Rectangle extends TwoDShapeEncap{
boolean issquare;
private double height, width;
//Height
void setHeight(double h){
height = h;
}
double getHeight(){
return height;
}
//Width
void setWidth(double w){
width = w;
}
double getWidth(){
return width;
}
double perimeter(double h, double w){
getHeight();
getWidth();
return getHeight() * 2 + getWidth() * 2;
}
double area(double a, double b){
//getWidth();
//getHeight();
return (getWidth() * getHeight()/2);
}
boolean testSquare(double h, double w){
//getHeight();
//getWidth();
if (getHeight() == getWidth())
issquare = true;
else issquare = false;
return issquare;
}
//*********************************************
//* add area and perimeter methods this class *
//*********************************************
//*************************************************************************
//* add a testSquare method to test if a particular rectangle is a square *
//*************************************************************************
}
//Add a circle class which includes area and circumference methods
class Circle extends TwoDShapeEncap{
double radius, diameter;
double area (double r){
radius = r;
return Math.PI * (radius * radius);
}
double perimeter (double r){
radius = r;
return 2 * (Math.PI * radius);
}
}
class TwoDShapeEncapDemoNew {
public static void main(String args[]) {
//Triangle
Triangle t = new Triangle();
t.setsideA(5.7);
System.out.println("The perimeter is " + t.getPerimeter());
System.out.println("If sideA is " + t.getsideA() );
System.out.println("The type is " + t.type);
System.out.println();
t.setsideB(7.3);
System.out.println("The perimeter is " + t.getPerimeter());
System.out.println("If sideA is " + t.getsideA() );
System.out.println("If sideB is " + t.getsideB() );
System.out.println("The type is " + t.type);
System.out.println();
t.setsideC(2.7);
System.out.println("The perimeter is " + t.getPerimeter());
System.out.println("If sideA is " + t.getsideA());
System.out.println("If sideB is " + t.getsideB());
System.out.println("If sideC is " + t.getsideC());
System.out.println("The type is " + t.type);
System.out.println();
//Rectangle
Rectangle r = new Rectangle();
r.setHeight(7.8);
r.setWidth(4.2);
System.out.println("The perimeter is " + r.perimeter());
System.out.println("The");
}
}
Error message:
Main.java:186: error: method perimeter in class Rectangle cannot be applied to given types; System.out.println("The perimeter is " + r.perimeter()); ^ required: double,double found: no arguments reason: actual and formal argument lists differ in length 1 error –
When you call:
System.out.println("The perimeter is " + r.perimeter());
in r.perimeter you must pass two parameters (as your signature wants)
Your method in Rectangle class:
double perimeter(double h, double w){
getHeight();
getWidth();
return getHeight() * 2 + getWidth() * 2;
}
So fix:
System.out.println("The perimeter is " + r.perimeter(r.getHeight(), r.getWidth()));
You also can fix your method perimeter without parameters because in the body you use getHeigth() and getWidth() properties
So you can write:
double perimeter(){
return getHeight() * 2 + getWidth() * 2;
}
The method perimeter in the class Rectangle expects two parameters, and you're not passing any. You could either call it like this:
r.perimeter(7.8,4.2);
Or redefine the method so it looks like this:
double perimeter(){
return getHeight() * 2 + getWidth() * 2;
}
Thats because you are defining the perimeter function like this:
double perimeter(double h, double w){
getHeight();
getWidth();
return getHeight() * 2 + getWidth() * 2;
}
and calling System.out.println("The perimeter is " + r.perimeter()); with no parameters.
Since you are not really using double h and double w for nothing, you just have to remove them from the method definition
double perimeter(){
return getHeight() * 2 + getWidth() * 2;
}
Since everybody is just facing the problem with the parameters I will face this problem: Getters are used to get the values of private fields if you're "outside" your class! If you're in a method in your class you don't have to use the getters, you can just use the variables themselfs:
Example:
public class SomeClass {
private int a;
public void setA(int anotherA) {
a = anotherA;
}
public int getA() {
return a;
}
public int getSquareOfA() {
// You don't use getA() to get the value now
// but you use a itself!
return a*a; // instead of 'return getA() * getA();'
}
}
You do have that problem at several points in your code!
According to your problem:
Your problem was that you're calling a method which has 2 parameters without any input parameters!
You can either remove the parameters of the method (which will be the logically right thing to do in your case), OR you pass some parameters.
In your specific case that means, change your perimiter() method as follows:
double perimiter() {
return (height + width) * 2;
// or if you want to impress your teacher ;) :
// return (height + width) << 1
}
Also you should change that methodname to getPerimiter() to keep up with your own naming conventions!
Modify your signature to remove the arguments.
class Rectangle extends TwoDShapeEncap{
///...
double perimeter(double h, double w){
getHeight();
getWidth();
return getHeight() * 2 + getWidth() * 2;
}
should be
class Rectangle extends TwoDShapeEncap{
///...
double perimeter(){
//Notice that you don't need to pass in these arguments
//as this function gets these arguments by itself.
return getHeight() * 2 + getWidth() * 2;
}

How do I keep the scanner class open to enter a new value for an instance

I have an assignment to create a Circle class which creates a circle using radius and then uses get to find the area, diameter, etc.
The next step in the assignment is to "Continuously use the Scanner object created at beginning to ask user to enter another value for radius." at first I tried to copy+paste the code to ask the user for radius and assign to a local variable like i did originally and I get the following error: "The assigned value is never used." Also the assignment says to change the value of the circle instance, not to make a new circle and assign that a different value.
I did some searches but am unsure how to proceed.
Code for my circle.java class:
public class Circle {
// intialize variable radius
private double radius;
// create constant PI
public final static double PI = 3.14159;
// Circle constructtor
public Circle(double r) {
radius = r;
}
// setRadius method
public void setRadius(double r) {
radius = r;
}
// getRaduis method
public double getRadius() {
return radius;
}
// getArea method
public double getArea() {
double area = PI * radius * radius;
return area;
}
// getDiameter method
public double getDiameter() {
double diameter = 2 * radius;
return diameter;
}
// getCircumference method
public double getCircumference() {
double circumference = 2 * PI * radius;
return circumference;
}
}
And the code for my program circleTest:
package circle;
import java.util.Scanner;
public class CircleTest {
public static void main(String[] args) {
double circleRadius;
double radius;
// import scanner
Scanner keyboard = new Scanner(System.in);
// ask user for raduis and store in a local variable
System.out.print("What is the radius of the circle?");
circleRadius = keyboard.nextDouble();
// Create a circle object called circle1
Circle circle1 = new Circle(circleRadius);
// display area using getArea method for circle1
System.out.println("The area of the circle is " + circle1.getArea());
// display diameter for circle1 using getDiameter method
System.out.println("The diamter of a the circle is "
+ circle1.getDiameter());
// display the circumference of circle1 using getCircumference
System.out.println("The circumference of a circle is "
+ circle1.getCircumference());
// ask user for radius and store in a local variable
System.out.print("What is the radius of the circle?");
circleRadius = keyboard.nextDouble();
}
}

Categories

Resources