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 7PE
Explanation of Solution
Method for returning count of all words in a string
public static int count_words (String str)
{
//declare variable state
int state = OUT;
//declare variable wc
int wc = 0;
//declare variable i
int i = 0;
//while loop executes
while(i<str.length())
{
//if condition
if(str.charAt(i) == '' || str.charAt(i) == '\n' || str.charAt(i) == '\t')
state = OUT;
//elseif condition
elseif (state==OUT)
{
...
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 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).
Write 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…
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 Java program that implements static method roundSum() so that given 3 ints: a, b, and c; return the sum of their rounded values. We'll round an int value up to the next multiple of 10 if its rightmost digit is 5 or more, so 15 rounds up to 20. Alternately, round down to the previous multiple of 10 if its rightmost digit is less than 5, so 12 rounds down to 10. roundSum(16, 17, 18) → 50roundSum(12, 13, 14) → 40roundSum(6, 4, 4) → 10 The examples of input and output you can see in the picture. (the code structure should be as in the second picture)arrow_forwardWrite a java method: public static String repeat(String str, int num) that returns the string str repeated n times. For Example: repeat("delta", 4) returns "deltadeltadeltadelta".arrow_forwardFor this problem you will write a method that prints out a pyramid. The boxes in each layer of the pyramid are stacked directly on top of one another (i.e. not offset between two boxes below). For example, below are shown two pyramids, one with base 5 and one with base 6. [] [][] [][][] [][][][] [][][][][] [][][][][][]arrow_forward
- Write a static method randomFixedLengthWordsthat takes int values N and W as arguments and returns an array of N string values thatare each strings of W characters from the alphabet.arrow_forwardImplement a method rand7 () given rand S (). That is, given a method that generates a random number between O and 4 (inclusive), write a method that generates a random number between O and 6 (inclusive)arrow_forwardWrite a method that will receive 3 integers and return the largest integer. public static int findMax(int a, int b, int c) {arrow_forward
- Write a Java program which reads a string from the user and calls a method findVowels() from the main() method which passes the string as argument and prints the number of occurrences of each vowel (i.e., a, e, i, o, and u) present in that string. Read the string from the user using the Scanner class. Sample input:The quick brown fox jumps over the lazy dogSample output:a: 1e: 3i: 1o: 4u: 2Sample input:Merry Christmas!Sample output:a: 1e: 1i: 1o: 0u: 0arrow_forwardSuppose that a Scanner object contains a bunch of tokens all on a single line separated by spaces. For example: 100.5 65 pizza 100 45.75 70 5.8 5 Also suppose that you have two integers n and m that are both positive. For example: n = 5 and m = 10 Write a static method: public static int partialSum(Scanner s, int n, int m) that takes a Scanner and two integers n and m as arguments and returns the sum of the integers read by the Scanner that are divisible by n and not divisible by m. In the above example, the integers divisible by 5 and not divisible by 10 are 65 and 5, so the result would be 70. If the Scanner is empty, or no tokens are valid integers, the result should be 0. You can assume the Scanner is not null.arrow_forward: Write a java program that count the number of letters in a String and find the first occurrence of S, U, I, T in the String and return indices of S, U, I, T. Join the three initialized Strings string1, string2, and string3. Count the number of white spaces in the complete string. Use toUpperCase() and replaceAll("\\s", "") methods to convert string into Upper Case and remove all white Spaces from the string. Count the number of letters in the string. Find the first occurrence of S, U, I, T in the String and return indices of S, U, I, T. Strings are given below: String string1 = "Globalization and Information Technology continue to process at a dramatic pace" String string2 = "To answer the demand of a changing society Pakistan higher education system needs to shift its focus from individuals who simply absorb knowledge to those who are able to creatively utilize knowledge to excel in unfamiliar situations"; String string3 = "These creative individuals should be able to track…arrow_forward
- 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. (in java)arrow_forwardWrite a java method that takes four integers as arguments (int a, int b, int c, int d), that prints Match if and only if at least three values match otherwise, it prints No Match.arrow_forwardConsider the following implementation of the compareTo() method for String.How does the third line help with efficiency?public int compareTo(String that){ if (this == that) return 0; // this line int n = Math.min(this.length(), that.length()); for (int i = 0; i < n; i++) { if (this.charAt(i) < that.charAt(i)) return -1; else if (this.charAt(i) > that.charAt(i)) return +1; } return this.length() - that.length();}arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
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