• 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

    • So is Ubuntu and Fedora (with GNOME). It's a welcome move and those that need X11 can easily install as pointed out in the article it but that won't stop a loud minority from whining of course. Funny but It will actually be easier to install X11 on Ubuntu then enable Flatpak support/Flathub and use them.
    • End of an era? Kubuntu is removing default support for X11 in new installs by David Uzondu X11, the old window system whose days have long felt numbered, just saw another one of its major supporters head for the exit. Kubuntu has decided to follow its parent distro's lead, making its next release, version 25.10, a Wayland-only affair for fresh installs. It seems many Linux developers see Wayland as the future. Just recently, Linux Mint started working to improve support for the protocol in Cinnamon, tackling lingering issues with keyboard layouts and input methods. You can even see the progress in KDE's development, where an upgrade to Wayland PiP is planned for KDE Plasma 6.5. So what's the logic behind dropping a session that, for the most part, still works? According to Kubuntu's Rik Mills, the team wants to "rip off this sticking plaster" now, in an interim release, rather than ###### off a lot of people by doing it in the next Long-Term Support version, 26.04. The developers feel that maintaining code for the aging X11 system holds back progress on security and new features that Wayland can enable more easily. Plus, supporting two separate display servers is a massive undertaking. Of course, this change might have some people worried, but relax; all is not lost if you still need the old session. If you're running hardware that acts up, like some older NVIDIA cards, or who relies on an ancient application that doesn't play nicely with the XWayland compatibility layer, you can still get your familiar session back. Just enter the following command in your terminal: sudo apt install plasma-session-x11 Once that command finishes, the X11 session will appear as an option on the login screen, so you can carry on as before. As OMGUbuntu notes, not everyone in the Ubuntu family is following its lead just yet. Other official flavors like Xubuntu, Ubuntu Budgie, and Ubuntu Cinnamon are expected to keep offering an X11 session on their default installs for this cycle.
    • Mangohud hasn't been built into "Steam Deck", it has been built into SteamOS. I understand that your goal is to try and praise MS for a simple feature that everyone else has, but we are comparing OS vs OS. Hardware does not have anything "built-in". Software does. Like it or not, SteamOS has it "built-in". And it is far superior to XBOX game bar's information.
    • Please don't inject yourself into a discussion you did not read or understand what was said. No where did I say Windows was hard to use. I knew when seen two notifications with your handle the replies they were going to be a waste of time to read.
    • Exactly. The UI feels a lot snappier when animations are disabled. I think I might actually give it a try for a while, I'm liking it so far.
  • Recent Achievements

    • First Post
      Johnny Mrkvička earned a badge
      First Post
    • Week One Done
      viraltui earned a badge
      Week One Done
    • One Month Later
      serfegyed earned a badge
      One Month Later
    • Dedicated
      firey earned a badge
      Dedicated
    • Dedicated
      fettermanj earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      644
    2. 2
      Michael Scrip
      221
    3. 3
      ATLien_0
      218
    4. 4
      Steven P.
      143
    5. 5
      +FloatingFatMan
      142
  • Tell a friend

    Love Neowin? Tell a friend!