Hi,
I just started learning JAVA and I'm really NOT sure how to manipulate values in different classes.
for example. I have a class 'class1' which updates some variables for its own use.
then I have another class 'class2' which should take the result of the computation in 'class1' and use that value to perform some other operations.
I then have the main program file in a separate file that returns the result of the two classes.
is this possible? I know that from my main program, I can change the values on the classes. but I cannot seem to get values from one class to another.
class1 code
public class Class1
{
double length; //line size
public Class1( double length )
{
setLength( length ); //calls method setLength
}
public void setLength( double lengthValue )
{
length = lengthValue;
}
public double getLength()
{
return length;
}
public String toString()
{
return "Line Size = " + length;
}
}
//THE SECOND CLASS
public class Class2 extends Class1{
public double width;
public Rectangle( double lengthValue)
{
width = lengthValue;
}
public void setWidth( double lengthValue )
{
width = lengthValue; //I THINK THIS IS WHERE THE PROBLEM IS
}
public double getWidth()
{
return width;
}
public double getSum()
{
return width * 2;
}
public String toString()
{
return "Sum is (" + width + " * " + 2 + ") = " + getSum();
}
}
when I run the main problem the toString message above displays 0
again,
thank you for all your help...
BB
I just started learning JAVA and I'm really NOT sure how to manipulate values in different classes.
for example. I have a class 'class1' which updates some variables for its own use.
then I have another class 'class2' which should take the result of the computation in 'class1' and use that value to perform some other operations.
I then have the main program file in a separate file that returns the result of the two classes.
is this possible? I know that from my main program, I can change the values on the classes. but I cannot seem to get values from one class to another.
class1 code
public class Class1
{
double length; //line size
public Class1( double length )
{
setLength( length ); //calls method setLength
}
public void setLength( double lengthValue )
{
length = lengthValue;
}
public double getLength()
{
return length;
}
public String toString()
{
return "Line Size = " + length;
}
}
//THE SECOND CLASS
public class Class2 extends Class1{
public double width;
public Rectangle( double lengthValue)
{
width = lengthValue;
}
public void setWidth( double lengthValue )
{
width = lengthValue; //I THINK THIS IS WHERE THE PROBLEM IS
}
public double getWidth()
{
return width;
}
public double getSum()
{
return width * 2;
}
public String toString()
{
return "Sum is (" + width + " * " + 2 + ") = " + getSum();
}
}
when I run the main problem the toString message above displays 0
again,
thank you for all your help...
BB
Comment