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:
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:
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.
Re: connecting two classes?
whahah i thought my idea would be possible.... i just tried something that came up to my mind
:-j8-}
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.
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......
Re: connecting two classes?
...
Copy paste, and change the class name. Like I said, not very useful.
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;
}
}
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;
}
}
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!
Re: connecting two classes?
ill just study its logics and it might be usefull for some future purposes tnx tnxt tnx alot!
\m/
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!
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?