Question

In: Computer Science

Get the file “HW4Part4b.java” from the repository. Modify the program class to match the new file...

Get the file “HW4Part4b.java” from the repository. Modify the program class to match the new file name. Complete it by writing a recursive static int function named recur that is defined as follows: if i ≤ 0 or j ≤ 0, recur(i, j) = 0. if i = j, recur(i, j) = i. if i > j, recur(i, j) = j. In all other cases, recur(i, j) = 2 · recur(i − 1, j) + recur(j − 1, i) Add necessary comments to the program and upload it to your repository.

public class HW4Part4b {
public static void main(String[] args) {
for (int i = 0; i <= 5; i++) {
for (int j = 0; j <= 5; j++) {
System.out.println("recur("+i+","+j+") = "+recur(i,j));
}
}
}
public static int recur(int i, int j) {
/* replace this comment with your code */
}
}

Solutions

Expert Solution

HW4Part4b

Implemented the recur() method in Java.

The recursive static int function named recur that is defined as follows:

if i ≤ 0 or j ≤ 0, recur(i, j) = 0. if i = j, recur(i, j) = i. if i > j, recur(i, j) = j. In all other cases, recur(i, j) = 2 · recur(i − 1, j) + recur(j − 1, i)

Java Program Code:

public class HW4Part4b
{
//Main Driver function
public static void main(String[] args) {
    for (int i = 0; i <= 5; i++) {
            for (int j = 0; j <= 5; j++) {
                    System.out.println("recur("+i+","+j+") = "+recur(i,j));
                    }
            }
    }

//recur() method
//params: int i, int j
//returns int
public static int recur(int i, int j) {
    //Base step of recur function (termination condition)
    if (i <= 0 || j <= 0) { 
        return 0;
    } 
    else if (i == j) {
        return i;
    }
    else if (i > j) {
        return j;
    }
    //The recurrence relation 
    else {
        return 2 * recur(i-1, j) + recur(j-1, i);
       //recursive call
    }
    
    }
}

Program Output Screenshot:

Output in text format:

recur(0,0) = 0
recur(0,1) = 0
recur(0,2) = 0
recur(0,3) = 0
recur(0,4) = 0
recur(0,5) = 0
recur(1,0) = 0
recur(1,1) = 1
recur(1,2) = 1
recur(1,3) = 1
recur(1,4) = 1
recur(1,5) = 1
recur(2,0) = 0
recur(2,1) = 1
recur(2,2) = 2
recur(2,3) = 4
recur(2,4) = 4
recur(2,5) = 4
recur(3,0) = 0
recur(3,1) = 1
recur(3,2) = 2
recur(3,3) = 3
recur(3,4) = 11
recur(3,5) = 11
recur(4,0) = 0
recur(4,1) = 1
recur(4,2) = 2
recur(4,3) = 3
recur(4,4) = 4
recur(4,5) = 26
recur(5,0) = 0
recur(5,1) = 1
recur(5,2) = 2
recur(5,3) = 3
recur(5,4) = 4
recur(5,5) = 5


Related Solutions

Modify this program so that it takes in input from a TEXT FILE and outputs the...
Modify this program so that it takes in input from a TEXT FILE and outputs the results in a seperate OUTPUT FILE. (C programming)! Program works just need to modify it to take in input from a text file and output the results in an output file. ________________________________________________________________________________________________ #include <stdio.h> #include <string.h> // Maximum string size #define MAX_SIZE 1000 int countOccurrences(char * str, char * toSearch); int main() { char str[MAX_SIZE]; char toSearch[MAX_SIZE]; char ch; int count,len,a[26]={0},p[MAX_SIZE]={0},temp; int i,j; //Take...
Using steps.py in this repository for a starter file, write a Python program to calculate average...
Using steps.py in this repository for a starter file, write a Python program to calculate average number of steps per month for a year. The input file (which you will read from the command line, see the sample program on how to read command line arguments in this repository) contains the number of steps (integer) a person took each day for 1 year, starting January 1. Each line of input contains a single number. Assume this is NOT a leap...
Modify your program from Learning Journal Unit 7 to read dictionary items from a file and...
Modify your program from Learning Journal Unit 7 to read dictionary items from a file and write the inverted dictionary to a file. You will need to decide on the following: How to format each dictionary item as a text string in the input file. How to covert each input string into a dictionary item. How to format each item of your inverted dictionary as a text string in the output file. Create an input file with your original three-or-more...
4. Modify the program geometryDemo to use the class basicGeometry and the threeSides code. Note from...
4. Modify the program geometryDemo to use the class basicGeometry and the threeSides code. Note from the comments in the main routine that it wants an input argument of 3 to solve for a triangle and 4 to solve for a square or rectangle. MODIFY the code to add another method in the 'threeSides' and 'fourSides' classes to return the perimeter. ASSUME the following for TRIANGLES. ** for AREA (1/2 * base * height) dimension1 is base, dimension2 is height...
Modify the program 5-13 from page 279 such that will also compute the class average. This...
Modify the program 5-13 from page 279 such that will also compute the class average. This class average is in addition to each individual student score average. To accomplish this additional requirement, you should do the following: 1. Add two more variables of type double: one for accumulating student averages, and one to hold the class average. Don't forget, accumulator variable should be initialized to 0.0. 2. Immediately after computing individual student average, add a statement that will accumulate the...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be sure to include start() and stop() methods to start and stop the clock, respectively.Then write a program that lets the user control the clock with the start and stop buttons.
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
5. Modify the program for Line Numbers from L09: In-Class Assignment. Mainly, you are changing the...
5. Modify the program for Line Numbers from L09: In-Class Assignment. Mainly, you are changing the problem from using arrays to ArrayLists. There are some other modifications as well, so read the instructions carefully. The program should do the following: –Ask the user for how many lines of text they wish to enter –Declare and initialize an **ArrayList** of Strings to hold the user’s input –Use a do-while loop to prompt for and read the Strings (lines of text) from...
You are to modify your payroll program from the last assignment to take the new copy...
You are to modify your payroll program from the last assignment to take the new copy of the payroll file called DeweyCheatemAndHow.txt which has the fields separated by a comma to use as input to your program and produce a file that lists all the salaried employee and their gross pay, then each hourly employee and their gross pay and finally each piece rate employee and their gross pay . You are to process all the entries provided in the...
Question: Java Programming. ** Modify Current Program. Current Program class Account{       //instance variables   ...
Question: Java Programming. ** Modify Current Program. Current Program class Account{       //instance variables    private long accountNumber;    private String firstName;    private String lastName;    private double balance;       //constructor    public Account(long accountNumber, String firstName, String lastName, double balance) {        this.accountNumber = accountNumber;        this.firstName = firstName;        this.lastName = lastName;        this.balance = balance;    }    //all getters and setters    public long getAccountNumber() {        return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT