Creating a switch-case statement

Assignment Help JAVA Programming
Reference no: EM131588933

EXERCISE 6- I

Creating a switch-case Statement
Try creating a switch statement using a char value as the case, Include a default behavior if none of the char values match.
Creating Loops Constructs (OCA Objectives 5. I , 5.2, 5.3, 5.4, and 5.5) 3 2 I
 Make sure a char variable is declared before the switch statement.
 Each case statement should be followed by a break.
 The default case can be located at the end, middle, or top.

EXERCISE 6-2

Creating a Labeled while Loop
Try creating a labeled while loop. Make the label outer and provide a condition to check whether a variable age is lcss than or equal to 21, Within the loop, increment age by 1, Every time the program goes through the loop, check whether age is 16. If it is, print the message 'get your driver's license' and continue to the outer loop. If
not, print 'Another year."
 The outer label should appear just before the whil e loop begins.
 Make sure age is declared outside of the while loop,

EXERCISE 6-3

Propagating and Catching an Exception
In this exercise you're going to create two methods that deal with exceptions. One of the methods is the main ( ) method, which will call another method. If an exception is thrown in the other method, main c) must deal with it, A final ly statement will be included to indicate that the program has completed. The method that main ( will call will be named reverse, and it will reverse the order of the characters in a string. If the string contains no characters, reverse will propagate an exception up to the math ( ) method,

1. Create a class called Prepagat e and a main ( ) method, which will remain empty for now.

2. Create a method called reverse. It Lakes an argument of a String and returns a string.

3. In reverse, check whether the string has a length of 0 by using the string. length () method, If the length is 0, the reverse method will throw an exception,

4. Now include the code to reverse the order of the string, Because this isn't the main topic of this chapter, the reversal code has been provided, but feel free to try it on your own,
String reverseStr = mm;
for(int i=s,length()-1;i>=0;--i)
reverseStr += s.charAt(i);
return reverseStri

5. Now in the main ( ) method you will attempt to call this method and deal with any potential exceptions. Additionally, you will include a finally statement that displays when main ( ) has finished.

EXERCISE 6-4

Creating an Exception
In this exercise we attempt to create a custom exception, We won't put in any new methods (it will have only those inherited from Except ion), and because it extends Exception, the compiler considers it a checked exception. The goal of the program is to determine whether a command-line argument representing a particular food (as a string) is considered bad or okay.

1. Let's first create our exception. We will call it BadFoodExcept ion. This exception will be thrown when a bad food is encountered.

2. Create an enclosing class called MyException and a main ( ) method, which will remain empty for now.

3. Create a method called checkFood ) , It takes a String argument and throws our exception if it doesn't like the food it was given. Otherwise, it tells us it likes the food, You can add any foods you aren't particularly fond of to the list,

4. Now in the main ( ) method, you'll get the command-line argument out of the string array and then pass that string on to the check Food method. Because it's a checked exception, the check Food U method must declare it, and the main method must handle it (using a t ry/catch). Do not have main ( ) declare the exception, because if main ( } ducks the exception, who else is back there to catch it? (Actually, main ( ) can legally declare excep¬tions, but don't do that in this exercise.)
As nifty as exception handling is, it's still up to the developer to make proper use of it. Exception handling makes organizing our code and signaling problems easy, but the exception handlers still have to be written. You'll find that even the most complex situations can be handled, and your code will be reusable, readable, and maintainable.

Reference no: EM131588933

Questions Cloud

Identify the various ways java manages streams : Define I/O and identify the various ways Java manages streams -
What is the identical price p that all firms will charge : Also suppose the short run cost functions are the same as the long run cost functions. If the market price P is $10, what is the output level of each firm?
Why models are important for understanding organized crime : I need help writing a 900-word compare and contrast essay that compare distinctions between bureaucratic and patron-client organizations.
Location through the gps feature : "My employer monitors my e-mail and internet usage at work and tracks my location through the GPS feature of my cell phone.
Creating a switch-case statement : Creating a switch-case Statement - Propagating and Catching an Exception - Create a method called reverse - Create an enclosing class called MyException
Review the basic concepts of epidemiology : This will provide a framework for characterizing the population-level risks associated with exposures resulting from the disaster.
How relevant is the information to today : How relevant is the information to today? How similar or different are the ideas presented in the articles? How would you apply the information in your career?
Who is the governing authority for licensing attorneys : Who is the governing authority for licensing and monitoring attorneys in your area?
Analyzing a real world market : Write an essay on the potential managerial problems of analyzing a real world market while only considering the nature of said market to be purely competitive.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Compare the average completion times of all jobs

Compare the average completion times of all jobs for each scheduling algorithm. Output the details of each algorithm's execution. You need to show which jobs are selected at what times as well as their starting and stopping burst values. You can choo..

  Implement your pseudocode into actual working java code

Write a pseudocode for method and Implement your pseudocode into actual working java code.

  Java program should be divided in at least 3 classes

This Java program should be divided in at least 3 classes like Display.java, Input.java and ErrorHandling.java

  Identify the primary key for each entity with an asterisk

Identify the entities and attributes hidden in the Excel spread sheet - Identify each column on the spread sheet as a potential attribute

  Computer science and information system

In the College of Computer Science and Information System, forty students were asked to rate the quality of teaching of his Course Instructor in the class room environment on a scale of 1 to 10 (1 means minimum standard of teaching and 10 means ex..

  Create a temperature sensor simulator

create a temperature sensor simulator. First, display a menu that will ask the user what season to simulate (1) winter (2) spring (3) summer (4) fall or (5) to exit. Once the use selects the season your program should ask the user how many simulation..

  Write down a java program which reads a text file called

write a java program which reads a text file called mydata.txt containing the following numbers 23 18 78 64 28 39 92 93

  Prepare a functional java program

prepare a functional java program that I can run in my compiler (Netbeans) that allows you to control a running back with the arrow keys and avoid opposing play

  Create a project using the classes in the doc sharing area

create a project using the classes in the Doc Sharing area labeled A Simple LinkedList class. Compile it, run it, and review the code that is given carefully. This code tests the LinkedList class provided in the lecture.

  Which correctly creates an array of five empty strings

Which correctly creates an array of five empty Strings? Which of the following statements is preferred to create a string "Welcome to Java"?

  Create application of your choice utilizing swing components

Create an application of your own choice utilizing the Swing Components. Example: You can make an application of your own choice showing the values of cars depending upon the models or years chosen.

  Develop a solution for bookstore inventory management system

Develop an object oriented solution for Bookstore Inventory Management System. Identify the necessary objects and actions that are required to produce your solutions.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd