Java Reference
In-Depth Information
reads: “Which method stubs would you like to create?” You may choose to check the box next to
"public static void main(String[] args)"
to automatically create a
main
method.
3.
You should automatically have the basis for the class body shown here:
public class SwitchClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
If not, you can type it yourself. You do not need the comments, which are denoted with
/**
or
//
. In Eclipse, they will appear as blue or green text. Comments are useful for explaining what
the code is doing, but are never compiled or executed by Java.
public class SwitchClass {
public static void main(String[] args){
}
}
4.
Recall that the
main
method provides the starting point and ordering for the execution of your pro-
gram. A
switch
statement requires a variable to be initialized before the statement so that it can be
evaluated for each of the cases. Initialize a string with the following statement:
String loanType = "Commercial";
It should be placed after
(String[] args){
and before the next
}
.
5.
In this example, you will use the value of the string to determine how to set the value of a double
variable. In the next line, declare a double variable
interestRate
. It does not need its value initial-
ized here.
double interestRate;
6.
Now add the
switch
statement. Remember to use the
loanType
variable as the conditional
expression.
switch(loanType){
}
7.
Now add your cases inside the
switch
statement.
case "Residential":
interestRate = 0.055;
break;
case "Commercial":
interestRate = 0.062;
Search WWH ::
Custom Search