Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 4.4, Problem 4.4.2CP
Program Plan Intro
Comparing strings:
String class contains the method which is used to compare two strings.
Some examples of string methods used for comparing strings are:
- “equals(s1)” - Returns true if the given string is equals to string “s1”.
- “compareTo(s1)” - Returns an integer greater than “0” if the string is greater, returns “0” if both the strings are equals and returns less than “0” if the given string less than s1.
- “contains(s1)” -Returns true if “s1” is a substring of the given string.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
In Java, please write a program that asks the user to enter a string, and then converts that string to Morse code. Use hyphens for dashes and periods for dots.
Morse Code Table
Character
Code
Character
Code
Character
Code
Character
Code
space
space
6
- . . . .
G
--.
Q
--.-
comma
--..--
7
-- . . .
H
. . . .
R
.-.
period
.-.-.-
8
---..
I
..
S
. . .
question mark
..--..
9
----.
J
.---
T
-
0
-----
A
.-
K
-.-
U
..-
1
.----
B
- . . .
L
.-..
V
. . . -
2
..---
C
-.-.
M
--
W
.--
3
. . . --
D
-..
N
-.
X
-..-
4
. . . .-
E
.
O
---
Y
-.--
5
. . . ..
F
..-.
P
.--.
Z
--..
Suppose s1 and s2 are two strings. Which of the following statement or expression is valid in Java ?
a - String s3 = s1 + s2;
b - s1 >= s2;
c - int i = s1.length;
d - s1.charAt(0) = '5';
Here is the question:
Write a program that inserts parentheses, a space, and a dash into a string of 10 user-entered numbers to format it as a phone number. For example, 5153458912 becomes (515) 345-8912. If the user does not enter exactly 10 digits, display an error message. Continue to accept user input until the user enters 999.
Here is the code given:
import java.util.*;
public class PhoneNumberFormat {
public static void main(String[] args) {
// Write your code here
}
}
Chapter 4 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Ch. 4.2 - Evalute the following method calls: (a)...Ch. 4.2 - True or false? The argument for trigonometric...Ch. 4.2 - Write a statement that converts 47 degrees to...Ch. 4.2 - Write a statement that converts PI to an angle in...Ch. 4.2 - Write an expression that obtains a random integer...Ch. 4.2 - Prob. 4.2.6CPCh. 4.2 - Prob. 4.2.7CPCh. 4.3 - Use print statements to find out the ASCII code...Ch. 4.3 - Which of the following are correct literals for...Ch. 4.3 - How do you display the characters \ and "?
Ch. 4.3 - Evaluate the following: Int i = '1'; int j ='1' +...Ch. 4.3 - Can the following conversions involving casting be...Ch. 4.3 - Show the output of the following program: public...Ch. 4.3 - Write the code that generates a random lowercase...Ch. 4.3 - Show the output of the following statements:...Ch. 4.4 - Suppose s1, s2, and s3 are three strings, given as...Ch. 4.4 - Prob. 4.4.2CPCh. 4.4 - Show the output of the following statements (write...Ch. 4.4 - Prob. 4.4.4CPCh. 4.4 - Let s1 be " Welcome " and s2 be " welcome ". Write...Ch. 4.4 - Write one statement to return the number of digits...Ch. 4.4 - Write one statement to return the number of digits...Ch. 4.5 - If you run Listing 4.3 GuessBirthday.java with...Ch. 4.5 - If you enter a lowercase letter such as b, the...Ch. 4.5 - What would be wrong if lines 6 and 7 are in...Ch. 4.6 - Prob. 4.6.1CPCh. 4.6 - Prob. 4.6.2CPCh. 4.6 - Show the output of the following statements: (a)...Ch. 4 - (Geometry: area of a pentagon) Write a program...Ch. 4 - (Geometry: great circle distance) The great circle...Ch. 4 - (Geography: estimate areas) Use the GPS locations...Ch. 4 - (Geometry: area of a hexagon) The area of a...Ch. 4 - (Geometry: area of a regular polygon) A regular...Ch. 4 - (Random points on a circle) Write a program that...Ch. 4 - (Corner point coordinates) Suppose a pentagon is...Ch. 4 - (Find the character of an ASCII code) Write a...Ch. 4 - (Find the Unicode of a character) Write a program...Ch. 4 - (Guess birthday) Rewrite Listing 4.3,...Ch. 4 - (Decimal to hex) Write a program that prompts the...Ch. 4 - (Hex to binary) Write a program that prompts the...Ch. 4 - (Vowel or consonant?) Write a program that prompts...Ch. 4 - (Convert Letter grade to number) Write a program...Ch. 4 - (Phone key pads) The international standard...Ch. 4 - (Random character) Write a program that displays a...Ch. 4 - (Days of a month) Write a program that prompts the...Ch. 4 - (Student major and status) Write a program that...Ch. 4 - (Business: check ISBN-10) Rewrite Programming...Ch. 4 - (Process a string) Write a program that prompts...Ch. 4 - (Check SSN) Write a program that prompts the user...Ch. 4 - (Check substring) Write a program that prompts the...Ch. 4 - 23 (Financial application: payroll) Write a...Ch. 4 - (Order three cities) Write a program that prompts...Ch. 4 - (Generate vehicle plate numbers) Assume that a...Ch. 4 - (Financial application: monetary units) Rewrite...
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
- For Java Write an application that counts the words in a String entered by a user. Words are separated by any combination of spaces, periods, commas, semicolons, question marks, exclamation points, or dashes. Figure 7-17 shows a typical execution.arrow_forwardWrite an application that counts the words in a String entered by a user. Words are separated by any combination of spaces, periods, commas, semicolons, question marks, exclamation points, or dashes. Figure 7-17 shows a typical execution.arrow_forwarduse java : write an application that reads English-Language phrase and encodes it into morse code. also write an application that reads a phrase in morse code and converts it into the English-Language equivalent . Use one blank between each Morse-code letter and three blanks between each Morse-coded word.arrow_forward
- Program41.javaWrite a program that prompts the user to enter a full name consisting of three names (first middle last) into one String variable. Then use methods of class String to: print the number of characters in the full name, excluding spaces print just the middle name and the number of characters in it. print the three initials of the name print the last name in all lower case. print the full name in the usual alphabetical format (Last, First Middle) and proper case.arrow_forwardCode in python only. You are given four integer esteems a, b, c and m. Check if there exists a string that contains: a letters 'A'; b letters 'B'; c letters 'C'; no different letters; precisely m sets of nearby equivalent letters (precisely m such positions I that the I-th letter is equivalent to the (i+1)- th one). Input :The primary line contains a solitary integer t (1≤t≤104) — the number of testcases. Every one of the following t lines contains the portrayal of the testcase — four integers a, b, c and m (1≤a,b,c≤108; 0≤m≤108). Output :For each testcase print "YES" if there exists a string that fulfills every one of the necessities. Print "NO" in case there are no such strings. You might print each letter regardless you need (thus, for instance, the strings yEs, indeed, Yes and YES will all be perceived as sure reply).arrow_forwardThe files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. // Converts a string to lowercase, and // displays the string's length // as well as a count of letters public class DebugSeven4 { public static void main(String[] args) { String aString = "HELP!! I need to get 37 things DONE today!!"; int numLetters = 0. int stringLength = aString.Length(); System.out.println("In all lowercase, the sentence is: "); for(int i = 0; i < stringLength; i++) { char ch = Character.toTheLowerCase(aString.charAt(i)); System.out.print(ch); if(Character.isLetter(ch)) numLetters--; } System.out.println(); System.out.println ("The number of CHARACTERS in the string is " + stringLength); System.out.println(The number of LETTERS is "…arrow_forward
- In Java, an identifier, which can be class name or method name, is a series (string) of the following groups/types of characters: {fill/continue from 2) (1) Lower-case alphabet characters: a – z, (2) ……………..… (3) …………….…… (4) ……………... (5) ………….arrow_forwardGiven the following: strFirstName = “Java” strLastName = “is fun!” Write a statement that concatenates the two string values and assigns them to strFullName, so the new string appears as Java is fun!.arrow_forwardProgram41.javaWrite a program that prompts the user to enter a full name consisting of three names (first middle last) into one String variable. Then use methods of class String to: print the number of characters in the full name, excluding spaces print just the middle name and the number of characters in it. print the three initials of the name print the last name in all lower case. print the full name in the usual alphabetical format (Last, First Middle) and proper case. Program51.java Write a program that uses a while loop to generate 49 random integers, all from 20 to 49, inclusive. The same loop should print the integers in seven rows and right-aligned in columns seven characters wide. The program should report both the count and the total of integers in the 20s, 30s, and 40s. See Sample Output.Sample Output Program52.java Write a program that prompts the user for a binary number (from 3 to 8 bits) as a String and converts it to base-10 decimal value. There are several ways to…arrow_forward
- In java please help with the following: Given a string that contains space separated words, write an application that displays the words in ascending order (If two words are the same, display only one). You need to use the right data structure for storing the words. For example, for input:String input="apple banana apple orange blueberry"; The program will print out: apple banana blueberry orangearrow_forwardin java: Write a program that reads the following information and prints a payroll statement: Employee’s name (e.g., Smith) Number of hours worked in a week (e.g., 10) Hourly pay rate (e.g., 9.75) Federal tax withholding rate (e.g., 20%) State tax withholding rate (e.g., 9%) Your program must use proper formatting for the output either by using the printf command or String.formatarrow_forwardGiven the following Java code: String str1 = “Java is useful!”; String str2= “Java is cool”; write the result of to each of the following questions: write a Java expression that compares if str1 is equal to str2 write a Java expression that displays string str1arrow_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