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

K

KyoGaSuki

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.java
*
* try1 application
*
* @author
* @version 1.00 2008/4/1
*/
import java.util.*;
import javax.swing.*;
import java.io.*;
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;
}

fileIn.close();
fileOut.close();
}
 
M

Mark Space

KyoGaSuki said:
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*.

No need to hide, you are doing ok.

/**
* @(#)try1.java
*
* try1 application
*
* @author
* @version 1.00 2008/4/1
*/

This is good, always document your code. You should fill in the comment
above.
System.fileOut.println("Height= " + height + ", Radius= " + radius +
", Base Area= " + area + ", Lateral Area= " + latArea + ", Surface
Area= " + surfArea + ", Volume= " + volume);

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.
 
R

RedGrittyBrick

KyoGaSuki said:
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.java
*
* try1 application
*
* @author
* @version 1.00 2008/4/1
*/
import java.util.*;
import javax.swing.*;
import java.io.*;
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: ");

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
file.

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

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

}

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

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;
or
return 3.1415927f * r * r;

depending on your desired precision.

<snip>
 
H

Hendrik Maryns

RedGrittyBrick schreef:
KyoGaSuki wrote:

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;
or
return 3.1415927f * r * r;

depending on your desired precision.

But why use float in the first place? To make life more difficult?
Just use double, forget the f suffixes and use Math.PI.

H.
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFH82VEe+7xMGD3itQRAhEBAJ9eKcp2SwXOr67cYcsBWMGNR96gRACfafbI
MTR37gJFvDM30mdDmd+0jq0=
=TWu5
-----END PGP SIGNATURE-----
 
R

RedGrittyBrick

Hendrik said:
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.

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 :)
 
L

Lew

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

So if they use 3 for PI, we do the calculations in int?
 
P

Patricia Shanahan

RedGrittyBrick said:
Hendrik Maryns wrote: ....

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

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
 
R

Roedy Green

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

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.
 
K

KyoGaSuki

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...
 
K

KyoGaSuki

Thank you all so much for your help...it 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 ^^;;
 
A

Arved Sandstrom

KyoGaSuki said:
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*:

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
(http://java.sun.com/docs/books/tutorial/java/index.html), and go through it
in detail. The OO basics lesson
(http://java.sun.com/docs/books/tutorial/java/javaOO/index.html) covers a
lot of the ground you are stumbling over right now.

AHS
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top