import java.util.Scanner;
System.out.println("HELLO WORLD!");
Scanner scanner = new Scanner(System.in);
System.out.println("What is your name?");
String name = scanner.nextLine();
System.out.println("Hello " + name +"!!!");
HELLO WORLD!
What is your name?
Hello Kaiden!!!
Java Hello Hacks
Build your own Jupyter Notebook meeting these College Board and CTE competencies. It is critical to understand Static versus Instance Now, this is College Board requirement!!!
Explain Anatomy of a Class in comments of program (Diagram key parts of the class). Comment in code where there is a definition of a Class and an instance of a Class (ie object) Comment in code where there are constructors and highlight the signature difference in the signature Call an object method with parameter (ie setters). Additional requirements (Pick something)
Go through code progression of understanding Class usage and generating an Instance of a Class (Object).
a. Build a purposeful dynamic Class, using an Object, generate multiple instances: - Person: Name and Age - Dessert: Type and Cost - Location: City, State, Zip
b. Create a static void main tester method to generate objects of the class.
c. In tester method, show how setters/mutators can be used to make the data in the Object dynamically change
Go through progression of understanding a Static Class. Build a purposeful static Class, no Objects.
Calculate common operations on a Date field, age since date, older of two dates, number of seconds since date
Calculate stats functions on an array of values: mean, median, mode.
import java.util.Scanner;
public class Person {
private String person;
public Person() {
this.setPerson("Kaiden Do, 16");
}
public Person(String info) {
this.setPerson(info);
}
public void setPerson(String info) {
this.person = info;
}
public String getPerson() {
return this.person;
}
public static void main(String[] args) {
Person p1 = new Person();
Person p2 = new Person("Joe, 65");
System.out.println(p1.getPerson());
System.out.println(p2.getPerson());
Scanner sc = new Scanner(System.in);
System.out.println("Enter a name:");
String name = sc.nextLine();
System.out.println("Enter an age:");
String age = sc.nextLine();
Person p3 = new Person(name+", "+age);
System.out.println(p3.getPerson());
}
}
Person.main(null);
Kaiden Do, 16
Joe, 65
Enter a name:
Enter an age:
Bob, 1000
Java Console Games Hacks
To start the year, I want you to consider a simple Java console game or improve on the organization and presentation of the games listed.
Make RPS, Tic-Tack-Toe, and Higher Lower into different cells and objects. Document each cell in Jupyter Notebooks. Simplify logic, particularly T-T-T. What could you do to make this more simple? Java has HashMap (like Python Dictionary), Arrays (fixed size), ArraLists (Dynamic Size). Run the menu using recursion versus while loop. Try to color differently. Look over 10 units for College Board AP Computer Science A. In your reorganized code blocks and comments identify the Units of Code Used. Answer why you think this reorganization and AP indetification is important?
import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers
public class ConsoleGame {
public final String DEFAULT = "\u001B[0m"; // Default Terminal Color
public ConsoleGame() {
Scanner sc = new Scanner(System.in); // using Java Scanner Object
boolean quit = false;
while (!quit) {
this.menuString(); // print Menu
try {
int choice = sc.nextInt(); // using method from Java Scanner Object
System.out.print("" + choice + ": ");
quit = this.action(choice); // take action
} catch (Exception e) {
sc.nextLine(); // error: clear buffer
System.out.println(e + ": Not a number, try again.");
}
}
sc.close();
}
public void menuString(){
String menuText = ""
+ "\u001B[35m___________________________\n"
+ "|~~~~~~~~~~~~~~~~~~~~~~~~~|\n"
+ "|\u001B[0m Menu! \u001B[35m|\n"
+ "|~~~~~~~~~~~~~~~~~~~~~~~~~|\n"
+ "| 0 - Exit |\n"
+ "| 1 - Rock Paper Scissors |\n"
+ "| 2 - Higher or Lower |\n"
+ "| 3 - Tic Tac Toe |\n"
+ "|_________________________| \u001B[0m\n"
+ "\n"
+ "Choose an option.\n"
;
System.out.println(menuText);
}
private boolean action(int selection) {
boolean quit = false;
switch (selection) { // Switch or Switch/Case is Control Flow statement and is used to evaluate the user selection
case 0:
System.out.print("Goodbye, World!");
quit = true;
break;
case 1:
rps();
break;
case 2:
horl();
break;
case 3:
ticTacToe();
break;
default:
//Prints error message from console
System.out.print("Unexpected choice, try again.");
}
System.out.println(DEFAULT); // make sure to reset color and provide new line
return quit;
}
public void horl(){
System.out.println("Higher or Lower");
System.out.println("You have three guesses to guess the number I am thinking of between 1-8.");
System.out.println("If you guess the number correctly, you win!");
Scanner scHL = new Scanner(System.in);
int randomG = (int) (Math.random() * 8) + 1;
int guess = scHL.nextInt();
for(int i = 3; i > 0; i--){
if(guess == randomG){
System.out.println("You win!");
break;
}
else if(guess > randomG){
System.out.println("The number is lower");
}
else if(guess < randomG){
System.out.println("The number is higher");
}
guess = scHL.nextInt();
}
System.out.println("Game over.");
scHL.close();
}
public void rps(){
System.out.println("Rock Paper Scissors");
System.out.println("Type r for rock, p for paper, or s for scissors");
Scanner scRPS = new Scanner(System.in);
String userChoice = scRPS.nextLine().toLowerCase();
Boolean quit = false;
int random = (int) (Math.random() * 3);
while(quit == false){
if(userChoice.equals("r")){
if(random == 1){
System.out.println("You chose rock \nThe computer chose paper \nYou lose!");
}
else if(random == 2){
System.out.println("You chose rock \nThe computer chose scissors \nYou win!");
}
else{
System.out.println("You chose rock \nThe computer chose rock \nIt's a tie!");
}
quit = true;
}
else if(userChoice.equals("p")){
if(random == 1){
System.out.println("You chose paper \nThe computer chose paper \nIt's a tie!");
}
else if(random == 2){
System.out.println("You chose paper \nThe computer chose scissors \nYou lose!");
}
else{
System.out.println("You chose paper \nThe computer chose rock \nYou win!");
}
quit = true;
}
else if(userChoice.equals("s")){
if(random == 1){
System.out.println("You chose scissors \nThe computer chose paper \nYou win!");
}
else if(random == 2){
System.out.println("You chose scissors \nThe computer chose scissors \nIt's a tie!");
}
else{
System.out.println("You chose scissors \nThe computer chose rock \nYou lose!");
}
quit = true;
}
else{
System.out.println("Invalid input, try again");
userChoice = scRPS.nextLine();
}
}
scRPS.close();
}
public void ticTacToe(){
System.out.println("Tic Tac Toe");
Scanner scTTT = new Scanner(System.in);
String[] board = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
String player = "X";
String player2 = "O";
int turn = 0;
Boolean quit = false;
System.out.println("Do you want to play against a friend or the computer?");
System.out.println("Type 1 for friend, 2 for computer");
int choice = scTTT.nextInt();
//make tic tac toe using player1 and player2
if(choice == 1){
System.out.println("Type the number of the square you want to place your piece in");
while(quit == false){
System.out.println("Player 1's turn (X)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move = scTTT.nextInt();
if(board[move - 1].equals("X") || board[move - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move - 1] = player;
turn++;
if(board[0].equals("X") && board[1].equals("X") && board[2].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[3].equals("X") && board[4].equals("X") && board[5].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[6].equals("X") && board[7].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[3].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[1].equals("X") && board[4].equals("X") && board[7].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[5].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[4].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[4].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(turn == 9){
System.out.println("It's a tie!");
quit = true;
}
else{
System.out.println("Player 2's turn (O)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move2 = scTTT.nextInt();
if(board[move2 - 1].equals("X") || board[move2 - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move2 - 1] = player2;
turn++;
if(board[0].equals("O") && board[1].equals("O") && board[2].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[3].equals("O") && board[4].equals("O") && board[5].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[6].equals("O") && board[7].equals("O") && board[8].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[0].equals("O") && board[3].equals("O") && board[6].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[1].equals("O") && board[4].equals("O") && board[7].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
else if(board[2].equals("O") && board[5].equals("O") && board[8].equals("O")){
System.out.println("Player 2 wins!");
quit = true;
}
}
}
}
}
}
if(choice == 2){
String computer = "O";
System.out.println("Type the number of the square you want to place your piece in");
while(quit == false){
System.out.println("Player 1's turn (X)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move = scTTT.nextInt();
if(board[move - 1].equals("X") || board[move - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move - 1] = player;
turn++;
if(board[0].equals("X") && board[1].equals("X") && board[2].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[3].equals("X") && board[4].equals("X") && board[5].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[6].equals("X") && board[7].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[3].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[1].equals("X") && board[4].equals("X") && board[7].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[5].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[0].equals("X") && board[4].equals("X") && board[8].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(board[2].equals("X") && board[4].equals("X") && board[6].equals("X")){
System.out.println("Player 1 wins!");
quit = true;
}
else if(turn == 9){
System.out.println("It's a tie!");
quit = true;
}
else{
System.out.println("Computer's turn (O)");
System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
int move2 = (int)(Math.random() * 9) + 1;
if(board[move2 - 1].equals("X") || board[move2 - 1].equals("O")){
System.out.println("That square is already taken, try again");
}
else{
board[move2 - 1] = computer;
turn++;
if(board[0].equals("O") && board[1].equals("O") && board[2].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[3].equals("O") && board[4].equals("O") && board[5].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[6].equals("O") && board[7].equals("O") && board[8].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[0].equals("O") && board[3].equals("O") && board[6].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[1].equals("O") && board[4].equals("O") && board[7].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
else if(board[2].equals("O") && board[5].equals("O") && board[8].equals("O")){
System.out.println("Computer wins!");
quit = true;
}
}
}
}
}
}
scTTT.close();
}
static public void main(String[] args) {
new ConsoleGame(); // starting Menu object
}
}
ConsoleGame.main(null);
[35m___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|[0m Menu! [35m|
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower |
| 3 - Tic Tac Toe |
|_________________________| [0m
Choose an option.
3: Tic Tac Toe
Do you want to play against a friend or the computer?
Type 1 for friend, 2 for computer
Type the number of the square you want to place your piece in
Player 1's turn (X)
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
Computer's turn (O)
1 | 2 | 3
4 | 5 | 6
7 | 8 | X
Player 1's turn (X)
1 | 2 | 3
4 | 5 | 6
7 | O | X
Computer's turn (O)
1 | 2 | X
4 | 5 | 6
7 | O | X
Player 1's turn (X)
1 | O | X
4 | 5 | 6
7 | O | X
Player 1 wins!
[0m
[35m___________________________
|~~~~~~~~~~~~~~~~~~~~~~~~~|
|[0m Menu! [35m|
|~~~~~~~~~~~~~~~~~~~~~~~~~|
| 0 - Exit |
| 1 - Rock Paper Scissors |
| 2 - Higher or Lower |
| 3 - Tic Tac Toe |
|_________________________| [0m
Choose an option.
0: Goodbye, World![0m
import java.util.Scanner;
import java.lang.Math;
import java.lang.Character;
public class Hangman {
private static String[] words = {"APPLE","BANANA","ORANGE","LEMON","BLUEBERRY","STRAWBERRY","LIME","WATERMELON","MANGO","KIWI"};
static void drawBoard(int step){
String[] boards = {
" _______\n | |\n |\n |\n |\n |\n |\n------",
" _______\n | |\n | O\n |\n |\n |\n |\n------",
" _______\n | |\n | O\n | |\n |\n |\n |\n------",
" _______\n | |\n | O\n | /|\n |\n |\n |\n------",
" _______\n | |\n | O\n | /|\\\n |\n |\n |\n------",
" _______\n | |\n | O\n | /|\\\n | /\n |\n |\n------",
" _______\n | |\n | O\n | /|\\\n | / \\\n |\n |\n------"};
System.out.println(boards[step]);
}
static boolean ready(){
Scanner scannerplay = new Scanner(System.in);
String response;
while (true) {
System.out.println("Are you ready to play? (y/n)");
response = scannerplay.nextLine();
if (response.equalsIgnoreCase("y")){
return true;
} else if (response.equalsIgnoreCase("n")) {
return false;
}
}
}
static void wordLines(String word){
int length = word.length();
for (int i = 0; i < length; i++) {
System.out.print("_ ");
}
System.out.println("");
}
static String wordRand(){
int randomNumber = (int) (Math.random() * 10);
String randomWord = words[randomNumber];
return randomWord;
}
static void guess(String word){
Scanner scguess = new Scanner(System.in);
String letterGuess;
int wrong = 0;
int[] positions = new int[word.length()];
int count = 0;
int correct = 0;
HashSet<Character> guessedLetters = new HashSet<>();
while (wrong < 6 && correct < word.length()){
System.out.println("Guess a letter");
letterGuess = scguess.nextLine();
if (letterGuess.length() == 1) {
char letG = Character.toUpperCase(letterGuess.charAt(0));
if (!Character.isLetter(letG) || guessedLetters.contains(letG)){
continue;
} else {
guessedLetters.add(letG);
count=0;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == letG) {
positions[count] = i;
count++;
}
}
if (count>0){
System.out.print("Yes: "+letG+" ");
for (int i = 0; i < count; i++) {
System.out.print(positions[i]);
}
System.out.println("");
drawBoard(wrong);
wordLines(word);
}
else if (count==0){
System.out.println("No: "+letG);
wrong++;
drawBoard(wrong);
wordLines(word);
}
for (int i = 0; i < word.length(); i++) {
char currentChar = word.charAt(i);
if (currentChar == letG) {
correct++;
} else {
}
}
System.out.println("");
}
}
}
if (wrong == 6){
System.out.println("You lose, the word was: "+word);
}
if (correct == word.length()){
System.out.println("You win!!, the word was: "+word);
}
}
public static void main(String[] args) {
while (true) {
String tester = wordRand();
if (!ready()) {
System.out.println("GOODBYE!");
return;
}
drawBoard(0);
wordLines(tester);
guess(tester);
}
}
}
Hangman.main(null);
Are you ready to play? (y/n)
_______
| |
|
|
|
|
|
------
_ _ _ _
Guess a letter
No: K
_______
| |
| O
|
|
|
|
------
_ _ _ _
Guess a letter
Yes: L 0
_______
| |
| O
|
|
|
|
------
_ _ _ _
Guess a letter
Yes: I 1
_______
| |
| O
|
|
|
|
------
_ _ _ _
Guess a letter
Yes: M 2
_______
| |
| O
|
|
|
|
------
_ _ _ _
Guess a letter
Yes: E 3
_______
| |
| O
|
|
|
|
------
_ _ _ _
You win!!, the word was: LIME
Are you ready to play? (y/n)
GOODBYE!