Go Back   Java Programming Forums > Java Standard Edition Programming Help > Object Oriented Programming


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 31-08-2009, 06:07 AM
chronoz13's Avatar
Java kindergarten
 

Join Date: Mar 2009
Location: Squatters Mansion
Posts: 405
Thanks: 113
Thanked 21 Times in 21 Posts
chronoz13 is on a distinguished road

I'm feeling Stressed
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

Java Code
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"


Java Code
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-----


Java Code
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..."

Java Code
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



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 01-09-2009, 01:32 AM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,342
Thanks: 5
Thanked 284 Times in 256 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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:

Java Code
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:

Java Code
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.
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
  #3 (permalink)  
Old 01-09-2009, 02:53 AM
chronoz13's Avatar
Java kindergarten
 

Join Date: Mar 2009
Location: Squatters Mansion
Posts: 405
Thanks: 113
Thanked 21 Times in 21 Posts
chronoz13 is on a distinguished road

I'm feeling Stressed
Default Re: connecting two classes?

whahah i thought my idea would be possible.... i just tried something that came up to my mind
Reply With Quote
  #4 (permalink)  
Old 01-09-2009, 05:16 AM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,342
Thanks: 5
Thanked 284 Times in 256 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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.
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
The Following User Says Thank You to helloworld922 For This Useful Post:
chronoz13 (01-09-2009)
  #5 (permalink)  
Old 01-09-2009, 05:37 AM
chronoz13's Avatar
Java kindergarten
 

Join Date: Mar 2009
Location: Squatters Mansion
Posts: 405
Thanks: 113
Thanked 21 Times in 21 Posts
chronoz13 is on a distinguished road

I'm feeling Stressed
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......
Reply With Quote
  #6 (permalink)  
Old 01-09-2009, 02:16 PM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,342
Thanks: 5
Thanked 284 Times in 256 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
Default Re: connecting two classes?

...

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

Java Code
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;
     }
}
Java Code
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;
     }
}
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
The Following User Says Thank You to helloworld922 For This Useful Post:
chronoz13 (01-09-2009)
  #7 (permalink)  
Old 01-09-2009, 03:36 PM
chronoz13's Avatar
Java kindergarten
 

Join Date: Mar 2009
Location: Squatters Mansion
Posts: 405
Thanks: 113
Thanked 21 Times in 21 Posts
chronoz13 is on a distinguished road

I'm feeling Stressed
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!
Reply With Quote
  #8 (permalink)  
Old 01-09-2009, 03:37 PM
chronoz13's Avatar
Java kindergarten
 

Join Date: Mar 2009
Location: Squatters Mansion
Posts: 405
Thanks: 113
Thanked 21 Times in 21 Posts
chronoz13 is on a distinguished road

I'm feeling Stressed
Default Re: connecting two classes?

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

Reply With Quote
  #9 (permalink)  
Old 01-09-2009, 03:41 PM
chronoz13's Avatar
Java kindergarten
 

Join Date: Mar 2009
Location: Squatters Mansion
Posts: 405
Thanks: 113
Thanked 21 Times in 21 Posts
chronoz13 is on a distinguished road

I'm feeling Stressed
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!
Reply With Quote
  #10 (permalink)  
Old 01-09-2009, 08:15 PM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,342
Thanks: 5
Thanked 284 Times in 256 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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?
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
The Following User Says Thank You to helloworld922 For This Useful Post:
chronoz13 (02-09-2009)
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] linking two different classes John Object Oriented Programming 11 27-04-2009 07:57 PM
Computing Classes tazjaime What's Wrong With My Code? 3 26-04-2009 06:18 PM
[SOLVED] using 2 classes AZBOY2000 Object Oriented Programming 7 21-04-2009 11:55 AM
Connecting to Oracle from within Eclipse kairamr Java IDEs 1 12-11-2008 09:16 PM
Help using Abstract classes with arrays crazydeo Collections and Generics 2 10-06-2008 04:45 PM


100 most searched terms
Search Cloud
2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java http://www.javaprogrammingforums.com/object-oriented-programming/3713-limiting-decimal-places-double.html java 2d arraylist java actionlistener java addactionlistener java double format java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programmers forum java programming forum java programming forums java programming help java project ideas java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextarea font color jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream two dimensional arraylist java writing ipod apps

All times are GMT. The time now is 12:05 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.