Question

In: Computer Science

Description: For this assignment, you are required to write 3 recursive methods described below. Create a...

Description:

For this assignment, you are required to write 3 recursive methods described below. Create a class named HW06 and implement the following 3 methods . The implementation MUST use recursive calls.

- static int multiplyPositiveIntegers(int a, int b)

//multiplies two positive integers using recursion (no use of * allowed). Returns the result of multiplication. (30 points)

- static void recursiveBubbleSort(int[] arr)

//Sorts an array using bubble sort in a recursive manner. Hint: do one pass(which puts the biggest element at the end) then recur. (35 points)

- static String reverseString(String input)

//reverses a string in a recursive manner. returns the reversed string. You can use charAt() and substring() methods. (35 points)

Do not rename methods or change method signatures. You may add other methods or overload methods if you want. But, I should be able to call methods as described above.

Here is the skeleton:

public class HW06 {

    static void recursiveBubbleSort(int[] arr) {

     

    }

    static String reverseString(String input) {

       

    }

    static int multiplyPositiveIntegers(int a, int b)    {

       

    }

}

Solutions

Expert Solution

NOTE:If you are stuck somewhere feel free to ask in the comments...but do not give negative rating to the question as it affects my answering rights........I will try to help you out.....

import java.io.*;
import java.util.*;
public class HW06{
     static void helper(int[] arr,int n) {
         //the recursive function corrsponds to the outer for loop
         if (n == 1)
            return;//base case, if only one element means it is already sorted
      
       //the for loop below corresponds to one single pass of the bubble sort
       //After the first pass the largest element is placed in the end
        for (int i=0; i<n-1; i++)
            if (arr[i] > arr[i+1])
            {
                // swap the elements arr[i], arr[i+1]
                int temp = arr[i];
                arr[i] = arr[i+1];
                arr[i+1] = temp;
            }
      
        // Largest element is fixed, now recur for the next 
        helper(arr, n-1);
    }
    static void recursiveBubbleSort(int[] arr) {
        int n=arr.length;
       helper(arr,n);//since we need an extra variable to keep trak upto which element we have
       //sorted hence a helper function is needed....which is a recursive function
    }

    static String reverseString(String input) {
        if (input.isEmpty())
            return input;//base case, if string is empty it means already reversed
        //here we recursively call for the substring starting from the next index 
        //after adding the character at the current index to the output string
        //since this a recursive function the string is added in the end after finishing the recursive call
        return reverseString(input.substring(1)) + input.charAt(0);
    }


//the idea of multiplication is simple eg it says 2*3
//the it adding 2 three times.....meaning the smaller no is added larger number of times
    static int multiplyPositiveIntegers(int a, int b)    {
        // if a is less than  b then swap
        if (a < b) 
            return multiplyPositiveIntegers(b, a); 
      
        // iteratively calculate  a times sum of b
        else if (b != 0) 
            return (a + multiplyPositiveIntegers(a, b - 1)); 
      
        // if any of the two numbers is  
        // zero return zero 
        else
            return 0;
    }
    public static void main(String args[]) {
      int arr[] = {10, 2, 5, 12, 21, 11, 9};
        System.out.println("Initial array : ");
        System.out.println(Arrays.toString(arr));
        recursiveBubbleSort(arr);
        System.out.println("Sorted array : ");
        System.out.println(Arrays.toString(arr));
        String str="Welcome to java";
        System.out.println("Original String: "+str);
        System.out.println("Reversed String: "+reverseString(str));
        System.out.println("Product 23*45 equals: "+multiplyPositiveIntegers(23, 45));
    }
}

Output:-->


Related Solutions

Assignment Description: You will be required to conduct analysis of a technology/system application and write a...
Assignment Description: You will be required to conduct analysis of a technology/system application and write a formal paper. The analysis need to be of an application technology used in either (a) a clinical setting for managing health data, patient care, etc or (b) educational setting for improvement of teaching and learning of students, patients, etc. The Technology Application Analysis will be an independent activity for each student to be completed using resources available in your agency, IT department, on the...
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then,...
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents: Please use the following comments in your python script: # ************************************************* # COP3375 # Student's full name ( <<< your name goes here) # Week 8: Assignment 1 # ************************************************* #...
for this assignment, you are required to write a Retail Innovation Strategy Essay write a 3-5...
for this assignment, you are required to write a Retail Innovation Strategy Essay write a 3-5 page paper dicussing how innovation should be incorporated into a retail strategy. Specifically identify two retail innovations to ensure you have a global reach
Optional Assignment 1: Method Man Assignment Description Write some methods and call them from the main....
Optional Assignment 1: Method Man Assignment Description Write some methods and call them from the main. Tasks Write a method called nickelbackMeme with a void return type, a string parameter, and an int parameter The method outputs the text “LOOK AT THIS…” plus the string parameter at the end. The method will do this as many times as the int parameter. If 0 or a negative number is entered for the int parameter, output “WHAT THE HELL IS ON JOEY’S...
The Problem Below are a series of problems you need to solve using recursive methods BY...
The Problem Below are a series of problems you need to solve using recursive methods BY using java . You will write a program that will read commands from an input file, with each command referring to one of the recursive problems to be executed. Each command will be followed (on the same line of input) by the respective parameters required for that problem. (15 points for main method) DescArrayCheck (15 points) Write a recursive method that checks whether an...
Assignment Description and Instructions: As a group of prospective entrepreneurs, you are required to prepare a...
Assignment Description and Instructions: As a group of prospective entrepreneurs, you are required to prepare a business plan for a new skin care business (not an existing business/company) to acquire financing. ALL group members are expected to collaborate and contribute to the business plan. Each member will be required to complete peer evaluation of the other team members using the rubric provided. The plan is expected to include: Title page. This should include the company’s name, logo, address and contact...
For this assignment you are required to create two loan amortization schedules. However, after you create...
For this assignment you are required to create two loan amortization schedules. However, after you create the first one, you can simply just copy and paste to add rows for the second schedule. Your directions are as follows: Create a loan amortization schedule using Excel for a $36,000 car loan that will be repaid over 60 months at an annual interest rate of 4.5%. What is your monthly payment? What is the total amount you have paid over the life...
Write a program calls each of the three methods described below and prints out the result...
Write a program calls each of the three methods described below and prints out the result of the two methods which return value reverse- a void method that reverses the elements of the array. Print the array before you call the method and after call the method. getMin- an int method that returns the smallest value in the array Sample output array: 22,34,21,35,12,4,2,3,99,81 array in reverse: 81,99,3,2,4,12,35,21,34,22 the smallest number is 2
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, and write them in reverse order to an output file. 1. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. For example, assume your package name is FuAssign5 and your main class name is FuAssignment5, and your executable files are in “C:\Users\2734848\eclipse-workspace\CIS 265 Assignments\bin”. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT