简体   繁体   English

创建一个自定义类! 爪哇

[英]creating a custom class! Java

I am new to Java and am working on an assignment. 我是Java新手,正在从事作业。 I am supposed to create my own class "Cylinder" and call methods from it to be able to print out the radius and height and then change it and print it out again. 我应该创建自己的类“ Cylinder”并从中调用方法,以便能够打印出半径和高度,然后对其进行更改并再次打印出来。 I got it to print the set values for cylinder1 and the cylinder1.setRadius/setHeight..my problem is I have to have it print something like Cylinder Radius: and Height:...but when it prints all i get is 3.04.0 or whatever it is set to...I have spent a long time trying to figure this out please any assistance is appreciated!! 我得到它来打印cylinder1和cylinder1.setRadius / setHeight ..的设置值。我的问题是我必须让它打印类似Cylinder Radius:and Height:...的东西,但是当它打印时,我得到的是3.04.0或任何设置...我花了很长时间试图弄清楚这一点,请提供任何帮助!

public class Funtimes{

  public static void main(String[] args){
    Cylinder cylinder1= new Cylinder(1,2);
    System.out.println(cylinder1);

    cylinder1.setRadius(3);
    cylinder1.setHeight(4);
    System.out.println(cylinder1);
  }}

class Cylinder
{
  private double radius, height, area, volume;

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

  public double getRadius() {
    return radius;
  }

  public double getHeight() {
    return height;
  }

  public double getArea() {
    double area = (2 * Math.PI * radius * height) + (2 * Math.PI * Math.pow(radius, 2));
    return area;
  }

  public void setRadius(double r) {
    radius = r;
  }

  public void setHeight(double h) {
    height = h;
  }

  public double calcVolume() {
    double volume = Math.PI * Math.pow(radius, 2) * height;
    return volume;
  }

  public String toString (){
    StringBuilder StBuild = new StringBuilder();
    StBuild.append(radius).append(height);
    return StBuild.toString();        
  }

  public static void main(String[] args) {
    Cylinder cylinder1 = new Cylinder(5, 5);
    System.out.println(cylinder1);
  }

}

Modify your toString() 修改你的toString()

public String toString () {
    return String.format("Radius: %s Height: %s", radius, height);       
}

使用StBuild.append("Radius:").append(radius).append("Height:").append(height)

You can change the toString method to something like this 您可以将toString方法更改为如下所示

   public String toString(){
      return "Cylinder Radius" + this.radius + " Height" + this.height;
   }

In your case you are appending radius and height values together in a String, which is why you get the response. 在您的情况下,您要将半径和高度值一起附加在字符串中,这就是为什么要获得响应的原因。

StBuild.append("Radius: ").append(radius);
StBuild.append("Height: ").append(height);

You could use String.format(String, Object...) to generate a formatted String with one line 1 like 您可以使用String.format(String, Object...)生成一行1的格式化String ,例如

@Override
public String toString() {
    return String.format("Cylinder Radius: %.2f Height: %0.2f", radius, height);
}

You could do something similar with your StringBuilder approach (but it's a little harder to read, IMO so I won't make it a one liner). 您可以使用StringBuilder方法执行类似的操作(但IMO有点难以阅读,因此我不会将其作为一个衬板)。 Something like, 就像是,

@Override
public String toString() {
    StringBuilder StBuild = new StringBuilder();
    StBuild.append("Cylinder Radius: ").append(radius)
            .append(" Height: ").append(height);
    return StBuild.toString();
}

1 And I recommend you always use the Override annotation, it can save you hours of frustration when your method is intended to override something from a superclass (but doesn't). 1 并且我建议您始终使用Override注释,当您的方法打算覆盖超类中的某些内容时,它可以节省您数小时的沮丧(但不是)。

Edit your toString method as follows: 如下编辑您的toString方法:

public String toString (){
    StringBuilder stBuild = new StringBuilder();
    stBuild.append("Cylinder Radius:");
    stBuild.append(radius);
    stBuild.append(" and Height:");
    stBuild.append(height);
    return StBuild.toString();        
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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