Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Learning Java (Classes/Constructors

Name: Anonymous 2011-10-20 18:58

What do I need to look into to understand this better?
My professor gave an assignment to create a class called Circle in which I store the radius and diameter, using a constructor, then create a program that populates those variables using user input. I'm a bit confused because the book uses Rectangle as an example, and it seems that "Rectangle" and "box" are specifically part of java, whereas using "Circle" and "round" doesn't work for me.

I'm about to completely scratch what I have, copy the rectangle code, complile and run it to check it works, then try to customize it to work with a circle. Any advice would be much appreciated. Thanks. (Please don't just post code and do it for me.)

Name: Anonymous 2011-10-20 22:33

Welcome to Java!

interface ICircle {
    public void setRadius(double radius);
    public double getRadius();
    public void setDiameter(double diameter);
    public double getDiameter();
}

class Circle implements ICircle {
    private double radius;
    private double diameter;
   
    public void setRadius(double radius) {
        this.radius = radius;
    }
   
    public double getRadius() {
        return this.radius;
    }
   
    public void setDiameter(double diameter) {
        this.diameter = diameter;
    }
   
    public double getDiameter() {
        return this.diameter;
    }
}

class CircleFactory {
    public ICircle create() {
        return new Circle;
    }
}

public class CircleDriver {
    public static void main(String[] args) {
        ICircle circle = (new CircleFactory()).create();
        Scanner input = new Scanner(System.in);
       
        System.out.println("Please enter a radius and a diameter.");
        circle.setRadius(input.nextDouble());
        circle.setDiameter(input.nextDouble());
       
        System.out.println("Your radius is: " + circle.getRadius());
        System.out.println("Your diameter is: " + circle.getDiameter());
    }
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List