Question

Implement the static method declared as follows: /** * Divides {@code n} by 2. * *...

Implement the static method declared as follows:

/**

* Divides {@code n} by 2.

*

* @param n

*            {@code NaturalNumber} to be divided

* @updates n

* @ensures 2 * n <= #n < 2 * (n + 1)

*/

private static void divideBy2(NaturalNumber n) {...}

()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()

Implement the static method declared as follows:

/**

* Checks whether a {@code String} is a palindrome.

*

* @param s

*            {@code String} to be checked

* @return true if {@code s} is a palindrome, false otherwise

* @ensures isPalindrome = (s = rev(s))

*/

private static boolean isPalindrome(String s) {...}

Recursion needs to be used for both of these, and the NaturalNumber class needs to be used for the top method.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
/**
 * Checks whether a {@code String} is a palindrome.
 *
 * @param s
 *            {@code String} to be checked
 * @return true if {@code s} is a palindrome, false otherwise
 * @ensures isPalindrome = (s = rev(s))
 */
private static boolean isPalindrome(String s) {
    if(s.length() <= 1) {
        return true;
    } else {
        if(s.charAt(0) != s.charAt(s.length()-1)) {
            return false;
        } else {
            return isPalindrome(s.substring(1, s.length()-1));
        }
    }
}

Need Natural N umber class to answer first method

Add a comment
Know the answer?
Add Answer to:
Implement the static method declared as follows: /** * Divides {@code n} by 2. * *...
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
  • Implement the instance method flip declared as follows (this is a Queue): /** * Reverses ("flips")...

    Implement the instance method flip declared as follows (this is a Queue): /** * Reverses ("flips") {@code this}. * * @updates this * @ensures this = rev(#this) */ public void flip(); Rewrite flip, including the contract, so that it is a static method. http://web.cse.ohio-state.edu/software/common/doc/

  • IN JAVA. Implement a method, public static boolean isPalindrome(String) to determine whether the specified String is...

    IN JAVA. Implement a method, public static boolean isPalindrome(String) to determine whether the specified String is a palindrome or not. A palindrome is a phrase that reads the same forward​and backward; not including punctuation, spaces, or letter case. Some example palindromes include: Just the method please..

  • Your job is to implement the root static method for NaturalNumber using the interval halving root...

    Your job is to implement the root static method for NaturalNumber using the interval halving root algorithm. Use the following skeleton (Java language) and complete the root method to achieve the proper output: import components.naturalnumber.NaturalNumber; import components.naturalnumber.NaturalNumber2; import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; /** * Program with implementation of {@code NaturalNumber} secondary operation * {@code root} implemented as static method. * * @author Put your name here * */ public final class NaturalNumberRoot { /** * Private constructor so this utility class...

  • Java Software Originals, Inc., has been hired by Eaton Wright, the "pizza king", to help automate...

    Java Software Originals, Inc., has been hired by Eaton Wright, the "pizza king", to help automate a new chain of pizza delivery stores. SOI's system engineering staff have asked you to implement a prototype for the telephone operator's console. To start the project they have assigned you to implement the following two static methods (the first of which will be used to read files containing menus of pizza sizes and their prices, and pizza toppings and their prices; and the...

  • JAVA QUESTION - BalancedParentheses Implement the class BalancedParentheses with a method isBalanced that receives a String...

    JAVA QUESTION - BalancedParentheses Implement the class BalancedParentheses with a method isBalanced that receives a String and checks whether the brackets in it are balanced. import java.util.Scanner; public class BalancedParentheses { public static void main(String[] args) { checkParentheses("((a + b) * t/2 * (1 - t)"); checkParentheses("(a + b) * t)/(2 * (1 - t)"); checkParentheses("a + ((a + b) * t)/(2 * (1 - t))"); System.out.println("Enter an expression: "); Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); checkParentheses(s);...

  • instructions These questions should test if you have understood the contents of Chapter 18 "Recursion". 1....

    instructions These questions should test if you have understood the contents of Chapter 18 "Recursion". 1. What will this method return if you call it this: xMethod (4) static int xMethod (int n) { if (n == 1) return 1; else return n + xMethod (n - 1); } 1. 10 2. 11 3. 12 4. 9 2. Analyze the following code: public class Test { public static void main (String [] args) { int [] x = {1, 2,...

  • The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode...

    The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode root) { } public static void main(String[] args) { TreeNode a = new TreeNode(1); TreeNode b = new TreeNode(2); TreeNode c = new TreeNode(3); a.left = b; a.right = c; System.out.println(isValidBST(a)); TreeNode d = new TreeNode(2); TreeNode e = new TreeNode(1); TreeNode f = new TreeNode(3); d.left = e; d.right = f; System.out.println(isValidBST(d)); } } TreeNode.java class TreeNode { int val; TreeNode left; TreeNode...

  • Part 1. Primitive Types, Sorting, Recursion for Homework.java a) Implement the static method initializeArray that receives...

    Part 1. Primitive Types, Sorting, Recursion for Homework.java a) Implement the static method initializeArray that receives as a parameter an array of integers. Use a for loop and an if statement to put 0s in the odd positions of the array and 5s in the even positions. b) Implement the static method printArray that receives as a parameter an array of integers. Use a for statements to print all the elements in the array. c) Implement the static method insertionSort...

  • Write python code for the following Implement a method Boolean containPattern(String s) to find the following...

    Write python code for the following Implement a method Boolean containPattern(String s) to find the following special pattern in String s: Sequence1&Sequence2 Where Sequence 1 and Sequence 2 do not contain symbol ‘&’ Sequence 2 should be the reversed version of Sequence 1 e.g. input: a+b&b+a output: true input: 1+3&3-1 output: false

  • Modification to be completed by group member 4: In the processLineOfData method, write the code to...

    Modification to be completed by group member 4: In the processLineOfData method, write the code to handle case "M" of the switch statement such that: A Manager object is created using the firstName, lastName, salary, and bonus local variables. Notice that salary and bonus need to be converted from String to double. You may use parseDouble method of the Double class as follows:               Double.parseDouble(salary) Call the parsePaychecks method in this class passing the Manager object created in the previous step...

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