In: Computer Science
Q1# Consider the following string:
String hello = "Hi girls! hope your assignment1 is very
easy.";
a. What is the value displayed by the expression
hello.length()?
b. What is the value returned by the method call
hello.charAt(12)?
c. Write an expression that refers to the letter p in the string
referred to by hello
Q2# Modify the program to use a "while-do" loop instead of "for"
loop.
int sum = 0;
int number = lowerbound; // declare and init loop index
variable
while (number <= upperbound) { // test
sum += number;
++number; // update
}
1.
a)
String length is :45 that means there are 45 charecters in the string
b)
charecter at 12 index position is "p"
c)
Index of the letter p is 12
hello.indexOf("p");
This is the expresion which used to find the charecter at 12 th index position
2.
Using for loop
int sum = 0;
for( int number=lowerbound;number<=upperbound;number++)
{
sum+=number;
}
using do while loop
int sum = 0;
int number =lowerbound; // declare
and init loop index variable
do
{
if(number <=
upperbound)
{
sum += number;
++number;
}
}
while(number <=
upperbound);
}