Ask Java Expert


Home >> Java

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.

Java, Programming

  • Category:- Java
  • Reference No.:- M92397647
  • Price:- $25

Priced at Now at $25, Verified Solution

Have any Question?


Related Questions in Java

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As