Question

In my Java Course, there's an exercise for making a class called Area with 4 overloaded constructors to calculate the area of a circle, a triangle, a rectangle or a cillinder.

After solving 6 errors, I still have 3 left.

This is the code I used:

import java.lang.Math;

class Area {

    public double pi = Math.PI;
    private int b, l, w, area;
    private double r, h;

    public Area(){
    }

    public Area(double radio){
        r = radio;
        area = pi * r * r;
    }

    public Area(int base, double alt){
        b = base;
        h = alt;
        double o5 = 0.5;
        double db = (double) b;
        area = o5 * db * h;
    }

    public Area(int lar, int anc){
        l = lar;
        w = anc;
        area = l * w;
    }

    public Area(double radio, double alt){
        r = radio;
        h = alt;
        area = pi * r * r * h;
    }
}

public class JavaCLab2P144 {

    /**
    * @param args
    */
public static void main(String[] args){

    Area circ = new Area(4.0);
    Area tria = new Area(6,3.0);
    Area rect = new Area(2,4);
    Area cili = new Area(4.0,10.0);

    System.out.println("Area de un circulo:\t" + circ);
    System.out.println("Area de un triangulo:\t" + tria);
    System.out.println("Area de un rectangulo:\t" + rect);
    System.out.println("Area de un cilindro:\t" + cili);
    }
}

This is the error I get:

java/JavaCLab2P144/JavaCLab2P144.java:14: error: possible loss of precision
        area = pi * r * r;
                      ^
   required: int
   found:    double
java/JavaCLab2P144/JavaCLab2P144.java:22: error: possible loss of precision
            area = o5 * db * h;
                       ^
  required: int
  found:    double
java/JavaCLab2P144/JavaCLab2P144.java:34: error: possible loss of precision
        area = pi * r * r * h;
                          ^
  required: int
  found:    double
3 errors
Was it helpful?

Solution

You cannot implicitly convert from double to it since you will possibly lose precision as errors indicate. You need an explicit cast to it using cast operator : (int) after assignment operator and becaude of order of ops you should put parens around multiplications like so

Area= (int)(pi * r * r);

There are many other issues to consider in your class such as choice of it for area instead of double since result is a double. As other poster stated you should use double instead for area field. You also don't need to declare variable for pi since it'd already a static cost on math or at least make it private static final

OTHER TIPS

Problem is that double can have values like 1.5 which in case you want to change into int could be 1 or 2 (or is just you're mistake in coding). So java tells you that you have to do something about it.

To solve it you can cast double to int, which cuts to full number

double x = 1.2;
double y = 1.7;
int a = (int) x; // a == 1
int b = (int) y; // b == 1

Other option (in you're case better) is using Math#round(double) function

double x = 1.2;
double y = 1.7;
// Math.round(double) return long, so you also have to cast it into int
int a = (int) Math.round(x); // a == 1
int b = (int) Math.round(y); // b == 2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top