Bellow are some simple Java programs for beginners, they deal with boolean statements such as if else and switch case
Write a program that asks the user their age and then tells her who is older, them or you (the programmer).
[THINKING]:
how mamy pieces of information are there in this problem
how many things (statements) are controlled in this problem – what are they
import javax.swing.JOptionPane;
public class older {
public static void main(String[] args) {
final int AGE = 18; //programmers age
int userAge;
String userName;
String inputString;
userName = JOptionPane.showInputDialog("Hello there, whats your name?");
inputString = JOptionPane.showInputDialog("Whats your age?");
userAge = Integer.parseInt(inputString);
if (userAge == AGE) {
System.out.println(userName + ", you are same age as me!");
}
else if (userAge < AGE) {
System.out.println(userName + ", you are younger than me!");
}
else { //userAge > AGE
System.out.println(userName + ", you are older than me, grandpa!");
}
System.exit(0);
}
}
It’s a gamble [pun] but let’s try to model a slot machine.
There are five prizes:
1 – $100,000,000.00
2 – $750,000.00
3 – $90,000.00
4 – $1,000.00
5 – $101.01
Since we can’t really spin anything, we’ll have to model this in a very simple way.
This will have to be the model of a floor manager going with a winner to the cashier and telling the cashier how much to give.
We’ll model the cashier giving out money by just writing the amount on the screen.
The floor manager uses the numbers 1 through 5 as shown above to tell the cashier the prize winnings.
OK it’s not really a slot machine!
Two steps:
1. get a prize number from the guard (should we think about validating this? – Yes, but we’ll do that later)
2. display the prize
What should happen when there is no wnner?
(nothing)
You might consider saying something but that really isn’t part of the job of this program.
What about saying something like – that is not a valid entry?
Definitely not part of this program’s job.
We will eventually do “input validity checking” but as a separate step that would be completed before getting this part of the code.
Constraints – use an if form for this problem
In a later program, we’ll use the switch form.
Testing
You must always thoroughly test you code.
How many tests should you make in this program to completely test it?
import javax.swing.JOptionPane;
public class slot {
public static void main(String[] args) {
final int PRIZE_1 = 1;
final int PRIZE_2 = 2;
final int PRIZE_3 = 3;
final int PRIZE_4 = 4;
final int PRIZE_5 = 5;
int userNum;
String userName;
String inputString;
userName = JOptionPane.showInputDialog("Hello there, whats your name?");
inputString = JOptionPane.showInputDialog(userName + " please pick a number 1 through 5");
userNum = Integer.parseInt(inputString);
if (userNum == PRIZE_1) {
System.out.println(userName + ", you have won the 1st prize of $100,000,000.00");
}
else if (userNum == PRIZE_2) {
System.out.println(userName + ", you have won the 2nd prize of $750,000.00");
}
else if (userNum == PRIZE_3) {
System.out.println(userName + ", you have won the 3rd prize of $90,000.00");
}
else if (userNum == PRIZE_4) {
System.out.println(userName + ", you have won the 4th prize of $1,000.00");
}
else if (userNum == PRIZE_5) {
System.out.println(userName + ", you have won the 5th prize of $101.01");
}
else {
System.out.println(userName + ", you have won nothing, sorry bye bye :)");
}
System.exit(0);
}
}
Everyone’s a WINNER!!!
Here’s how to win this lottery:
The user picks a number.
If it’s 7, they win the grand prize
If it’s a 3 or an 8, they win the second prize
If it’s a 4, a 9, or a 1, they win the third prize
If they win none of these, they get a consolation prize
Just write some words on the screen to indicate which prize was won.
import javax.swing.JOptionPane;
public class prize {
public static void main(String[] args) {
final int GRAND = 7;
final int SECOND_1 = 3;
final int SECOND_2 = 8;
final int THIRD_1 = 4;
final int THIRD_2 = 9;
final int THIRD_3 = 1;
int userNum;
String userName;
String inputString;
userName = JOptionPane.showInputDialog("Hello there, whats your name?");
inputString = JOptionPane.showInputDialog("please pick a number?");
userNum = Integer.parseInt(inputString);
if (userNum == GRAND) {
System.out.println(userName + ", you have won the grand prize!");
}
else if (userNum == SECOND_1 || userNum == SECOND_2) {
System.out.println(userName + ", you have won the second prize!");
}
else if (userNum == THIRD_1 || userNum == THIRD_2 || userNum == THIRD_3) {
System.out.println(userName + ", you have won the third prize!");
}
else {
System.out.println(userName + ", you have won the consolation prize of a ballon!");
}
System.exit(0);
}
}
same as above only using switches
import javax.swing.JOptionPane;
public class switchWins {
public static void main(String[] args) {
final int GRAND = 7;
final int SECOND_1 = 3;
final int SECOND_2 = 8;
final int THIRD_1 = 4;
final int THIRD_2 = 9;
final int THIRD_3 = 1;
int userNum;
String userName;
String inputString;
userName = JOptionPane.showInputDialog("Hello there, whats your name?");
inputString = JOptionPane.showInputDialog(userName + " please pick a number 1 through 5");
userNum = Integer.parseInt(inputString);
switch(userNum)
{
case GRAND:
System.out.println(userName + ", you have won the 1st prize of $100,000,000.00");
break;
case SECOND_1:
System.out.println(userName + ", you have won the 2nd prize of $750,000.00");
break;
case SECOND_2:
System.out.println(userName + ", you have won the 2nd prize of $750,000.00");
break;
case THIRD_1:
System.out.println(userName + ", you have won the 2nd prize of $750,000.00");
break;
case THIRD_2:
System.out.println(userName + ", you have won the 2nd prize of $750,000.00");
break;
case THIRD_3:
System.out.println(userName + ", you have won the 2nd prize of $750,000.00");
break;
default:
System.out.println(userName + ", you have won the 2nd prize of $750,000.00");
break;
}
System.exit(0);
}
}