Java_Project_6_answers
.docx
keyboard_arrow_up
School
University of North Georgia, Dahlonega *
*We aren’t endorsed by this school
Course
1302
Subject
Computer Science
Date
Apr 29, 2024
Type
docx
Pages
7
Uploaded by melroyoliver
Java Project 6: Recursion, Searching & Sorting Algorithms
(60 points + 10 pts bonus)
Submission due date: Sunday 11:59 pm, 4/14/2024
CSCI 1302
Submission includes:
1)
The screenshots of the required
program and the correct result for Q1
2)
The answers for Q2 ~ Q3
Q1
. (10 pts) Write Java program with a recursive method called evenfact(
N
) which
takes in a number N and returns the factorial of the even numbers between the given number N and 2. For example:
evenfact
(4) returns 8, because 2*4=8
evenfact
(9) returns 384, because 2*4*6*8=384
Q2.
(10 pts) Given the sorted list:
2 8 15 38 76 100 138 270 386
To trace the execution for a binary search
, searching for the number 270
.
Step 1: find index 4, compare 76 and 270, because 76 < 270, pointer moves to left N Right Y.
Step 2: find index 6, compare 138 and 270, because 138 < 270, pointer moves to left N Right Y.
Step 3: find index 7, compare 270 and 270, because 270 = 270, pointer moves to left N Right N.
Result: Number 270 is found at index 7.
Q3. (Total 40 pts) Given the following list:
100 15 8 76 138 270 38 To implement the different sort algorithms to sort values from smallest to the biggest. Please fill out each step and find the update list content. If some change happens in the step, please fill the change information, otherwise leave the blank because there is no change.
1)
Selection sort (10 pts)
Step 1: swap 100 and 8, the list: 8, 15, 100, 76, 138, 270, 38
Step 2: swap 15 and 15, the list: 8, 15, 100, 76, 138, 270, 38
Step 3: swap 100 and 38, the list: 8, 15, 38, 76, 138, 270. 100
Step 4: swap 76 and 76, the list: 8, 15, 38, 76, 138, 270, 100
Step 5: swap 138 and 100, the list: 8, 15, 38, 76, 100, 270, 138
Step 6: swap 270 and 138, the list: 8, 15, 38, 76, 100, 138, 270
2)
Insertion sort (10 pts)
Step 1: insert 15, the list: 100, 15, 8, 76, 138, 270
Step 2: insert 8, the list: 8, 100, 15, 76, 138, 270, 38
Step 3: insert 76, the list: 8, 100, 15, 76, 138, 270, 38
Step 4: insert 138, the list: 8, 15, 76, 100, 138, 270, 38
Step 5: insert 270, the list: 8, 15, 76, 100, 138, 270, 38
Step 6: insert 38, the list: 8, 15, 38, 76, 100, 138, 270
3)
Bubble sort
(10 pts)
Stage 1:
the first iteration
Step 1: swap 100 and 15, the list: [15, 100, 8, 76, 138, 270, 38]
Step 2: swap 100 and 8, the list: [15, 8, 100, 76, 138, 270, 38]
Step 3: swap 100 and 76, the list: [15, 8, 76, 100, 138, 270, 38]
Step 4: swap 100 and 138, the list: [15, 8, 76, 100, 138, 270, 38]
Step 5: swap 138 and 270, the list: [15, 8, 76, 100, 138, 270, 38]
Step 6: swap 270 and 38, the list: [15, 8, 76, 100, 138, 38, 270]
After the first iteration, the list is partially sorted as follows: [15, 8, 76,
100, 138, 38, 270]
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Related Questions
JAVA
Question 2:
For two integers m and n, their GCD (Greatest Common Divisor) can be computed by a recursive method. Write a recursive method gcd(m,n) to find their Greatest Common Divisor.
Method body:
If m is 0, the method returns n. If n is 0, the method returns m. If neither is 0, the method can recursively calculate the Greatest Common Divisor with two smaller parameters: One is n, the second one is m mod n (or m % n). The recursive method cannot have loops.
Note: although there are other approaches to calculate Greatest Common Divisor, please follow the instructions in this question, otherwise you will not get the credit.
main method:
Prompt and read in two numbers to find the greatest common divisor.
Call the gcd method with the two numbers as its argument. Print the result to the monitor.
Example program run:
Enter m:
12
Enter n:
28
GCD(12,28) = 4
And here is what I have so far,
package CSCI1302;import java.util.*;public class RecursionDemo {
public static void…
arrow_forward
Determine whether a string is a palindromeA palindrome is a string of characters that reads the same from right to left as it does from left to right, regardless of punctuation and spaces.The specifications for this assignment are:
•Write and test a non-recursive solution in Java that determines whether a string is a palindrome
•Your program should consist of at least two methods:
(1) the main method
(2) the method which performs the task of determining whether the specified string is a palindrome. You should name this method isPalindrome. You should name the class that contains your “main” method and the isPalindrome method FindPalindrome.
•You must use a Stack and a Queue in your solution:
Write your own Stack and Queue based on the Vector in the Java API and use those in your solution. You should name those classes StackVector and QueueVector respectively. You already have access to the relevant exception classes and interfaces for the above ADTs.
•All of your belong to a Java…
arrow_forward
Java -
When the compiler compiles your program, how is a recursive call treated differently than a non-recursive method call?
What property of fractals lends itself to recursive thinking?
arrow_forward
write a code in java (using recursion)
arrow_forward
Task #1 Tracing Recursive Methods
1. Copy the file Recursion.java (see Code Listing 16.1) from the Student Files or
as directed by your instructor.
2. Run the program to confirm that the generated answer is correct. Modify the
factorial method in the following ways:
a. Add these lines above the first if statement:
int temp;
System.out.println ("Method call
"calculating
"Factorial of: " + n);
Copyright © 2019 Pearson Education, Inc., Hoboken NJ
b. Remove this line in the recursive section at the end of the method:
return (factorial(n - 1) * n);
c. Add these lines in the recursive section:
temp - factorial (n - 1) ;
System.out.println ("Factorial of: " +
(n - 1) + " is " +
temp);
return (temp * n);
3. Rerun the program and note how the recursive calls are built up on the run-time
stack and then the values are calculated in reverse order as the run-time stack
"unwinds".
arrow_forward
import java.util.Scanner;
public class LabProgram { // Recursive method to draw the triangle public static void drawTriangle(int baseLength, int currentLength) { if (currentLength <= 0) { return; // Base case: stop when currentLength is 0 or negative }
// Calculate the number of spaces needed for formatting int spaces = (baseLength - currentLength) / 2;
if (currentLength == baseLength) { // If it's the first line, don't output spaces before the first '*' System.out.println("*".repeat(currentLength) + " "); } else { // Output spaces and asterisks System.out.println(" ".repeat(spaces) + "*".repeat(currentLength) + " "); }
// Recursively call drawTriangle with the reduced currentLength drawTriangle(baseLength, currentLength - 2); } public static void drawTriangle(int baseLength) { drawTriangle(baseLength, baseLength); }
public static…
arrow_forward
import java.util.Scanner;
public class LabProgram { // Recursive method to draw the triangle public static void drawTriangle(int baseLength, int currentLength) { if (currentLength <= 0) { return; // Base case: stop when currentLength is 0 or negative }
// Calculate the number of spaces needed for formatting int spaces = (baseLength - currentLength) / 2;
if (currentLength == baseLength) { // If it's the first line, don't output spaces before the first '*' System.out.println(" ".repeat(spaces) + "*".repeat(currentLength)); } else { // Output spaces and asterisks System.out.println(" ".repeat(spaces) + "*".repeat(currentLength)); }
// Recursively call drawTriangle with the reduced currentLength drawTriangle(baseLength, currentLength - 2); }
public static void drawTriangle(int baseLength) { drawTriangle(baseLength, baseLength); }
public…
arrow_forward
Do not use static variables to implement recursive methods.
USING JAVA
1.
Using Big Oh notation, indicate the time requirement for each of the following tasks in the worst case. Describe which operations are assumed to take constant time to.
After arriving at a party, you shake hands with each person there. n is the number of persons in the party.
Each person in a room shakes hands with everyone else in the room. n is the number of persons in the room.
You climb a flight of stairs. n is the number of stairs
After entering an elevator, you press a button to choose a floor. n is the number of floors
You ride the elevator from the ground floor up to the nth floor.
You read a book twice. n is the number of pages in the book
Using Big Oh notation, indicate the time requirement of each of the following tasks in the worst case.
Display all the integers in an array of integers.
Display all the integers in a chain of linked nodes.
Display the nth integer in an array of integers.
Compute…
arrow_forward
Demonstrate Recursion Assignment Instructions
Overview
The programs we’ve discussed so far are generally structured as methods that call one another in a hierarchical manner. For some problems, it’s useful to have a method call itself—this is known as a recursive method. Such a method can call itself either directly or indirectly through another method. Recursion is an important topic discussed at length in upper-level computer-science courses.
Instructions
Write a recursive method printArray() that displays all the elements in an array of integers, separated by spaces. The array must be 100 elements in size and filled using a for loop and a random number generator. The pseudo-code for this is as follows:
//Instantiate an integer array of size 100
//fill the array
For (int i=0; i<array.length; i++)
Array[i] = random number between 1 and 100 inclusive
printArray(integer array);
For this assignment make sure that your screen shots show your program running and that your runtime…
arrow_forward
solve q6 only please
arrow_forward
solve q5 only please
arrow_forward
Question 5
When writing a recursive method,
O you do not need to know ahead of time exactly how many levels of recursion will occur.
O you must keep count of how many recursion call levels you have traversed.
O you must make sure the method does not take any input parameters.
arrow_forward
Java code
Screenshot and output is must
arrow_forward
write a code in java (using recursion)
arrow_forward
Do not use static variables to implement recursive methods.
USING JAVA
USING:
// P5
public static long computePay(int day) {
}
You have been offered a job that pays as follows:
On the first day, you are paid 1 cent, on the second day, 2 cents, on the third day, 4 cents and so on. In other words, your pay doubles every day. Write a recursive method computePay that for a given day number computes the pay in cents.
Assume that you accumulate all the money that you are paid. Write a recursive method computeSavings that computes the sum that you have accumulated on a given day. Show the output of computePay and computeSavings for day number 39.
arrow_forward
please help me with the following
1) make a sierpenski triangle using java and recursion. Make it similar to the code below. And please comment the code
2) Please make the follow code out put on the console. It a t square fractal
import java.awt.image.*;import java.awt.Color;import java.io.*;import javax.imageio.*;
public class Main{ static final int DIMENSION = 1000; static BufferedImage image = new BufferedImage(DIMENSION, DIMENSION, BufferedImage.TYPE_INT_RGB); static final int WHITE = Color.WHITE.getRGB(); static final int BLACK = Color.BLACK.getRGB();
private static void drawSquare(int x, int y, int side) { if (side <= 0) return; else { int left = x - side/2; int top = y - side/2; int right = x + side/2; int bottom = y + side/2;
for (int i = left; i < right; i++) for (int j = top; j < bottom; j++) { image.setRGB(i, j, BLACK); }…
arrow_forward
Written explaination required
arrow_forward
Java language
Write a recursive method to add all of the odd numbers between two numbers (start and end) and return the result. The method receives these numbers as parameters.
arrow_forward
java program
arrow_forward
PLZ help with the followig IN JAVA
Pick the best answer
Recursive methods may include a recursive call
Recursive methods may not use iteration
Recursive methods must include a recursive call
none of the above
arrow_forward
Pascal's triangle is a useful recursive definition that tells us the coefficients in the expansion of the polynomial (x + a)^n. Each element in the triangle has a coordinate, given by the row it is on and its position in the row (which you could call a column). Every number in Pascals triangle is defined as the sum of the item above it and the item above it and to the left. If there is a position that does not have an entry, we treat it as if we had a 0 there.
*picture of the pascals triangle*
Given the following recursive function signature, write the recursive function that takes a row and a column and finds the value at that position in the triangle. Assume that the triangle starts at row 0 and column 0.
Examples: pascal(2, 1) -> 2, pascal(1, 2) -> 0
public int pascal(int row, int column) {
}
arrow_forward
solve q4 only please
arrow_forward
How to apply this python code in the problem? What are the base cases and recursive cases that should be used?
def createList(n): #Base Case/s #TODO: Add conditions here for your base case/s #if <condition> : #return <value> #Recursive Case/s #TODO: Add conditions here for your recursive case/s #else: #return <operation and recursive call>
#remove the line after this once you've completed all the TODO for this function return []
def removeMultiples(x, arr): #Base Case/s #TODO: Add conditions here for your base case/s #if <condition> : #return <value> #Recursive Case/s #TODO: Add conditions here for your recursive case/s #else: #return <operation and recursive call>
#remove the line after this once you've completed all the TODO for this function return [] def Sieve_of_Eratosthenes(list): #Base Case/s if len(list) < 1 : return list #Recursive Case/s else:…
arrow_forward
Java - Number Pattern
arrow_forward
Please help me with the tasks below using java
arrow_forward
I want solution with step
arrow_forward
Using Java programming write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows:
7 * 4=4+4+4+4+4+4+4
arrow_forward
9. Ackermann's Function
Ackermann's function is a recursive mathematical algorithm that can be used to test how well
a computer performs recursion. Write a method ackermann (m, n), which solves Ackermann's
function. Use the following logic in your method:
If m = 0 then return n + 1
If n = 0 then return ackermann (m
Otherwise, return ackermann(m
1, 1)
1, ackermann (m, n -
1))
arrow_forward
Recursion is the best and the fastest way to solve any problem.True or False
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Related Questions
- JAVA Question 2: For two integers m and n, their GCD (Greatest Common Divisor) can be computed by a recursive method. Write a recursive method gcd(m,n) to find their Greatest Common Divisor. Method body: If m is 0, the method returns n. If n is 0, the method returns m. If neither is 0, the method can recursively calculate the Greatest Common Divisor with two smaller parameters: One is n, the second one is m mod n (or m % n). The recursive method cannot have loops. Note: although there are other approaches to calculate Greatest Common Divisor, please follow the instructions in this question, otherwise you will not get the credit. main method: Prompt and read in two numbers to find the greatest common divisor. Call the gcd method with the two numbers as its argument. Print the result to the monitor. Example program run: Enter m: 12 Enter n: 28 GCD(12,28) = 4 And here is what I have so far, package CSCI1302;import java.util.*;public class RecursionDemo { public static void…arrow_forwardDetermine whether a string is a palindromeA palindrome is a string of characters that reads the same from right to left as it does from left to right, regardless of punctuation and spaces.The specifications for this assignment are: •Write and test a non-recursive solution in Java that determines whether a string is a palindrome •Your program should consist of at least two methods: (1) the main method (2) the method which performs the task of determining whether the specified string is a palindrome. You should name this method isPalindrome. You should name the class that contains your “main” method and the isPalindrome method FindPalindrome. •You must use a Stack and a Queue in your solution: Write your own Stack and Queue based on the Vector in the Java API and use those in your solution. You should name those classes StackVector and QueueVector respectively. You already have access to the relevant exception classes and interfaces for the above ADTs. •All of your belong to a Java…arrow_forwardJava - When the compiler compiles your program, how is a recursive call treated differently than a non-recursive method call? What property of fractals lends itself to recursive thinking?arrow_forward
- write a code in java (using recursion)arrow_forwardTask #1 Tracing Recursive Methods 1. Copy the file Recursion.java (see Code Listing 16.1) from the Student Files or as directed by your instructor. 2. Run the program to confirm that the generated answer is correct. Modify the factorial method in the following ways: a. Add these lines above the first if statement: int temp; System.out.println ("Method call "calculating "Factorial of: " + n); Copyright © 2019 Pearson Education, Inc., Hoboken NJ b. Remove this line in the recursive section at the end of the method: return (factorial(n - 1) * n); c. Add these lines in the recursive section: temp - factorial (n - 1) ; System.out.println ("Factorial of: " + (n - 1) + " is " + temp); return (temp * n); 3. Rerun the program and note how the recursive calls are built up on the run-time stack and then the values are calculated in reverse order as the run-time stack "unwinds".arrow_forwardimport java.util.Scanner; public class LabProgram { // Recursive method to draw the triangle public static void drawTriangle(int baseLength, int currentLength) { if (currentLength <= 0) { return; // Base case: stop when currentLength is 0 or negative } // Calculate the number of spaces needed for formatting int spaces = (baseLength - currentLength) / 2; if (currentLength == baseLength) { // If it's the first line, don't output spaces before the first '*' System.out.println("*".repeat(currentLength) + " "); } else { // Output spaces and asterisks System.out.println(" ".repeat(spaces) + "*".repeat(currentLength) + " "); } // Recursively call drawTriangle with the reduced currentLength drawTriangle(baseLength, currentLength - 2); } public static void drawTriangle(int baseLength) { drawTriangle(baseLength, baseLength); } public static…arrow_forward
- import java.util.Scanner; public class LabProgram { // Recursive method to draw the triangle public static void drawTriangle(int baseLength, int currentLength) { if (currentLength <= 0) { return; // Base case: stop when currentLength is 0 or negative } // Calculate the number of spaces needed for formatting int spaces = (baseLength - currentLength) / 2; if (currentLength == baseLength) { // If it's the first line, don't output spaces before the first '*' System.out.println(" ".repeat(spaces) + "*".repeat(currentLength)); } else { // Output spaces and asterisks System.out.println(" ".repeat(spaces) + "*".repeat(currentLength)); } // Recursively call drawTriangle with the reduced currentLength drawTriangle(baseLength, currentLength - 2); } public static void drawTriangle(int baseLength) { drawTriangle(baseLength, baseLength); } public…arrow_forwardDo not use static variables to implement recursive methods. USING JAVA 1. Using Big Oh notation, indicate the time requirement for each of the following tasks in the worst case. Describe which operations are assumed to take constant time to. After arriving at a party, you shake hands with each person there. n is the number of persons in the party. Each person in a room shakes hands with everyone else in the room. n is the number of persons in the room. You climb a flight of stairs. n is the number of stairs After entering an elevator, you press a button to choose a floor. n is the number of floors You ride the elevator from the ground floor up to the nth floor. You read a book twice. n is the number of pages in the book Using Big Oh notation, indicate the time requirement of each of the following tasks in the worst case. Display all the integers in an array of integers. Display all the integers in a chain of linked nodes. Display the nth integer in an array of integers. Compute…arrow_forwardDemonstrate Recursion Assignment Instructions Overview The programs we’ve discussed so far are generally structured as methods that call one another in a hierarchical manner. For some problems, it’s useful to have a method call itself—this is known as a recursive method. Such a method can call itself either directly or indirectly through another method. Recursion is an important topic discussed at length in upper-level computer-science courses. Instructions Write a recursive method printArray() that displays all the elements in an array of integers, separated by spaces. The array must be 100 elements in size and filled using a for loop and a random number generator. The pseudo-code for this is as follows: //Instantiate an integer array of size 100 //fill the array For (int i=0; i<array.length; i++) Array[i] = random number between 1 and 100 inclusive printArray(integer array); For this assignment make sure that your screen shots show your program running and that your runtime…arrow_forward
- solve q6 only pleasearrow_forwardsolve q5 only pleasearrow_forwardQuestion 5 When writing a recursive method, O you do not need to know ahead of time exactly how many levels of recursion will occur. O you must keep count of how many recursion call levels you have traversed. O you must make sure the method does not take any input parameters.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education