QuestionQuestion

Fundamentals of Computer Science
String and Math Classes, and Methods
1. Assume s1, s2, and s3 are String variables initialized to "Amanda", "Bobby", and "Chris", respectively. Which String variable or variables are changed by each of the following statements. If none, say so.
System.out.println (s1);
s1 = s3.toLowerCase ();
System.out.println (s2.replace ('B', 'M'));
s3 = s2.concat (s1);
What output is produced by the following code fragment?
String s1 = "Foundations";
String s2;
System.out.println (s1.charAt (1));
s2 = s1.substring (0, 5);
System.out.println (s2);
System.out.println (s1.length ());
System.out.println (s2.length ());
2. Write a declaration statement of a String variable called front, and initialize it to the first 4 characters of another String object called description.
Write a statement that prints the value of a String object called title in all uppercase letters.
Determine if the length of String object word is odd or even and print an appropriate message.
3. Write a loop to determine the number of times the character 'a' appears in the String object word. Print the result.
Print the characters in the String object word backward.
4. What is the value of each of the following expressions? Ensure that the result shows the correct type. Check the Math class description, if necessary.
Math.abs (10) + Math.abs (-10)
Math.pow (2, 3)
Math.pow (3, 2)
Math.sqrt (16)
Write a declaration statement for a double variable named result and initialize it to 5 raised to the power of 2.5.
Write an assignment statement that computes the square root of the sum of num1 and num2 and assigns the result to num3.
Assign a value for a double variable called volume, given the radius of the sphere r. (Use the formula volume = 4πr3/3. The constant π is specified as Math.PI in Java.)
5. Write a method named maxOfTwo that accepts two int parameters, named num1 and num2, and returns the larger of the two.
Write a method named sumRange that accepts two int parameters, named lower and upper, that represent a range. The method returns 0 if the second parameter is less than the first. Otherwise, the method returns the sum of all integers in the range (both inclusive). For example, if lower and upper have values 1 and 5 respectively, the method computes 1 + 2 + 3 + 4 + 5 and returns 15.  
6. Write a method called isLarger that accepts two floating point parameters, named num1 and num2 (of type double) and return true if the first parameter is greater than the second; and false otherwise.
Write a method called isAlpha that accepts a char parameter named ch and returns true if that character is either an uppercase or lowercase alphabetic character.
Write a method called alarm that prints the string Alarm! multiple times on separate lines. The method accepts one int parameter named n that specifies the number of times the string is printed. Print an error message if the parameter is less than 0. The method does not return any value.   

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

1. Assume s1, s2, and s3 are String variables initialized to "Amanda", "Bobby", and "Chris", respectively. Which String variable or variables are changed by each of the following statements. If none, say so.
System.out.println (s1);             No string is changed.
s1 = s3.toLowerCase (); s1 is changed.
System.out.println (s2.replace ('B', 'M')); No string is changed.
s3 = s2.concat (s1); s3 is changed.
What output is produced by the following code fragment?
String s1 = "Foundations";
String s2;
System.out.println (s1.charAt (1)); F
s2 = s1.substring (0, 5);
System.out.println (s2); Found
System.out.println (s1.length ()); 11...

By purchasing this solution you'll be able to access the following files:
Solution.docx.

50% discount

Hours
Minutes
Seconds
$35.00 $17.50
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats