Untitled
unknown
plain_text
2 years ago
7.9 kB
9
Indexable
Never
package labs; import static org.junit.Assert.assertArrayEquals; import java.util.Scanner; public class Lab01 { //get string for user input public static String getString(String userQuestion) { Scanner inKey = new Scanner(System.in); System.out.print(userQuestion); String input = inKey.nextLine(); return input; } //Method to enter and put a space between each letter of a word to make it a diagonal public static void diagonal(String word) { char[] inputWord = word.toCharArray(); String space = " "; int spaceNum = 0; for(int i =0; i<word.length(); i++) { for (int o = 0; o<spaceNum; o++) { System.out.print(space); } System.out.println(inputWord[i]); spaceNum++; } } //Method to enter and put a space between each letter of a word to make it a diagonal public static void diagonals(String userInput) { //if there is 2 words, resets it between the space String[] words = userInput.split(" "); for(int i = 0; i <words.length; i++) { diagonal(words[i]); } } //makes a letter into a binary public static String letterToBinary(char input) { int x = Integer.valueOf(input); String str = ""; for(int i =7; i>=0; i--) { if (( x / (int) Math.pow(2, i)) > 0) { str += "1"; x -= Math.pow(2, i); } else { str += "0"; } } return str; } //makes a whole sentence into a binary public static String sentenceToBinary(String userSentence) { char[] binary = userSentence.toCharArray(); String x = ""; for(int i =0; i<userSentence.length(); i++){ x+=letterToBinary(binary[i]); } return x; } /* * ONLY PLAY WITH THIS FOR #2 * ONLY PLAY WITH THIS FOR #2 * ONLY PLAY WITH THIS FOR #2 * ONLY PLAY WITH THIS FOR #2 */ public static void main(String[] args) { //For #2 // Scanner inKey = new Scanner(System.in); // System.out.print("Enter a single word: "); // String input = inKey.nextLine(); String input = getString("Enter a single word: "); diagonal(input); //System.out.print("Enter a sentence or Phrase: ");\ String input2 = getString("Enter a sentence or Phrase: "); diagonals(input2); System.out.print(sentenceToBinary(getString("Enter a sentence: "))); } } package labs; import java.util.Scanner; public class PerformanceTask { private static int dealCard; private static int dealCard2; private static int totalDeal; private static int computerDeal; private static int totalComputer; private static int compterTotalHit; /* * Instance Variables (class parameters) * - Accessible AND changeable in ANY method * Very convenient, but dangerous. * - declared only!! * - You must initialize them before using * * Place them all here. */ private static int playerHandTotal; private static String playerHand; //welcome to the game public static void welcomeMessage() { System.out.println("BLACK - JACK!\n" + "Come try your luck and\n" + "maybe take home a buck!"); } //start to play the game public static void playGame() { playerTurn(); drawCard(); computerTurn(); printWinner(); resetGame(); } //Scanner to see what the use enters public static String getString(String userQuestion) { Scanner inKey = new Scanner(System.in); System.out.print(userQuestion); String input = inKey.nextLine().toLowerCase(); return input; } //player turn public static void playerTurn() { System.out.println("Your Turn!!"); //random int for the card worth int player = (int) (Math.random() * 11 + 1); int computer = (int) (Math.random() * 11 + 1); //int for card deals dealCard = player; player = (int) (Math.random() * 11 + 1); dealCard2 = player; if (player >=22) { dealCard2 = 1; //total deal card } totalDeal += (dealCard + dealCard2); System.out.println("Your Hand:" + dealCard + " " + dealCard2 + " = "+ totalDeal); computer = (int) (Math.random() * 11 + 1); System.out.println("Comp Hand " + computer); totalComputer+=computer; } public static void drawCard() { //System.out.print((getString("hit (h) or stay(s)"))); String sup = getString("hit (h) or stay(s)"); int player = (int) (Math.random() * 11 + 1); //see if user wants to hit card while(totalDeal<21) { sup = getString("hit (h) or stay(s)"); System.out.println(sup); if (sup.equals("h")) { compterTotalHit++; player = (int) (Math.random() * 11 + 1); int dealCard3 = player; totalDeal+=dealCard3; System.out.println("You got a " + dealCard3 + " total deal = " + totalDeal); } //if they don't want to, code breaks and moves on else if(sup.equals("s")){ break; } //if they put another letter, tells them to put the correct letter else { System.out.println("Wrong letter, try again"); } } } //computer turn public static void computerTurn() { System.out.println("Computer's turn"); //random int generater to give card value int computer = (int) (Math.random() * 11 + 1); //computer card value, tells user what computer got computerDeal = computer; System.out.println("The computer got " + computerDeal + " and the computer total is " + totalComputer); totalComputer+=computerDeal; //while loop so computer will keep hitting till it gets higher then user, only if user is lower then 21 while(totalComputer<totalDeal&&totalDeal<=21){ computer = (int) (Math.random() * 11 + 1); computerDeal = computer; //total computer value totalComputer+=computerDeal; System.out.println("The computer got " + computerDeal + " and the computer total is " + totalComputer); } } //who won? code to see who won public static void printWinner() { if(totalDeal>21) { System.out.println("You busted...computer wins..."); } else if(totalDeal== totalComputer) { System.out.println("You tied...computer wins..."); } else if(totalComputer>21) { System.out.println("Computer busted...you wins!!"); } else if(totalDeal<totalComputer) { System.out.println("You lose... computer wins..."); } else if(totalComputer<totalDeal) { System.out.println("You Win!!!"); } } //reset game void public static void resetGame() { //ask user if they want to play String finish = getString("Good game, play again? Yes(y) or No(n)"); //if they say yes does the following if(finish.equals("y")) { //resets the ints to reset the game dealCard=0; dealCard2=0; totalDeal=0; computerDeal=0; totalComputer=0; compterTotalHit=0; playerHandTotal=0; //resets game playGame(); } } /* * Keep the starter code * Feel free to change it to suit your needs. */ public static void main(String[] args) { //initialize instance variables playerHandTotal = 0; char keepPlaying = 'y'; //Welcome them to the game //Place welcome message method here! welcomeMessage(); while (keepPlaying == 'y') { //just puts a space after the welcome message System.out.println(" "); //Start game here playGame(); //Ask if they want to play again? //Loop until they give a valid answer keepPlaying = 'a'; //so the loop will start while (keepPlaying != 'y' && keepPlaying != 'n') { /* * You only need the first letter * So I... * - made it lower case * - then grabbed the first character as a char */ keepPlaying = getString("\nPlay again? (Y or N): ").toLowerCase().charAt(0); if (keepPlaying != 'y' && keepPlaying != 'n') { System.out.println("\nI didn't regcognize that answer."); } } } //Say goodbye System.out.println("\nIt's been fun!"); System.out.println("Come back soon!"); } }