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 4.7, Problem 34SC
Explanation of Solution
Given
//Initialize the required variable
boolean found = false;
//Check the condition using while loop
for (int position =0 ;!found && position < str.length(); position++)
{
//Assign the first position of string to the variable “ch”
Char ch = str.charAt(position);
//Check whether the ch equals to null
if (ch == ' ')
{
//Assign...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write a program that reads a character and a string (an entire line, which may include spaces or tabs), and outputs the number of times the character appears in the string. Hint: use getline(cin, mystring) to read the entire line including spaces. Use mystring.length() to know how many characters.
But to avoid a glitch when getline gets an empty string following a cin, do the following sequence:
cin >> mychar; /* get character. cin leaves the newline in the input buffer*/ cin.ignore(80,'\n'); /* remove newline from the buffer, which getline would see as empty line*/ cout << "Enter a string: "; //prompt for string getline(cin, mystring);
Example program run (the user types only the word "Monday" and the letter "a"):
Enter character to count: a Enter a string: Monday count = 1
Example program run:
Enter character to count: a Enter a string: Today is Monday count = 2
Example program run:
Enter character to count: b Enter a string: Today is Monday count = 0
#include…
What happens if you try to multiply a string by a string, "a" * "b"?
Write a program that checks matching words
- First asks the user to enter 2 String variables word1 and word2
- Save these in two String variables.
- Use string methods to answer below questions:
o Are these words entered same (ignore case)?
o Are these words entered same (case sensitive)?
- Test for different inputs
- Write a For loop to print each character of word1 on a separate line
Chapter 4 Solutions
Big Java Late Objects
Ch. 4.1 - How many years does it take for the investment to...Ch. 4.1 - If the interest rate is 10 percent per year, how...Ch. 4.1 - Modify the program so that the balance after each...Ch. 4.1 - Suppose we change the program so that the...Ch. 4.1 - What does the following loop print? int n = 1;...Ch. 4.2 - Hand-trace the following code, showing the value...Ch. 4.2 - Hand-trace the following code, showing the value...Ch. 4.2 - Hand-trace the following code, assuming that a is...Ch. 4.2 - Trace the following code. What error do you...Ch. 4.2 - Prob. 10SC
Ch. 4.3 - Write the for loop of the InvestmentTable.java...Ch. 4.3 - How many numbers does this loop print? for (int n...Ch. 4.3 - Write a for loop that prints all even numbers...Ch. 4.3 - Write a for loop that computes the sum of the...Ch. 4.3 - How would you modify the for loop of the...Ch. 4.4 - Prob. 16SCCh. 4.4 - Rewrite the input check do loop using a while loop...Ch. 4.4 - Suppose Java didnt have a do loop. Could you...Ch. 4.4 - Write a do loop that reads integers and computes...Ch. 4.4 - Write a do loop that reads integers and computes...Ch. 4.5 - What does the SentinelDemo.java program print when...Ch. 4.5 - Why does the SentinelDemo.java program have to...Ch. 4.5 - What would happen if the declaration of the salary...Ch. 4.5 - In the last example of this section, we prompt the...Ch. 4.5 - Prob. 25SCCh. 4.6 - Prob. 26SCCh. 4.6 - Google has a simple interface for converting...Ch. 4.6 - Consider a modification of the program in Self...Ch. 4.6 - Prob. 29SCCh. 4.6 - Produce a storyboard for a program that compares...Ch. 4.7 - What total is computed when no user input is...Ch. 4.7 - Prob. 32SCCh. 4.7 - What are the values of position and ch when no...Ch. 4.7 - Prob. 34SCCh. 4.7 - Prob. 35SCCh. 4.7 - Prob. 36SCCh. 4.8 - Why is there a statement System.out.println(); in...Ch. 4.8 - How would you change the program to display all...Ch. 4.8 - Prob. 39SCCh. 4.8 - What do the following nested loops display? for...Ch. 4.8 - Prob. 41SCCh. 4.9 - Prob. 42SCCh. 4.9 - You need to write a program for DNA analysis that...Ch. 4.9 - Prob. 44SCCh. 4.9 - Consider the task of finding numbers in a string....Ch. 4.10 - How do you simulate a coin toss with the...Ch. 4.10 - How do you simulate the picking of a random...Ch. 4.10 - Why does the loop body in Dice.java call...Ch. 4.10 - Prob. 49SCCh. 4.10 - Prob. 50SCCh. 4 - Given the variables String stars = ""; String...Ch. 4 - What do these loops print? a. int i = 0; int j =...Ch. 4 - What do these code snippets print? a. int result =...Ch. 4 - Write awhile loop that prints a. All squares less...Ch. 4 - Write a loop that computes a. The sum of all even...Ch. 4 - Provide trace tables for these loops. a. int i =...Ch. 4 - What do these loops print? a. for (int i = 1; i ...Ch. 4 - What is an infinite loop? On your computer, how...Ch. 4 - Write a program trace for the pseudocode in...Ch. 4 - What is an off-by-one error? Give an example from...Ch. 4 - What is a sentinel value? Give a simple rule when...Ch. 4 - Which loop statements does Java support? Give...Ch. 4 - How many iterations do the following loops carry...Ch. 4 - Write pseudocode for a program that prints a...Ch. 4 - Prob. 15RECh. 4 - Write pseudocode for a program that reads a...Ch. 4 - Write pseudocode for a program that reads a...Ch. 4 - Rewrite the following for loop into a while loop....Ch. 4 - Rewrite the following do loop into a while loop....Ch. 4 - Provide trace tables of the following loops. a....Ch. 4 - What do the following loops print? Work out the...Ch. 4 - What do the following program segments print? Find...Ch. 4 - Prob. 23RECh. 4 - Add a storyboard panel for the conversion program...Ch. 4 - In Section 4.6, we decided to show users a list of...Ch. 4 - Change the storyboards in Section 4.6 to support a...Ch. 4 - Draw a flowchart for a program that carries out...Ch. 4 - In Section 4.7.5, the code for finding the largest...Ch. 4 - What are nested loops? Give an example where a...Ch. 4 - The nested loops for (int 1 = 1; 1 = height; i++)...Ch. 4 - Suppose you design an educational game to teach...Ch. 4 - In a travel simulation, Harry will visit one of...Ch. 4 - Write programs with loops that compute a. The sum...Ch. 4 - Write programs that read a sequence of integer...Ch. 4 - Write programs that read a line of input as a...Ch. 4 - Complete the program in How To 4.1 on page 171....Ch. 4 - Write a program that reads a set of floating-point...Ch. 4 - Translate the following pseudocode for finding the...Ch. 4 - Translate the following pseudocode for randomly...Ch. 4 - Write a program that reads a word and prints each...Ch. 4 - Write a program that reads a word and prints the...Ch. 4 - Write a program that reads a word and prints the...Ch. 4 - Write a program that reads a word and prints the...Ch. 4 - Write a program that reads a word and prints all...Ch. 4 - Write a program that reads a string and prints the...Ch. 4 - Write a program that reads a sequence of words and...Ch. 4 - Write a program that prints all powers of 2 from...Ch. 4 - Write a program that reads a number and prints all...Ch. 4 - Prob. 18PECh. 4 - Write a program that reads an integer and...Ch. 4 - Write a program that reads an integer and...Ch. 4 - Write a program to plot the following face.Ch. 4 - Write a graphical application that displays a...Ch. 4 - Enhance Worked Example 4.1 to check that the...Ch. 4 - Mean and standard deviation. Write a program that...Ch. 4 - The Fibonacci numbers are defined by the sequence...Ch. 4 - Factoring of integers. Write a program that asks...Ch. 4 - Prime numbers. Write a program that prompts the...Ch. 4 - The game of Nim. This is a well-known game with a...Ch. 4 - The Drunkards Walk. A drunkard in a grid of...Ch. 4 - The Monty Hall Paradox. Marilyn vos Savant...Ch. 4 - A simple random generator is obtained by the...Ch. 4 - The Buffon Needle Experiment. The following...Ch. 4 - In the 17th century, the discipline of probability...Ch. 4 - Write a program that reads an initial investment...Ch. 4 - Currency conversion. Write a program that first...Ch. 4 - Write a program that first asks the user to type...Ch. 4 - Your company has shares of stock it would like to...Ch. 4 - Write an application to pre-sell a limited number...Ch. 4 - You need to control the number of people who can...Ch. 4 - Credit Card Number Check. The last digit of a...Ch. 4 - In a predator-prey simulation, you compute the...Ch. 4 - Projectile flight. Suppose a cannonball is...Ch. 4 - Radioactive decay of radioactive materials can be...Ch. 4 - The photo at left shows an electric device called...Ch. 4 - Write a graphical application that draws a spiral,...Ch. 4 - Prob. 28PPCh. 4 - Draw a picture of the four-leaved rose whose...
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 loop to compute and print the number of occurrences of ‘!’ in a string named question. in javaarrow_forwardWrite a program that takes a string as input and outputs the string in all capital letters and the length of the string. The output should follow the format from the example below. Example:Enter a DNA strand: aAtcDNA strand: AATCLength: 4Tip: make use of the .upper() method, the len() function, and the ‘\t’ escape character to add tabs.arrow_forwardIn Java- Write a program to take input a string and find its lenghth using loops only.arrow_forward
- What is wrong with this loop? Two logic errors – please provide explanation. public static void main(String[] args) { for (int i = 10; i >= 20; i++); { System.out.print(i + " " ); } }arrow_forwardI put in my while loop, and now it has caused my col, num, and tol variables to come out as errors and I don't know why. public static void main(String[] args) { int min = 19; int num1=0, num2=0, num3=0; int tol1=0, tol2=0, tol3=0; int max = 50; int i = 0; while (i < 49) { int col[]=new int[49]; i++; } int ran = (int)(Math.random() * (max - min + 1) + min); if( ran <= 20 && ran < 30){ num1 += 1; tol1 += ran; } if(ran <= 30 && ran < 40){ num2 += 1; tol2 += ran; } if(ran <= 40 && ran < 50){ num3 += 1; tol3 += ran; } int col(i)= ran; } { for(int i = 0; i < 49; i++ ){ if(i == 7 || i == 14 || i == 21 || i == 28 || i == 35 ||i == 42){ System.out.println("\n"); } System.out.printf(("%7d"),+ col[i]); } System.out.printf("\n"); System.out.printf("Found "+ num1 +" number(s) in the…arrow_forwardGiven a string, find the first non-repeating character in it and return it'sindex. If it doesn't exist, return -1.For example:s = "leetcode"return 0.s = "loveleetcode",return 2..arrow_forward
- Given a text string, create and return a new string of same length where all vowels have been reversed, and all other characters are kept as they were. For simplicity, in this problem only the characters aeiouAEIOU are considered vowels, and y is never a vowel. For example, given the text string "Uncle Sente lives in Russia", this method should create and return the string "Ancli Sunti levis en Resseu".Furthermore, to make this problem more interesting and the result look more palatable, this method must maintain the capitalization of vowels based on the vowel character that was originally in the position that each new vowel character is moved into. For example, "Ilkka Markus" should become "Ulkka Markis" instead of "ulkka MarkIs". Use the handy character classification methods in the Character utility class to determine whether some particular character is in upper or lower case, and convert some character to its upper or lower case version as needed. Here's the tester it must…arrow_forwardJAVA: Replace the for loop below with a code using the "While" loop: int s = 0; int c = 0; boolean valid = true; for(int i = 4; i <= 20 || valid; i = i+2) { if(i%4 != 0) { s = s + i; c++; } if(i > 20) { valid = false; } } System.out.println(s / c);arrow_forwardGiven a number of petals, print a string which repeats the phrases "Loves me" and "Loves me not" for every alternating petal. The last phrase printed (for the last petal picked) should be in all caps. Remember to include a comma and space between phrases, as shown in examples below. Sample Input 1 3 Sample Output 1 "Loves me, Loves me not, LOVES ME" Sample Input 2 1 Sample Output 2 "LOVES ME"arrow_forward
- do some changes in the code and bring the same output as in the image attachedpublic class algorithm { public static void main(String[] args){String w = " abab";String table[][] = new String[w.length()][w.length()];if (w.isEmpty()){System.out.println("@\n");} for(int i=1; i<w.length();i++){if(w.charAt(i)=='a')table[i][i]="R";else if(w.charAt(i)=='b')table[i][i]="T";}for(int l=2; l<=w.length();l++){for(int i=1; i<w.length()-l+1;i++){int j = i+l-1;for(int k=i; k<=j-1;k++){if(table[i][k].contains("T") && table[k+1][j].contains("R"))table[i][j]="T,R"; //Not sure how to do this part?else if(table[i][k].contains("R") && table[k+1][j].contains("T"))table[i][j]="S"; }} }System.out.println("Finished");System.out.println(w);for(int i=1;i<w.length();i++){for(int j=1;j<w.length();j++){System.out.print(table[i][j]+"\t");}System.out.println(""); }//System.out.println((table[1][w.length()-1]));//if (table[1][w.length()-1].equals("S"))// System.out.println("Accept");}…arrow_forwardif strings are immutable, like in Java, we can assume that they have a static length True Falsearrow_forwardI need help getting this program to run. public class RunLengthEncoding { public static String encode (String input) { //This is to check for any empty srting if(input == null || input.length() == 0) return ""; //This is to show the rresults StringBuilder result = new StringBuilder(); //Variables to store the count of current character and the previous character int count = 1; char prev = input.charAt(0); //The for loop for (int i = 1; i < input.length(); i++) { char curr = input.charAt(i); //An if else statement in case the current any if all characters that get repeated if(curr == prev) { count++; } else { //Encode the string if it's length is greater than 4 if(count > 4) { result.append("/");…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