Question

Write a java program that demonstrates recursion. This program can be in a java GUI frame...

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

package recursion;

import java.util.Scanner;

// Defines a class TruckLoaded

public class TruckLoaded

{

// Defines a recursive method to load pack to truck

void loadTruck(int numberOfBoxes)

{

// Checks if number of boxes is less than 5

// then display the message and stop the program

if(numberOfBoxes < 5)

{

System.out.println("There are not enough to pack another truck.");

System.exit(0);

}// End of if condition

// Otherwise more than 5 boxes available

else

{

// Display the message

System.out.println("Truck Loaded");

// Recursively calls the method with deducting 5 as parameter

loadTruck(numberOfBoxes -= 5);

}// End of else

}// End of method

// main method definition

public static void main(String[] args)

{

// Scanner class object created

Scanner sc = new Scanner(System.in);

// Accepts number of boxes from the user

System.out.print("\n Enter number of boxes: ");

int numberOfBoxes = sc.nextInt();

// Calls the method using anonymous object of the class

new TruckLoaded().loadTruck(numberOfBoxes);

}// End of main method

}// End of class

Sample Output 1:

Enter number of boxes: 23
Truck Loaded
Truck Loaded
Truck Loaded
Truck Loaded
There are not enough to pack another truck.

Sample Output 2:


Enter number of boxes: 32
Truck Loaded
Truck Loaded
Truck Loaded
Truck Loaded
Truck Loaded
Truck Loaded
There are not enough to pack another truck.

Add a comment
Know the answer?
Add Answer to:
Write a java program that demonstrates recursion. This program can be in a java GUI frame...
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
  • Help Java Javafx Write up some simple sample code to demonstrate indirect recursion. You can just...

    Help Java Javafx Write up some simple sample code to demonstrate indirect recursion. You can just use print statements to demonstrate how they call one another.

  • Can you write with comments please. Thank you. Write a Java Program that asks the user...

    Can you write with comments please. Thank you. Write a Java Program that asks the user to enter the responses on the Console and write the responses to a text file called responses.txt (in the same directory as the program) Write another JAVA prorgram to read responses.txt file and calculate the frequency of each rating in the file and output the number and frequency in two columns (number, frequency) on the Console.

  • JAVA Create a simple Graphical User Interface (GUI) in java with the following requirements:  The...

    JAVA Create a simple Graphical User Interface (GUI) in java with the following requirements:  The interface components will appear centered in the interface, and in two rows. o Row one will have a Label that reads “Please enter a valid integer:” and a Text Field to take in the integer from the user. o Row two will have a Button that when pressed converts the integer to binary  The conversion will be completed using recursion – A separate...

  • can someone please help me with this. I need to use java. Recursion Write and run...

    can someone please help me with this. I need to use java. Recursion Write and run a program that reads in a set of numbers and stores them in a sequential array. It then calls a recursive function to sort the elements of the array in ascending order. When the sorting is done, the main function prints out the elements of the newly sorted array Hint: How do you sort an array recursively? Place the smallest element of the array...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • Write the code in java programming language To get some practice with recursion. You can do...

    Write the code in java programming language To get some practice with recursion. You can do all this in one driver program. Write a recursive method to compute the result of the Fibonacci sequence: Fibonacci(N) = Fibonacci(N -1) + Fibonacci(N-2) N == 0 is 0 N == 1 is 1 Testing: Display the result for Fibonacci(N) and the number of function calls for N = 2, 5 and 10.

  • CSC151 Stock Portfolio GUI Project Goal You are to write a GUI program that will allow...

    CSC151 Stock Portfolio GUI Project Goal You are to write a GUI program that will allow a user to buy, sell and view stocks in a stock portfolio. This document will describe the minimum expected functions for a grade of 90. Your mission is to “go and do better.” You’ll find a list of enhancement options at the end of this document. Objectives By the end of this project, the student will be able to • write a GUI program...

  • In C++ syntax please Write a program that implements and demonstrates a linked list using functions....

    In C++ syntax please Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that stores an integer and then your program will include the following functions appendo- This function accepts the head pointer (by reference) of a linked list and an integer as it's only arguments. It then creates a node, stores the integer argument in the node, and adds it to the end of the list. findo-This function accepts...

  • JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using...

    JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using recursion. The method signatures are included in the starter code below, with a more detailed explanation of what function the method should perform. You will be writing the following methods: stringClean() palindromeChecker() reverseString() totalWord() permutation() You will be using tools in the String class like .substring(), .charAt(), and .length() in all of these methods, so be careful with indices. If you get stuck, think...

  • Write a program to pack boxes with blobs. Write two classes and a driver program. A...

    Write a program to pack boxes with blobs. Write two classes and a driver program. A Blob class implements the following interface. (Defines the following methods.) public interface BlobInterface { /** getWeight accessor method. @return the weight of the blob as a double */ public double getWeight(); /** toString method @return a String showing the weight of the Blob */ public String toString(); } A Blob must be between 1 and 4 pounds, with fractional weights allowed. A default constructor...

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
ADVERTISEMENT