help with switch statements
From: acerpower (Dhanesh81_at_intnet.mu)
Date: 06/29/04
- Next message: Mike Crenshaw: "newPixels vs newPixels(x,y,w,h);"
- Previous message: FISH: "Re: Can't read file when called from Applet?"
- Next in thread: Andrew Thompson: "Re: help with switch statements"
- Reply: Andrew Thompson: "Re: help with switch statements"
- Reply: Roedy Green: "Re: help with switch statements"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 29 Jun 2004 08:39:21 +0400
Hi,
I am new to java programming and also to this newsgroup ,. I am actually
following Java courses now,
I had a few homeworks which Im unable to do ,
hoping to get some help from you ,
with thanks
Vimal
Lab 4.5.6 Switch Statements
Estimated Time: 25 minutes
Learning Objective:
. In this lab activity, the student will include the use of the Java
language control structure switch in the
method definitions.
Description / Scenario:
. switch statements are also known as branching statements. They are a
special kind of selection
control that allows for more than two choices when the condition is
evaluated. In the switch statement
the expression must be assignment compatible with an int type. The keyword
break ends the
sequence of actions and exits from this control structure.
. The switch statement syntax is as follows.
switch (expr1) {
case constant1:
statements;
break;
case constant2:
statements;
break;
default:
statements;
break;
}
. There are several keywords and symbols that must be used in this control
structure.
The keyword switch begins the selection. The braces follow the expression.
The
keyword case tests if the condition is equal to the constant. The colon (:)
at the end of
this statement is required. The keyword break ends the sequence of actions
and exits
from this control structure. The keyword default specifies the set of
actions that will be
executed if no match was found between the expression and each constant.
. Build a class Vowel. This class will have a main method with a single char
variable, and
implement program decisions. Its function will be to use the input from args
to test if its
first character is a vowel. If it is not a vowel, a message will be
displayed using a
print() method. This lab will also have students go back to the API and look
up one of
the String class methods charAt(). This lab demonstrates several new Java
features.
File Management:
Open BlueJ. Click on Project from the BlueJ main menu and select New. Select
c:/ in the
New Project window and in the Look in: list box. Double click the javacourse
folder listed
in the text window and a different New Project window will open with
javacourse in the
Look in: list box. Double click the chap4 folder listed in the text window
and a different
2 - 2 Fundamentals of Java Programming - Lab 4.5.6 Copyright . 2003, Cisco
Systems, Inc.
New Project window will open with chap4 in the Look in: list box. Type
lab4.5.6 in the File
name text box and click on Create to create a lab4.5.6 subfolder in the
chap4 folder.
Preparation:
. Open BlueJ
Tasks:
Step 1 Create Vowel class
Create the Vowel class, including a main method.
Step 2 Look up charAt() method
Go to the API and in the String class look up the charAt() method. This will
be used in
the form args[0].charAt(0). Why use args[0]? What is the
value of args[0].charAt(0)? What is the value of
args[0].charAt(int index) if the int index is 1 or 2?
Step 3 Assign character value
Assign the resulting character value to a local variable of type char.
Compare that value
to each of the vowels in the alphabet. Implement this class using the switch
statement.
Step 4 Print message
If the input character is not a vowel, print out a message telling the user.
What is the impact on your output if you do not use break statements in your
program,
and the input character is a vowel?
Step 5 Add comments/Run class
Be sure to add a complete set of comments. Compile, run and test the new
class.
- Next message: Mike Crenshaw: "newPixels vs newPixels(x,y,w,h);"
- Previous message: FISH: "Re: Can't read file when called from Applet?"
- Next in thread: Andrew Thompson: "Re: help with switch statements"
- Reply: Andrew Thompson: "Re: help with switch statements"
- Reply: Roedy Green: "Re: help with switch statements"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]