Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
JAVA
In order to compute a power of two, you can take the next-lower power and double it. For example, if you want to compute 2^11 and you know that 2^10=1024, then 2^11=2×1024=2048. Write a recursive method
public static int pow2(int n)where n is the exponent, that is based on this observation. If the exponent is negative, return -1.
import java.util.Scanner;
public class PowerTwo
{
public static int pow2(int n)
{
/* code goes here */
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
while (in.hasNextInt())
{
int exponent = in.nextInt();
System.out.println(pow2(exponent));
}
}
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 5 steps with 3 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- A palindrome is a string that reads the same forward and backward. For example,“deed” and “level” are palindromes. Write an algorithm in pseudocode that testswhether a string is a palindrome. Implement your algorithm as a static method inJava. The method should call itself recursively and determine if the string is apalindrome.Write a main method that reads a string from the user as input and pass it to thestatic method. You need to validate the string and make sure it contains only letters(no digits or special characters are allowed). The main method should displaywhether the string is a palindrome or not. a) Read the string from the user and validate it.b) Create a method that calls itself recursively and returns whether thestring is a palindrome or not.c) Include a test table with at least the following test cases:1. Invalid input. For example, string contains a number or specialcharacter.2. A valid string that is a palindrome.3. A valid string that is not a palindrome.arrow_forwardI need help creating a code below - Write a recursive method called digitCount() that takes a positive integer as a parameter and returns the number of digits in the integer. Hint: The number of digits increases by 1 whenever the input number is divided by 10. Ex: If the input is: 345 the method digitCount() returns and the program outputs: 3arrow_forwardWrite a complete java program that will store the weekly sales data for a small shop that sells candles into two dimensional array of type double, the shop has five candle scents as follow: week 1 sales week 2 sales week 3 sales week 4 sales Candle Scent 1 41.41 23.33 4.09 1.20 Candle Scent 2 33.86 22.30 27.85 17.35 Candle Scent 3 8.58 7.97 0.61 28.39 Candle Scent 4 10.23 32.62 22.21 21.91 Candle Scent 5 43.16 12.57 20.33 2.40 Your program should calculate and prints the followings: Total sa for week 1 Total sales for week 2 Total sales for week 3 Total sales for week 4 Maximum sales with the week number and candle scent number was found. Minimum sales with the week number and candle scent number was found. Sample run: Total Sales of Week 1 = 137.25 Total Sales of Week 2 = 98.80 Total Sales of Week 3 = 75.09 Total Sales of Week 4 = 71.26 Maximum Sales = 43.16 found week 1 for candle scent 5 Minimum Sales = 0.61 found in week 3 for candle scent 3arrow_forward
- Write a Java method (do not write a class, only the method) with the following signature: Return type: void Parameters: 2D integer array Name: printDiagonal Implement the method code that will print out the values of the 2D array along the Diagonal from left to right. For example, if the array had the following values: 56 78 98 21 33 55 98 44 11 The method should print to the console: 56,33,11 The above 2D array example is only given for you to understand the problem Do not hard code the values in your solution or assume the array is the same as the given data. Your method should work for all 2D integer arrays.arrow_forwardGiven a string, write a method that returns the number of occurrences of substrings "baba" or "mama" in the input string recursively. They may overlap. Do not use any loops within your code. Do not use any regular expressions and methods such as matches, split, replaceAll. "Maa mana, Test case 1: countBabaMama ("aba babaa amama ma") 2 Test case 2: countBabaMama ("bababamamama") 4arrow_forwardWrite a Method Given a positive integer num, sum all numbers from 0 to num that are divisible by 4 using recursion. Example: If num = 19, then sumDivisible would return 40 (4+8+12+16), and if num = 8, then sumDivisible would return 12 (4+8). public int sumDivisible(int num) { }arrow_forward
arrow_back_ios
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