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.”
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.
Write a java program that demonstrates recursion. This program can be in a java GUI frame...
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 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.
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...
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...
Write a Java program that is able to apply a hash function to a first and last name (key) of a person, and lookup the appropriate index in a hash table modeling a telephone directory. The telephone number (value) should be returned on doing a lookup (you can pre-populate the table with 20 entries for this purpose). You can use existing Java code (or libraries) to model the hash table, however, your program should be customized to use the hash...
Hi, I need help with this Java GUI program. The instruction and code can be found below thanks. This GUI is the start of a simple number entry GUI class. The program displays four Buttons and every time the user clicks on a button, the corresponding button label appears on the bottom of the GUI. As a hint, you might break up the design of the GUI into two parts: one part holds the buttons, the other holds the completed...
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 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.
Write a simple GUI-based Java program that may be used to control a washing machine. Use suitable Swing components to allow the washing machine operator to perform the following functions: 1) Switch the machine on. 2) Choose a temperature from a list. 3) Spin speed selection buttons - can be 600, 800 or 1200 RPM. 4) Display the current status of the wash cycle. Show the top-level design of the GUI, including any Panels and related Layout Manager objects that...
In Java, write an application that inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print 4 2 3 3 9 Using GUI input and message dialog Thank you