Java Program Given the declaration String s = “letterhead”; and using the String
ID: 3875091 • Letter: J
Question
Java Program
Given the declaration String s = “letterhead”; and using the String class
1. Write the expression to find index of the character ‘h’.
2. Write the expression to retrieve the character ‘r’ from the String.
3. Write the expression to get the substring “ette”.
4. Write the expression to find the index location of the last 'e'.
Using the Math class
1. Write the expression to calculate e1.7
2. Write the expression to find the larger of two values x and y
3. Write the expression to generate a random number in the range [0.0, 1.0)
4. Write the expression to generate a random number in the range [0.0, 20.0) (hint: this won’t be a new method, but will require some math after you make the call).
5. Write the expression to generate a random number in the range [5.0, 6.0) (same hint as before)
6. Write the expression to generate a random number in the range [10.0, 15.0) (think about combining the 2 steps from 4 and 5)
7. Write the expression to generate a random integer in the range [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
More searching through the API
1. Write a statement to instantiate a Point object with coordinates (3, 4) and binding it to an appropriate reference variable.
2. What package do you need to import to use a Point object?
3. What are the coordinates of a Point object if you don't specify any?
Explanation / Answer
we will use the string built-in method to find the expressions
1. s.indexOf('h');
ans will be 6
2. s.CharAt(5)
charAt method retruns the charachter at given index.
3. s.substring(1,5)
substring method returns the substring between the start and end index given as parameters.
4.s.lastIndexOf('t').
this method returns last index position for the given char value.
-------------------------------------------------------------------------------------------------------------------------------------------------
we will be using the built-in Math class for this
1. Math.exp(1.7) -> 5.4739473917272
exp(double a) method simply returns the value e a.
2. Math.max(x,y)
Math.max() method returns the maximum number of the given two numbers.
3. Math.random()
This method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
4. Math.random() * 20 .
0-1 * 20 = 0 - 20.
5. Math.random() + 5
0-1 + 5 = 5-6
6. Math.random() * 5 + 10.
7. 1 + (int)(Math.random() * 9)
(int)(Math.random() * 9) will generate the integers from 0 - 9 so after adding 1 it will become 1 -10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.