• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Having problems calling a method?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this assignment in my Java class that has me stumped and I cant figure out what Im doing wrong. The assignment is to create a java app that uses 4 methods to get the length, height, of a rectangle, than compute it to turn it into the area of a rectangle and then finally display the outcome on the screen.
The program was already started for us and all we have to do is create the methods to make it do what its supposed to do. I thought i had it down, but I am getting an error because I am not passing a argument to the methods when calling them. The program follows this basis for operations:



This is the part of the app that was already created for us, we are supposed to create the methods. What I did was create a method that would use a scanner object to get the users input, asking them to input the length of the rectangle. The method then puts that input into a length variable and then returns the value so that the length variable in the above code holds the value inputted by the user. I do not understand why this is not working though. Netbeans keeps telling me I have to use an agruement when callling the method.

Can someone explain to me why you must use an argument when calling a method? This seems to be counterproductive, at least with the knowledge I have thus far. If my methods are supposed to be designed to break the tasks of the program into chunks then the way I have wrote it seems the most logical. If the program is supposed to use a length variable and assign the value of the getLength method to that variable I dont understand what argument I could possibly pass to it that would do anything?

The method itself asks for the input, and then returns it to the length variable. The only argument I could pass, would be the value needed for length in the first place and would therefore make using the method a waste of time???

Here is how I wrote the method for getLength:



I havent hit a spot yet in this course that has stumped me and this is my first one. I know there are other ways I can write this and get the assignment done, but I just want to know what is wrong with the way I have it now, and if you TRULY ALWAYS have to pass an argument to a method when calling it. Can you just call a method with no arguments, let the method do its thing, and return the value needed for the variable?

Thanks in advance for any help I get. And by the way, this is my first post here, so what is up to all Java Masters and I hope to become a part of this community, one day being able to help newcomers like myself.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean like this?That's a method that takes no parameters, yes?
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Are you sure you have copied the getArea call correctly? That doesn't look right. You oughtn't to have to pass the size to a getArea method; the information should already be available in the object. Similarly for the displayData method, which I would write like thisNow you have to put the appropriate code into the toString() method. You will find only a little information in the Java™ Tutorials, I am afraid.

What do you know about objects, and the classes they are derived from? Read the appropriate chapter in your book, or the Java™ Tutorial section. (Beware: I think there is a misprint: they show three fields in the Bicycle class as private, a few lines after describing those line with "public" in. In that instance, "private" is correct.)
You should find enough information in that Java Tutorials section to answer all these questions, but I am very suspicious about getArea(length, width) or simllar.
 
Shane Miller
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yea, that is what Im talking about, but it is not working when I write the program. The directions for this assignment say that the methods themselves have to contain instructions to the user to allow them to enter the rectangles width and length. So therefore, when I write the program, all the work is being done by the methods and it is then assigning the users input into the length and width variables.

The next two methods, are as follows: The first does the calculations to determine the area of the rectangle based on the users input.
The second displays all of the information on the screen.

The problem I am having is that since the instructions I have tell me to make the methods I have to create ask the user for their input there is no arguments that I can pass to the methods when calling them. If you notice in the code I posted, when I call the first method, (getLength) it is supposed to assign its results to the length variable. Seeing as the instructor wants us to make the getLength method that which is responsible for gathering the users input I am stumped. I would normally, just run the code in the beginning of the program to gather that information from the user. But since I have to call the getLength method to do that I get an error.

Maybe I am overexplaining this. but it comes down to this; the getLength and getWidth methods have to be that which gathers the info from the user. Because of this when I run the code at the beginning I get a error because there is no argument!!!

What kind of argument could I use? There isnt one..... The getLength method is gathering the data from the user, its not like I can insert a value as an argument so that it would use the argument as the values for the length of the rectangle and the width of the triangle. This is my problem.

Put simply, is it possible to run a method without a argument if the method is supposed to be returning a value? As in this case the getLength method is supposed to get data from the user using a Scanner object and then return their input to the length varialbe.

Am i making sense?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not really :)

Method parameters are not required to call a method if the method doesn't define any parameters.Perfectly valid. In your case the method would have code to get data from the user however you want to do that.
 
Shane Miller
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem I am having is that I keep getting this error message from Netbeans: method getLength in class AreaRectangle cannot be applied to given types. required double found no arguments.

The thing is I have everything set up the right way I think. Take a look at this code and tell me if theres anyting wrong here:



This is not the whole app obviously, I havent finished the rest as I cant seem to get past this error message that has to do with the first line that says:

length = getLength();

The rest of the code is fine, but there is a red x indicating a problem with that line of code and I cant figure out why. THere is no argument i can pass to that getLength method, as the method itself does everything. All i need it to do is assign the users input to the length variable.

Any ideas as to whats wrong here?
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Afraid not. You don't have a Rectangle class there at all. The nearest you will get in the Java Tutorial section is the last Bicycle class. You should be able to adapt that to a Rectangle.
You should also be able to get up getLength() and getWidth() methods in it.

Have you been taught about design of classes to create objects? You need to revise that.
 
Shane Miller
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have only learned how to declare variables, the if statement, the loops, and now I am on methods. I am only following the assignment I was given. which was a java file that was started for me. This was included on a cd in my textbook for class. I had to open the rectangle file, which contained what you see in the previous post. The only thing I added to it was the getLength method at the bottom.

My instructions are to right four methods for the four methods that are being called in the program. Basically thats what this assignment is, the rest of the program was already written, I have to only write the methods. Which have to ask the user for input, for the length and width and then calculate the area and then display the outcome.

I am confused when you say about the bicycle and the getLength and getWidth methods and that I dont have a Rectangle class there at all?

What do you mean?

From what I know thus far, I am only supposed to create methods for this assignment. I have had no trouble thus far with any assignment but cant seem to figure this out?

Basically all we do so far in this class is create java files with the main method and then go from there. I have seen a lot on the java tutorials were you create classes and then use them from another java file. Like a few of the tutorials I have done had a file called bike.java and then bikeTest.java. I would have to edit the bike.java file and then run it with bikeTest . This we havent really done in my class at school.

We make all of our java files with a main method and then go from there with the variables and loops or what not.

Even the online courses I have taken have been different from Sun Academic Initiave. Were do you suggest I start? and any books you recommend. NOw you have me thinking I am not learning in the right order or somehting?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This getLength method has a parameter, when you call it you don't call it with one. If you don't want the method to require a parameter, it shouldn't take a parameter. I'm suspicious about this being a static method, and part of the class it's part of, but that's a different issue.
 
Shane Miller
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand what you mean, but that is what are assignment is asking us to do so I have to follow there guidelines or Ill get told I did not follow directions:)

As far as the parameter, I am confused, if the method getLength, is supposed to return a value to the length variable, then isnt the only way to do that by adding a parameter?

I was under the impreesion that if a method returns a value than the format for writing the method header is the way I did it there?

So what your saying is that I can remove the parameter (double x) and still return the value that the user inputs, and it will assign it to the length variable when the method is called?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shane Miller wrote:As far as the parameter, I am confused, if the method getLength, is supposed to return a value to the length variable, then isnt the only way to do that by adding a parameter?


No. Parameters are (generally) an *input* to the method. The return type determines whether or not the method returns a value, and the type of that value.
 
Shane Miller
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks alot, I got it now and i works as it should. From the chapter readings they gave us it never mentions what you just told me and this is why I could not figure this out.

Man I spend like 3 hours on this:(

Have you ever heard of HeadFirst Java or Thinking In Java and if you have what one do you think is better. I just got both of them and dont know which one to start first.

I am currently reading Starting Out With Java From Control Structures Through Objects via my schools selected text. Funny thing is, this class is supposed to be an intro to software development, and the prof is only discussing Java and nothing else, and to make it even worse. Its lilke going on week 5 of class and we are only on loops. I have gone ahead due to how slow the class is moving, he spent like 3 weeks on declaring variables and doing a simple hello world program!!!

That is why I am taking the Sun Academic Initiave Online Course I see I wasted a bunch of money on this college course, we will be lucky to finish the book by the end of semeseter.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neither is better; they're just different.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:Neither is better; they're just different.

Both good, but different from each other.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it has taken 5 weeks to get to loops, I would not worry about that. There is lots and lots to learn; you will do better going slowly, and I think you have confused yourself by trying to rush ahead.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic