This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Can anyone help me with this statement? Assume that area, base, and height are previously defined short variables. Code a single statement to calculate the area of a triangle (one-half the base times the height) and assign the result to area.
Hi, Welcome to JavaRanch! Hopefully, you ought to be able to do this after reading the first chapter of any Java text. In any case, here at the Ranch we love to help people figure things out for themselves. Things you need to know:
"=" is the assignment operator, for assigning a value to a variable;
"*" is the multiplication operator;
"/" is the division operator;
A gotcha: make sure that at least one operand of every operator is a floating-point number -- i.e., has a decimal point. Otherwise, the fractional part of the result will be truncated, and you will get the wrong answer (often zero!)
Ditto. This question doesn't deserve to be answered.
It does deserve to be answered. He didn't demand an answer he could cut and paste. He didn't ask us to email him the answer because he can't be bothered to come back here to look. He asked a basic question in good faith, and is just in need of a little encouragement. We don't discriminate at the Ranch. Rank newbies and seasoned veterans alike are welcome here.
I have deleted the post EFH chose to respond to. I personally will delete any comment that discourages a fellow poster from asking a simple question in the Beginner's Forum. JavaRanch does not set a lower limit on the merit of any question. While we do screen for such things as questions taken from certification exams, and we often comment plainly on questions that appear to solicit answers for schoolwork, we address those issues only. There are questions with an ulterior motive, but there is no question that is too simple in this forum. If you feel differently, I suggest you simply let it be and move on to things more worthy of your attention, [ February 06, 2004: Message edited by: Michael Ernest ]
Make visible what, without you, might perhaps never have been seen. - Robert Bresson
Let's make it absolutely clear that this is the beginner's forum and NO question is too basic to be answered here. This is JavaRanch - A Friendly Place for Java Greenhorns. Brian, please accept our apologies for anyone who made you feel unwelcome. If Ernest's answer wasn't clear enough, feel free to ask for more help!
Here is an example that might help you figure this out. Suppose we wanted to calculate how much tip to give our waitress on a bill of $45.67. We might code it like this: float tip = 45.67f * .15f; Notice we had to put a "f" at the end of each constant. That's because the default for decimal constants is to make them doubles. A double won't fit in a float so we would have had a problem. There a couple of solutions to that. The easiest would be to do this: double tip = 45.67 * .15; There are other solutions involving casting but we can save those for another day! Good luck!
Originally posted by Thomas Paul: Let's make it absolutely clear that this is the beginner's forum and NO question is too basic to be answered here. This is JavaRanch - A Friendly Place for Java Greenhorns. Brian, please accept our apologies for anyone who made you feel unwelcome. If Ernest's answer wasn't clear enough, feel free to ask for more help!
Yikes! So when people basically just post their homework assignment verbatim (like Brian apparently did), y'all would rather either completely ignore him, or oblige by spoon-feeding him the answer? Seems that my simple post of "Sheriff, you're too nice" wasn't inflammatory at all, yet you zapped it. Sheesh!
Billybob - FWIW, I thought your response was "on the line," but another sheriff felt differently. We don't have a uniform system of interpretation, but that's volunteers for you. We do want to make it as inviting as possible for newcomers. The cases where the solicitation to do someone's homework is clear, though, is usually none too subtle. We get topics often marked "Urgent" that ask for a piece of code to be written based on some problem. In this case, it's a request for help, not code, and Ernest and Thomas are both taking the request at face value. All three of us are teachers in our own lives; we won't be spoon-feeding anybody anything. I for one think the posts that answer the topic show that.
Joined: May 05, 2000
Originally posted by Billybob Marshall: Yikes! So when people basically just post their homework assignment verbatim (like Brian apparently did), y'all would rather either completely ignore him, or oblige by spoon-feeding him the answer?
Billybob, Brian did not ask for the code. He did not ask us to do his homework. He asked for some help. If Brian had asked "could someone give me the code for a program to calculate the area of a triangle" then it would have been a different story. In that case we would have told him that we don't do people's homework but we do help people to do their own assignments. Brian is apparently new to programming and he asked for some help. To turn him away is unfriendly and that is not what JavaRanch is all about.
Keep in mind that the result of an expression is always considered an int, if you're using variables of int size or smaller. (In this case, short.) So if your Area variable is also a short, it won't work without an explicit cast. If Area is int everything will be just fine.