When I try to compile this code, I keep getting this error: > javac Triangle.jav
ID: 3623561 • Letter: W
Question
When I try to compile this code, I keep getting this error:> javac Triangle.java
Triangle.java:38: inconvertible types
found : void
required: java.lang.String
s = (String) btsvec.addElement(1);
^
Note: Triangle.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Here is my code:
import java.util.*;
public class Triangle
{
public double a;
public double b;
private double c;
Vector btsvec = new Vector();
public void setC(double x)
{
if (x <= 0)
System.out.println("Value must be above 0");
else
this.c = x;
btsvec.addElement(x);
}
public double getC()
{
return this.c;
}
public void showAllCs()
{
System.out.println(btsvec.toString());
}
public boolean isRightTriangle;
public boolean isRightTriangle()
{
if (a*a + b*b == c*c)
this.isRightTriangle = true;
else
this.isRightTriangle = false;
}
public Triangle()
{
this.a = 3;
this.b = 4;
this.c = 5;
String s;
s = (String) btsvec.addElement(1);
}
private static int numMade = 0;
public Triangle(double a, double b, double c)
{
this.a = a;
this.b = b;
this.c = c;
btsvec.addElement(c);
numMade = ++numMade;
}
}
I don't know what I'm missing. Can anyone help?
Explanation / Answer
please rate - thanks
the type of vector wasn't specified
I don't know what you were doing with
s = (String) btsvec.addElement(1);
but it compiles now, and I corrected it to what I think you want
import java.util.*;
public class Triangle
{
public double a;
public double b;
private double c;
private boolean isRightTriangle;
Vector<Double> btsvec = new Vector<Double>();
public void setC(double x)
{
if (x <= 0)
System.out.println("Value must be above 0");
else
{this.c = x;
btsvec.addElement(x);
}
}
public double getC()
{
return this.c;
}
public void showAllCs()
{
System.out.println(btsvec.toString());
}
public void isRightTriangle()
{
if (a*a + b*b == c*c)
this.isRightTriangle = true;
else
this.isRightTriangle = false;
}
public Triangle()
{
this.a = 3;
this.b = 4;
this.c = 5;
btsvec.addElement(a);
btsvec.addElement(b);
btsvec.addElement(c);
}
private static int numMade = 0;
public Triangle(double a, double b, double c)
{
this.a = a;
this.b = b;
this.c = c;
btsvec.addElement(a);
btsvec.addElement(b);
btsvec.addElement(c);
numMade = ++numMade;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.