Question

1. Given the following multi-way if statement, provide a switch statement, using proper java syntax, that...

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

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice)....
Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice). This program will read in three integers with Scanner, put the values in an array of int, and then print the product of the three values. Example output of the program is shown below, with user input shown in bold: Enter first integer: 3 Enter second integer: 4 Enter third integer: 5 Product: 60 More details about the method you need to write are...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
Revise SimpleLinkedListTest Revise the file SimpleLinkedListTest.java given in the lecture in the following ways: Create and...
Revise SimpleLinkedListTest Revise the file SimpleLinkedListTest.java given in the lecture in the following ways: Create and populate a single linked list int values Use this list to test each method you implement in the following ways o Haveatleast1testcaseforsuccessfulcallingthetestedmethod o Haveatleast1testcaseforunsuccessfulcallingthetestedmethod.Forexamplecallingthemethod public int get(int index) and passing an invalid index. NOTE: since the code here is mainly for testing purpose, you do NOT HAVE to use the user input when populating the list and testing your methods. ---------CODE SImpleLinkedListTest------------ package 1234;...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the code to be written with these) I need Class river, Class CTRiver and Class Driver with comments so I can learn and better understand the code I also need a UML Diagram HELP Please! Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong()...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT