Well I'm getting into Java so I decided to post some of my work here. They're usually small applications such as a basic calculator, but as time progresses they will become more advanced and use multiple classes and methods.
Basic Calculator
Asks the user for two numbers and stores them in the variables num1 and num2. Then it adds the two and displays the answer.
import java.util.Scanner;
public class Think{
public static void main(String[] args){
Scanner userInput = new Scanner(System.in);
double num1, num2;
System.out.print("Please enter the first number: ");
num1 = userInput.nextInt();
System.out.println();
System.out.print("Please enter the second number: ");
num2 = userInput.nextInt();
double userAnswer = num1 + num2;
System.out.println("The answer is " + userAnswer);
}
}
Angle Identifier
Asks the user to input a measurement of an angle in degrees. Then it will take that measurement and classify it as acute, obtuse, straight or right.
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner userInput = new Scanner(System.in);
double angleDegree;
String angleType = "Please enter a degree.";
System.out.print("Please enter the degree of the angle: ");
angleDegree = userInput.nextDouble();
System.out.println();
if (angleDegree == 90)
angleType = "Right";
else if (angleDegree > 90)
angleType = "Obtuse";
else if (angleDegree < 90)
angleType = "Acute";
else if(angleDegree == 180)
angleType = "Straight";
System.out.println("The angle is " + angleType);
}
}
Switch Statement Tester
Asks the user to input 1, 2 or 3. Then it will output a line depending on what they entered. The switch statement is basically an alternative to the if-statement.
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner userInput = new Scanner(System.in);
int input;
System.out.print("Please enter 1, 2, or 3: ");
input = userInput.nextInt();
System.out.println();
switch(input){
case 1:
System.out.println("You entered one!");
break;
case 2:
System.out.println("You entered two!");
break;
case 3:
System.out.println("You entered three!");
break;
default:
System.out.println("Please enter 1, 2 or 3!");
break;
}
}
}
Craps Game
You roll two dice. Each die has six faces, which contain one, two, three, four, five and six spots, respectively. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on the first throw, you win. If the sum is 2, 3 or 12 on the first throw (called “craps”), you lose (i.e., the “house” wins). If the sum is 4, 5, 6, 8, 9 or 10 on the first throw, that sum becomes your “point.” To win, you must continue rolling the dice until you “make your point” (i.e., roll that same point value). You lose by rolling a 7 before making your point.
import java.util.Random;
public class Craps{
private static final Random diceRoll = new Random();
private enum GameStatus{ CONTINUE, WON, LOST };
public static void main(String args[]){
int playerPoint = 0;
GameStatus gameStatus;
int sumOfDice = rollDice();
switch (sumOfDice){
case 7:
case 11:
gameStatus = GameStatus.WON;
break;
case 2:
case 3:
case 12:
gameStatus = GameStatus.LOST;
break;
default:
gameStatus = GameStatus.CONTINUE;
playerPoint = sumOfDice;
System.out.printf("Roll again. Your current point is: %d\n", playerPoint);
break;
}
while (gameStatus == GameStatus.CONTINUE){
sumOfDice = rollDice();
if (sumOfDice == playerPoint)
gameStatus = GameStatus.WON;
else if (sumOfDice == 7)
gameStatus = GameStatus.LOST;
if (gameStatus == GameStatus.WON)
System.out.println("Player has won! :D");
else if (gameStatus == GameStatus.LOST)
System.out.println("Player has lost. :(");
}
}
public static int rollDice(){
int rollOne = 1 + diceRoll.nextInt(6);
int rollTwo = 1 + diceRoll.nextInt(6);
int sum = rollOne + rollTwo;
System.out.printf("You rolled a %d and %d with the sum of %d.\n", rollOne, rollTwo, sum);
return sum;
}
}