have to creat a geosolid class that has 3 sub class i choose sphere cylinder and cone, i got the first two to work but my cone class works except when it calculates the volume it keep getting 0 no matter what number is use for the hieght and radius

import java.util.Scanner;
import java.text.DecimalFormat;

public class Cone extends GeoSolids
{
 double vol, surf_area;
 int size = 0;

 public Cone(int radi, int h)
  {
   super(radi, h);

  }

 public double calcV()
  {
   vol = 1/3 * Math.PI * height * Math.pow(radius, 2);
   return vol;
  }

 public double calcSurf_A()
  {
   surf_area = Math.PI * radius * size
                + Math.PI * Math.pow(radius, 2);
   return surf_area;
  }


 public String toString()
  {
    DecimalFormat fmt = new DecimalFormat("0.##"); 
     return super.toString() + "\nThe volume is " + fmt.format(vol)
                   + "\nThe surface area is " + fmt.format(surf_area);
  }
}

cna some please help me fix this problem

Recommended Answers

All 10 Replies

You just need to specify the 1/3 so that it uses floating point division instead of int division, so change it to 1.0/3.0 or 1/3d

1/3d * Math.PI * height * Math.pow(radius, 2);

You were getting zero because 1/3 as an int is zero, causing the entire expression to yield zero. Always keep that in mind when mixing integer and floating point primitives in expressions.

You just need to specify the 1/3 so that it uses floating point division instead of int division, so change it to 1.0/3.0 or 1/3d

1/3d * Math.PI * height * Math.pow(radius, 2);

You were getting zero because 1/3 as an int is zero, causing the entire expression to yield zero. Always keep that in mind when mixing integer and floating point primitives in expressions.

thank you
maybe ur.ll be able ot help me with the next part how would i be able to convert the whole program into a gui the easiest way?

ok i guess i should specify, i know how ot create gui's i was just having a hard time with this one
one more thing
for my parent class GeoSolid we have to create a generic geometric solid
i dont think I did that right may you look at my code and give some tips or pointers on how to fix it ?

import java.util.Scanner;
import java.text.DecimalFormat;


public class GeoSolids
{
 int radius;
 int height;

  public GeoSolids (int radi, int h)
   {
     radius = radi;
      height = h;
    }

  public GeoSolids ()
   {
     radius = 0;
      height = 0;
    }

  public void setRadius(int radi)
  {
   radius = radi;
  }

 public double getRadius()
  {
   return radius;
  }

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

 public double getHeight()
  {
   return height;
  }

  public String toString()
  {
   DecimalFormat fmt = new DecimalFormat("0.##");  
   String result = "The radius is " + fmt.format(radius) + "\nThe height is " + fmt.format(height);
    return result;

  }
}

i have attached the complete files for my program just in case you had any questions about why I did some things.
thanks again for all your help

Your Test class, when you using generics

public class Test {

    public static void main(String[] args) {
        GeoSolids circle = new Sphere(5);
        System.out.println(circle);

        /// and
     
        GeoSolids[] solid = new GeoSolids[3];
        solid[0] = new Sphere(5);
        solid[1] = new Cone(5,5);
        solid[2] = new Cylinder(5,5);
        for (int i = 0; i < 3; i++) {
            System.out.println(solid[i]);
        }
    }
}

complete code of GeoSolids class

public abstract class GeoSolids {

    public abstract double getVolume();

    public abstract double getArea();
}

example

public class Sphere extends GeoSolids {

    int radius;
// konstruktor
    public Sphere(int radius) {
        this.radius = radius;

    }

   ////  override abstract methods

    }
    public String toString(){
        return ///name of shape, input parameters, volume,area
    }
}

you don't need store in shape classes volume and area, always you can get them : getVolume() or getArea().
All code you have wrote . (some changes - and generic program is ready.

ok so if i get and set the volume and surface area in the GeoSolid class
as an abstract do i still need to put the formulas in the subclasses since the 3 geosolids formulas are diff?

If functions declared inside GeoSolids-class are abstract ,then
we must implement them in not-abstract subclasses of GeoSolids-class.
Sphere-class is not abstract,
then we must override functions getVolume() and getSurf_Area().
Also in Cone-class and Cylinder-class. For all shape math. formula is different.
I little simplified your classes.
Write the other two classes in the same way from scratch.

4 hours later..
A look at the problem from the other side
a simplest generics example:

package dnmooresimplest;

public class MainGenerics<E extends Comparable<? super E>> {

    private E e;

    public MainGenerics(E e) {
        this.e = e;
    }

    public E getValue() {
        return e;
    }

    public static void main(String[] args) {
        Integer i = Integer.valueOf(1);
        Double d = Double.valueOf(1.111111111111111111111111111111);
        Float f = Float.valueOf(1.11111111111111111111111111111F);
        Byte b = Byte.valueOf((byte) 127);

        MainGenerics mainInteger = new MainGenerics(i);//[unchecked] 
        System.out.println(mainInteger.getValue());


        MainGenerics mainDouble = new MainGenerics(d);//[unchecked] 
        System.out.println(mainDouble.getValue());

        MainGenerics mainFloat = new MainGenerics(f);//[unchecked] 
        System.out.println(mainFloat.getValue());

        MainGenerics mainByte = new MainGenerics(b);//[unchecked] 
        System.out.println(mainByte.getValue());

        System.out.println("================");

        MainGenerics<Integer> maini = new MainGenerics<Integer>(i);
        System.out.println(maini.getValue());

        MainGenerics<Double> maind = new MainGenerics<Double>(d);
        System.out.println(maind.getValue());

        MainGenerics<Float> mainf = new MainGenerics<Float>(f);
        System.out.println(mainf.getValue());

        MainGenerics<Byte> mainb = new MainGenerics<Byte>(b);
        System.out.println(mainb.getValue());

    }
}

Result of compliation:

compiled with option --->  -Xlint:unchecked
     Compiling 1 source file to C:\Documents and Settings\j3c\Moje dokumenty\NetBeansProjects\dnmooreSimplest\build\classes
C:\...\src\dnmooresimplest\MainGenerics.java:21: warning: [unchecked] unchecked call to MainGenerics(E) as a member of the raw type dnmooresimplest.MainGenerics
        MainGenerics mainInteger = new MainGenerics(i);
C:\...\src\dnmooresimplest\MainGenerics.java:25: warning: [unchecked] unchecked call to MainGenerics(E) as a member of the raw type dnmooresimplest.MainGenerics
        MainGenerics mainDouble = new MainGenerics(d);
C:\...\src\dnmooresimplest\MainGenerics.java:28: warning: [unchecked] unchecked call to MainGenerics(E) as a member of the raw type dnmooresimplest.MainGenerics
        MainGenerics mainFloat = new MainGenerics(f);
C:\...\src\dnmooresimplest\MainGenerics.java:31: warning: [unchecked] unchecked call to MainGenerics(E) as a member of the raw type dnmooresimplest.MainGenerics
        MainGenerics mainByte = new MainGenerics(b);
4 warnings

Result of run:

run:
1
1.1111111111111112
1.1111112
127
================
1
1.1111111111111112
1.1111112
127
BUILD SUCCESSFUL (total time: 2 seconds)

..."one more thing for my parent class GeoSolid we have to create a generic geometric solid" ...
-Main should looks like:

public static void main(String[] args) {
// for Integer values as parameter
        GeoSolids<Integer> circle = new Sphere<Integer>(5);
        System.out.println(circle);
// or for Double values
        GeoSolids<Double> circle = new Sphere<Double>(Double.valueOf(5.0D);
        System.out.println(circle);
//......
}

What are generics?
Gilad Bracha 2004 "Generics allow you to abstract over types."
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.