View Full Version : Java methods
The_Doctor
03-14-2007, 17:14
This is my first ever method, is it alright?
//Area of Squares
public double Area_of_Squares(double Square_Side_Length, int Further_Number_of_Squares, int Square_Size_Increment, double Square_Area, int Counter)
{
System.out.println("Enter square side length");
Square_Side_Length = data_input.nextDouble();
System.out.println("Enter further number of squares to be calculated");
Further_Number_of_Squares = data_input.nextInt();
System.out.println("Enter size increment");
Square_Size_Increment = data_input.nextInt();
for(Counter=1; Counter==Further_Number_of_Squares; Counter++)
{
Square_Area = Square_Side_Length*Square_Side_Length;
System.out.printf("Area of Square is f%" , Square_Area);
Square_Side_Length = Square_Side_Length + Square_Size_Increment;
}
return 1;
}
Also, how do I call this method.
doc_bean
03-14-2007, 17:31
This is my first ever method, is it alright?
//Area of Squares
public double Area_of_Squares(double Square_Side_Length, int Further_Number_of_Squares, int Square_Size_Increment, double Square_Area, int Counter)
{
System.out.println("Enter square side length");
Square_Side_Length = data_input.nextDouble();
System.out.println("Enter further number of squares to be calculated");
Further_Number_of_Squares = data_input.nextInt();
System.out.println("Enter size increment");
Square_Size_Increment = data_input.nextInt();
for(Counter=1; Counter==Further_Number_of_Squares; Counter++)
{
Square_Area = Square_Side_Length*Square_Side_Length;
System.out.printf("Area of Square is f%" , Square_Area);
Square_Side_Length = Square_Side_Length + Square_Size_Increment;
}
return 1;
}
Also, how do I call this method.
just type a=Area_of_Squares(ss,fn,ss,sa,c) with proper numbers (or variabels) as arguments, that should do the trick iirc.
EDIT: in your for command, shouldn't you use <= instead of == ? It's the continue condition, not ending condition you should use iirc
EDIT2: why do you return 1 instead of the actual area ?
Sasaki Kojiro
03-14-2007, 18:31
Since the user gives you the input you don't need any of those parameters.
So:
public AreaOfSquares(){
*get info*
*calculate area*
}
You would have to declare squareArea somewhere else. The other variables will have to be declared somewher as well, either locally or globally.
for loops are usually done like so:
for(int i = 0; i<*some number; i++){
*stuff*
}
Yours doesn't make any sense though. If you want the user to be able to keep entering squares you need to put the user input stuff inside the for loop. Then you can set the initial value of FurtherSquares to one, and ask them on the first time through the loop what the value should be, update the value, and have a boolean telling whether you asked already so that you don't ask again.
Blodrast
03-14-2007, 18:49
Well, congrats on your first Java method! :)
Also, just a remark - as far as style is concerned, C/C++ and Java have different "rules" for naming methods and such... the former use the underscores, the latter uses capitalization.
That's not to say underscores are forbidden in Java, but the capitalized notation is preferable.
For example, nextCount is typical Java notation. It's C/C++ equivalent would be next_count.
Sasaki's notation is more appropriate for a Java method name.
It's a minor nitpick.
The other guys' comments stand, too.
One more thing. About calling the method - since it's not static, then it will be invoked *for* some object. So, a.AreaOfSquares(...).
The_Doctor
03-14-2007, 20:11
EDIT: in your for command, shouldn't you use <= instead of == ? It's the continue condition, not ending condition you should use iirc
I am inexperienced.:help:
EDIT2: why do you return 1 instead of the actual area ?
I am inexperienced.:help:
Yours doesn't make any sense though. If you want the user to be able to keep entering squares you need to put the user input stuff inside the for loop. Then you can set the initial value of FurtherSquares to one, and ask them on the first time through the loop what the value should be, update the value, and have a boolean telling whether you asked already so that you don't ask again.
Its not my design. For my coursework I was given a design and told to create the code for it and to test it.
Sasaki Kojiro
03-15-2007, 01:20
Oh I see. The size of the square goes up in standard increments?
doc_bean
03-15-2007, 08:05
Its not my design. For my coursework I was given a design and told to create the code for it and to test it.
What was the exact design ? It can't be very long considering it's only one method, so you might as well share it with us :yes:
Since you're doing a relatively simple program, I'd avoid using input commands in the method, and would instead either decalre a seperate method for getting the inputs or just put it in the main code, but that might not work and is just a personal preference I assume (I'm not really experienced either).
Do listen to Sasaki's comment though, you only need to 'give' a method variables that have a specific value, like
public double multiply(double x,double y){return x*y;}
if you're going to input them within the method you can declare them als local variables.
The_Doctor
03-15-2007, 19:45
The size of the square goes up in standard increments?
Yes.
What was the exact design ? It can't be very long considering it's only one method, so you might as well share it with us
Actually there where 4 methods, but they where pretty much the same.
This is the design document:
Shape Area Calculation Program
Requirements Specification
1. The program will continually display a menu to the user until the user chooses to quit. The menu choices are:
1. Calculate area of squares
2. Calculate area of rectangles
3. Calculate area of triangles
4. Calculate area of circles
5. Quit
2. If the user chooses to calculate the area of squares, an ‘area_of_squares’ method will ask for the following data which will be used to calculate and display the areas of the squares:
First square side length (L)
Number of further square areas to calculate (N)
Square size increment (I)
e.g. If the user enters L = 5, N = 3, I = 2 then three square areas of
L = 5, L = 7 and L = 9 will be calculated and displayed.
The formula for calculating the area of a square is:
Area = L * L
3. The other ‘areas’ types will be same except for the variables involved and the names of the methods:
Area of Rectangles
First rectangle length (L)
First rectangle height (H)
Number of further rectangle areas to calculate (N)
Size increment (I)
Formula is:
Area = L * H
Area of Triangles
First triangle base length (L)
First triangle height (H)
Number of further triangle areas to calculate (N)
Size increment (I)
Formula is:
Area = (L/2) * H
Area of Circles
First circle radius (R)
Number of further circle area to calculate (N)
Size increment (I)
Formula is:
Area = PI * R * R
Where PI = 3.1415927
4. If the user chooses to quit then they should be asked ‘if they are sure’ and if they are not, the program continues as previously described, otherwise the program terminates.
Data Analysis
Menu choices are Integers
All area variables are floating point numbers
User quit verification is a String
I finished it off today, on the day it was supposed to be handed in:no: . None of my friends knew that you had to have methods in the program, if it wasn't for me they would have all messed up the coursework.:yes:
doc_bean
03-16-2007, 09:29
You seem to have done what was the assignment, even though i disagree with their general structure :smart:
The_Doctor
03-16-2007, 15:19
You seem to have done what was the assignment, even though i disagree with their general structure
I believe it was supposed to test our method making skills.
How would you have structured the program?
doc_bean
03-17-2007, 10:03
I believe it was supposed to test our method making skills.
How would you have structured the program?
I would have separated the calculations from the inputs, I also don't see the point of the increments :shrug:
The_Doctor
03-17-2007, 12:04
I also don't see the point of the increments
Neither do I.
I believe it was supposed to test our for loop making skills.
vBulletin® v3.7.1, Copyright ©2000-2025, Jelsoft Enterprises Ltd.