• 0

Converting to methods


Question

I am ready to submit an assignment for my programming fundamentals class (intro class to Java 1) and the use of methods is extra credit for each assignment, so I have completed the assignment but was needing help converting the code to use methods. The instructor says no less than 6 methods should be used (which is where I get lost) and each calculation needs to be in its own method. Anyways, any some help would be appreciated... (the class uses only psudocode)

 

Question: Create the logic for a program that continuously prompts the user for a number of dollars until the user enters 0. Pass each entered amount to a conversion method that displays a breakdown of the passed amount into the fewest bills; in other words, the method calculates the number of 20s, 10s, 5s, and 1s needed.

 

My current (psudo)Code: 
 

start
	Declarations
		num dollars
	dollars = inputDollarAmt()
	while dollars not equal to 0
		dollarConversion(dollars)
		dollars = inputDollarAmt()
	endwhile
stop
num inputDollarAmt()
	Declarations
		num dollars
		num QUIT = 0
	output “Enter a dollar amount or ”, QUIT, “ to quit”
	input dollars
return dollars

void dollarConversion(num dolls)
	Declarations
		num twenties
		num tens
		num fives
		num ones
	twenties = dolls / 20
	dolls = dolls % 20
	tens = dolls / 10
	dolls = dolls % 10
	fives = dolls / 5
	dolls = dolls % 5
	ones = dolls
	output “You will need: ”, twenties, “ 20s, ”, tens, “ 10s, ”, fives, “ 5s, and ”, ones, “ 1s”
return

 

Link to comment
https://www.neowin.net/forum/topic/1315002-converting-to-methods/
Share on other sites

3 answers to this question

Recommended Posts

  • 0
  On 27/11/2016 at 15:29, ghostprodigy333 said:

I  was needing help converting the code to use methods. The instructor says no less than 6 methods should be used (which is where I get lost)

Expand  

Well, at worst you can just break off single lines into their own method just to satisfy the requirement. Kind of an academic exercise but hey, this is academia right?

 

Start by converting your code to Java and perhaps it'll be a bit more complex than the pseudocode you have here (ex.: what if the user enters something else than a number?). That could give you ideas for extra methods.

 

By the way, the idea of declaring variables at the top of a function and assigning them only later is very strange in Java and basically every language but C... Wonder why your pseudocode has that all over the place. Please don't write Java like this:

 

int a;
int b;
a = bla;
b = bleh;

Instead do:
 

int a = bla;
int b = bleh;

 

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.