Passing values between multiple classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Buckaroo Banzai
    New Member
    • Mar 2007
    • 5

    #1

    Passing values between multiple classes

    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
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Please use code tags if you have to post code.
    2.)Use appropriate names for your classes (your class2 above will not compile).
    3.)You can use getters to get data from objects or pass an entire object to the constructor of another (deepending on your design).
    Last edited by r035198x; Jun 24 '08, 07:20 AM. Reason: Removed the long quote

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      moderator note: I have removed the offending message and two replies so that
      the show can go on as usual.

      kind regards,

      Jos

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        How to get values from your base class (Class1):
        read about super() if you want to call the toString() method (that you overload)from your base class (Class1) in your extended class (Class2).
        You can also pass values into your extended class if you call getter and setter methods from your base class which are not overloaded.

        Why it returns 0 and not for example 4 in your getSum() method if you call it with "new Class2(2)" is unclear to me; it should do it right from what I have seen. Maybe you have an error in your main() program ? I mean maybe you call it the wrong way?
        Please post your main program where you call these classes.

        Comment

        Working...