Jump to content

Java-class,methods and objects

Sebastian Kurpiel
Go to solution Solved by jslowik,

You're getting errors on the return because you're returning something from a method that has no return type. The method signature is also incorrect. You're also lacking getters and setters which means the setColor and setRadius calls in the driver class won't work.

 

Here's a functioning example of what your class should at least resemble. I added some comments in to hopefully help clear up any questions. Though feel free to ask more.

 

public class Circle {

    private String color;
    private int radius;

    /**
     * Constructor that accepts all values. You aren't using this, but I
     * included it out of habit
     *
     * @param color
     * @param radius
     */
    public Circle(String color, int radius) {
        this.color = color;
        this.radius = radius;
    }

    /**
     * This is the constructor that the CircleDriver class is calling
     *
     */
    public Circle() {
    }

    public String getColor() {
        return color;
    }

    /**
     * Setter method for color. Required for driver class as written
     *
     * @param color
     */
    public void setColor(String color) {
        this.color = color;
    }

    public int getRadius() {
        return radius;
    }

    /**
     * Setter method for radius. Required for driver class as written
     *
     * @param radius
     */
    public void setRadius(int radius) {
        this.radius = radius;
    }

    /**
     * Your display method. Void return type, handles the print line itself
     *
     */
    public void display() {
        System.out.println("I am a circle");
        System.out.println("My color is " + color);
        System.out.println("My radius is " + radius);
    }

    /**
     * Compute area. Returns a double which is output by the driver class
     *
     * @return - radius = pi r ^2
     */
    public double computeArea() {
        return (Math.PI * Math.pow(radius, 2));
    }

    /**
     * Compute circumference. Same as area, but different formula
     *
     * @return - circumference = 2 pi r
     */
    public double computeCircumference() {
        return (2 * Math.PI * radius);
    }

}

 

What's up guys, 

This is the problem

1.PNG.ea854db03c59301f0a121a83974dbd74.P

My code so far. 

import java.util.Scanner;

public class circle
{
    public String color;
    private double radius;
    private final double PI=3.14159;
    public void writeOutput(){
        System.out.println("Hello,I am a circle.");
        System.out.println("My color is " + color + ".");
        System.out.print;n("My radius is " + radius + ".");
    }
    public double aCircle.computeArea(){
        return PI*radius*radius;
    }
    public double aCircle.computeCircumference(){
        return 2*pi*radius;
    }
}
 

I keep getting an error at the public double acircle... parts

 

This is the driver my professor provided.

 

package lab7;

public class CircleDriver {
    
      public static void main(String[] args) {
            
            Circle aCircle = new Circle();
            
            aCircle.setColor("green");
            aCircle.setRadius(10);
            aCircle.display();
            
            Double circleArea = aCircle.computeArea();
            Double circumference = aCircle.computeCircumference();

            System.out.println("circle area: " + circleArea);
            System.out.println("circle circumference: " + circumference);
            System.out.println();
          }

}

 

Thanks for the help in advance.
 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Sebastian Kurpiel said:

What's up guys, 

This is the problem

1.PNG.ea854db03c59301f0a121a83974dbd74.P

My code so far. 

import java.util.Scanner;

public class circle
{
    public String color;
    private double radius;
    private final double PI=3.14159;
    public void writeOutput(){
        System.out.println("Hello,I am a circle.");
        System.out.println("My color is " + color + ".");
        System.out.print;n("My radius is " + radius + ".");
    }
    public double aCircle.computeArea(){
        return PI*radius*radius;
    }
    public double aCircle.computeCircumference(){
        return 2*pi*radius;
    }
}
 

I keep getting an error at the public double acircle... parts

 

This is the driver my professor provided.

 

package lab7;

public class CircleDriver {
    
      public static void main(String[] args) {
            
            Circle aCircle = new Circle();
            
            aCircle.setColor("green");
            aCircle.setRadius(10);
            aCircle.display();
            
            Double circleArea = aCircle.computeArea();
            Double circumference = aCircle.computeCircumference();

            System.out.println("circle area: " + circleArea);
            System.out.println("circle circumference: " + circumference);
            System.out.println();
          }

}

 

Thanks for the help in advance.
 

in the driver class, the circle object is called aCircle.

 

when the driver class calls a method from circle, it uses aCircle.methodName()

 

 

so the methods you are defining in your circle class shouldnt have the "aCircle.", that syntax is typically reserved for calling methods from objects and should never be used in method declarations. 

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Sebastian Kurpiel said:

What's up guys, 

This is the problem

1.PNG.ea854db03c59301f0a121a83974dbd74.P

My code so far. 

import java.util.Scanner;

public class circle
{
    public String color;
    private double radius;
    private final double PI=3.14159;
    public void writeOutput(){
        System.out.println("Hello,I am a circle.");
        System.out.println("My color is " + color + ".");
        System.out.print;n("My radius is " + radius + ".");
    }
    public double aCircle.computeArea(){
        return PI*radius*radius;
    }
    public double aCircle.computeCircumference(){
        return 2*pi*radius;
    }
}
 

I keep getting an error at the public double acircle... parts

 

This is the driver my professor provided.

 

package lab7;

public class CircleDriver {
    
      public static void main(String[] args) {
            
            Circle aCircle = new Circle();
            
            aCircle.setColor("green");
            aCircle.setRadius(10);
            aCircle.display();
            
            Double circleArea = aCircle.computeArea();
            Double circumference = aCircle.computeCircumference();

            System.out.println("circle area: " + circleArea);
            System.out.println("circle circumference: " + circumference);
            System.out.println();
          }

}

 

Thanks for the help in advance.
 

Also, your writeOutput should be called Display according to the assingment

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Stardar1 said:

in the driver class, the circle object is called aCircle.

 

when the driver class calls a method from circle, it uses aCircle.methodName()

 

 

so the methods you are defining in your circle class shouldnt have the "aCircle.", that syntax is typically reserved for calling methods from objects and should never be used in method declarations. 

What would be the proper syntax?  and would the write output be changed to public void display()

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Sebastian Kurpiel said:

What would be the proper syntax?  and would the write output be changed to public void display()

writeOutput should be changed to public void display() because that is what it is being called as. 

 

when the Circle object is created, the class it is created in (in this case, CircleDriver) does not have access to its methods, even though they are public. 

 

It has to access the object itself first.

 

Think of it like this, the object being created called "aCircle" has inside it all those methods you created in the Circle class. 

 

to "open" aCircle and get at the methods inside it, we use a dot.

 

<objectName>.<methodName>();

 

when the method is created, it is just the method name, same as a method created in your main class. 

Different PCPartPickers for different countries:

UK-----Italy----Canada-----Spain-----Germany-----Austrailia-----New Zealand-----'Murica-----France-----India

 

10 minutes ago, Stardar1 said:

Well, with an i7, GTX 1080, Full tower and flashy lights, it can obviously only be for one thing:

Solitaire. 

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Stardar1 said:

writeOutput should be changed to public void display() because that is what it is being called as. 

 

when the Circle object is created, the class it is created in (in this case, CircleDriver) does not have access to its methods, even though they are public. 

 

It has to access the object itself first.

 

Think of it like this, the object being created called "aCircle" has inside it all those methods you created in the Circle class. 

 

to "open" aCircle and get at the methods inside it, we use a dot.

 

<objectName>.<methodName>();

 

when the method is created, it is just the method name, same as a method created in your main class. 

import java.util.Scanner;

public class Circle
{
    public String color;
    private double radius, circleArea, circumference;
    private final double PI=3.14159;
    public void display(){
        System.out.println("Hello,I am a circle.");
        System.out.println("My color is " + setColor + ".");
        System.out.println("My radius is " + setRadius + ".");
    }
    public aCircle.computeArea(){ 
        return (PI*radius*radius);
    }
    public aCircle.computeCircumference{
        return (2*PI*radius);
    }
}

I'm getting an error when running the return parts, any suggestions?

Link to comment
Share on other sites

Link to post
Share on other sites

You're getting errors on the return because you're returning something from a method that has no return type. The method signature is also incorrect. You're also lacking getters and setters which means the setColor and setRadius calls in the driver class won't work.

 

Here's a functioning example of what your class should at least resemble. I added some comments in to hopefully help clear up any questions. Though feel free to ask more.

 

public class Circle {

    private String color;
    private int radius;

    /**
     * Constructor that accepts all values. You aren't using this, but I
     * included it out of habit
     *
     * @param color
     * @param radius
     */
    public Circle(String color, int radius) {
        this.color = color;
        this.radius = radius;
    }

    /**
     * This is the constructor that the CircleDriver class is calling
     *
     */
    public Circle() {
    }

    public String getColor() {
        return color;
    }

    /**
     * Setter method for color. Required for driver class as written
     *
     * @param color
     */
    public void setColor(String color) {
        this.color = color;
    }

    public int getRadius() {
        return radius;
    }

    /**
     * Setter method for radius. Required for driver class as written
     *
     * @param radius
     */
    public void setRadius(int radius) {
        this.radius = radius;
    }

    /**
     * Your display method. Void return type, handles the print line itself
     *
     */
    public void display() {
        System.out.println("I am a circle");
        System.out.println("My color is " + color);
        System.out.println("My radius is " + radius);
    }

    /**
     * Compute area. Returns a double which is output by the driver class
     *
     * @return - radius = pi r ^2
     */
    public double computeArea() {
        return (Math.PI * Math.pow(radius, 2));
    }

    /**
     * Compute circumference. Same as area, but different formula
     *
     * @return - circumference = 2 pi r
     */
    public double computeCircumference() {
        return (2 * Math.PI * radius);
    }

}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×