• Post Reply Bookmark Topic Watch Topic
  • New Topic

methods  RSS feed

 
brandon mac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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.

 
Jairo Navarrete
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just copy and paste it in between [code=java*]Here[/code*] take out the *
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done Please show us what you achieved.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!