Я продолжаю получать эти надоедливые ошибки ....несовместимые типы в Java
Figures.java:106: error: incompatible types: possible lossy conversion from double to int
Rectangle figure = new Rectangle(width, length, x, y);
^
Figures.java:116: error: incompatible types: possible lossy conversion from double to int
s = getDimension("side");
.... даже если я длина и ширина сайта в парном разряде, то getDimension является двойной, а в прямоугольнике. java file, я устанавливаю их как парные.
здесь Прямоугольник класс
// ---------------------------------
// File Description:
// Defines a Rectangle
// ---------------------------------
public class Rectangle extends Point
{
private int x, y; // Coordinates of the Point
private double length, width;
public Rectangle(int x, int y, double l, double w)
{
super(x, y);
length = l;
width = w;
}
public int getX() {return x;}
public int getY() {return y;}
public double getLength() {return length;}
public double getWidth() {return width;}
public double area() {return length * width;}
public String toString() {return "[" + x + ", " + y + "]" + " Length = " + length + " Width = " + width;}
}
и вот мой главный файл тестирования, Figures.java, в котором большая часть работы выполняется
// ---------------------------------
// Problem Description:
// Create geometric figures
// ---------------------------------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class Figures extends JApplet implements ActionListener
{
private static final int POINT = 0, // JButton IDs
CIRCLE = 1,
CYLINDER = 2,
RECTANGLE = 3,
CUBE = 4,
SQUARE = 5;
private static final DecimalFormat precision2 = new DecimalFormat("0.00");
private JButton[] f_buttons;
private String[] figures = {"Point", "Circle", "Cylinder",
"Rectangle", "Cube", "Square"};
public void init()
{
Container c = getContentPane();
c.setLayout(new FlowLayout());
f_buttons = new JButton[figures.length];
for (int i = 0; i < figures.length; i++)
{
c.add(f_buttons[i] = new JButton(figures[i]));
f_buttons[i].addActionListener(this);
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == f_buttons[POINT])
createPoint();
else if (e.getSource() == f_buttons[CIRCLE])
createCircle();
else if (e.getSource() == f_buttons[CYLINDER])
createCylinder();
else if (e.getSource() == f_buttons[RECTANGLE])
createRectangle();
else if (e.getSource() == f_buttons[CUBE])
createCube();
else if (e.getSource() == f_buttons[SQUARE])
createSquare();
}
private int getCoordinate(String msg)
{
String s= JOptionPane.showInputDialog("Enter " + msg + " coordinate");
return Integer.parseInt(s);
}
private double getDimension(String msg)
{
String s= JOptionPane.showInputDialog("Enter " + msg);
return Double.parseDouble(s);
}
private void createPoint()
{
Point figure = new Point(getCoordinate("x"), getCoordinate("y"));
showStatus(figures[POINT] + ": " + figure.toString());
}
private void createCircle()
{
int x = getCoordinate("x"),
y = getCoordinate("y");
double radius = getDimension("radius");
Circle figure = new Circle(radius, x, y);
showStatus(figures[CIRCLE] + ": " + figure.toString() +
"; Area = " + precision2.format(figure.area()));
}
private void createCylinder()
{
int x = getCoordinate("x"),
y = getCoordinate("y");
double radius = getDimension("radius"),
height = getDimension("height");
Cylinder figure = new Cylinder(height, radius, x, y);
showStatus(figures[CYLINDER] + ": " + figure.toString() +
"; Area = " + precision2.format(figure.area()) +
"; Volume = " + precision2.format(figure.volume()));
}
private void createRectangle()
{
int x = getCoordinate("x"),
y = getCoordinate("y");
double length = getDimension("length"),
width = getDimension("width");
Rectangle figure = new Rectangle(length, width, x, y);
showStatus(figures[RECTANGLE] + ": " + figure.toString() +
"; Area = " + precision2.format(figure.area()));
}
private void createCube()
{
int x = getCoordinate("x"),
y = getCoordinate("y"),
s = getDimension("side");
double radius = getDimension("radius"),
height = getDimension("height");
Cube figure = new Cube(height, radius, x, y);
showStatus(figures[CUBE] + ": " + figure.toString() +
"; Area = " + precision2.format(figure.area()) +
"; Volume = " + precision2.format(figure.volume()));
}
private void createSquare()
{
int x = getCoordinate("x"),
y = getCoordinate("y");
double radius = getDimension("radius"),
height = getDimension("height");
Square figure = new Square(height, radius, x, y);
showStatus(figures[SQUARE] + ": " + figure.toString() +
"; Area = " + precision2.format(figure.area()) +
"; Volume = " + precision2.format(figure.volume()));
}
}
Просто введите значения. Дело в том, что метод, который вы используете, принимает ints, вы предоставляете 'double'. Это означает, что вы теряете некоторую точность, поскольку число будет по существу усечено. – Rogue
Изменить на: 'Прямоугольник = новый прямоугольник (x, y, длина, ширина);' –