• 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

methods

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay so from my previous post and what I have learned so far from asking for help on here I shall start with english instead of what I think should be written for code.

Task: Convert cups to quarts

Declare the double variables

cups, quarts

Declare result variable of conversion
cupsToQuarts
4 cups = 1 quart

Ask user to provide input
"How many cups do you have?"

Use user input of cups and convert to quarts with method CupsToQuarts

Display results

So I know how to start with the variables and that it would be a public static double I believe but don't really understand the methods and calling business part

I have a file that I have started but it won't let me attach it as a .java file so not sure if that would be helpful to see what I have done so far.

 
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just copy and paste it in between [code=java*]Here[/code*] take out the *
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that is not how you work out an algorithm from English. Starting with “declare the double variable” means you are starting several stages along the process. You should be starting with

There are four cups to a quart.
Multiply quarts by four to get cups

No mention of double or anything.

And your variables should not be public static.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

brandon mac wrote:Okay so from my previous post and what I have learned so far from asking for help on here I shall start with english instead of what I think should be written for code.


Well, that's a good start but, as Campbell says, when you do so you should concentrate on what you need to do, not how you're going to do it. A lot of what you've written is Java converted to English, when what you need to do is the exact opposite.

How about this:

Task: Convert cups to quarts
Steps:
1. Get a number of cups.
2. Convert the number of cups to a number of quarts.
3. Display the number of quarts.

No Java, no mention of doubles or integers, or declarations, simply a statement of the steps required to complete the task. Notice also that we haven't even said how we're going to get the 'number of cups', because at this stage it isn't important.
We can decide whether we get it from the keyboard, or a file, or a GUI component, or indeed just initialise a variable for it, when we write our program; but that Step 1 tells us that we will probably want a method called something like 'getNumberOfCups()' that returns us a number of some sort.

And if we need to explain things a bit further - eg, what we need to do to convert from cups to quarts - we can break down that Step 2 into sub-steps (or simply add something like "1 quart == 4 cups" to clarify it).

Remember: When describing a solution, avoid thinking about Java. Think about the problem.

It's possibly also worth mentioning that an awful lot of problems/programs/methods follow a similar pattern:
1. Get some data.
2. Process it.
3. Display (or return) the results.
It's called the IPO model, and it's been around pretty much as long as programming languages.

HIH

Winston
 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was able to figure this one out and understand methods pretty well now thanks for everyone's assistance.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done Please show us what you achieved.
 
Liar, liar, pants on fire! refreshing plug:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic