Question

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

Get the file “HW4Part4a.java” from the repository. Modify the program class to match the new file name. Complete it by writing a recursive static int function named “pow2” with one parameter, an integer n. The function is defined as follows: if n ≤ 0 then pow2(n) = 1. Otherwise, pow2(n) = 2 · pow2(n − 1).

public class HW4Part4a {
public static void main(String[] args) {
for (int i = 0; i <= 20; i++) {
System.out.println("pow2("+i+") = "+pow2(i));
}
}
public static int pow2(int n) {
/* replace this comment with your code */
}
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER

Java code pasted below. The newly added code is shown in red colour.

public class HW4Part4a {
public static void main(String[] args) {
for (int i=0; i<=20; i++) {
System.out.println("pow2("+i+") = "+pow2(i));
}
}
public static int pow2(int n) {
if(n<=0)
return 1;
else
return (2*pow2(n-1));

}
}

Output Screen

D:\JavaSolutions>java HW4Part4a pow2(0) = 1 pow2(1) = 2 pow2(2) = 4 pow2(3) = 8 pow2(4) = 16 pow2(5) = 32 pow2(6) = 64 pow2 (

Add a comment
Know the answer?
Add Answer to:
Get the file “HW4Part4a.java” from the repository. Modify the program class to match the new file...
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
  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • 17. For the following program, draw a stack to find the output. public class RecApp public...

    17. For the following program, draw a stack to find the output. public class RecApp public static void main(String[] args) System.out.println(Sum(7)); public static int Sum(int n) if (n < 1) return 5; else return n + Sum (n-2); Output: (13) Stack: (4%) Sum (_ ) Sum(_) Sum(_) Function Value Parameter Function Value Parameter Function Value Parameter Function Value Parameter Function Value Parameter Sum( ) 7 + Sum (5) Sum (7) Write the recursive equation (Tn)) for the run time of...

  • Question 8 Identify the COMPILER ERRORS and EXCEPTIONS in this program. This program is saved in...

    Question 8 Identify the COMPILER ERRORS and EXCEPTIONS in this program. This program is saved in a file named MyProgram.java. public class MYProgram { public static void main( String [] args ) { int [] arrayOfIntegers = { 15, 20, 30 50, 60 }; for( int i = 0; i <= arrayOfIntegers.length(); i++ ) { System.out.println( "Hello" + i * 2 ) int i = 100 * arrayOfIntegers[ i ]; doSomething( i ); } } Public static void DoSomething( )...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly...

    Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly below): //Create public class count public class Count {     public static void main(String args[])     {         int n = getInt("Please enter an integer value greater than or equal to 0");                System.out.println("Should count down to 1");         countDown(n);                System.out.println();         System.out.println("Should count up from 1");         countUp(n);     }            private static void countUp(int n)     {...

  • given the following two classes Within a file named Rational.java, define a public class named Rational...

    given the following two classes Within a file named Rational.java, define a public class named Rational such that … the Rational class has two private instance variables, numerator and denominator, both of type int the Rational class defines two public accessor methods, numerator() and denominator() the Rational class defines a constructor that has one String parameter a throws clause indicates that this constructor could potentially throw a MalformedRationalException this constructor throws a MalformedRationalException in the following circumstances … When the...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

  • Help with a question in Java: What is the output from the following program? public class...

    Help with a question in Java: What is the output from the following program? public class Methods2 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { for (int j = 0; j <= i; j++){ System.out.print(fun(i, j) + "\t"); } System.out.println(); } } static long fun(int n, int k) { long p = 1; while (k > 0){ p *= n; } return p; } }

  • Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 {...

    Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 { public static void whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) { B[i]=A[i]*2; } A=B; } public static void main(String args[]) { int A[] = {10,20,30}; whatHappens(A); System.out.println(A[0]); } } will print 10. explain how it's works. Thanks public class WhatsPrinted3 { public static int[] whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) {...

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