Big Java Late Objects
2nd Edition
ISBN: 9781119330455
Author: Horstmann
Publisher: WILEY
expand_more
expand_more
format_list_bulleted
Concept explainers
Expert Solution & Answer
Chapter 5, Problem 5PE
Explanation of Solution
Method for returning string repeating n times
public static String repeat (String str, int n)
{
// declare string
String newword = "";
//execute for loop
for int i=0; i<n; i++)
{
//define variable newword
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a method that takes a String and a character as inputs and returns the number of instances of the input character within the input String.
For example, if "banana" and 'a' are input into the method, it should return 3.
If "apple" and 'p' are input, it should return 2
Write a java method that accepts an integer as parameter and returns a string as an expression of the sum of powers of 2 in increasing order. (Non-duplicate)
Sample Input: 20 Sample Output: 20 = 4+16
Sample Input: 2021 Sample Output: 2021 = 1+4+32+64+128+256+512+1024
Write a method public static String scramble(String word) that constructs a scrambled version of a given word, randomly flipping two characters other than the first and last one.
Then write a program that reads words and prints the scrambled words.
Chapter 5 Solutions
Big Java Late Objects
Ch. 5.1 - Consider the method call Math.pow(3, 2). What are...Ch. 5.1 - What is the return value of the method call...Ch. 5.1 - The Math.ceil method in the Java standard library...Ch. 5.1 - It is possible to determine the answer to Self...Ch. 5.2 - What is the value of cubeVolume(3)?Ch. 5.2 - Prob. 6SCCh. 5.2 - Provide an alternate implementation of the body of...Ch. 5.2 - Declare a method squareArea that computes the area...Ch. 5.2 - Consider this method: public static int...Ch. 5.3 - What does this program print? Use a diagram like...
Ch. 5.3 - Prob. 11SCCh. 5.3 - What does this program print? Use a diagram like...Ch. 5.4 - Prob. 13SCCh. 5.4 - What does this method do? public static boolean...Ch. 5.4 - Implement the mystery method of Self Check 14 with...Ch. 5.5 - How do you generate the following printout, using...Ch. 5.5 - Prob. 17SCCh. 5.5 - Prob. 18SCCh. 5.5 - Prob. 19SCCh. 5.5 - The boxString method contains the code for...Ch. 5.6 - Consider the following statements: int...Ch. 5.6 - Consider this method that prints a page number on...Ch. 5.6 - Consider the following method that computes...Ch. 5.6 - The comment explains what the following loop does....Ch. 5.6 - In Self Check 24, you were asked to implement a...Ch. 5.7 - Explain how you can improve the intName method so...Ch. 5.7 - Prob. 27SCCh. 5.7 - What happens when you call intName(0)? How can you...Ch. 5.7 - Trace the method call intName(72), as described in...Ch. 5.7 - Prob. 30SCCh. 5.8 - Which lines are in the scope of the variable i...Ch. 5.8 - Which lines are in the scope of the parameter...Ch. 5.8 - The program declares two local variables with the...Ch. 5.8 - There is a scope error in the mystery method. How...Ch. 5.8 - Prob. 35SCCh. 5.9 - Consider this slight modification of the...Ch. 5.9 - Consider this recursive method: public static int...Ch. 5.9 - Consider this recursive method: public static int...Ch. 5.9 - Prob. 39SCCh. 5.9 - The intName method in Section 5.7 accepted...Ch. 5 - In which sequence are the lines of the Cubes.java...Ch. 5 - Write method headers for methods with the...Ch. 5 - Give examples of the following methods from the...Ch. 5 - Prob. 4RECh. 5 - Consider these methods: public static double...Ch. 5 - Prob. 6RECh. 5 - Design a method that prints a floating-point...Ch. 5 - Write pseudocode for a method that translates a...Ch. 5 - Describe the scope error in the following program...Ch. 5 - For each of the variables in the following...Ch. 5 - Prob. 11RECh. 5 - Perform a walkthrough of the intName method with...Ch. 5 - Consider the following method: public static int...Ch. 5 - Consider the following method that is intended to...Ch. 5 - Suppose an ancient civilization had constructed...Ch. 5 - Give pseudocode for a recursive method for...Ch. 5 - Give pseudocode for a recursive method that sorts...Ch. 5 - Write the following methods and provide a program...Ch. 5 - Write the following methods and provide a program...Ch. 5 - Prob. 4PECh. 5 - Prob. 5PECh. 5 - Prob. 6PECh. 5 - Prob. 7PECh. 5 - Prob. 8PECh. 5 - Write methods public static double...Ch. 5 - Write a recursive method public static String...Ch. 5 - Write a recursive method public static boolean...Ch. 5 - Use recursion to implement a method public static...Ch. 5 - Use recursion to determine the number of digits in...Ch. 5 - Write a method that computes the balance of a bank...Ch. 5 - Write a method that tests whether a file name...Ch. 5 - It is a well-known phenomenon that most people are...Ch. 5 - Prob. 3PPCh. 5 - Use recursion to compute an, where n is a positive...Ch. 5 - Leap years. Write a method public static boolean...Ch. 5 - In Exercise P3.13 you were asked to write a...Ch. 5 - Prob. 10PPCh. 5 - Write a program that reads two strings containing...Ch. 5 - Prob. 12PPCh. 5 - Write a program that reads words and arranges them...Ch. 5 - Prob. 14PPCh. 5 - Write a program that reads two fractions, adds...Ch. 5 - Write a program that prints the decimal expansion...Ch. 5 - Write a program that reads a decimal expansion...Ch. 5 - Write two methods public static void...Ch. 5 - Write a program that reads in the width and height...Ch. 5 - Repeat Exercise P5.19 with hexagonal circle...Ch. 5 - Postal bar codes. For faster sorting of letters,...Ch. 5 - Write a program that reads in a bar code (with :...Ch. 5 - Write a program that converts a Roman number such...Ch. 5 - A non-governmental organization needs a program to...Ch. 5 - Having a secure password is a very important...Ch. 5 - Prob. 30PPCh. 5 - Prob. 31PPCh. 5 - Electric wire, like that in the photo, is a...Ch. 5 - The drag force on a car is given by FD=12v2ACD...
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
- Write a method with a while loop to prints 1 through n in square brackets. For example, if n = 6 print [1] [2] [3] [4] [5] [6]arrow_forwardWrite a method in have that checks if I put is greater than or equal to zero and is actually an integer and not a Stringarrow_forwardWrite a method that takes a String and a character as inputs and returns the number of instances of the input character within the input String. For example, if "banana" and 'a' are input into the method, it should return 3. If "apple" and 'p' are input, it should return 2. (in java)arrow_forward
- Write a Java method toDecimalString() that receives a string with hexadecimal symbols and returns another string where every hexadecimal symbol is converted individually to its decimal representation. If the received string contains a wrong hexadecimal value, it should return an error message.To test your method, write a main() method that enables the user to input a string in hexadecimal format, removes eventual extra spaces from it, and makes all characters of the string in upper case. Then, sends this string to toDecimalString() and displays the returned string (decimal format or error message).arrow_forwardWrite the method catDog.* * Given a String str, return true if the words "cat" and "dog" appear the same* number of times in the string.* * Examples: catDog("catdog") returns true catDog("catcat") returns false* catDog("1cat1cadodog") returns true* * @param str the String to examine.* @return true if there are the same number of cats and dogs.arrow_forwardConsider the following method: public a(String s) {int v = 0;for(int x = 1; x < s.length; x=x+1) {if(s[x] == 'a' || s[x]=='e' || s[x] == 'i' || s[x] == 'o' || s[x] == 'u') { v = v + 1; }}return v;} Exactly how many lines of code (statements) are executed by the method a() in the worst case? Express your answer in terms of n, the length of the string s. Hint: simplify your final answer as much as possible, and do not put spaces in your answer. Use juxtaposition for the multiplication operator, for example to write "nine times n" write "9n" not "9xn" or "9*n"; to write "four times (n+2)" write "4(n+2)", not "4x(n+2)" or "4*(n+2)". Do not write your answer in Big-O notation. Write the exact number of lines executed.arrow_forward
- Write a short recursive Java method that determines if a string s is a palindrome, that is, it is equal to its reverse. Examples of palindromes include 'racecar' and 'mom'. Test the method by asking the user to provide string entries to be checked. Hint: Check the equality of the first and last characters and recur (but be careful to return the correct value for both odd and even-length strings).arrow_forwardImplement a new concat method in the String class you worked on for HW2. The method should return a new String object that represents the text you would end up with if the argument String were "added onto the end of" this String. For example, in the code below, the String object answer should represent the text "bookstore" and true should be printed the screen. char[] bookA = {'b', 'o', 'o', 'k'}; char[] storeA = {'s', 't', 'o', 'r', 'e'}; char[] bookstoreA = {'b', 'o', 'o', 'k', 's', 't', 'o', 'r', 'e'}; String book = new String(bookA); String store = new String(storeA); String bookstore = new String(bookstoreA); String answer = book.concat(store); System.out.println(answer.equals(bookstore)); In the text area below, type in your definition of the concat method. (In other words, type in the code that should replace the //TODO comment.) Your code cannot call any methods. public class String { private char[] data; public String(char[] value) { data = new…arrow_forwardWrite a program which takes a String input and then prints the number of times the letter p is followed by a vowel (y is not considered a vowel here). The letter p, and the vowel that follows, may be upper or lower case. Hint - remember there are String methods which return a copy of the String it is called on with all letter characters changed to the same case. in java Sample run Input String: Peter Piper picked a pack of pickle peppers. Contains p followed by a vowel 8 times.arrow_forward
- Write a java program that reads a sets of integers and then finds and print the sum of the even and odd integersarrow_forwardWrite a Java method toDecimalString() that receives a string with hexadecimal symbols andreturns another string where every hexadecimal symbol is converted individually to its decimalrepresentation. If the received string contains a wrong hexadecimal value, it should return an errormessage.To test your method, write a main() method that enables the user to input a string in hexadecimalformat, removes eventual extra spaces from it, and makes all characters of the string in upper case.Then, sends this string to toDecimalString() and displays the returned string (decimal format orerror message).Hints:1. To declare an empty string use: String newString = "";2. The hexadecimal system is composed of 16 symbols: 0 to 9 and A to F.a. To check if a character c is a digit you can use: Character.isDigit(c);b. The decimal representation of A is 10, B is 11, C is12, D is 13, E is 14, and F is 15. Run Example 1:Enter a hexadecimal string: 2gkDecimal value is: Wrong hexadecimal value (g) Run…arrow_forwardWrite a Java method to count all vowels in a string.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT