简体   繁体   中英

How to use classes and methods in Java

I'm fairly new to Java and coding, but up until this point in the Java material, I haven't ran into any problems. What I really can't wrap my head around is how classes and methods actually work. I've been attempting to implement this over the last several hours to no avail:

Implement the class called Cylinder shown in UML below. The constructor accepts and initializes the radius and height for the Cylinder, while accessors and mutators allow them to be changed after object construction. The class also include methods that calculate and return the volume and surface area of the Cylinder. Lastly, it contains a toString method that returns the name of the shape, its radius, and its height. Create a main method which instantiates 4 Cylinder objects (any parameters), display them with toString() , change one parameter (your choice) in each, and display them again.

UML:

在此处输入图片说明

This is the code that I currently have:

class Cylinder {
    private double radius;
    private double height; 


    // Method #1
    private void rad (){

    }

    // Constructor
    public Cylinder (double radius, double height){
        this.radius = radius; 
        this.height = height;
    }


    // Method #2 for calculating volume.
    double calcVolume (){
        double volume = Math.PI * Math.pow(radius, 2) * height; 
        return volume; 
    }

    // Method #3 for calculating surface area.
    double calcArea (){
        double area = (2 * Math.PI * radius * height) + (2 * Math.PI * Math.pow(radius, 2));
        return area;
    }

    // toString method.
    public String toString (){
        StringBuilder sb = new Stringbuilder();
        sb.append(radius).append(height);
        return sb.toString();

    }
}

public class Murray_A03Q1 {

public static void main(String[] args) {

    Cylinder cylinder1 = new Cylinder(5, "can");
    System.out.println(cylinder1);

    Cylinder cylinder2 = new Cylinder(6, "cup");

    Cylinder cylinder3 = new Cylinder(7, "jar");

    Cylinder cylinder4 = new Cylinder(8, "paper roll");
}       

}

What I really don't understand is how to use the 'get' and 'set' methods. Additionally, I'm not completely sure how to implement the toString method.

The following errors that I can't figure out how to correct are:

  1. The constructor Cylinder() is undefined for - Cylinder cylinder1 = new Cylinder();

  2. Stringbuilder can't be resolved to a type for - StringBuilder sb = new Stringbuilder();

Thank you for your help!

What I really don't understand is how to use the 'get' and 'set' methods.

The purpose of getters and setters is for encapsulation. They allow you to get or set the values of the class' variables without having to declare them as being public.

For example, if you were to have

public double radius;
public double height;

You will be able to access them as

cylinder1.radius = 1;
cylinder2.height = 10;
int a = cylinder3.radius;
int b = cylinder3.height + cylinder4.radius;

etc.

Now if instead we had

private double radius;
private double height;

The above code will fail. Which is why we need getters and setters. As the names imply, getters "get" the variable.

public double getHeight() {
    return height;
}

While setters "set" the variable.

public void setHeight(double height) {
    this.height = height;
}

As for why we do it this way, theres a lot of information on that.

Additionally, I'm not completely sure how to implement the toString method.

As for how to implement the toString() method, all it has to do is return a String . But as for what the String contains, there is no hard rule for that. A good start would be to return the radius and height, like you have done.

The constructor Cylinder() is undefined for - Cylinder cylinder1 = new Cylinder();

Your constructor is public Cylinder (double radius, double height) . However, you are trying to make a cylinder that's an int and a String .

Cylinder cylinder1 = new Cylinder(5, "can");

Either

  • Change the constructor to something like public Cylinder(double radius, String name); or
  • The instantiate your Cylinders with two doubles, a radius and a height. eg

     Cylinder cylinder1 = new Cylinder(1.0, 2.0); 

Stringbuilder can't be resolved to a type for - StringBuilder sb = new Stringbuilder();

The only problem here is that you forgot to capitalize the b . It's StringBuilder not Stringbuilder .

An example of get and set methods:

 double getRadius() {
    return radius;
 }

 void setRadius(r) {
    radius = r;
 }

The constructor Cylinder() is undefined for - Cylinder cylinder1 = new Cylinder();

Something is attempting to invoke the default constructor (a constructor with no parameters). You can either find out exactly where this error is called, or else add a default constructor:

Cylinder() {
    this.radius = 0;
    this.height = 0;
}

Stringbuilder can't be resolved to a type for - StringBuilder sb = new Stringbuilder();

StringBuilder is actually the class java.lang.StringBuilder . Place this text at the top of your file to help it resolve the name.

import java.lang.StringBuilder;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM