1

My program calculates the surface area of a cone (pi * radius * slant-height). I'm using an if-elseif ladder which says that if the slant-height is left empty and the vertical-height is entered, the program will calculate the slant-height via Pythagoras theorem. But I don't know how to make the program accept a null value

I tried using if(slant_height==null) but it says that the types are incompatible and == is a "bad operator type"

{
public void CSAcone(double radius,double slant_height,double height) {
    if (slant_height == null)
    {
         slant_height=Math.sqrt((radius*radius) + (height*height));
         double CSA=(22*radius*slant_height) / 7;

         System.out.println("radius= "+radius);
         System.out .println("height= "+slant_height);
         System.out .println("Curved Suface Area= "+CSA);
    }

    if(height == null) 
    {
        double CSA=(22*radius*slant_height) / 7;

        System.out.println("radius= " + radius);
        System.out.println("height= " + slant_height);
        System.out.println("Curved Suface Area= " + CSA);
    }
}}

5
  • As the method signature is written, slant_height will never be null, because you cannot call CSAcone by providing a null value for that variable, the code wouldn't compile. Notice slant_height is double, not Double. Commented May 18, 2019 at 8:44
  • 1
    Read this, stackoverflow.com/questions/8643276/object-vs-primitive Commented May 18, 2019 at 8:54
  • Use object types, not primitive types. Commented May 18, 2019 at 9:41
  • You can use isEmpty() method for checking null value Commented May 18, 2019 at 9:53
  • 1
    @Saim, isEmpty() method is only applicable on strings Commented May 19, 2019 at 12:59

3 Answers 3

1

try this :

 public void CSAcone(double radius, double slant_height, double height) {
    if (slant_height == 0.0f) {
        slant_height = Math.sqrt((radius * radius) + (height * height));
        double CSA = (22 * radius * slant_height) / 7;
        System.out.println("radius= " + radius);
        System.out.println("height= " + slant_height);
        System.out.println("Curved Suface Area= " + CSA);
    }
    if (height == 0.0f) {
        double CSA = (22 * radius * slant_height) / 7;
        System.out.println("radius= " + radius);
        System.out.println("height= " + slant_height);
        System.out.println("Curved Suface Area= " + CSA);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I could have done that but it would mean that the user has to enter 0.0 in one of the boxes
0

Change your variable types to the wrapper classes(Integer, Double) of primitive types(int, double). In Java, primitive types cannot be null.

Comments

0

null can be cast to any reference type, but not to any primitive type such as int or boolean.

You can use primitive types and send a special value to specify emptyness.

Or just use object types (Integer, Boolean, Double etc.) and check null values(as you did).

public void CSAcone(Double radius,Double slant_height,Double height){
   if(radius == null){}
   if(slant_height == null){}
   if(height == null){}
   // ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.