chapter 4: Mathematical functions, characters, and strings

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Assume that x is a char variable that 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)

Write an expression that evaluates to true if and only if the string variable s does not equal the String literal end.

!s.equals("end")

Write a String constant consisting of exactly 5 exclamation marks

"!!!!!"

Write a String constant that is the empty string

""

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 + ")"

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 "http://www.turingscraft.com".

"http://" + address

The math class provides a static method, max, that accepts two int arguments and returns the value of the larger one. Two int variables, population1 and population2, have already been declared and initialized. Write an expression (not a statement!) whose value is the larger of population1 and population2 by calling max.

Math.max(population1, population2)

If a right triangle has sides of length A, B and C and if C is the largest, then it is called "hypotenuse" and its length is the square root of the sum of the squares of the lengths of the shorter sides (A and B). Assume that variables a and b have been declared as doubles and that a and b contain the lengths of the shorter sides of a right triangle: Write an expression for the length of the hypotenuse.

Math.sqrt(Math.pow(a,2)+(Math.pow(b,2)))

The length of a rectangle is stored in a double variable named length, the width in one named width. Write an expression whose value is the length of the diagonal of the rectangle

Math.sqt(Math.pow(length,2)+(Math.pow(width,2)))

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

String oneSpace = " ";

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

String win; String place; String show;

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);

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

System.out.println(message);

Two variables, num and cost have been declared and given values: num is an integer and cost is a double. Write a single statement that outputs num and cost to standard output. Print both values (num first, then cost), separated by a space on a single line that is terminated with a newline character. Do not output anything else.

System.out.println(num + " " + cost);

Assume that c is a char variable that has been declared and already given a value. Write an expression whose value is true if and only if c is a tab a tab character

c == '\t'

Declare a character variable named c

char c;

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) + "."

Given the string variables name1, name2, and name3, write a fragment of code that assigns the largest value to the variable max (assume all three have already been declared and have been assigned values.

if (name1.compareTo(name2)>0)max = name1;

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)

Given the String variable name, write an expression that evaluates to the third character of name.

name.charAt(2)

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

name.charAt(4)

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 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 character literal representing a comma

','

Write a character literal representing the digit 1

'1'

Writ a character literal representing the (upper case) letter A

'A'

Write a literal representing the character whose Unicode value is 65

'\u0041'

Write a literal representing the largest character value

'\uFFFF'

Write an expression that evaluates to true if and only if the string variable s equals the string "end"

(s.equals("end"))

Write an expression that evaluates to true if the value of the string variable s1 is greater than the value of string variable s2

(s1.compareTo(s2)>0)

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")

Given three int variables that have been declared and given values, areaCode, exchange, and lastFour, write a String expression whose value is the string equivalent of each of these variables joined by a single hyphen (-) so if areaCode, exchange, lastFour, had the values 800, 555, and 1212, the expression's value would be "800-555-1212". Alternatively, if areaCode, exchange, lastFour, had the values 212, 867, and 5309 the expression's value would be "212-867-5309".

areaCode + "=" + exchange + "-" + lastFour

Given the char variable c, write an expression that is true if and only if the value of c is not the space character

c != ' '

Assume that c is a char variable that has been declared and already given a value. Write an expression whose value is true if and only if c is a space character

c == ' '

Assume that c is a char variable that has been declared and already given a value. Write an expression whose value is true if and only if c is what is called a whitespace character (that is a a space or a tab or a newline- none of which result in ink being printed on paper).

c == ' ' || c == '\t' || c == '\n'

Assume that c is a char variable that has been declared and already given a value. Write an expression whose value is true if and only if c is an newline character.

c == '\n'

Assume that an int variable age has been declared and already given a value. Assume further that the user has just presented with the following menu: S: hanger steak, red potatoes, asparagus T: whole trout, long rice, brussel sprouts B: cheddar cheeseburger, steak fries, cole slaw Write some code that reads the String( S or T or B) that the user types in into a String variable choice that has already been declared and prints out a recommended accompanying drink as follows: if the value of age is 21 or lower, the recommendation is "vegetable juice" for steak, "cranberry juice" for trout, "soda" for the burger. Otherwise, the recommendations are "cabernet", "chardonnay", and "IPA" for steak, trout, and burger respectively. Regardless of the value of age, your code should print "invalid menu selection" if the character read into choice was not S or T or B.

choice=stdin.next(); if (age <=21){ if(choice.equals("S")) System.out.println("vegetable juice"); else if ( choice.equals("T")) System.out.println("cranberry juice"); else if (choice.equals ("B")) System.out.println("soda"); else System.out.println("invalid menu selection"); } else if (age>21){ if (choice.equals("S")) System.out.println("cabernet"); else if (choice.equals("T")) System.out.println("chardonnay"); else if (choice.equals("B")) System.out.println("IPA"); else System.out.println("invalid menu selection"); }

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.idexOf(','); clause = line.substring(0,pos);

Clunker Motors Inc. is recalling all vehicles in its extravagant line from model years 1999-2002. A boolean variable named recalled has been declared. Given a variable modelYear and a String modelName, write a statement that assigns true to recalled if the values of modelYear and modelName match the recall details and assigns false otherwise

recalled = (modelName.equals("Extravagant") && 1999 <= modelYear && modelYear <= 2002);

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

s.substring(2,10)

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

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

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

sentence.length()

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

text = "";


संबंधित स्टडी सेट्स

Race and Ethnicity as a Lived Experience

View Set

Biology- Unit 2- Book & lecture notes - Cancer & Genetics - ch 9, 10, 12

View Set

Ch. 15- Diseases of Lower GI Tract

View Set

IS 300: Introduction to Information Systems

View Set

AUTOMATIC EMERGENCY BRAKING AND INTELLIGENT FORWARD COLLISION WARNING

View Set

Chapter 5: The Law of Universal Gravitation

View Set

NTCI: Troubleshooting Advanced Services

View Set

Lesson 8 - Teen-Styled Rock Music in the Early 1960s

View Set