Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

New to Java - need help with basics (calling a function, etc.)  RSS feed

 
Philip Jenson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've programmed in the past (mainly in C++), but it has been a while. Furthermore, I was always kind of hanging on by a thread in my C++ classes.

I have been assigned my first program in my Java course. I'm not coming here in hopes to find someone to do it for me. Rather, I'm coming here so you guys can help me with this, ultimately helping me learn better.

The following code is beyond rough - it's embarrassing. Parts of it haven't even been touched yet. I would like someone to just look over it and let me know what I'm doing right and what I'm doing wrong. I really don't know how to pass variables from one function to the next. Basically, the program accepts an input of British pence, shillings, and pounds. The comments pretty much describe what needs done.

If anything needs to be clarified more, let me know. Thanks a lot.

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Philip Jenson:
... I really don't know how to pass variables from one function to the next...

Welcome to JavaRanch!

Not bad at all for a first program assignment!

First, note that "this" is implicit in instance (non-static) methods. So, for example, within formattedDisplay, there is no need to say this.toDecimal(); You only need to say toDecimal();

As far as passing values, here's a good example of what seems to be giving you trouble.

The compiler will tell you that it cannot find the symbol "decimal." Yes, it's declared in the toDecimal method, but it's only a local variable there, limited to the scope of the toDecimal method body. So when you're in the method formattedDisplay, the identifier "decimal" has no meaning.

The double value assigned to "decimal" is returned by the method. So if you want to print that value, you can simply place the call inside the println...

System.out.println( toDecimal() );

Alternatively, if you want to use this value in places other than just the println, you can declare a new local variable inside of formattedDisplay and assign the value to that...

double dec = toDecimal();
System.out.println(dec);

Note that you could name this new local variable "decimal" and there would be no conflict with the other local variable of the same name.

On the other hand, what if you declared an instance variable with the same name as a local variable? That brings us back to use of "this." For example, in your constructor, you could identify your arguments as pound, shilling, and pence. These would be local variables within the scope of the constructor. But these are also the names of instance variables. In this case, you distinguish between instance and local variables using "this."
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

The Java Tutorials are a really good place to start and I refer to them a lot still. Also, don't forget to check out the style guide to help you make your code more readable. it'll mention things like making sure your brackets match up on the same position, among others, and it really does help.

As far as passing variables from one method to the next, it's easy to see if you look at the way a method is created and a method is called.


A constructor doesn't return anything and must have the same name as the class (so that it knows it is a constructor!). But other than that, it works like a method. It says "I'd like an integer that I will call po, an integer that I will call s, and an integer that I will call pe. Oh by the way, I will call them this, regardless of what name you use when you send them to me."

In methods you also specify a return type. So in your commented out method you need to peform the appropriate actions, and return an entire OldBritishMoney object (this?).

The next bit is the constructor working on the class itself, the part above the constructor where you declared those variables that follow "this". You're saying my variable pound is now equal to po, so on and so forth down the line. Then you call the normalize method.


It looks here like you're doing math to reduce to the most pounds, shillings, and pence you can. If you look at the first two variables, you'll notice you don't use them again. Remember, a variable declared inside a method dies with that method. Now further down, you are saying "I want my object's pence to equal the remainder after I divide by twelve(perhaps finding out how many you have after you've converted to shillings, and getting rid of the ones that got converted to shillings?). Then you are saying "I want my object's shillings to equal it's pence divided by 12". What would happen if you had 12 pence to start with?

Other than that you've got a few undeclared variables. Going down through the program. Remember you have to tell java about the variable before you use it.

Oh, as far as passing the value, remember when you typed the constructor you told it what data you want it to work with, and how you'll refer to that data.

You're saying "I want to make a new object", and the constructor sees the three integers in the parenthesis and fills it's variables "po", "s", and "pe". The same concept follow for passing values into other methods. You just have to know what values it wants.

I'm learning myself but it looks good, just a few things to work out and it'll be humming in no time!

Nate
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Philip,

welcome to the Ranch. Learning to program is tough - but dont worry, you'll
get there

Are you up to date on the object oriented concepts? if not, I'd try to
read a tutorial/introduction to the basic concepts of oriented programming.
Here's a link to Sun's tutorial on OO programming concepts


I really don't know how to pass variables from one function to the next.

First of all, we call 'em methods in Java I consider a function more
coarse grained (i.e. a function could be solved by several methods). A
method like

has two input parameters (the integer item and the integer amount) and returns
an integer value (the return cost statement). An example:

We calculate the cost based on the id and the amount. Two integers are
passed, we pass the integer id to another method and finally return an
integer to the main method.

Does this make sence?

Let us look at your program...

I've see you lack two methods:
//return the sum of this OldBritishMoney and other
public OldBritishMoney add(OldBritishMoney other)

//return the difference of this OldBritishMoney and other
public OldBritishMoney subtract(OldBritishMoney other)

Try to tell (your welcome to use "words" instead of Java code) me how you
imagine you should add two "OldBritishMoney" to each other..

I'll check back later and help you with the rest.

/Svend Rost

Edit: Noticed I wasn't the first one to answer, removed error in the
code.
[ January 25, 2007: Message edited by: Svend Rost ]
 
Philip Jenson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just wanted to thank you guys for going out of your way to help me out. I'm at work right now and I'll be going to class as soon as leave here.

I glanced through your suggestions and they look great. I am going to sit down and carefully read them as soon as I get home. I can already tell that I'll be able to figure out a lot of the things I was stumped on.

I will post here a little later today with an update. Once again, thanks so much!
 
Philip Jenson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having much more luck calling methods, thanks to you guys. At this point, I am trying to write the methods add, subtract, and read.

I've see you lack two methods:
//return the sum of this OldBritishMoney and other
public OldBritishMoney add(OldBritishMoney other)

//return the difference of this OldBritishMoney and other
public OldBritishMoney subtract(OldBritishMoney other)

Try to tell (your welcome to use "words" instead of Java code) me how you
imagine you should add two "OldBritishMoney" to each other..

Alright, I think that I need to call toPence to convert the amount to its lowest denomination. Then I need to accept the second set of input and call toPence to do the same. I guess the problem lies in how I should do that.
Obviously, I am not really sure what the other does. I realize it has to do with the othe rset of input, but I'm not sure what to do with it.


And then there is this.
It was made clear that I had to use a scanner to accomplish this. Honestly, I'm not even sure what needs done. Do you guys know what is meant by "return the updated this OldBritishMoney from the keyboard"?

Here is the example for a scanner that I have been looking at:


Thanks marc weber, Nathan Leniz, and Svend Rost for your patience and understanding. I will be working on this tonight and I'll be checking these forums regularly.

Thanks!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Philip Jenson:
... Obviously, I am not really sure what the other does. I realize it has to do with the othe rset of input, but I'm not sure what to do with it...

Look at the argument list for these methods:

...methodName(OldBritishMoney other) {...

"OldBritishMoney" is a type, and "other" is a local variable referencing an instance of OldBritishMoney.

So you're working with 2 instances of OldBritishMoney. Within these methods, you can refer to the current instance using "this" and the other instance using "other". For example, other.toPence().

I'm a little unsure about the read method. Do you have any more information about what it's supposed to do? (I'm probably being too literal, but I'm confused by the word "return" when the method's return type is void, and how an "updated" instance would be "from the keyboard." Maybe all this means is read keyboard input to create a new OldBrisishMoney instance, then perform whatever calculations you need, and display the updated version.)
[ January 25, 2007: Message edited by: marc weber ]
 
Philip Jenson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

Look at the argument list for these methods:

...methodName(OldBritishMoney other) {...

"OldBritishMoney" is a type, and "other" is a local variable referencing an instance of OldBritishMoney.

So you're working with 2 instances of OldBritishMoney. Within these methods, you can refer to the current instance using "this" and the other instance using "other". For example, other.toPence().

I'm a little unsure about the read method. Do you have any more information about what it's supposed to do? (I'm probably being too literal, but I'm confused by the word "return" when the method's return type is void, and how an "updated" instance would be "from the keyboard." Maybe all this means is read keyboard input to create a new OldBrisishMoney instance, then perform whatever calculations you need, and display the updated version.)

[ January 25, 2007: Message edited by: marc weber ]


Alright, I see what you're talking about. So, let me try to apply that to the adding method.

Would I do something similar to this:

Now, that's probably way off. When it compiles, it lets me know that it is expecting type OldBritishMoney, but type int is found.

Oh, and about the read method. What you typed about it is exactly what I thought. It doesn't make sense (the instructor gave us the descriptions). In the meantime, would it be fathomable to return a formatted amount that has been normalized? If so, would that have anything to do with using a scanner?

Thanks.
 
Dan Longest
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Now, that's probably way off. When it compiles, it lets me know that it is expecting type OldBritishMoney, but type int is found.


The method add() is declared to receive a type OldBritishMoney and return that same time. Your local variable totalAdd is an integer, not OldBritishMoney, so you cannot just return it in place of OldBritishMoney. You could do something like



which instantiates a new OldBritishMoney object and initializes it with 0 pounds, 0 shillings, and totalAdd pence and then returns a reference to it. Code to call the add function might look like:



Seeing this, I might consider putting in a constructor that takes one integer value, the total number of pence you have, and converts that internally to appropriate quantities of pounds, shillings, and pence. At this point, you should be able to make appropriate modifications to what I attempted in case I didn't fully understand your class workings or intentions. I am no Java expert so if I erred, someone else can jump in here and teach us both.

Regards,
Dan
 
Philip Jenson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been looking over those tutorials and doing my best to learn what I need to do. It's like I understand a lot of it, but I'm unable to apply what I've learned to my program. I changed a few things in my code. My biggest problem now is that I can't get the add and subtract methods to work. I tried doing what some of you suggested, but I'm still having trouble. I'm going to post my current code. It's still embarrassingly bad, but it's at least slightly better than before.

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an example, the return type for the add method is OldBritishMoney...

This means that you must return a reference of this type. The toPence method returns a primitive int value, and when you perform arithmetic on ints, you get ints as a result. So the step you're missing is turning that numeric int result into a new instance of OldBritishMoney that you can return from the method.
 
Nathan Leniz
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Been playing the airport game today, yay! Back into your problem.

First, much better formatting. Second, think of each object as a container that holds the information in the class you've typed. So OldBritishMoney b, c, and d ALL have pence, shillings, pounds, and the individual methods you've made. But they know absolutely nothing about eachother.

Now, when you are working with one object, you don't have to use the keyword this, and you've edited a lot of it out. If you call your normalize method by you are saying "I want that specific container's normalize method to go to work." So, with that and you know you are working with containers(objects) after you've made them from your blueprint(class), in your add and subtract methods you need to return an entire OldBritishMoney container.

It's been said but here you are returning an integer, not an object. But you've sent it an object in this code...See, you've said "I want container d to equal container b plus container c. So let's look at how it does the work. b's add method is called, and c is sent as the argument. So in b's add, c becomes "other". You could do something like... and so on and so forth to do the addition if you wanted. You don't have to use keyword "this" because you know you are in b's add method. But then it has to return a container. So inside b you've called b's add method, added c to b, and you need to return an object. Since you've done the work with b... (But remember you need to use a generic term, because it could be any OldBritishMoney object's add method)

Hope this helps a bit, and hope I made sense.

Also in looking through it again you may want to look at your normalize method. Go line by line through it and see what it will do. Also, normalize isn't returning anything nor does it take any arguments. So if you call an object's normalize method, wouldn't it be "this" anyway?

Nate

Nate
[ January 25, 2007: Message edited by: Nathan Leniz ]
 
Philip Jenson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for all your help. Every response was useful. I'm bery close to being done, but there's a few things wrong. The read() method takes in one set of numbers and returns another. Also, it seems like some of my values are being truncated. I'm expecting 5.21 to be returned from toDecimal, but 5.0 is returned.

Here is my current code and the testing file that I used.



The file that I'm testing it with:

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Philip Jenson:
...it seems like some of my values are being truncated. I'm expecting 5.21 to be returned from toDecimal, but 5.0 is returned...

Indeed, truncation is exactly what's happening. Why? You have an int, toPence, divided by another int, the literal 240. Since the operands are of type int, the result will be of type int, and int division always truncates. It's only after the resulting int is calculated and truncated that it's widened to type double to be returned by the method.

To perform the operation as doubles, you can add the suffix 'd' to your literal (240d). Then the int toPence will be widened to type double before the operation, and the result will be a double.
 
Philip Jenson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marc,

That did the trick! Thanks a lot...I thought it was going to be much more difficult than that.
 
Philip Jenson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's only one thing confusing me. No matter what I input to the program, d = 2�0s6p is returned from the read() function. I'm scratching my head right now at this...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Philip Jenson:
There's only one thing confusing me. No matter what I input to the program, d = 2�0s6p is returned from the read() function. I'm scratching my head right now at this...

The return type of the read method is void, so nothing is actually returned.

Beyond that, the read method doesn't affect any changes outside of its own method body. The reason is that the variable "d" is declared inside the method body, so it's a "local variable," restricted to the scope of the method. Note that this is an entirely different "d" than the one declared locally in your main method of Tester. The instance that "d" references remains unchanged since printing out the result of the subtraction (which is 2�0s6p).

It looks like read is not supposed to return anything, but instead modify the current instance based on user input. So rather than creating a new instance...
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!