Calling Methods Again [so, are you guys sick of me yet?]

Discussion in 'Java' started by KyoGaSuki, Apr 1, 2008.

  1. KyoGaSuki

    KyoGaSuki Guest

    So! I finally think I understand writing a method! ...of course...I
    still can't seem to figure out how to call it right, and therefore I
    don't know if the method is right *if that made any sense*.

    This is what I have so far *hides*:

    * @(#)
    * try1 application
    * @author
    * @version 1.00 2008/4/1
    import java.util.*;
    import javax.swing.*;
    public class try1 {

    public static void main(String[] args)throws FileNotFoundException
    Scanner fileIn = new Scanner(new FileReader("Cylinders.txt"));
    PrintWriter fileOut = new PrintWriter("Output3.txt");
    System.fileOut.print("Please Enter The Radius: ");
    float radius = fileIn.nextFloat();
    System.fileOut.print("Please Enter The Height: ");
    float height = fileIn.nextFloat();
    System.fileOut.println("Height= " + height + ", Radius= " + radius +
    ", Base Area= " + area + ", Lateral Area= " + latArea + ", Surface
    Area= " + surfArea + ", Volume= " + volume);


    public static float baseArea(float radius){
    float area;
    area = 3.14*radius*radius;
    return area;

    public static float latArea(float radius, float height){
    float circum = (radius*2)*3.14;
    latArea = circum * height;
    return latArea;

    public static float surfArea(float area, float latArea){
    float surfArea = (area*2)+latArea;
    return surfArea;

    public static float volume(float area, float height){
    float volume = area*height;
    return volume;

    KyoGaSuki, Apr 1, 2008
    1. Advertisements

  2. KyoGaSuki

    Mark Space Guest

    No need to hide, you are doing ok.

    This is good, always document your code. You should fill in the comment
    Whoops! radius and height are ok. They are _local variables_.

    To call a method, you need to add () at the end, so the compiler knows
    it's a method, not a local variable.

    For example:
    System.out.println( "Base Area = " + baseArea(radius) );

    You call baseArea(radius) with the () and the argument (in this case, I
    assume you argument = the local variable radius) so the compiler will
    know the difference between a method baseArea() and a local variable
    named baseArea.

    See if you can fix the others like that.
    Mark Space, Apr 1, 2008
    1. Advertisements

  3. Chase Preuninger, Apr 1, 2008
  4. 1) "fileOut" is a local variable, it mustn't be prefixed by "System".

    2) I can't understand why you are writing interactive prompts to a text

    Mark has already pointed out that
    ", Base Area= " + area
    should probably be
    ", Base Area= " + baseArea(radius)

    You could just write
    public static float baseArea(float radius){
    return 3.14f * radius * radius;

    Note 3.14f is a float, 3.14 is a double.

    I suspect it would be better to use either
    return ((float)Math.PI) * r * r;
    return 3.1415927f * r * r;

    depending on your desired precision.

    RedGrittyBrick, Apr 2, 2008
  5. RedGrittyBrick schreef:
    But why use float in the first place? To make life more difficult?
    Just use double, forget the f suffixes and use Math.PI.

    Hendrik Maryns
    Ask smart questions, get good answers:

    Version: GnuPG v2.0.4-svn0 (GNU/Linux)
    Comment: Using GnuPG with SUSE -

    -----END PGP SIGNATURE-----
    Hendrik Maryns, Apr 2, 2008
  6. When replying I considered that, then I wondered what purpose float
    serves, then I assumed that the Java gods must have known some reason
    for it's existence - sometimes people may have a valid reason for using
    float? Someone using 3.14 as a value of PI may be just such a case :)
    RedGrittyBrick, Apr 2, 2008
  7. KyoGaSuki

    Lew Guest

    So if they use 3 for PI, we do the calculations in int?
    Lew, Apr 2, 2008
  8. When in Indiana ...
    RedGrittyBrick, Apr 2, 2008
  9. I think float does have uses, but they are rather specialized. It is
    useful, for example, in some seismic data processing. I would use it if,
    and only if, both of the following were true:

    1. The application has so much floating point data that a factor of two
    difference in the space it occupies has a significant effect on
    performance, or size of problem that can be solved, or some other
    measure that matters.

    2. I am sure the required precision can be achieved using float. Being
    sure float is good enough is can be harder than being sure double is
    good enough. Often, a very simple worst-case analysis will show double
    is good enough, but sophisticated numerial analysis is needed for float.

    Patricia Shanahan, Apr 2, 2008
  10. KyoGaSuki

    Roedy Green Guest

    you are needlessly introducing variables. You could write that more
    tersely as:

    public static float surfArea( float area, float latArea ){
    return area*2+latArea;

    the name of the function documents what you are computing.
    Roedy Green, Apr 2, 2008
  11. KyoGaSuki

    KyoGaSuki Guest

    2) I can't understand why you are writing interactive prompts to a text
    Oops -- this file originally started out with a prompt where the user
    would input the radius and height, and it would calculate the
    different outputs, but while attempting to get it to work I decided to
    see if I could finally learn to successfully import from a file...I
    must have forgotten to delete that part...
    KyoGaSuki, Apr 3, 2008
  12. KyoGaSuki

    KyoGaSuki Guest

    Thank you all so much for your is funny, actually -- I
    posted that code fearing more than anything that my methods were so
    far from being correct, but it turns out that I think those are like,
    the only thing partially correct ^^;;
    KyoGaSuki, Apr 3, 2008
  13. You're getting there. I still remember my first exposure to FORTRAN in the
    mid-70's...punched cards and batch jobs and all that good stuff, and only a
    fuzzy idea of what it is I was doing for the first part of the course. For
    some weird reason (perhaps an early sign of my affinity for functional
    languages?) I thought that variables, once assigned, shouldn't
    change...amongst other things.

    Other posters have indicated current problems with your posted code. At this
    stage of the game, if you haven't already done so, I'd strongly recommend
    the Java Tutorial, "Learning the Java Language" trail
    (, and go through it
    in detail. The OO basics lesson
    ( covers a
    lot of the ground you are stumbling over right now.

    Arved Sandstrom, Apr 3, 2008
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.