1.
Given the following multi-way if statement, provide a switch
statement, using proper java syntax, that will provide the same
function.
Char grade;
String tstmsg;
if (grade == ‘A’)
{
tstmsg = “Excellent”;
}
else if (grade == ‘B’)
{
tstmsg = “Good”;
}
else if (grade == ‘C’)
{
tstmsg = “OK”;
}
else
{
tstmsg = “Study More”;
}
2.Write the following for statement as a while statement.
for (k = 0; k < 3; k++)
{
System.out.println (“Hello”);
}
3.Given the following method, you will find 3 coding errors.
Identify the errors and provide the corrected code.
public void theTestMethod(passedValue)
{
int theNumber;
theNumber = passedValue
theNumber = theNumber + 1;
return theNumber;
}
4.Using tstArray from the question above, provide the proper syntax required to extract the third character of the array and assign it to charItem
5.If you were to use a subscript variable to access your array, what data type must it be?
6.When coding a program that uses an input file, how would you create a new scanner object as on input file? The name of the input text file is critterinventory.txt.
7.When processing through an input file, what method would you use to determine if there are more records to be read from the input file? Assume the name of the Scanner object is inFile.
8.Given the following code, what is the value of a after the
statement is executed?
double b = 6.1;
int a = (int) b;
PLEASE ANSWER ALL
Answer 1:
switch(grade){
case 'A': tstmsg="Excellent";break;
case 'B': tstmsg="Good";break;
case 'C': tstmsg="Ok";break;
default: tstmsg="Study More";
}
Answer 2:
int k=0;
while(k<3){
System.out.println (“Hello”);
k++;
}
Answer 3:
// parameter type is missing
// we are returning the value so type must be int
public int theTestMethod(int passedValue)
{
int theNumber;
theNumber = passedValue; // semicolon missed here
theNumber = theNumber + 1;
return theNumber;
}
Answer 4:
charItem=tstArray[2]; // 3rd char will be stored at index 2
As per policy we can answer 1 question per post. Please post the
remianing questions as separate post.Thanks
Get Answers For Free
Most questions answered within 1 hours.