MPL Java - Chapter 3

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Exercise 20963 Given a String variable word, write a String expression that parenthesizes the value of word. So, if word contains "sadly", the value of the expression would be the String "(sadly)"

"(" + word + ")"

Exercise 20818 Write an expression whose value is the number of characters in the following String: "CRAZY!\n\\\t\\\\\\\\\\n. . . .\\ \\\r\007'\\'\"TOOMUCH!" Suggestion: copy/paste the above String into your code-- it's too crazy to try to copy it by typing.

"CRAZY!\n\\\t\\\\\\\\\\n. . . .\\ \\\r\007'\\'\"TOOMUCH!".length()

Exercise 20820 WORK AREA Given a String variable address, write a String expression consisting of the string "http://" concatenated with the variable's String value. So, if the variable refers to "www.turingscraft.com", the value of the expression would be "http://www.turingscraft.com".

"http://" + address

Exercise 20606 Given an int variable datum that has already been declared, write a few statements that read an integer value from standard input into this variable.

Scanner input=new Scanner(System.in); datum=input.nextInt();

Exercise 20815 Write the declaration of two String variable named background and selectionColor and initialize them to "white" and "blue" respectively.

String background="white"; String selectionColor="blue";

Exercise 20974 Declare a String variable named empty, and initialize it to the empty String.

String empty="";

Exercise 20814 Write the declaration of a String variable named foreground and initialize it to "black".

String foreground="black";

Exercise 20975 Declare a String variable named mailingAddress.

String mailingAddress;

Exercise 20950 Declare a String variable named oneSpace, and initialize it to a String consisting of a single space.

String oneSpace=" ";

Exercise 20757 There are two String variables, s1 and s2, that have already been declared and initialized. Write some code that exchanges their values. Declare any other variables as necessary.

String s3=s1; s1=s2; s2=s3;

Exercise 20812 Write the declaration of a String variable named title.

String title;

Exercise 20813 Write the declaration of three String variables named win, place, and show.

String win; String place; String show;

Exercise 20978 Assume that word is a String variable. Write a statement to display the message "Today's Word-Of-The-Day is: " followed by the value of word. The message and the value of word should appear together, on a single line on standard output.

System.out.println("Today's Word-Of-The-Day is: "+ word);

Exercise 20977 Assume that message is a String variable. Write a statement to display its value on standard output.

System.out.println(message);

Exercise 20875 Given the String variable address, write an expression that returns the position of the first occurrence of the String "Avenue" in address.

address.indexOf("Avenue")

Exercise 20882 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.substring(word.indexOf("dr"),4);

Exercise 20847 Given three String variables that have been declared and given values, firstName, middleName, and lastName, write an expression whose value is the values of each these variables joined by a single space. So if firstName, middleName, and lastName, had the values "Big", "Bill", and "Broonzy", the expression's value would be "Big Bill Broonzy". Alternatively, if firstName, middleName, and lastName, had the values "Jerry", "Lee", and "Lewis", the expression's value would be "Jerry Lee Lewis".

firstName + " " + middleName + " " + lastName

Exercise 20960 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.substring(0,sentence.indexOf(" "));

Exercise 20969 Write a statement that reads a word from standard input into firstWord. Assume that firstWord. has already been declared as a String variable. Assume also that stdin is a variable that references a Scanner object associated with standard input.

firstWord=stdin.next();

Exercise 20816 Assume that the String variable named foreground has already been declared. Assign it the value "red".

foreground="red";

Exercise 20881 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.charAt(0) + "." + middle.charAt(0) + "." + family.charAt(0) + "."

Exercise 20848 Given three String variables that have been declared and given values, gold, silver, and bronze, write an expression whose value is the values of each these variables joined by a newline character. So if gold, silver, and bronze, had the values "Arakawa", "Cohen", and "Slutskaya", the expression, if it were printed would have the names "Arakawa", "Cohen", and "Slutskaya" each appearing on a separate line. (Do NOT print anything in this exercise: just write the expression.)

gold + "\n" + silver + "\n" + bronze

Exercise 20884 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.

int start=sentence.indexOf(" ") + 1; int end=sentence.indexOf(" ", start+1); secondWord=sentence.substring(start,end);

Exercise 20822 Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is the first character of the value of name. So if the value of name were "Smith" the expression's value would be 'S'.

name.charAt(0)

Exercise 20872 Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is the second character of the value of name. So if the value of name were "Smith" the expression's value would be 'm'.

name.charAt(1)

Exercise 20828 Write an expression that whose value is the fifth character of the String name.

name.charAt(4)

Exercise 20952 Assume that name is a variable of type String that has been assigned a value. Write an expression whose value is the last character of the value of name. So if the value of name were "Blair" the expression's value would be 'r'.

name.charAt(name.length()-1)

Exercise 20877 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)

Exercise 20878 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)

Exercise 20880 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())

Exercise 20966 Three business partners are forming a company whose name will be of the form "Name1, Name2 and Name3". However, they can't agree whose name should be first, second or last. Help them out by writing code that reads in their three names and prints each possible combination exactly once, on a line by itself (that is, each possible combination is terminated with a newline character). Assume that name1, name2 and name3 have already been declared and use them in your code. Assume also that stdin is a variable that references a Scanner object associated with standard input. For example, if your code read in "Larry", "Curly" and "Moe" it would print out "Larry, Curly and Moe", "Curly, Larry and Moe", etc., each on a separate line.

name1 = stdin.next(); name2 = stdin.next(); name3 = stdin.next(); System.out.println(name1 + ", " + name2 + " and " + name3); System.out.println(name1 + ", " + name3 + " and " + name2); System.out.println(name2 + ", " + name1 + " and " + name3); System.out.println(name2 + ", " + name3 + " and " + name1); System.out.println(name3 + ", " + name2 + " and " + name1); System.out.println(name3 + ", " + name1 + " and " + name2);

Exercise 20972 Assume that name has been declared suitably for storing names (like "Amy", "Fritz" and "Moustafa"). Assume also that stdin is a variable that references a Scanner object associated with standard input. Write some code that reads a value into name then prints the message "Greetings, NAMEVALUE!!!" on a line by itself where NAMEVALUE is replaced the value that was read into name. For example, if your code read in "Hassan" it would print out "Greetings, Hassan!!!" on a line by itself.

name=stdin.next(); System.out.println("Greetings, " + name + "!!!");

Exercise 20971 Assume that name has been declared suitably for storing names (like "Misha", "Emily" and "Sofia"). Assume also that stdin is a variable that references a Scanner object associated with standard input Write some code that reads a value into name then prints the message "Greetings, NAME" on a line by itself, where NAME is replaced the value that was read into name. For example, if your code read in "Rachel" it would print out "Greetings, Rachel" on a line by itself.

name=stdin.next(); System.out.println("Greetings, " + name);

Exercise 20970 Assume that name and age have been declared suitably for storing names (like "Abdullah", "Alexandra" and "Zoe") and ages respectively. Assume also that stdin is a variable that references a Scanner object associated with standard input. Write some code that reads in a name and an age and then prints the message "The age of NAME is AGE" on a line by itself, where NAME and AGE are replaced by the values read in for the variables name and age. For example, if your code read in "Rohit" and 70 then it would print out "The age of Rohit is 70" on a line by itself. There should NOT be a period in the output.

name=stdin.next(); age=stdin.nextInt(); System.out.println("The age of " + name + " is " + age);

Exercise 20883 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.

pos=line.indexOf(','); clause=line.substring(0,pos);

Exercise 20821 Write an expression that concatenates the String variable suffix onto the end of the String variable prefix.

prefix+suffix

Exercise 20879 WORK AREA Write an expression that results in a String consisting of the third through tenth characters of the String s.

s.substring(2,10)

Exercise 20873 Given a String variable named sentence that has been initialized, write an expression whose value is the the very last character in the String referred to by sentence.

sentence.charAt(sentence.length() - 1)

Exercise 20819 Given a String variable named sentence that has been initialized, write an expression whose value is the number of characters in the String referred to by sentence.

sentence.length()

Exercise 20871 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

Exercise 20810 Given the String variable str, write an expression that evaluates to the character at index 0 of str.

str.charAt(0)

Exercise 20968 Write a statement that reads a floating point value from standard input into temperature. Assume that temperature. has already been declared as an double variable. Assume also that stdin is a variable that references a Scanner object associated with standard input.

temperature=stdin.nextDouble();

Exercise 20817 Assume that theString variable named text has already been declared. Assign to it the empty string.

text = "";

Exercise 20967 Write a statement that reads an integer value from standard input into val. Assume that val has already been declared as an int variable. Assume also that stdin is a variable that references a Scanner object associated with standard input.

val=stdin.nextInt();

Exercise 20876 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)

Exercise 20874 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())


Ensembles d'études connexes

chapter 13- metals, paint, and soil

View Set