This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

string object  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a Java program to determine how many cartons are needed to package candy bars.  A carton holds 24 candy bars.

The user enters the number of candy bars.  The program displays the number of cartons.  

Note:  that if the user enters 25, the number of cartons = 2.

I am beginner.
 


int candybar;
int carton;
Scanner scan = new Scanner (System.in);
System.out.println("Enter the number of candybar:");
candybar = scan.nextInt();
carton = ;
System.out.println




I have no idea where to go from that
 
Marshal
Posts: 63822
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't try writing code until you know what you are planning to do. Start by working out the formula you are going to use.
 
Ranch Foreman
Posts: 319
2
Fedora Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems like you just divide the number by the size of the carton and if theres a remainder you add an extra carton.  Done
 
Greenhorn
Posts: 28
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ana,
I am new to Java as well. Please take your time and don't give a half of an answer.
What have you tried to write?
Did you answer the question about what you are trying to accomplish?
Think in real world scenario and everyday math.
You have some key components correct.
Best to you,
SF
 
Ranch Hand
Posts: 67
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to find the Algorithm. On a scrap paper write the step by step instruction.

Try Problem Solving.

Think about the problem. Do you understand the problem? Einstein said if you can't explain your explanation to a child then you yourself didn't understand it.

So try to think about the problem. How to calculate the cartons from the max. number of candies per carton and the total number of candies?

Think about +, -, *, % and /?

What selection structure can you use? if..else,  if ... if else....else or switch case? Do you need loops to repeat certain redundant tasks?

Think about these things.

You know I was in your shoes once when I was learning Java. Read your heart out. Read the online tutorials. Concentrate. What the problem is trying to tell you?

I have college certificate. But learning a programming language was not easy. But if you have the will power everything is possible. Don't give up hope.


Make this your challenge that you will solve this problem by yourself. Try hours. Days. But you have to know what you are doing.

I believe you will write the solution by working hard.

God bless.

 
Mark Ii
Ranch Hand
Posts: 67
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you mean if there are multiple of 24 candy bars then carton = candybars/24?
Also, did you mean if total candy bar = total candy bar + total candy bars mod 24  means carton = (total candy bar / 24) + 1?

Also did you mean if total candy bar <= 0 then carton = 0?

Also, did you mean if total candy bar =  total candy bar mod 24 then carton = 1?
 
Mark Ii
Ranch Hand
Posts: 67
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Ii wrote:Did you mean if there are multiple of 24 candy bars then carton = candybars/24?
Also, did you mean if total candy bar = total candy bar + total candy bars mod 24  means carton = (total candy bar / 24) + 1?

Also did you mean if total candy bar <= 0 then carton = 0?

Also, did you mean if total candy bar =  total candy bar mod 24 then carton = 1?




For each of the four conditions, how can you flow your program? What Selection Structure should you use? Ask questions. A lot of them. From top to bottom. And try to find answers by googling. Post your question about the problem here.  Hope it helps.
 
Campbell Ritchie
Marshal
Posts: 63822
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Ii wrote:. . . And try to find answers by googling.

That is the one bit of iffy advice amongst lots of good adice. You never know whether a web search will return something good or something bad, and there is so much bad code out there that beginners may pick the wrong post

Post your question about the problem here.  Hope it helps.

Of course that helps We shall tell you soon enough whether your code is good o bad
 
Mark Ii
Ranch Hand
Posts: 67
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Mark Ii wrote:. . . And try to find answers by googling.

That is the one bit of iffy advice amongst lots of good adice. You never know whether a web search will return something good or something bad, and there is so much bad code out there that beginners may pick the wrong post

Post your question about the problem here.  Hope it helps.

Of course that helps We shall tell you soon enough whether your code is good o bad




Sir, Why are you being so pessimistic? What I wanted to say is that I know Google doesn't always give you a valid answer. But once you practice with coding from valid tutorials and try, finally you will be able to pick and choose what answer is the best one.

I didn't say that completely trust Google. But Mr. Ritchie even to search for good tutorials you have to use Google. And to pick the best mango from the worst tutorials you need to study.

And evaluating my code I am a beginner also. I believe you yourself was a beginner. So we learn from mistakes and become expert afterward.

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

Mark Ii wrote:. . . Sir, Why are you being so pessimistic?

Because I have seen the sort of rubbish posted on the net. Because I have seen people believe the bad stuff, or misinterpret the good stuff.

I am not being pessimistic, but realistic.

What I wanted to say is that I know Google doesn't always give you a valid answer.

But you didn't say that in the post I quoted.

But once you practice with coding from valid tutorials and try, finally you will be able to pick and choose what answer is the best one. . . .

But that takes time, more time than our beginners have got.

Will delete the accidental duplications.
 
Mark Ii
Ranch Hand
Posts: 67
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Mark Ii wrote:. . . Sir, Why are you being so pessimistic?

Because I have seen the sort of rubbish posted on the net. Because I have seen people believe the bad stuff, or misinterpret the good stuff.

I am not being pessimistic, but realistic.

What I wanted to say is that I know Google doesn't always give you a valid answer.

But you didn't say that in the post I quoted.

But once you practice with coding from valid tutorials and try, finally you will be able to pick and choose what answer is the best one. . . .

But that takes time, more time than our beginners have got.

Will delete the accidental duplications.



Mr. Ritchie I am too simple a person. That's true. I don't understand the complicated process. Thank you for clearing that up.
 
Campbell Ritchie
Marshal
Posts: 63822
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need to be sarcastic.
 
Campbell Ritchie
Marshal
Posts: 63822
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not saying any more now.
 
Mark Ii
Ranch Hand
Posts: 67
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:There is no need to be sarcastic.



No. Mr. Ritchie. I am not being sarcastic or joking with you. I am really a simple man. The complexity of this world sometimes baffles me. That's all.
 
Campbell Ritchie
Marshal
Posts: 63822
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case I apologise.
 
Mark Ii
Ranch Hand
Posts: 67
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to computer simulation, we could emulate two blackholes colliding.


But we never saw blackhole up close neither with our eyes or through our all kinds of telescope.

But we have Hawking Radiation Theory and Stephen Hawking wrote papers on it. There is Alice and Bob theory by Theoretical Physicist Leonard Susskind.

We have no proof. All is a mirage of blackhole. Or about dark matter. Illusive. Never saw it. But can sense it. There.

If only the universe were simple like bits and bytes and petabytes of a mainframe computer.




 
Mark Ii
Ranch Hand
Posts: 67
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:In which case I apologise.



No need to apologize, Mr. Ritchie. It was my mistake. I wasn't clear. Sorry.
 
Campbell Ritchie
Marshal
Posts: 63822
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Ii wrote:. . . . Sorry.

Apology accepted
 
Steven Fraser
Greenhorn
Posts: 28
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since I am teaching myself Java, may I post what I have for this problem?
It doesn't work completely.
I am not sure what to do but maybe a cattle prod in the right direction will help us newbies figure it out.
Thank you,
Steve
 
Sheriff
Posts: 5813
150
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your willingness to share, however we try to let the OP post their code first before anyone else posts theirs.
 
Steven Fraser
Greenhorn
Posts: 28
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a little confused since being on this site. I see many posts that don't ever seem to be "resolved", this one included.
Did the original poster ever figure this out? I did and am excited to share, only because of the satisfaction of figuring it out, not because I am "all that and a box of candy bars".
I am new to Java, old in life and just love it.
Sorry for the excitement coming from a Grandfather... ;-)
Cheers,
Steve
 
Sheriff
Posts: 13384
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We often see people post and then for some reason or another, they don't follow up when they see they can't get canned code from this site. Not sure if that is the reason for this OP but since enough time has passed, I think you're fine to share your solution without risking the aiding and abetting of academic dishonesty. So go ahead and please show your solution but be ready to take comments, positive and otherwise.

Another alternative is to start your own thread and just cite this thread as the  basis for your question. To reference this thread, you can grab the link from the forum's topic listing, then paste it in and use the "URL" button to make it a clickable link when writing your post in the message editor.

Basically, your new thread would start with something like this (feel free to copy it verbatim)

Hello, ranchers!

I was reading through [url=https://coderanch.com/t/707168/java/string-object]this thread[/url] and thought it might be fun to try to solve it myself. Here's the code I came up with:
[code=java]
(Your code goes here)
[/code]

Your comments or questions go here
 
Steven Fraser
Greenhorn
Posts: 28
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Junilu!
 
If you're gonna buy things, buy this thing and I get a fat kickback:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!