The first iteration of our rock-paper-scissors application worked well, but the code certainly left some opportunities forrefactoring, especially in terms of using Java conditional statements effectively.
It's often asserted that Java has an additional else...if statement. That isn't technically true.
The Java language provides three constructs for performing conditional logic:
It's often asserted that Java has an additional else...if statement, which isn't technically true. You can nest if statements inside of an else block, and the code to do so puts the else and if statements right next to each other, but that's just happenstance. The else...if statement isn't a special Java construct in itself.
The syntax for an if…else statement looks like this:
if (boolean condition) {
} else {
}
The first code block executes if theboolean condition is true, and the second code block executes if the boolean condition is false. Here's how the rock-paper-scissors (orroshambo) application would look if we introduced the else keyword into the mix.
import javax.swing.*;
public class Roshambo {
public static void main(String args[]) {
String prompt = "Will it be rock, paper or scissors?";
String clientGesture = null;
String serverGesture = "rock";
String outcome = "error";
clientGesture = JOptionPane.showInputDialog(prompt);
if (clientGesture.equalsIgnoreCase("rock")) {
outcome = "tie";
} else if (clientGesture.equalsIgnoreCase("scissors")) {
outcome = "loss";
} else if (clientGesture.equalsIgnoreCase("paper")) {
outcome = "win";
}
JOptionPane.showMessageDialog(null, outcome);
}
}
When the code is run, the UI looks exactly the same as before, although the implementation is now much cleaner and more efficient (Figure 1).
Figure 1. Swing-based components providing a UI for the roshambo application.
Another of the Java conditional statements, the switch statement, can be useful when a variety of different conditions might be true based on the value of a single char, integer or a String. With the roshambo application, theclientGesture String can take on the values of rock, paper or scissors, and depending upon which is selected, the variable namedoutcome is assigned a different value. This is a perfect scenario for using a switch statement. Replacing the if-based conditional logic in the current application with a switch would look like this:
import javax.swing.*;
public class Roshambo {
public static void main(String args[]) {
String prompt = "Will it be rock, paper or scissors?";
String clientGesture = null;
String serverGesture = "rock";
String outcome = "error";
clientGesture = JOptionPane.showInputDialog(prompt);
switch (clientGesture) {
case "rock":
outcome = "tie";
break;
case "paper":
outcome= "win";
break;
case "scissors":
outcome = "loss";
break;
default:
outcome="error";
}
JOptionPane.showMessageDialog(null, outcome);
}
}
A few things to know about the switch statement:
In the grand scheme of things, all computer programs come down to three key processes:
As mentioned earlier, only three constructs in Java pertain to performing conditional logic: the if, else and switch statements. Once you have mastered them, you have mastered the art of using Java conditional statements.
Which software development language do you think compliments Java the most?Let us know.
For those interested in learning Java, or for those who know Java and are interested in leveraging those skills in order to learn a complimentary language like JavaScript or Ruby, TheServerSide is providing a number of problem-driven tutorials that will help you master the fundamentals of these languages.
The catalog of tutorials currently includes the following:
An ADR is only as good as the record quality. Follow these best practices to establish a dependable ADR creation and maintenance ...
At some point, all developers must decide whether to refactor code or rewrite it. Base this choice on factors such as ...
API proxies and gateways help APIs talk to applications, but it can be tricky to understand vendor language around different ...
A principal engineer says Postman's Spec Hub will help the company shift to a spec-first API development process for its ...
AWS Kiro, a project developed by a 'small, opinionated team within AWS,' prioritizes spec-driven development in the workflows of ...
Docker is expanding the Docker Compose spec to accommodate AI agents in an effort to bring AI development closer to existing ...
AI is transforming PaaS with automation and cost-efficient features, but will it eventually replace cloud platforms? Industry ...
Even though Q-Day might be several years away, enterprises should develop a strategic plan to prepare for the future. Experts ...
Businesses can find security vulnerabilities when they push their workloads to the edge. Discover the pitfalls of cloud edge ...
Runtime security and tokenization stand to play a bigger role in attack surface management, a development that could influence ...
Check out the latest security news from the Informa TechTarget team.
How CISOs design and build their security teams is as important as the technology they select to safeguard their organizations' ...
Compare Datadog vs. New Relic capabilities including alerts, log management, incident management and more. Learn which tool is ...
Many organizations struggle to manage their vast collection of AWS accounts, but Control Tower can help. The service automates ...
There are several important variables within the Amazon EKS pricing model. Dig into the numbers to ensure you deploy the service ...