• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compilation errors?  RSS feed

 
William Coville
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm currently working through a few assignments for my Java course, and I understand the logic behind what the question is asking. However, when I submit an answer, it says that there are compilation errors in my answers. Normally, it would point out where the answers are, but it does not specify what is wrong and I cannot for the life of me find any of these problems.

If anyone is able to detect what's wrong with the code I'd love to know. I've posted the code below. All object types and variable names have been initialized in the questions.


The object in these problems is the Bowl class, which I've provided the source code for below.

Thanks for your help!

[ Moderator edit: added code tags for you this time. Next time, please UseCodeTags ←click ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Coville wrote:If anyone is able to detect what's wrong with the code I'd love to know.

All you've posted is three methods. Is this all of your code ? Methods can't exist outside of a class.
Have a look at your Bowl class. Notice that all of your variables and all the methods are inside the

lines

Also in future, UseCodeTags when posting code. It makes it a lot easier to read.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should put your code in between the code tags when you post - it will make your code easier to read. I hope you manage to get some answers!
*Edit* someone posted the same time as me sorry!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in this method

You probably get something like "cannot find symbol".

You have at least one other bug though. Look at your anyEmptyBowls method carefully. What happens on the first iteration through the for-loop?

And Welcome to the Ranch!
 
William Coville
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
William Coville wrote:If anyone is able to detect what's wrong with the code I'd love to know.

All you've posted is three methods. Is this all of your code ? Methods can't exist outside of a class.
Have a look at your Bowl class. Notice that all of your variables and all the methods are inside the

lines

Also in future, UseCodeTags when posting code. It makes it a lot easier to read.


These methods are to be added to the class. The problems are done on an online program, and as such they are assumed to exist within the class.

Though I will try to find out what the Code Tags are in order to use them in the future.
 
William Coville
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:The problem is in this method

You probably get something like "cannot find symbol".

You have at least one other bug though. Look at your anyEmptyBowls method carefully. What happens on the first iteration through the for-loop?

And Welcome to the Ranch!


I'm not getting "cannot find symbol" because the program simply isn't telling me what the error is. It just says that it found some kind of error.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Coville wrote:These methods are to be added to the class. The problems are done on an online program, and as such they are assumed to exist within the class.

Well that makes it difficult for us to tell you what the problem is.
We can only guess at what is missing because it's going to be added automatically and what is missing because you need to add it yourself. We're just wasting time taking wild guesses.
If you want help you're going to have to find a way to show us the code that your online compiler is seeing.

William Coville wrote:Though I will try to find out what the Code Tags are in order to use them in the future.

UseCodeTags <-- that's a link, clicking on it will tell you all you need to know.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can put the code in a proper class as suggested by others, then compile it yourself. You will see the compiler message I mentioned if you do that. There's only one line of code in that method and there are only a few suspects that you need to examine. A "symbol" is any name: a variable, a method, or a type. In this case, you left out something(s) that make the symbol incomplete. Look at all your suspects and see other usages in your code. When you see the differences, you'll find your bug.

Code tags are explained on our wiki page: UseCodeTags (←click)
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not the only bug in that method I pointed out though. We'll get to that once you resolve the more basic error you made.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something you should probably do first though is to try and find out why your online program is not telling you what your compilation errors are.
Finding compilation errors can be difficult when you know what the error is - finding it when you don't know what line it is on or what the error is makes it much more difficult.
 
William Coville
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
William Coville wrote:These methods are to be added to the class. The problems are done on an online program, and as such they are assumed to exist within the class.

Well that makes it difficult for us to tell you what the problem is.
We can only guess at what is missing because it's going to be added automatically and what is missing because you need to add it yourself. We're just wasting time taking wild guesses.
If you want help you're going to have to find a way to show us the code that your online compiler is seeing.

William Coville wrote:Though I will try to find out what the Code Tags are in order to use them in the future.

UseCodeTags <-- that's a link, clicking on it will tell you all you need to know.


The stuff I posted is the code that the compiler is seeing. To my knowledge there simply isn't any additional code for me to supply.

Thanks for the CodeTags link, I'll use them in the future.
 
William Coville
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:You can put the code in a proper class as suggested by others, then compile it yourself. You will see the compiler message I mentioned if you do that. There's only one line of code in that method and there are only a few suspects that you need to examine. A "symbol" is any name: a variable, a method, or a type. In this case, you left out something(s) that make the symbol incomplete. Look at all your suspects and see other usages in your code. When you see the differences, you'll find your bug.

Code tags are explained on our wiki page: UseCodeTags (←click)


Placing it in a compiler helped me find the solution with the first method (the method is supposed to return a double but instead the code was returning an array), however it finds no errors with the second piece of code I've added and says that there is an unexpected type at this point:



Does this imply I can't set a boolean to a certain value in this method?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there anything that's preventing you from opening a text editor, writing out a proper class, put the methods pointed out to you that should be in a proper class, and compiling it on your own?

We do not know anything about the online program you are using to compile the code you gave. We can only tell you how we would solve it on our own, with a regular text editor and the standard JDK, and we have done that already, multiple times. If you insist in just using that online program, then there's not much more help we can extend to you and you'll have to ask someone who does know what that online program is doing.

Edit: Ok, strike all that. I see you've already done it. Thanks for doing that
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Coville wrote:The stuff I posted is the code that the compiler is seeing. To my knowledge there simply isn't any additional code for me to supply.

You're giving confusing information.

As I said earlier, methods cannot exist on their own. They have to be within a class structure, so you would need (at a minimum) the following

Your reply to this was that the methods get added to the class, which implies the online program is seeing more than you are supplying, but now you are saying that isn't the case.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Coville wrote:Does this imply I can't set a boolean to a certain value in this method?

That's not what you're actually telling the computer to do here. You're telling it to: "Call the getEmpty() method and assign it the value of 'true'"

You can't assign a value to method call so the compiler rejects this instruction. Look at the other methods you wrote in Bowl that deal with its "emptiness".

As a matter of style:
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Coville wrote:says that there is an unexpected type at this point:



Does this imply I can't set a boolean to a certain value in this method?

Look at your method name. You want to set a value, but does a name with the prefix get, suggest it is a method for setting information or fetching information ?
 
William Coville
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
William Coville wrote:Does this imply I can't set a boolean to a certain value in this method?

That's not what you're actually telling the computer to do here. You're telling it to: "Call the getEmpty() method and assign it the value of 'true'"

You can't assign a value to method call so the compiler rejects this instruction. Look at the other methods you wrote in Bowl that deal with its "emptiness".

As a matter of style:


This helped solve problem 3 (I was using the wrong method and had failed to notice I was using get over setEmpty).

I'm not really sure how to deal with the second piece, however. The code is supposed to check the array to see if any Bowl objects are empty, and if any of them are it returns true. Without using an if statement I'm not really sure how else to check each value.
 
William Coville
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
William Coville wrote:The stuff I posted is the code that the compiler is seeing. To my knowledge there simply isn't any additional code for me to supply.

You're giving confusing information.

As I said earlier, methods cannot exist on their own. They have to be within a class structure, so you would need (at a minimum) the following

Your reply to this was that the methods get added to the class, which implies the online program is seeing more than you are supplying, but now you are saying that isn't the case.


The methods are being written as though they are already inside the class. The code is supposed to look like this (though the site doesn't show the whole picture):

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Coville wrote:The code is supposed to check the array to see if any Bowl objects are empty, and if any of them are it returns true. Without using an if statement I'm not really sure how else to check each value.

Again, it's a case of your instructions not matching your intentions. What does return do? When are you telling the computer to return? Is it able to get through the array as far as it should? Put yourself in the computer's place and try to follow the instructions as the computer would do it. If you still can't find the problem, then you're not understanding how the loop and/or return statement works.
 
William Coville
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
William Coville wrote:The code is supposed to check the array to see if any Bowl objects are empty, and if any of them are it returns true. Without using an if statement I'm not really sure how else to check each value.

Again, it's a case of your instructions not matching your intentions. What does return do? When are you telling the computer to return? Is it able to get through the array as far as it should? Put yourself in the computer's place and try to follow the instructions as the computer would do it. If you still can't find the problem, then you're not understanding how the loop and/or return statement works.


After rewriting the code, and adding an additional boolean to return, segment 2 is functional. All of the problems listed in the OP have been fixed! Thanks a bunch for your help!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


As I said earlier, you should avoid code like this:
if (empties[j].getEmpty() == true) { return true; }
else { return false; }


It's like saying: "If the shirt is wet, say yes, otherwise say no"
On the other hand, the following simply asks: "Is the shirt wet?"

return shirt.isWet()

By convention, you can and should try to name getters that return a boolean value like this: isEmpty(), isFull(), hasEmptyElement(), etc. rather than getEmpty(), getFull(), getEmptyElement(), etc. because it reads more naturally.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
step through this code in your head and on paper:


Let's assume the first bowl is not empty. What happens???

we enter the for loop. j is set to 0. We look at empties[0].getEmpty. Is should return false. What happens?
 
William Coville
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My friend and I have solved the problem, we did not complete the isSuper method. Thanks to all for your help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!