Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: connecting two classes?

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default connecting two classes?

    my main program is logicaly simple , i just have to reverse a string and change it back,

    the problem is this.. here's the codes


    this first class will reverse the String that will be passed to each method //i dont have problems with this class

    public class FirstReverse
     
    {
     
        private static String reversed;
     
        public FirstReverse(String word)
     
        {
            reverse(word);
        } 
     
        private void reverse(String word)
        {
            reversed = new StringBuilder(word).reverse( ).toString( );        
        }
     
        public String getFirstReverse( )
        {
            return reversed;
        }
     
    }


    *Another Classs*

    HERE IS THE PROBLEM:

    --i want to revert back the strings that i have already reversed using the first class "FirstReverse"


    public class SecondReverse
     
    {
        private static String secondReverse;
     
        public SecondReverse(FirstReverse firstReversedWord)
        {
            reverseAgain(firstReversedWord);
        }
     
        private  void reverseAgain(FirstReverse firstReversedWord)
        {
            String tempReversedWord;
     
            tempReversedWord = firstReversedWord.toString( );
     
            secondReverse = new StringBuilder(tempReversedWord).reverse( ).toString( );
     
        }
     
        public String getSecondReverse( )
        {
            return secondReverse;
        }





    -----here's the main program that uses this two classes-----


    import java.util.*;
     
    public class SampleReverse
     
    {
     
        private static Scanner scanner = new Scanner(System.in);
     
        public static void main(String[] args)
        {
            String word,
                   firstReversedWord,
                   secondReversedWord;
     
            System.out.print("Enter word: ");
            word = scanner.nextLine( );
     
     
     
            FirstReverse firstRev = new FirstReverse(word);
            firstReversedWord = firstRev.getFirstReverse( );
     
            SecondReverse secondRev = new SecondReverse(firstRev);
            secondReversedWord = secondRev.getSecondReverse( );
     
     
     
     
            System.out.println("First Reverse:  " + firstReversedWord);
            System.out.println("Second Reverse:  " + secondReversedWord);
     
        }
    }







    the problem is the secondReversedWord and its method only diplays a String represtentaion of
    a memory address .(in the main program)


    that's the problem

    ---

    *I SOLVED IN IN THIS WAY* "BUT BUT BUT BUT..."

    public class SecondReverse
     
    {
        private static String secondReverse;
     
        public SecondReverse(String reversedWord)
        {
            reverseAgain(reversedWord);
        }
     
        private  void reverseAgain(String reversedWord)
        {
     
            secondReverse = new StringBuilder(reversedWord).reverse( ).toString( );
     
        }
     
        public String getSecondReverse( )
        {
            return secondReverse;
        }
    }

    *BUT BUT IF I DID IT THIS WAY THEN THIS CLASS CAN HANDLE ANYTHING BY ITSELF*
    *IT DOES DO THE THINGS THAT THE "FirstReverse Class" do

    my point is this



    1st: I want to reverse A String by passing it to the firstClass "First Reverse" //this part is easy

    2nd: I want to revert it back by PASSING THE object of the "FirstReverse" class INTO the
    "SecondReverse class" that has alreay the reversed String



    -----------------------------------------------------------------------------------------------------
    in other words. the "SecondReverse Class" CANNOT GENERATE ANYTHING IF I'M NOT YET REVERSING A STRING
    USING THE "FirstReverse Class"


    or in other words.

    THE "SecondReverse CLASS" IS USELESS IF THE USER IS NOT USING THE "FirstReverse CLASS" FIRST

    *FirstReverse --------->>> SecondReverse*


    -----


    i want to connect the second class from the first class, something like that....


    is my program possible?
    -----

    i hope you understand the logic that i want to make on this program..

    please help... tnx


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: connecting two classes?

    ??? That makes no sense. If you want to reverse a string, you don't need two classes to do it. Simply have your first class reverse itself:

    public class ReverseString
    {
         String txt;
         public ReverseString(String str)
         {
              this.txt = str;
         }
     
         public void doReverse()
         {
              txt= new StringBuilder(txt).reverse().toString(); 
         }
     
         public String toString()
         {
              return txt;
         }
    }

    So, to call:

    public class Tester
    {
         public static void main(String[] args)
         {
              ReverseString a = new ReverseString("Test");
              a.doReverse();
              System.out.println("Test reversed is " + a);
              a.doReverse();
              System.out.println("Reversed again is " + a);
         }
    }

    As for returning a memory address, unless you have an overriding toString method in your class, it'll use the default Object class toString method, which (you guessed it) returns the memory address as a string.

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: connecting two classes?

    whahah i thought my idea would be possible.... i just tried something that came up to my mind

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: connecting two classes?

    Oh, it's definately possible. The question is why would you want to do that? In essence, you're creating two different identical classes Not too many uses I can think of for that.

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (September 1st, 2009)

  6. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: connecting two classes?

    i just want to try something hehehehe.. so its possible ?

    how can i do that with this kind of problem??

    can you please modify it please.......

    i want to solve this one......

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: connecting two classes?

    ...

    Copy paste, and change the class name. Like I said, not very useful.

    public class ReverseString1
    {
         String txt;
         public ReverseString(String str)
         {
              this.txt = str;
         }
     
         public static String reverseAnother(ReverseString1 string)
         {
              return newStringBuilder(string).reverse().toString();
         }
     
     
         public static String reverseAnother(ReverseString2 string)
         {
              return newStringBuilder(string).reverse().toString();
         }
     
         public void doReverse()
         {
              txt= new StringBuilder(txt).reverse().toString(); 
         }
     
         public String toString()
         {
              return txt;
         }
    }

    public class ReverseString2
    {
         String txt;
         public ReverseString(String str)
         {
              this.txt = str;
         }
     
         public static String reverseAnother(ReverseString1 string)
         {
              return newStringBuilder(string).reverse().toString();
         }
     
     
         public static String reverseAnother(ReverseString2 string)
         {
              return newStringBuilder(string).reverse().toString();
         }
     
         public void doReverse()
         {
              txt= new StringBuilder(txt).reverse().toString(); 
         }
     
         public String toString()
         {
              return txt;
         }
    }

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (September 1st, 2009)

  9. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: connecting two classes?

    ahh so world.. it's possible but its not advisable? ryt?

    ahh if thats the case ill be aware of these things...

    i'm just trying to mix up something... anyway tnx for the effort sir..!! i really appreciate it tnx alot again!

  10. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: connecting two classes?

    ill just study its logics and it might be usefull for some future purposes tnx tnxt tnx alot!


  11. #9
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: connecting two classes?

    defining a class is for ONE specific task.

    creating a class that can only be used by using another class FIRST doesnt make any sense

    we are creating something for such a specific purpose .. do i have a point here sir?

    if i am.. then ill take this as another knowledge for my career tnx sir!

  12. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: connecting two classes?

    Yes, design a class for a purpose. However, that purpose may have many functions (and uses). One of the key elements of Java is to not re-design something when it works great, especially if all you're doing is copying

    Here's how I design classes:
    1. What do I want the class to do?
    2. What are some (as close to all as possible, but not all) ways this class could be used?
    3. How do I want to implement 1 & 2 (fields and methods)?

    Try to keep 1 as tight as possible, and expand 2. 3 is just to make it work. You will also reach many points where you will need to make compromises. In these cases, 2 and 3 change
    2a. What are some (as close to all as possible, but not all) ways this class could be used?
    2b. Which are more important, and which are less important?
    3a. Which do I choose to (not to) implement?
    3b. What should I optimize for?

  13. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (September 2nd, 2009)

Similar Threads

  1. [SOLVED] How to link two different class?
    By John in forum Object Oriented Programming
    Replies: 11
    Last Post: April 27th, 2009, 02:57 PM
  2. Why output is displaying 0 for calculation in java?
    By tazjaime in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 26th, 2009, 01:18 PM
  3. [SOLVED] Java program using two classes
    By AZBOY2000 in forum Object Oriented Programming
    Replies: 7
    Last Post: April 21st, 2009, 06:55 AM
  4. Replies: 1
    Last Post: November 12th, 2008, 04:16 PM
  5. Java program with abstract class along with two subclasses
    By crazydeo in forum Collections and Generics
    Replies: 2
    Last Post: June 10th, 2008, 11:45 AM