Any java people spare 5 minutes?

Associate
Joined
11 Aug 2004
Posts
339
Location
Edinburgh/Armagh
Q4.
A shop stocks 4 kinds of chocolate bars: “Mars”, “Snickers”, “Bounty” “Twix”.
This can be declared in a program as:
String[] bars = {“Mars”, “Snickers”, “Bounty” “Twix”}
Another array can be declared which will keep track of the stock of each kind of
chocolate bar, e.g:
int[] stock = new int[4]
Each stock can be initialised to 100 , as below or otherwise:
for(int i=0; i<stock.length; i++){
stock = 100;
}
Write a program which will keep track of the stock of each bar. The program should
have two static methods: Write a driver program which will test both of these
methods.
/* The method below will be called from main when the shop owner buys in stock.
Stock and bars are as before. barType is String read in from the user indicating which
type of bar was bought by shop owner. quantity stores the quantity of bars bought in
by the shop owner. Implement the pseudocode below*/
static void addStock(int[] stock, String[] bars, String
barType, int quantity)
PSEUDOCODE:
BEGIN SWTICH on bartype
Case 1
What is stored in bar type equals “Mars”,
(e.g. barType.equalsIgnorecase(bars[0])
Update position 0 of stock quantity
(e.g. stock(bars[0]) += quantity)
Break out of switch
Case 2
What is stored in bar type equals “Snickers”
Update position 1 of stock with quantity
Bread
Case 3
What is stored in bar type equals “Bounty”
Update position 2 of stock with quantity
Break
Case 4
What is stored in bar type equals “Twix”
Update position 3 of stock with quantity
Break
Default
Incorrect value entered
Inform user
END SWITCH





Got a lab exam tomorrow and cant work out the switch part of this at all, would anyone be able to turn that pseudocode into actual java code for me? Have had a few shots at it, but keep getting lots of errors.

Any help much appreciated.
 
This is trivial Java, you really shouldn't be asking people on a forum to write your lab answers for you.

Ask for help as means to learn, not as a means to cheat.
 
its not a means to cheat. Its question out of a learning exercise, how is that cheating. Turns out you cant put a string into a swtich statement, so that why I was having problems.

Thanks your help folks (-phreaky)
 
jonesy, it's usually considered bad form to ask people to solve a problem for you in its entirety without first providing at least some evidence that you've tried to solve it yourself.
 
Last edited:
yawn,

why do you two feel obliged to post ? Find something better to do with your time. Maybe go help someone with a java question, at least that might be productive.
 
It certainly won't. No wonder graduates are all so clueless.

Threads like these do make me realize that even though I never went to uni or college to learn any programming, I could still probably apply for a programming job of some kind and get the job just by showing what I can do rather than waving around a bit of paper.
 
Threads like these do make me realize that even though I never went to uni or college to learn any programming, I could still probably apply for a programming job of some kind and get the job just by showing what I can do rather than waving around a bit of paper.

That's exactly what I did. :)
 
yawn,

why do you two feel obliged to post ? Find something better to do with your time. Maybe go help someone with a java question, at least that might be productive.

qEKcN.gif
 
Urgh!
Please, try this for yourself!

I'll assume that you have at least made an effort, much as I am not totally sure. Either way, i'll give you a hand.

You can't switch on a String, as you found out. So,.. you could create a hashmap maybe. Try looking up this page http://www.java-tips.org/java-se-tips/java.util/how-to-use-of-hashmap.html

Your hashmap is like a lookup take, you put in all your values such as
Mars is one
Snickers is two
etc etc.
Then, you can run the switch on the hashtable.get("Marsorwhatever"), and compare it to the values in the switch ( 1 for mars, 2 for snickers etc ).

I hope this helps.
 
You can now switch on String using JDK7
emoticon_nerd.gif


You can also switch on enum type in JDK6 etc, which looks like it might suit your scenario quite nicely, but probably outside the scope.
 
Do you have to use two arrays? Like Nutterz said it makes more sense to just use a HashMap with the chocolate name as the key and the stocky quantity as the value.
 
Threads like these do make me realize that even though I never went to uni or college to learn any programming, I could still probably apply for a programming job of some kind and get the job just by showing what I can do rather than waving around a bit of paper.

The coding we do at uni is waaaaay more complex than this thread's question :P (Not to say you are wrong)
 
The coding we do at uni is waaaaay more complex than this thread's question :P (Not to say you are wrong)

Surely it totally depends on the year and the degree. My degree isn't computer science but we still did Java in the first two years and it was aimed at complete beginners so for people to say "all grads are useless" is silly :rolleyes: In my opinion programming isn't something you can master in a 4 year course, there is just too much to learn along with everything else.

But to the OP, look at the documentation before posting on forums as it should point you in the right direction or offer alternative solutions plus surely they have some kind of recommended reading list for your unit?
 
The coding we do at uni is waaaaay more complex than this thread's question :P (Not to say you are wrong)

Oh no I guessed that it was very early beginner stuff, but even so, when I first started teaching myself the switch case was something I learnt in the first week of ****ing about. (I've been at it for 4 years now :))

The one thing I do like about programming is that there is always more stuff to learn, whether it's new technologies or better methods, but it's a subject where there is always room for improvement.
 
Last edited:
The one thing I do like about programming is that there is always more stuff to learn, whether it's new technologies or better methods, but it's a subject where there is always room for improvement.

Now that's a great attitude to have, one I hope you can keep up in the longer term. I'm often surrounded by people at work who are very stuck in their ways with programming, and if you try and approach something at a different angle (but ultimately achieve the same result) it's often regarded as wrong.

I'm just thankful I've got the bit of paper to wave at people, that opens the door to show them what I can do ;)
 
"Hi Please do my homework!"

And re the anti uni lot - this is prob a first years work who is 2 months into programming so lay off the whole "Im better than you" malarky!
 
Back
Top Bottom