Lab Sheet 2
Lab Sheet 2
2. Write a java program to set a string (firstName) to your first name and another string
(lastName) to your last name. Declare a variable (age) of type integer and assign your
age into that variable. Declare a char variable and initialize it by the first letter of your
name.
Print out all the variables and objects.
3. Write a java program to set a string (Greeting) to the string “Hello there”, and print
out the following:
a. The length of the string.
b. The string in lower case.
c. The string in upper case.
d. The substring from position 2.
e. The substring from position 2 to 7.
f. The comparison result with string Greet=”Hello There”
11. Suppose sam is an object of a class named Person and suppose increaseAge is a
method for the class Person that takes one argument that is an integer. How do you
write an invocation of the method increaseAge using sam as the calling object and
using the argument 10 ?
12. The following code is supposed to output the string in lowercase letters but it
has an error. What is wrong?
String test = "WHY ARE YOU SHOUTING?";
test.toLowerCase();
System.out.println(test);
13. Given the following fragment that purports to convert from degrees Celsius to degrees
Fahrenheit, answer the following questions:
double celsius = 20;
double fahrenheit;
fahrenheit = (9 / 5) * celsius + 32.0;