Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

[BEGINNER ] If statement in applet  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doing an assignment, its a vending machine that has 4 items and a message is meant to be displayed whenever each of the items in selected or the program terminated.
It's melting my head, cant find anything about it on google.

This is only the beginning of my code.

1. I don't know if I should have the items showing in the applet as a label  or gdrawstring.
2. I am trying to implement an if statement to display a gdrawstring if the statement is met. I just want to know I'm on the right track with the code. In this scenario upon pressing 5 the program is meant to terminate and display the total amount of the products. However I am unable to as " VendingMachine.java:51: error: incompatible types: int cannot be converted to boolean if(total = 5)"

What is the correct way to implement this?






 
Rancher
Posts: 1916
15
Android Eclipse IDE Java Java ME
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Single = sign is assignment operator

 
Ola Krip
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Swastik Dey wrote:Single = sign is assignment operator



OMG WTF I swear I tried it before but I've been messing around with this code for so long now that I probably  messed something up last time I tired it. Thank you omg I  feel so dumb!

Can you advise me if the items like water, soup etc. should be as a label or gdrawstring?
 
Swastik Dey
Rancher
Posts: 1916
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can you advise me if the items like water, soup etc. should be as a label or gdrawstring?



What do you mean by this?
 
Marshal
Posts: 61753
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why are you writing applets? They are obsolete. As for a label or writing text onto a panel, that depends on what you are going to do with it.
 
Ola Krip
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Why are you writing applets? They are obsolete. As for a label or writing text onto a panel, that depends on what you are going to do with it.



College assigment..

This is my new code now
I have to implement a counter so that it counts how many times each product is entered. Upon pressing 5, the program is meant to terminate and show how many items each product was entered.
I have one textfield for the user input (per my lecturers request) and I don't know how to implement the counter so that I can display the results for each proudct at the end. I think my lecturer said we need 5 counters but I  cant grab my head around how to input that into the program. I need a while loop for every counter right?

 
Campbell Ritchie
Marshal
Posts: 61753
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ola Krip wrote:. . . College assigment..  . . .

In which case, why are they teaching applets?
How are you going to get any actions to work? You have several labels with prices on, but can you add action listeners to them? Why have you made the class implement action listener? You might see that in many books, but I think it violates the principles of object‑orientation. It is also unnecessary since the introduction of λs in 2014.
I don't understand how your count of items makes people select a different drink.

I think you are going to have to go back to your design and decide what you are going to do. Write down on paper, without using any computing words, how you are going to count items.
 
Ola Krip
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Ola Krip wrote:. . . College assigment..  . . .

In which case, why are they teaching applets?
How are you going to get any actions to work? You have several labels with prices on, but can you add action listeners to them? Why have you made the class implement action listener? You might see that in many books, but I think it violates the principles of object‑orientation. It is also unnecessary since the introduction of λs in 2014.
I don't understand how your count of items makes people select a different drink.

I think you are going to have to go back to your design and decide what you are going to do. Write down on paper, without using any computing words, how you are going to count items.



So basically I have 1 text field for the user to input the item he wants, and for example when he picks out water and tea he then wants to finish the program. So he pressed 5 to terminate. Upon pressing 5 its meant to show

Water selected : 1
Soup selected : 0
Tea Selected : 1
etc.

This part I don't understand how to implement.
I'll probably just have to continue researching and trying to figure it out then.
Thanks
 
Ola Krip
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it ,all good. thanks all
 
Campbell Ritchie
Marshal
Posts: 61753
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ola Krip wrote: . . . This part I don't understand how to implement.  . . .

You need to design it before you can implement it. But you seem to have something working. Please show us it and let's see if we can't get some more marks for you.
 
Ola Krip
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Ola Krip wrote: . . . This part I don't understand how to implement.  . . .

You need to design it before you can implement it. But you seem to have something working. Please show us it and let's see if we can't get some more marks for you.



This will probably give a headache to people who actually know java but this is where I'm at. I have pretty much everything working somehow except for calculating the total. The total is always 1 item behind.  don't get it.  For example, if you have 1 Tea it shows the total as 0. And After the 2nd item input is when it start calculating. Its hard to explain but see the screenshots. I have it so it multiplies the counter by the product cost.. I can't figure this out.. See the screenshots of the applet running






applet-1.PNG
[Thumbnail for applet-1.PNG]
applet-2.PNG
[Thumbnail for applet-2.PNG]
 
Campbell Ritchie
Marshal
Posts: 61753
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't understand the design. You are adding one to the coffee counter if you click the coffee label? Surely you should push a coffee button. So, you push the coffee button and order one cup of coffee. That means coffee counter = 1 and counter = 1. Then you click for tea, and now coffee counter = 1 and tea counter = 1 and counter = 2. That is what I would expect. Then you have the option of clicking another drink, or finishing. You obviously have only small tables which will only accommodate five drinks, so you have to stop at five. Or maybe it is because you are charging $70 for a cup of coffee and nobody can afford more than five? Or are those numbers coordinates?
I can't understand why you have those tests for counter in the paint() method. What's more, the paint() method is changing the counters. I think that looks wrong.
 
Ola Krip
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I can't understand the design. You are adding one to the coffee counter if you click the coffee label? Surely you should push a coffee button. So, you push the coffee button and order one cup of coffee. That means coffee counter = 1 and counter = 1. Then you click for tea, and now coffee counter = 1 and tea counter = 1 and counter = 2. That is what I would expect. Then you have the option of clicking another drink, or finishing. You obviously have only small tables which will only accommodate five drinks, so you have to stop at five. Or maybe it is because you are charging $70 for a cup of coffee and nobody can afford more than five? Or are those numbers coordinates?
I can't understand why you have those tests for counter in the paint() method. What's more, the paint() method is changing the counters. I think that looks wrong.



The number are coordinates. We are not meant to have a button. We are meant to have 1 textfield to input the choice. We need to make a self defined total with a method. The code works, caluclates fine except it doesn't calculate if the product is just :1 ..
 
Ola Krip
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ola Krip wrote:

Campbell Ritchie wrote:I can't understand the design. You are adding one to the coffee counter if you click the coffee label? Surely you should push a coffee button. So, you push the coffee button and order one cup of coffee. That means coffee counter = 1 and counter = 1. Then you click for tea, and now coffee counter = 1 and tea counter = 1 and counter = 2. That is what I would expect. Then you have the option of clicking another drink, or finishing. You obviously have only small tables which will only accommodate five drinks, so you have to stop at five. Or maybe it is because you are charging $70 for a cup of coffee and nobody can afford more than five? Or are those numbers coordinates?
I can't understand why you have those tests for counter in the paint() method. What's more, the paint() method is changing the counters. I think that looks wrong.



The number are coordinates. We are not meant to have a button. We are meant to have 1 textfield to input the choice. We need to make a self defined total with a method. The code works, caluclates fine except it doesn't calculate if the product is just :1 ..



The total calculates the money due. The money is either 2.00 dollar or 1.5
 
Ola Krip
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I can't understand the design. You are adding one to the coffee counter if you click the coffee label? Surely you should push a coffee button. So, you push the coffee button and order one cup of coffee. That means coffee counter = 1 and counter = 1. Then you click for tea, and now coffee counter = 1 and tea counter = 1 and counter = 2. That is what I would expect. Then you have the option of clicking another drink, or finishing. You obviously have only small tables which will only accommodate five drinks, so you have to stop at five. Or maybe it is because you are charging $70 for a cup of coffee and nobody can afford more than five? Or are those numbers coordinates?
I can't understand why you have those tests for counter in the paint() method. What's more, the paint() method is changing the counters. I think that looks wrong.



How else would I count how many times a person chose the product, because the amount each item is chosen is meant to display at the end along with the total money due.
 
Ola Krip
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added money symbols for clarification.
The applet is meant to display how many time the listed products are chosen and show the total amount of money due.
However my code does not calculate if the product is selected 1 times, it starts calculating the total at the 2nd item
So if there is 5 waters chosen at $2 dollars each, it shows total as $8 - I am always $2 short
anpple.PNG
[Thumbnail for anpple.PNG]
 
Greenhorn
Posts: 6
IntelliJ IDE Java Mac Netbeans IDE Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Ola Krip wrote:. . . College assigment..  . . .

In which case, why are they teaching applets?
How are you going to get any actions to work? You have several labels with prices on, but can you add action listeners to them? Why have you made the class implement action listener? You might see that in many books, but I think it violates the principles of object‑orientation. It is also unnecessary since the introduction of λs in 2014.
I don't understand how your count of items makes people select a different drink.

I think you are going to have to go back to your design and decide what you are going to do. Write down on paper, without using any computing words, how you are going to count items.



A lot of academies are teaching applets still... is gone now in JDK 11.
I have in my course an assignment to create a card simulator in applets...
 
Campbell Ritchie
Marshal
Posts: 61753
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ola Krip wrote:. . .  We are not meant to have a button. We are meant to have 1 textfield to input the choice. . . . .

You are now in a position where you get good marks for following instructions rather than writing good code. I have seen too many over‑specified assignments, which deprive the candidates of the opportunity to design the application properly. Most of them also require sub‑optimal code. You will have to write poor quality code so you can have good marks .

You can add an action listener to a text component, I think. You would still have to work out from the numbers whether you want tea or coffee.

The code works, caluclates fine except it doesn't calculate if the product is just :1 ..

That tells me it doesn't work.
“I'd like a cup of coffee, please.”
“That'll be $70, please.”
“How much!!?”
“If you brought a friend with you, who had a cup of tea, we could work out the bill correctly.”
Hahahahahahahahaha!
 
Ola Krip
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LOL YOU TELLING ME MY CODE IS MESSED UP DID FIX IT.
I took the counters out form the paint method and it works.... thanks







 
Campbell Ritchie
Marshal
Posts: 61753
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Victor Nizeyimana wrote:. . . A lot of academies are teaching applets still... is gone now in JDK 11.
I have in my course an assignment to create a card simulator in applets...

I suppse if they let everybody find out early they are still teaching applets, they wouldn't have any applicants. I am relived to see they have disappeared. Has the Applet class been removed or has it been deprecated? Let's have a look. The Applet class is still there, but, as you said

The Applet link wrote:Deprecated.
The Applet API is deprecated, no replacement.

It says it was deprecated in Java9.
 
Victor Nizeyimana
Greenhorn
Posts: 6
IntelliJ IDE Java Mac Netbeans IDE Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Victor Nizeyimana wrote:. . . A lot of academies are teaching applets still... is gone now in JDK 11.
I have in my course an assignment to create a card simulator in applets...

I suppse if they let everybody find out early they are still teaching applets, they wouldn't have any applicants. I am relived to see they have disappeared. Has the Applet class been removed or has it been deprecated? Let's have a look. The Applet class is still there, but, as you said

The Applet link wrote:Deprecated.
The Applet API is deprecated, no replacement.

It says it was deprecated in Java9.



I see it in the doc... Only some of my teachers are still using JDK 10 and is still working... for some strange reason.
I use now at this moment: jdk1.8.0_191.jdk = JDK 8
 
Campbell Ritchie
Marshal
Posts: 61753
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ola Krip wrote:LOL YOU TELLING ME MY CODE IS MESSED UP DID FIX IT.
I took the counters out form the paint method and it works.... thanks  . . .

In reverse order:-
  • 1: That's a pleasure
  • 2: I still don't understand how you are getting from counter = 2 to prices for tea.
  • 3: Please avoid ALL UPPER CASE WRITING, even when you are fixing code.
  •  
    Victor Nizeyimana
    Greenhorn
    Posts: 6
    IntelliJ IDE Java Mac Netbeans IDE Redhat TypeScript
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Welcome to the Ranch

    Thank you Missing some edit button from my last post

    Can you please check out my post: https://coderanch.com/t/702905/java/options-card-shuffling-applet

    Thanks!
    Victor
     
    Ola Krip
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Ola Krip wrote:LOL YOU TELLING ME MY CODE IS MESSED UP DID FIX IT.
    I took the counters out form the paint method and it works.... thanks  . . .

    In reverse order:-
  • 1: That's a pleasure
  • 2: I still don't understand how you are getting from counter = 2 to prices for tea.
  • 3: Please avoid ALL UPPER CASE WRITING, even when you are fixing code.



  • Okay. Thank you.
    I am concerned tho as you said why are they teaching us applets, now I'm worried that when I would ever try to get a programming/software development job I will be using outdated stuff! I didn't know applets are not used anymore. Hopefully my whole course is not like this...
     
    Campbell Ritchie
    Marshal
    Posts: 61753
    193
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Victor Nizeyimana wrote:. . . Missing some edit button from my last post . . .

    We disabled the edit button because we had problems in the past with changes to previous posts.
     
    Victor Nizeyimana
    Greenhorn
    Posts: 6
    IntelliJ IDE Java Mac Netbeans IDE Redhat TypeScript
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Victor Nizeyimana wrote:. . . Missing some edit button from my last post . . .

    We disabled the edit button because we had problems in the past with changes to previous posts.



    Oke Good to know! Just purchased Pie. I shared some slice with you, hope you like it!
     
    Campbell Ritchie
    Marshal
    Posts: 61753
    193
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ola Krip wrote:. . . I'm worried that when I would ever try to get a programming/software development job I will be using outdated stuff! I didn't know applets are not used anymore. . . .

    I am rather more worried that your teachers don't seem to know that. Make sure to keep posting on a good website (there is one here) where people will tell you pretty quickly if you are using outdated stuff.

    And yes, avoid saying anything about applets when looking for jobs. Unfortunately, if your assessment tells you to use applets, you will have to use applets to get good marks. Be sure to compile your code with Java9+ and ask your teacher about the warnings
     
    Campbell Ritchie
    Marshal
    Posts: 61753
    193
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Victor Nizeyimana wrote:. . .  some of my teachers are still using JDK 10 and is still working... . . .

    It has been deprecated, not deleted. Read about the @Deprecated annotation.
    Thank you for the pie, which was delicious
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!