Java supports Logical Operators. These operators can be demonstrated by using Boolean statements generated by using the truth tables.
The purpose is to “conjunct” two Boolean statements. Using logical AND will require that two statements have to be true to generate a true statement. If one of the statements is false, the resulting statement will be false.
// image
The purpose is to “disjunction” two Boolean statements. Using logical OR will require that at least one statement be true to generate a true statement. If both statements are false, the resulting statement will be false.
// image
The purpose of this statement is to flip the value of the Boolean statement to its opposite value. For example, if the statement is true, “not” true results in false, and “not” false results in true.
The switch case allows us to perform decisions based on the current "state" of a variable. These variables can have the form of int, char, byte, and String.
Example
Re-do problem DecisionExample06.java requesting the user to enter [D], [R], or [I]. Ensure the program uses the switch only with the allowed data types mentioned in class (i.e., int, char, and byte).
Let us first request information from the user. Notice that the variable option will contain the user’s input.
System.out.println("Please enter party [D], [R], [I]");
char option = input.nextLine().charAt(0);
switch( option )
{
case 'D':
System.out.println("you vote Democrat");
case 'R':
System.out.println("you vote Republican");
case 'I':
System.out.println("you vote Independent");
}
}
The switch will hold the variable option that will control the flow of the decision-making. Notice that there are three possible cases: 'D’, ‘R’, or ‘I’. If we test this program as follows:
Please enter party [D], [R], [I]
[I]
You vote Independent
However, if we enter:
Please enter party [D], [R], [I]
[D]
you vote Democrat
you vote Republican
you vote Independent
The switch case allows to determine when a case is satisfied, then executes the case’s scope. However, once the case is satisfied, the program must leave the case immediately, or it will go through all the other cases. Therefore, we need to “break" the flow of decisions. We use the break keyword for this. The new code looks as follows:
case 'D':
System.out.println("you vote Democrat");
break;
case 'R':
System.out.println("you vote Republican");
break;
case 'I':
System.out.println("you vote Independent");
break;
What would happen if the user enters an input that does not belong to D, R, or I? We must address this situation the same way we did with the if-else statement. In the switch case context, an else is called the default. The default keyword permits addressing the remaining cases that are not listed. Typically, it is used to address error messages or feedback to the computer user.
Another situation that we need to address is in case the user provides you with upper and lower case input. In the switch-case, we can list similar cases together by stacking them together. Here is the solution of incorporating the default and the multiple cases
switch( option )
{
case 'D':
case 'd':
System.out.println("you vote Democrat");
break;
case 'R':
case 'r':
System.out.println("you vote Republican");
break;
case 'I':
case 'i':
System.out.println("you vote Independent");
break;
default:
System.out.println("Incorrect Input");
}
What is wrong with this statement?
char option = input.toUpperCase().charAt(0);
Example
Problem 13:
Scanner input = new Scanner(System.in);
System.out.println("Please enter selection [A], [B], [C]");
char packageOption = input.nextLine().charAt(0);
System.out.println("Please enter total of minutes");
int minutes = input.nextInt();
switch( packageOption )
{
case: 'A':
case: 'a':
{
}
case: 'B':
case: 'b':
{
}
case: 'C':
case: 'c':
{
}
default:
System.out.println("Invalid Package Input");
}
if(minutes <= 450)
{
System.out.println("Total bill amount is: $39.99");
}
else
{
int additional = minutes - 450;
double total = (additional * 0.45)+ 39.99;
//double subtotal = additional * 0.45;
//subtotal += 39.99; // same as: subtotal = subtotal + 39.99;
System.out.println("Total bill amount is: $"+total);
}
AP CS A
[Comparing String Objects]
CON-1.H Compare object references using Boolean expressions in program code.
CS1
[Comparing String Objects]
130.421.c.6.k Compare objects using reference values and a comparison routine;
Design, write, test, and debug a program that effectively uses the different structured data types provided in the language like strings, arrays/lists, dictionaries, sets
Write a program that uses some language-provided libraries and frameworks (where applicable).
Read a given program, and explain what it does.
Trace the flow of control during the execution of a program (both correct and incorrect).
Use appropriate terminology to identity elements of a program (e.g., identifier, operator, operand)
Strings and string processing
Write programs that work with text by using string processing capabilities provided by the language.
Explain the importance of algorithms in the problem-solving process.