aspose file tools*
The moose likes Beginning Java and the fly likes Beginner...Need Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Beginner...Need Help" Watch "Beginner...Need Help" New topic
Author

Beginner...Need Help

Brian Minor
Greenhorn

Joined: Feb 06, 2004
Posts: 1
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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!)


    [Jess in Action][AskingGoodQuestions]
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24184
        
      34

    Originally posted by Sadanand Murthy:

    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.
    Michael Ernest
    High Plains Drifter
    Sheriff

    Joined: Oct 25, 2000
    Posts: 7292

    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
    Thomas Paul
    mister krabs
    Ranch Hand

    Joined: May 05, 2000
    Posts: 13974
    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!


    Associate Instructor - Hofstra University
    Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
    Thomas Paul
    mister krabs
    Ranch Hand

    Joined: May 05, 2000
    Posts: 13974
    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!
    Billybob Marshall
    Ranch Hand

    Joined: Jan 27, 2004
    Posts: 202
    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!
    Michael Ernest
    High Plains Drifter
    Sheriff

    Joined: Oct 25, 2000
    Posts: 7292

    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.
    Thomas Paul
    mister krabs
    Ranch Hand

    Joined: May 05, 2000
    Posts: 13974
    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.
    Adam Altmann
    Greenhorn

    Joined: Nov 15, 2003
    Posts: 21
    Back on topic...

    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.


    SCJP 1.4
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: Beginner...Need Help