• 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

Java coursework help with error

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, Ranch users
I faced a problem while trying to run a project i created for Introduction to Programing aka Introduction to JAVA (college course) coursework.
Well i had to create a simple ticket machine called TrollMachine which sells tickets through a menu (GUI is the next step)
Here are some project instructions
PDF file
(if someone wants to know what project is about)

When i try to run TrollMachine(executable class) i get the following error

Its probably from getTicketData(ticket); method but i dont know whats wrong with it and how can i fix this.

Code consist of two classes (Ticket and Troll_Machine)
Additional question ( Should i use Array List or simple Array? )

PS: Sorry if code confuse you i was asked to break huge lines (i also omit some spaces)




PS1: i tried to write the code first as ticket Array instead of Arraylistbut i had an error aswell

PS2: Open to suggestions and ways to improve my code >
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error message is telling you you are trying to access index 0 of a collection that has no elements

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0



and it's telling you the problem is on line 103 of the Troll_Machine class.

at Troll_Machine.getTicketData(Troll_Machine.java:103)



so look at the code in that area and see if you can find the problem. (hint look at the for loop, in particular the termination part)
 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to say somehow that arraylist has 4 tickets inside. But i dont get how should i do that.

Do i need to add the following line in method aswell (Somehow?)
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Angelos Naoumis wrote:I need to say somehow that arraylist has 4 tickets inside. But i dont get how should i do that.

Do i need to add the following line in method aswell (Somehow?)


No. You need to ADD the tickets to the List.

All
  new ArrayList<Ticket>(4)
says is "create me a List with enough room for 4 tickets".

It still has 0 tickets in it after it's created though.

HIH

Winston
 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is that the correction way to do so?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know. Depends whether you need any more information to create tickets. Since you have a Ticket class which takes a no‑arguments constructor, that will create 4 tickets. But your ticket objects have −1 as their price; I think that is not what you want. I think you should require the three details in the constructor. Also where does availableq come from? Why are you setting it to −1?

You appear to have misread the instructions. It doesn't say anything about Lists, but arrays. I think you start with 200 motorcycle tickets at €1.25 each; whenever you sell one you reduce the available number by one.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Angelos Naoumis wrote:


It looks like you have misunderstood how the get(int) method of a List works. Go to the List API documentation (just follow the link) to get some clarity.

The above code is saying that you expect that the first item on the list will always be an object whose description you want to set to "Motorcycle", the second to "Car", and so on. Since you're passing in the list as a parameter, there really should be no predetermined position for anything. In fact, as already noted, you shouldn't assume there's anything in the list at all.

Angelos Naoumis wrote:PS2: Open to suggestions and ways to improve my code >


Ok.

In the code below, you have the declared the parameter type to be ArrayList<Ticket>. Prefer declaring it as List<Ticket> instead.

ArrayList is an implementation. List, however, is the interface. Prefer declaring formal types as interfaces rather than specific implementations. This makes your code more flexible and resilient to change. For example, if you want to use some other implementation of List, or if you decide to pass in something from a utility that does not specify what kind of List implementation it returns. Tying yourself down to a specific implementation like that makes your code more brittle and less accommodating.

Also, imagine going through your house with a pad of Post-It® Notes and labeling everything like "bed", "chair", "table", "TV", "drawers", "sink", "couch", ... you get what I mean. That's what you're basically doing by putting comments like the ones below all throughout your code:
Putting in comments like these is just "busy work" -- on the surface, it gives the feeling that you're doing something useful but in reality, it's totally pointless.

Reserve comments for things that are truly needing comments, like an explanation of WHY you're doing something. Everything else regarding WHAT you're doing and/or HOW you're doing it should be explained by code itself. It's called "writing intention-revealing code" which basically boils down to using names that reflect their purpose. So that when you read the code, it tells you exactly what it's doing there.

For example, would anyone else know what "initialQ" means? What does the "Q" stand for? Spell it out, so other people will know what's on your mind. The comment "// end of getInitialQ" does not do anything to clarify that idea either, as I already noted.
 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Don't know. Depends whether you need any more information to create tickets. Since you have a Ticket class which takes a no‑arguments constructor, that will create 4 tickets. But your ticket objects have −1 as their price; I think that is not what you want. I think you should require the three details in the constructor. Also where does availableq come from? Why are you setting it to −1?

You appear to have misread the instructions. It doesn't say anything about Lists, but arrays. I think you start with 200 motorcycle tickets at €1.25 each; whenever you sell one you reduce the available number by one.


I want to know how many tickets machine sells [I put 10 tickets in the machine and it sells 3 tickets whats the quantity left in the machine? = Available Quantity aka avaiableq] (+ i need a counter for total amount of tickets sold)
I wanted the available quantity to be equal to initial quantity at first and when sellTicket runs low the amount of it by 1.
Price and quantities for both initial and available was my major fight with my professor (still got no clear answer) . I asked her why should i ask the user to put that information when i should know before whats the price and quantities but she responded me saying "The owner of the ticket sale runs the program first, set information and then the ticket buyer continues from that point on". So i didnt know what to set on constructor so i used -1 as impossible number
About description of the ticket still got nothing for response, he says thats the way coursework ask it.

Thanks Junilu Lacar , i am still new to java i comment almost everything i think i got the point here. Almost removed every useless comment
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stop typing and step away from the keyboard. Get your story straight in your head first before trying to tell the computer how to do it.

It sounds like this is the general idea:

Start a ticket sales campaign:
Ask the owner how many tickets he/she has available to sell.
Ask the owner how much each ticket will be sold for.

Sell tickets:
Ask the user how many tickets they want
Tell the user how much they need to pay for that many tickets
Adjust number of tickets you have left available to sell.

So, it sounds to me like your professor was right.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . Putting in comments like these is just "busy work" -- on the surface, it gives the feeling that you're doing something useful but in reality, it's totally pointless. . . .

Not quite totally pointless; it allows us to see where the code has been misunderstood, e.g. Troll_Machine (sic) line 105. That is not the end of a while loop.
In view of this sort of comment, it might be worthwhile putting // end comments at the end of that loop, but nowhere else. As Junilu has told you end comments are usually pointless.
The code formatting is dreadful. When you got rid of the long lines you also got rid of all your indentation. Correct indentation enables you to see where all the loops and methods end without needing comments. I can't help you by restoring the indentation because there is a risk you would lose marks. The instructions you gave restrict how much help you are allowed. You might do well to get a decent text editor and set up its options to help you program, rather than having the editor hindering you.

By the way: underscores _ are only allowed in constant names or package names. It isn't Troll machine (‍)
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:By the way: underscores _ are only allowed in constant names or package names. It isn't Troll machine (‍)


Technically, they are allowed. By convention they are only used in constant names. I don't think I've ever seen package names that use underscores.

And let's not start discriminating against Trolls now. They're fantastic and some stories just wouldn't be the same without them.
 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot think of a way changing do-while to while

PS: i updated my format using the topic you send me, indeed it looks more clear
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read your instructions again very carefully. In particular, this one:

2) Create a Toll-Machine object, which consists of an ID and an array of 4 ticket objects

What does that tell you about where to keep your ticket objects?
 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Read your instructions again very carefully. In particular, this one:

2) Create a Toll-Machine object, which consists of an ID and an array of 4 ticket objects

What does that tell you about where to keep your ticket objects?


What ID stands for?
Did not notice the array object you are a true savior
Also i find out another mistake one the outer while of both menu and submenu (where the code must exit when no more tickets left for sale i exit the code only when menu option is set to 0)
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go ask your professor what the ID is for. I don't see anything specific in those requirements that would actually require an ID but maybe that's just something that the professor overlooked. Maybe he/she is expecting it to be used when you're displaying the status of the Toll Machine. Again, talk it over with your professor to get some clarification.

Also, the instructions seem pretty straightforward. And one of the objectives is for you to "Apply top-down design and stepwise-refinement to solve a given problem" so maybe you want to start actually doing that.

Stepwise-refinement means that you start by writing a little bit of code and make that work. This code will do only a small part of whatever it is you want the program to do eventually. Once you get that one small part working, then you go back and add some more code and make that work on top of what you had working before. You keep repeating this process, which is what "iteration" means, and you keep adding one small bit of functionality at a time until you have added everything that you wanted in the first place.
 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot understand the Apply top-down design and stepwise-refinement, i did what i though it is but i am not sure.
( Instead of having the code inside ifs i am calling another method to do so ) thats what i did and i think Apply top-down design and stepwise-refinement is.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the code here (with short lines and indentation) rather than the pastebin link. Since some people are reluctant to click such links, I have deleted it.
I am afraid I haven't found good definitions of top down an bottom up design by searching.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Has someone told you that the do/while loops are bad? I think they are fine.

Much more important is to get code out of the main() method and into some kind of driver method. Notice that you have everything static. This is a sign that you need to move your code.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It says so in our old JavaRanch style guide. I should have said I don't believe do loops are bad when I directed OP's attention to it. The style guide does however say it is hard to undertand because it is not obvious where it starts and ends, so an // end comment might be worthwhile.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It says so in our old JavaRanch style guide. I should have said I don't believe do loops are bad when I directed OP's attention to it. The style guide does however say it is hard to undertand because it is not obvious where it starts and ends, so an // end comment might be worthwhile.


I don't know, if it's not obvious where a do-while loop starts and ends, then one or more of the following are probably true:
1. Indentation needs to be fixed to make it obvious
2. There's too much code around the loop -- extract it to its own method
3. There's too much code inside the loop -- extract / compose

I think if you do the above, you can pretty much invalidate any superfluous "// end do" comment
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:[. . . Technically, they are allowed. By convention they are only used in constant names. I don't think I've ever seen package names that use underscores.

I should have said convention doesn't allow underscores, shouldn't I? The Java Tutorials say you should add underscores to package names when your website might start with a number or look like a keyword. So a 7-zip package would be named
com._7_zip.mypackage
and one from short.com would start
com.short_.something.

And let's not start discriminating against Trolls now. . . .

I was more worried that we were discriminating against Rolls.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:[. . . I think if you do the above, you can pretty much invalidate any superfluous "// end do" comment

I did say “might”. Making the structure of the code obvious is far better.

And I have never actually believed that you should avoid do loops.
 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone delete my last post (the one with full code)? (I cannot edit) Some class mates told me that i may lose credits if the machine we use to upload copyright it with your site.(Even if thats my code i uploaded here = someone else's work)
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should check that sort of thing with your teacher rather than classmates. If there is any problem about plagiarism, the fact that you can show your code with your name is your defence.
If you are prohibited from discussing large parts of assessed code on this sort of website, your teacher ought to have told you already.
 
Angelos Naoumis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell Ritchie , may you please remove my code
I talked to my teacher and told me to do so in order to stop the program from finding plagiarism (in order to avoid extra troubles). I don't know yet what the troubles may i be in but i must somehow remove the code from the next (thats what she told me)

PS: I may have higher troubles if i don't remove the code posted
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Copying your own code from the same assessment does not constitute plagiarism, but if you have rules against posting your complete solution I shall remove it.
reply
    Bookmark Topic Watch Topic
  • New Topic