Question

(*Use Netbeans or hardcode if you choose to use a GUI over console app.) Write a...

(*Use Netbeans or hardcode if you choose to use a GUI over console app.)

Write a java program that demonstrates recursion. This program can be in a java GUI frame or as a console application. On this one it is up to you. (It would probably work better as a console program for this particular program.)

The input for this program is a number of boxes. Ask the user for this with a textbox or a prompt on the command line.

Demonstrate recursion with a function called LoadTruck() that takes a number of boxes to be packed as a parameter. This function should print “Truck Loaded” and subtract 5 from the number of boxes and recursively call itself until there are less than 5 boxes. That time through it should print “There are not enough to pack another truck.”

(If you wish to use a GUI, it might be best to “print” the lines by concatenating them onto the text in a textArea along with a newline character. There may be other ways you can think of to do this in a GUI using other controls. Anyway you display the trucks loaded is fine.)

This program must have a recursive method. If you use a loop to load the trucks it will not count for any of the points for this one. This lab is to demonstrate recursion.


Example of output for 23 boxes:

“Truck Loaded”

“Truck Loaded”

“Truck Loaded”

“Truck Loaded”

“There are not enough to pack another truck.”

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question, here is the console application .

========================================================================
import java.util.Scanner;

public class Truck {

    public static void LoadTruck(int boxes){
        if(boxes<5){
            System.out.println("There are not enough to pack another truck.");
        }else{
            System.out.println("Truck Loaded");
            boxes-=5;
            LoadTruck(boxes);
        }

    }
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of boxes you like to load: ");
        int boxes = scanner.nextInt();
      LoadTruck(boxes);
    }
}

=================================================================

Code screenshot and output

Thanks a lot, please do give a thumbs up : )

Add a comment
Know the answer?
Add Answer to:
(*Use Netbeans or hardcode if you choose to use a GUI over console app.) Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT