Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

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

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 ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

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 - java program using array of objectsobjectives

Assignment - 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 Menu ...

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

Part a specification - robot simulationpart a

PART A Specification - Robot Simulation PART A Requirements To complete this assignment you will use the supplied eclipse project Robot P1/. It is already set up to execute a simple arm movement loop which you will build ...

Can someone help me please with those question1what is the

Can someone help me please with those question 1:what is the best data type for student id datatime,currency,number,decimal 2:which relationshipis preferable? one to one,one to many,many to many 3:if you add table A's pr ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

  • 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