Ask Experts Questions for FREE Help !
Ask
    A Lonely Star's Avatar
    A Lonely Star Posts: 1, Reputation: 1
    New Member
     
    #1

    Nov 22, 2013, 01:16 AM
    What does it mean by the term :Type mismatch: cannot convert from double to float
    1. Hi, Iam a beginner in the amazing world of programming . And I am trying to learn JAVA on my own. While practicing , using abstract method I wrote this code to calculate the area of some geometrical figures. But the compiler gave the message :

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Type mismatch: cannot convert from double to float

    at Shape.<init>(AbstractMethodDemo.java:6)
    at Square.<init>(AbstractMethodDemo.java:15)
    at AbstractMethodDemo.main(AbstractMethodDemo.java:60 )

    but after trying hard I can't make out what's the problem here with double and float. Please could you give me an explanation ? Help me please

    2.code:

    abstract class Shape
    {
    public static float pi = 3.142;
    protected float height;
    protected float width;

    abstract float area();
    }

    class Square extends Shape
    {
    Square(float h, float d)
    {
    height = h;
    width = d;
    }

    float area()
    {
    return height * width;
    }
    }

    class Rectangle extends Shape
    {
    Rectangle(float h, float w)
    {
    height = h;
    width = w;
    }

    float area()
    {
    return height * width;
    }
    }

    class Circle extends Shape
    {
    float radius;

    Circle(float r)
    {
    radius = r;
    }

    float area()
    {
    return Shape.pi * radius *radius;
    }
    }

    public class AbstractMethodDemo
    {
    public static void main(String args[])
    {
    Square sObj = new Square(5,5);
    Rectangle rObj = new Rectangle(5,7);
    Circle cObj = new Circle(2);

    System.out.println("Area of square : " + sObj.area());
    System.out.println("Area of rectangle : " + rObj.area());
    System.out.println("Area of circle : " + cObj.area());
    }
    }
    Scleros's Avatar
    Scleros Posts: 2,165, Reputation: 262
    Hardware Expert
     
    #2

    Nov 30, 2013, 04:57 AM
    The compiler found an object of type double where it needed a type of float, hence the type mismatch. The compiler tells you the line numbers of the errors. What compiler are you using? The Oracle command line compiler shows the exact error position.

    You can either cast the double to a float or change the float to a double.

    Also read Data Types and note the info about double and where it would apply in your code.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Is there any Float Switch in which the float slides up and down on the rod? [ 3 Answers ]

Is there any Float Switch in which the float slides up and down on the rod?

Engineering term for a particular type of resistance to force [ 0 Answers ]

Hello Is there an engineering term that references a body of material’s resistance to penetration? Interested in the body being fluid (water) as well as semi sold (i.e. granular cereal foods). One case in point: I am attempting to configure a weight that will penetrate water but will...

Screeching Fluidmaster type 400 Float valve [ 8 Answers ]

Hi About 8 months ago I fitted Fluidmaster type 400 Float valve to my toilet cistern. It has recently started to make a (loud) screeching noise at the start of the flush. Any thoughts on cause and cure. Cheers David:)

Convert double sink to single [ 3 Answers ]

It is very hard to wash pots and pans so I want to convert my 33" double sink into a deeper single sink (9 inches). I have a limited budget so I wanted to know if my plumber is going to charge me a lot of money for this? Currently I have a disposal on 1 side and a drain on the other. Is this...

Convert plumbing from single vanity to double [ 25 Answers ]

We had a single sink vanity in place. Now we're remodeling and will be getting a 60" double sink vanity. Unfortunately I cannot use the stubs/drain that are existing, because the vanity has cabinet/drawer vertical supports that are right where the current supplies stub out. Can someone tell me...


View more questions Search