Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, April 15, 2016

creating a custom class! Java

creating a custom class! Java


I am new to Java and am working on an assignment. 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. 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!!

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);    }    }  

Answer by Marko vlai for creating a custom class! Java


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

Answer by KERiii for creating a custom class! Java


Modify your toString()

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

Answer by Areca for creating a custom class! Java


Edit your toString method as follows:

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

Answer by Arun Iyer for creating a custom class! Java


You can change the toString method to something like this

   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.

Answer by Shear Plane for creating a custom class! Java


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

Answer by Elliott Frisch for creating a custom class! Java


You could use String.format(String, Object...) to generate a formatted String with one line1 like

@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). Something like,

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

1And 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).


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.