Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
profjpbaugh authored Jan 27, 2022
1 parent 30b3b9a commit 2b9b700
Show file tree
Hide file tree
Showing 7 changed files with 377 additions and 0 deletions.
23 changes: 23 additions & 0 deletions section6/CountDown.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

public class CountDown {
public static void main(String[] args) {
//countDownFrom(10);

countUpTo(0, 10);
}//end main

public static void countDownFrom(int num) {
if(num >= 0) {
System.out.println(num);
countDownFrom(num - 1);
}
}//end countDownFrom

public static void countUpTo(int from, int to) {
if(from <= to) {
System.out.println(from);
countUpTo(from + 1, to);
}
}//end countUpTo

}
43 changes: 43 additions & 0 deletions section6/FunWith2DArrays.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.util.Random;

public class FunWith2DArrays {
public static void main(String[] args) {
//2 row x 3 column
int[][] my2DArray = new int[2][3]; //2 rows x 3 columns

fill2DArray(my2DArray);
print2DArray(my2DArray);
System.out.println();
twice2DArray(my2DArray);
print2DArray(my2DArray);
}//end main

public static void fill2DArray(int[][] twoDArr) {
Random rand = new Random();

for(int i = 0; i < twoDArr.length; i++) {
for(int j = 0; j < twoDArr[i].length; j++) {
//randomize 0 through 99 (inclusive)
//row i, column j
twoDArr[i][j] = rand.nextInt(100); //0 ... 99 (inclusive)
}
}//end for i
}//end fill2DArray

public static void print2DArray(int[][] twoDArr) {
for(int[] arr : twoDArr) {
for(int num : arr) {
System.out.print(num + " ");
}//end of inner for loop
System.out.println();
}
}//end print2DArray

public static void twice2DArray(int[][] twoDArr) {
for(int i = 0; i < twoDArr.length; i++) {
for(int j = 0; j < twoDArr[i].length; j++) {
twoDArr[i][j] *= 2;
}//end for j
}//end for i
}//end twice2DArray
}
19 changes: 19 additions & 0 deletions section6/MethodCalls.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@


public class MethodCalls {
public static void main(String[] args) {
doSomething();
}//end main

public static void doSomething() {
System.out.println("In doSomething!");

int result = getSomeValue();

System.out.println("result: " + result);
}

public static int getSomeValue() {
return 150;
}
}
31 changes: 31 additions & 0 deletions section6/MethodOverloading.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

public class MethodOverloading {
public static void main(String[] args) {
int result = getResult(5);
System.out.println(result);

result = getResult(5,4);
System.out.println(result);

result = getResult(5, "12");
System.out.println(result);

System.out.println(getResult("John", "Baugh"));
}//end main

public static int getResult(int num) {
return num * 2;
}//end getResult

public static int getResult(int num1, int num2) {
return num1 * num2;
}//end getResult

public static int getResult(int num1, String value) {
return num1 * Integer.parseInt(value);
}//end getResult

public static String getResult(String str1, String str2) {
return str1 + " " + str2;
}
}
44 changes: 44 additions & 0 deletions section6/MethodsDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@


public class MethodsDemo {
public static void main(String[] args) {
printHello();
printNumber(10); //argument 10 is passed in
int result = giveMe10();
double dResult;
System.out.println(result);

result = addThese(3, 5); //arguments 3 and 5 passed in
System.out.println(result);

dResult = square(5.5);
System.out.println(dResult);

System.out.println(square(7.25));

}//end main

//void, parameterless
public static void printHello() {
System.out.println("Hello there!");
}//end printHello

//void, parameterized
public static void printNumber(int a) {
System.out.println("The number is " + a);
}//end printNumber

//value-returning, parameterless
public static int giveMe10() {
return 10;
}//end giveMe10

//value-returning, parameterized
public static int addThese(int num1, int num2) {
return num1 + num2;
}//end addThese

public static double square(double num) {
return num * num;
}//end square
}
27 changes: 27 additions & 0 deletions section6/Proj6_1_SumOfElements.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.util.ArrayList;

public class Proj6_1_SumOfElements {
public static void main(String[] args) {
ArrayList<Integer> someList =
new ArrayList<>();

someList.add(10);
someList.add(22);
someList.add(55);
someList.add(121);

int sum = sumElements(someList);

System.out.println("sum is " + sum);
}//end main

public static int sumElements(ArrayList<Integer> list) {
int sum = 0;

for(int i = 0; i < list.size(); i++) {
sum += list.get(i);
}//end for i

return sum;
}//end sumElements
}
190 changes: 190 additions & 0 deletions section6/Proj6_2_TicTacToe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@

import java.util.Scanner;


public class Proj6_2_TicTacToe {

public static void main(String[] args) {
runGame();
}//end main

public static void runGame() {
String winner = "";
boolean xTurn = true;
int theRow = 0;
int theCol = 0;
String[][] gameBoard = new String[3][3];

initializeGameBoard(gameBoard);

//print initial board
printCurrentBoard(gameBoard);

//"" signals no winner
while(winner.equals("")) {
if(xTurn) {
System.out.println("It is X's turn");
}
else {
System.out.println("It is O's turn");
}

getUserInput(xTurn, gameBoard);
System.out.println(); //extra spacing

printCurrentBoard(gameBoard); //reprint board
winner = getWinner(gameBoard);
xTurn = !xTurn; //flip it!

if(winner.equals("") && isBoardFull(gameBoard)) {
winner = "C";
}//end if
}//end while

//cat's game?
System.out.println();

if(winner.equals("C")) {
System.out.println("It was the Cat's game! NO WINNER!");
}
else {
System.out.println("The winner is " + winner);
}//end if-else
}//end runGame

public static void initializeGameBoard(
String[][] gameBoard) {

for(int i = 0; i < gameBoard.length; i++) {
for(int j = 0; j < gameBoard[i].length; j++) {
gameBoard[i][j] = " ";
}//end for j
}//end for i
}//end initializeGameBoard

public static void printCurrentBoard(
String[][] gameBoard) {

for(int i = 0; i < gameBoard.length; i++) {
for(int j = 0; j < gameBoard[i].length; j++) {
System.out.print(gameBoard[i][j]);
if(j < 2) {
System.out.print(" | ");
}
//next line
}//end for j
System.out.println();
if(i < 2) {
System.out.print("- - - - -");
}
//to add space between the board and next
//output
System.out.println();
}//end for i
}//end printCurrentBoard

public static void getUserInput(boolean xTurn,
String[][] gameBoard) {
Scanner keyboard = new Scanner(System.in);

int row = -1;
int col = -1;
boolean keepAsking = true;

while(keepAsking) {
System.out.println("Please enter the row THEN "
+ "the column, each from 0, 1, or 2, "
+ "separated by a space");

row = keyboard.nextInt();
col = keyboard.nextInt();
keyboard.nextLine(); //consume newline

//if we are in bounds with row and column
if(row >= 0 && row <= 2 &&
col >= 0 && col <= 2) {
//valid and in-range selection
//but, is it occupied by an X or O already?
if(!cellAlreadyOccupied(row, col, gameBoard)) {
keepAsking = false;
}
else {
System.out.println("That cell is already occupied!");
}//end if-else
}//end if
}//end while

//by the time it gets here, we know it's a VALID row and column
//in range, and not already occupied!
if(xTurn) {
gameBoard[row][col] = "X";
}
else {
gameBoard[row][col] = "O";
}

}//end getUserInput

public static boolean cellAlreadyOccupied(int row,
int col, String[][] gameBoard) {
return !gameBoard[row][col].equals(" ");
}//end cellAlreadyOccupied

public static String getWinner(
String[][] gameBoard) {
final int ROWS = gameBoard.length;
final int COLS = gameBoard[0].length;

for (int i = 0; i < ROWS; i++) {
if (!gameBoard[i][0].equals(" ") &&
gameBoard[i][0].equals(gameBoard[i][1]) &&
gameBoard[i][1].equals(gameBoard[i][2])) {
return gameBoard[i][0]; //we have a match (horizontal)!
}
}//end for

//check columns
for (int i = 0; i < COLS; i++) {
if (!gameBoard[0][i].equals(" ") &&
gameBoard[0][i].equals(gameBoard[1][i]) &&
gameBoard[1][i].equals(gameBoard[2][i])) {
return gameBoard[0][i]; //we have a match (vertical)!
}
}//end for

//check diagonals
//there are two diagonals, which we can test manually / no-loop

//upper-left to bottom right diagonal
if (!gameBoard[0][0].equals(" ") &&
gameBoard[0][0].equals(gameBoard[1][1]) &&
gameBoard[1][1].equals(gameBoard[2][2])) {
return gameBoard[0][0]; //we have a diagonal match!
}

//lower-left to upper right diagonal
if (!gameBoard[2][0].equals(" ") &&
gameBoard[2][0].equals(gameBoard[1][1]) &&
gameBoard[1][1].equals(gameBoard[0][2])) {
return gameBoard[2][0]; //we have a diagonal match!
}

//if we get to here...
return ""; //no winner yet!
}//end getWinner

public static boolean isBoardFull(
String[][] gameBoard) {

int countFill = 0;

for(int i = 0; i < gameBoard.length; i++) {
for(int j = 0; j < gameBoard[i].length; j++) {
if(!gameBoard[i][j].equals(" ")) {
countFill++;
}
}//end for j
}//end for i
return countFill == 9; //all 9 spaces filled with non-space
}//end isBoardFull
}//end class

0 comments on commit 2b9b700

Please sign in to comment.