Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning values from a one method in a class....to another method in a class?

 
Gus Parker
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
I am having huge problems, I just don't seem to be able to do it, I have tried everything, and I'm sure I'm doing something wrong. Please if anybody could help me it would be great.

I'm trying to pass a string, into a method within' another class. I know this might seem silly, but I've tried many different things, if someone could help me out or point me in the right direction?

Below is a copy of the Class's I'm working with......
I have put the problem area's (x2) and explanations in green.










Any help would be much appreciated?
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this might seem silly, but I've tried many different things, if someone could help me out or point me in the right direction?


Well, just what "many different things" have you tried?



The target methods doesn't take any parameterts, or return anything.



And at the source, you don't even call the target method.

Henry
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write down a pair of tiny little classes which pass and return information to each other. Solve a little problem, then apply the techniques to a larger problem.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Campbell said !!!

Oh, that advice is SO wonderful!!!

I would guess that we were able to test 95% of the topics in our SCJP book with less than 20 lines of code.
 
Gus Parker
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. So i've done that and think i can apply this to my main program, before i go any further, may I ask 3 questions(below code)?










I still have a couple of questions? Maybe Im still doing it wrong but...
1. What is the point in "round.setRadius("");"...Does that sort of set it to default or something?
2. in the code chunk below, what is the point in creating "String P" ....I could only get it to work with a random string dropped in there?

public void setRadius(String p) {
radius = "Test";
}

3. Would it be wise to put these values into a constructor as well or wouldnt it matter - It does the same thing?
 
priya rishi
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. What is the point in "round.setRadius("");"...Does that sort of set it to default or something?


try this,
public void setRadius(String p) {
// radius = "Test";
}

2. in the code chunk below, what is the point in creating "String P" ....I could only get it to work with a random string dropped in there?
public void setRadius(String p) {
radius = "Test";
}
This is one of the Object Oriented Concepts - Data Encapsulation , where you make the instance variables private and use the getter and setter methods(Java Beans Standards) to access them.

In your example you can set the value for radius, like this

public void setRadius(String p)
{
radius = p;
}

3. Would it be wise to put these values into a constructor as well or wouldnt it matter - It does the same thing?


constructor will be invoked only when instantiation of an object is done.
one might even use inheritance to access the members of a class.


note: the default value for String is null.
 
Gus Parker
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
priya rishi wrote:
1. What is the point in "round.setRadius("");"...Does that sort of set it to default or something?


try this,
public void setRadius(String p) {
// radius = "Test";
}

2. in the code chunk below, what is the point in creating "String P" ....I could only get it to work with a random string dropped in there?
public void setRadius(String p) {
radius = "Test";
}
This is one of the Object Oriented Concepts - Data Encapsulation , where you make the instance variables private and use the getter and setter methods(Java Beans Standards) to access them.

In your example you can set the value for radius, like this

public void setRadius(String p)
{
radius = p;
}

3. Would it be wise to put these values into a constructor as well or wouldnt it matter - It does the same thing?


constructor will be invoked only when instantiation of an object is done.
one might even use inheritance to access the members of a class.


note: the default value for String is null.


thanks very much for your reply priya, very helpful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic