MyProgrammingLab - Chapter 9: Text Processing and More about Wrapper Classes (Tony Gaddis)
Assume that x is a char variable has been declared and already given a value. Write an expression whose value is true if and only if x is NOT a letter.
!Character.isLetter(x)
Assume that x is a char variable has been declared and already given a value. Write an expression whose value is true if and only if x is an hexadecimal (Base 16) digit (0-9 plus A-F or a-f).
(x >= 'A' && x <= 'F') || (x >= 'a' && x <= 'f') || (x <= '9' && x >= '0')
Assume that x is a char variable has been declared and already given a value. Write an expression whose value is true if and only if x is a decimal digit (0-9).
Character.isDigit(x)
Assume that x is a char variable has been declared and already given a value. Write an expression whose value is true if and only if x is an octal (Base 8) digit (0-7).
Character.isDigit(x) && x <= '7'
Assume that x is a char variable has been declared and already given a value. Write an expression whose value is true if and only if x is a letter.
Character.isLetter(x)
Assume that x is a char variable has been declared and already given a value. Write an expression whose value is true if and only if x is a lower-case letter.
Character.isLowerCase(x)
Assume that x is a char variable has been declared and already given a value. Write an expression whose value is true if and only if x is a upper-case letter.
Character.isUpperCase(x)
Write a sequence of statements that finds the first comma in the String line, and assigns to the variable clause the portion of line up to, but not including the comma. You may assume that an int variable pos, as well as the variables line and clause, have already been declared.
clause = line.substring(0, line.indexOf(','));
Assume that word is a variable of type String that has been assigned a value. Assume furthermore that this value always contains the letters "dr" followed by at least two other letters. For example: "undramatic", "dreck", "android", "no-drip". Assume that there is another variable declared, drWord, also of type String. Write the statements needed so that the 4-character substring word of the value of word starting with "dr" is assigned to drWord. So, if the value of word were "George slew the dragon" your code would assign the value "drag" to drWord.
drWord = word.split("dr")[1];
A String variable, fullName, contains a name in one of two formats: last name, first name (comma followed by a blank), or first name last name (single blank) Extract the first name into the String variable firstName and the last name into the String variable lastName. Assume the variables have been declared and fullName already initialized. You may also declare any other necessary variables.
firstName = fullName.contains(",") ? fullName.split(", ")[1] : fullName.split(" ")[0]; lastName = fullName.contains(",") ? fullName.split(", ")[0] : fullName.split(" ")[1];
Assume that sentence is a variable of type String that has been assigned a value. Assume furthermore that this value is a String consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence." Assume that there is another variable declared, firstWord, also of type String. Write the statements needed so that the first word of the value of sentence is assigned to firstWord. So, if the value of sentence were "Broccoli is delicious." your code would assign the value "Broccoli" to firstWord.
firstWord = sentence.split(" ")[0];
Assume that given, middle and family are three variables of type String that have been assigned values. Write an expression whose value is a String consisting of the first character of given followed by a period followed by the first character of middle followed by a period followed by the first character of family followed by a period: in other words, the initials of the name. So if the values of these three variables were "John" "Fitzgerald" "Kennedy", then the expression's value would be "J.F.K.".
given.substring(0,1) + "." + middle.substring(0,1) + "." + family.substring(0,1) + "."
Suppose a reference variable of type Double called myDouble is already declared. Create an object of type Double with the initial value of 1.5 and assign it to the reference variable myDouble.
myDouble = new Double(1.5);
Suppose a reference variable of type Double called myDouble has already been declared. There is also a double variable x that has been declared and initialized. Create an object of type Double with the initial value of x and assign it to the reference variable myDouble.
myDouble = new Double(x);
Suppose a reference variable of type Integer called myInt is already declared. Create an object of type Integer with the initial value of 1 and assign it to the reference variable myInt.
myInt = new Integer(1);
Suppose a reference variable of type Long called myLong is already declared. Create an object of type Long with the initial value of two billion and assign it to the reference variable myLong.
myLong = new Long(2000000000);
Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is a String containing the first character of the value of name. So if the value of name were "Smith" the expression's value would be "S".
name.substring(0,1)
Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is a String containing the second character of the value of name. So if the value of name were "Smith" the expression's value would be "m".
name.substring(1,2)
Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is a String containing the last character of the value of name. So if the value of name were "Smith" the expression's value would be "h".
name.substring(name.length()-1, name.length())
Write a method, getEmailDomain, that is passed a String argument that is an email address and returns the domain name part. So if "[email protected]" is passed, the method returns "salzberg.de". Assume that the argument will always be a correctly formatted email address.
public String getEmailDomain(String s) { return s.split("[@]")[1]; }
Write a method, getEmailUserName, that is passed a String argument that is an email address and returns the user-name part. So if "[email protected]" is passed, the method returns "mozart". Assume that the argument will always be a correctly formatted email address.
public String getEmailUserName(String email) { return email.split("@")[0]; }
Write a method, getFirstLine, that is passed a String argument and that returns the first line. (Recall that lines are terminated with the "\n" character.) Assume that the argument contains at least one complete, newline-terminated line.
public String getFirstLine(String s) { return s.split("\n").length > 0 ? s.split("\n")[0] : ""; }
Write a method, getSecondLine, that is passed a String argument and that returns the second line, without its newline character. (Recall that lines are terminated with the "\n" character.) Assume that the argument contains at least two complete, newline-terminated lines.
public String getSecondLine(String s) { return s.split("\n").length > 1 ? s.split("\n")[1] : ""; }
Write a method, makeEmailAddress, that is passed two String arguments: the first is a username, like "leadbelly" and the second is a domain name, like "blues.com". The method returns an email address formed from joining these with an "@": "[email protected]".
public String makeEmailAddress(String u, String d) { return u + "@" + d; }
Write a method, isEmailAddress, that is passed a String argument. It returns true or false depending on whether the argument has the form of an email address. In this exercise, assume that an email address has one and only one "@" sign and no spaces, tabs or newline characters.
public boolean isEmailAddress(String s) { return s.matches("\\w+@\\w+"); }
Write an expression that results in a String consisting of the third through tenth characters of the String s.
s.substring(3,10)
Assume that sentence is a variable of type String that has been assigned a value. Assume furthermore that this value is a String consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence." Assume that there is another variable declared, secondWord, also of type String. Write the statements needed so that the second word of the value of sentence is assigned to secondWord. So, if the value of sentence were "Broccoli is delicious." your code would assign the value "is" to secondWord.
secondWord = sentence.split(" ")[1];
Given a String variable named sentence that has been initialized, write an expression whose value is the index of the very last character in the String referred to by sentence.
sentence.length()-1
Assume that word is a variable of type String that has been assigned a value. Write an expression whose value is a String consisting of the first three characters of the value of word. So if the value of word were "dystopia" the expression's value would be "dys".
word.substring(0,3)
Assume that word is a variable of type String that has been assigned a value. Write an expression whose value is a String consisting of the last three characters of the value of word. So if the value of word were "biggest" the expression's value would be "est".
word.substring(word.length()-3, word.length())