• 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.
  • Posts

    • The 2025 Complete Splunk Beginner Bundle is now 25% off by Steven Parker Today's highlighted deal comes via Neowin Deals store, where you can save 75% on The 2025 Complete Splunk Beginner Bundle. Splunk is a powerful data platform used to gather information from multiple sources and index it for efficient access. You can then use collected data to create visualizations, analytics, and a variety of automated and security-related functions. With its web-style interface, Splunk is easy to use and is utilized by many companies worldwide. What's Included: Splunk Fundamentals for Effective Management of SOC and SIEM Oak Academy 38 Lessons (3.5h) Lifetime $20.00 Value Splunk | Splunk Core Certified User Certification Prep Lab Oak Academy, 63 Lessons (6h),Lifetime, $20.00 Value Splunk | Splunk Core Certified Power User SPLK 1002 Prep Oak Academy, 53 Lessons (5.5h), Lifetime, $20.00 Value Splunk| Splunk Enterprise Certified Admin Certification Prep Oak Academy, 68 Lessons (8.5h), Lifetime, $20.00 Value Requirements Basic understanding of IT and networking concepts Familiarity with Linux and Windows operating systems A computer with internet access for hands-on practice Good to Know Length of time users can access this course: lifetime Access options: desktop or mobile Redemption deadline: redeem your code within 30 days of purchase Experience level required: all levels Certificate of Completion ONLY Updates included Closed captioning NOT available NOT downloadable for offline viewing Learn more about our Lifetime deals here! Lifetime access to this 2025 Complete Splunk Beginner Bundle normally costs $80, but this deal can be yours for just $19.99, that's a saving of $60. For full terms, specifications, and info, click the link below. Get the 2025 Complete Splunk Beginner Bundle for just $19.99 (75% off MSRP: $80) Although priced in U.S. dollars, this deal is available for digital purchase worldwide. We post these because we earn commission on each sale so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. Other ways to support Neowin Whitelist Neowin by not blocking our ads Create a free member account to see fewer ads Make a donation to support our day to day running costs Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: Neowin benefits from revenue of each sale made through our branded deals site powered by StackCommerce.
    • and you got scammed because 2077 launch its one of the worse thing i ever remember and even to this day the game never reached its potential
    • Basic Auth needs to die especially for anything internet bound and exchange in the office 365 suite is extremely internet bound as the cloud service it is. Microsoft has been announcing it for years now. There is always a balance between security and usability, in this case it should be glaringly obvious.
    • the more they want to charge more the more people won't buy anything at launch and wait until its reduced price or discount, hell i even waited until its free on epic game store. and smoke pass model its not viable and having others to pay 80 dollars to subside it wont help
  • Recent Achievements

    • Apprentice
      Wireless wookie went up a rank
      Apprentice
    • Week One Done
      bukro earned a badge
      Week One Done
    • One Year In
      Wulle earned a badge
      One Year In
    • One Month Later
      Wulle earned a badge
      One Month Later
    • One Month Later
      Simmo3D earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      558
    2. 2
      ATLien_0
      258
    3. 3
      +FloatingFatMan
      182
    4. 4
      Michael Scrip
      125
    5. 5
      Steven P.
      101
  • Tell a friend

    Love Neowin? Tell a friend!