Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions ProgExer1 - Prime Numbers/PrimeNumbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class PrimeNumbers
{
public static void main(String[] args)
{
int count=0 ,i , j;
for(i=1; i<=100; i++)
{
for(j=i-1; j>i/2; j--)
{
if(i%j==0)
{
break;
}
}
if(j==1)
{
count++;
System.out.print(i + "\t");
if(count%5==0)
{
System.out.println();
}
}
}
}
}

52 changes: 52 additions & 0 deletions ProgExer2 - Quarters, Dimes, and Pennies/QuartersDimesPennies.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.util.*;
public class QuartersDimesPennies
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int[] coinValue = {25, 10, 1};
int[] coinDenomination = new int[3];
int[] arrchange = new int[2];
int amountOfChange;
String yesOrNo = "y";

do
{
System.out.print("\nEnter the amount of change (from 1-99): ");
amountOfChange = input.nextInt();
if(amountOfChange > 100 || amountOfChange < 1)
{
System.out.println("Error: Invalid amount, range is from 1 to 99.");
continue;
}
int temp = amountOfChange;
for(int i = 0; i < coinValue.length; i++)
{
arrchange = computeCoin(coinValue[i],amountOfChange);
amountOfChange = arrchange[1];
coinDenomination[i] = arrchange[0];
}
System.out.println(temp + " cents can be given as");
System.out.printf("%d quarter(s) %d dime(s) %d penny(pennies)\n\n",coinDenomination[0],coinDenomination[1], coinDenomination[2]);
System.out.print("Do you want to enter another amount? [y|n]");
input.nextLine();
yesOrNo = input.nextLine();

}while(yesOrNo.equalsIgnoreCase("y"));
}

public static int[] computeCoin(int coinValue, int amountLeft)
{
int[] arr = new int[2];
int number = amountLeft;
if((coinValue > 0 && coinValue < 100) && (amountLeft >= 0 && amountLeft < 100))
{
number /= coinValue;
amountLeft %= coinValue;
arr[0] = number;
arr[1] = amountLeft;
return arr;
}
return arr;
}
}
35 changes: 35 additions & 0 deletions ProgExer3 - Tic Tac Toe/Tic Tac Toe/GameBoard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public class GameBoard
{
public String[][] positions;
public GameBoard()
{
positions = new String[][] {{"1","2","3"},{"4","5","6"},{"7","8","9"}};

}

public void showBoard()
{
for(int i=0; i<this.positions.length; i++)
{
for(int j=0; j<this.positions[i].length; j++)
{
System.out.print(this.positions[i][j] + " ");
}
System.out.println();
}
}

public void showBoard(String[][] positions)
{
for(int i=0; i<this.positions.length; i++)
{
for(int j=0; j<this.positions[i].length; j++)
{
System.out.print(this.positions[i][j] + " ");
}
System.out.println();
}
}


}
9 changes: 9 additions & 0 deletions ProgExer3 - Tic Tac Toe/Tic Tac Toe/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Main
{
public static void main(String[] args)
{
TicTacToeGame game = new TicTacToeGame("player 1", "player 2");

game.startGame();
}
}
36 changes: 36 additions & 0 deletions ProgExer3 - Tic Tac Toe/Tic Tac Toe/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
public class Player
{
public String name;
public String position;
public String XorO;

public Player()
{

}

public Player(String name)
{
this.name = name;
}


public void placePosition(GameBoard board)
{
for(int i=0; i<board.positions.length; i++)
{
for(int j=0; j<board.positions[i].length; j++)
{
if(this.position.equals(board.positions[i][j]))
{
if(board.positions[i][j].equals("X") || board.positions[i][j].equals("O"))
{
System.out.println("\nError: Invalid position. Please choose a position not marked an 'X' or 'O'.");
}
else
board.positions[i][j] = this.XorO;
}
}
}
}
}
154 changes: 154 additions & 0 deletions ProgExer3 - Tic Tac Toe/Tic Tac Toe/TicTacToeGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import java.util.Scanner;
public class TicTacToeGame
{
public Player[] players = new Player[2];
public String[] xO = {"X","O"};
public GameBoard boardGame;
public Scanner input = new Scanner(System.in);
public boolean isWinner = false;
public int count = 0;


public TicTacToeGame(String playerName1, String playerName2)
{
this.players[0] = new Player(playerName1);
this.players[1] = new Player(playerName2);

boardGame = new GameBoard();
}

public void startGame()
{
assignXorO(this.players[0], this.players[1]);
System.out.println("\nThis is a tic-tac-toe game. This game requires two players.");
System.out.println(this.players[0].name + " will be marked by \'" + this.players[0].XorO +"\' and "+ this.players[1].name + " will be marked by '" + this.players[1].XorO + "'.");
System.out.println("Whosoever first creates a 3-X line or 3-O line wins.\n");

this.boardGame.showBoard();

do
{
if(this.count > 8)
{
System.out.println("The game was a draw!");
break;
}

this.playerTurn(0);
this.boardGame.showBoard();
int x = this.announceWinner(this.winnerChecker());

if(x == 1)
break;

if(this.count > 8)
{
System.out.println("The game was a draw!");
break;
}

this.playerTurn(1);
this.boardGame.showBoard();
this.announceWinner(this.winnerChecker());


}while(!(this.isWinner));
}

public void playerTurn(int i)
{
System.out.print('\n' + this.players[i].name + ", your turn\nChoose a position in the board: ");
this.players[i].position = input.nextLine();

this.players[i].placePosition(this.boardGame);
this.count++;
System.out.println();
}



private void assignXorO(Player p1, Player p2)
{

int randNumPlayer = (Math.random() * 10) > 5? 0: 1;
int randNumXorO = (Math.random() * 10) > 5? 0: 1;

this.players[randNumPlayer].XorO = this.xO[randNumXorO];

int randNumPlayer2 = (randNumPlayer == 0)? 1: 0;
int randNumXorO2 = (randNumXorO == 0)? 1: 0;

this.players[randNumPlayer2].XorO = this.xO[randNumXorO2];
}

public int announceWinner(String xOro)
{
if(xOro == null)
return 0;
if(this.players[0].XorO.equals(xOro))
{
System.out.println(this.players[0].name + " wins!");
return 1;
}
else
{
System.out.println(this.players[1].name + " wins!");
return 1;
}

}


public String winnerChecker()
{
int i=0;
if(this.boardGame.positions[i][0].equals(this.boardGame.positions[i][1]) && this.boardGame.positions[i][1].equals(this.boardGame.positions[i][2]))
{
this.isWinner = true;
return this.boardGame.positions[i][0];
}
if(this.boardGame.positions[0][i].equals(this.boardGame.positions[1][i]) && this.boardGame.positions[1][i].equals(this.boardGame.positions[2][i]))
{
this.isWinner = true;
return this.boardGame.positions[0][i];
}
i++;
if(this.boardGame.positions[i][0].equals(this.boardGame.positions[i][1]) && this.boardGame.positions[i][1].equals(this.boardGame.positions[i][2]))
{
this.isWinner = true;
return this.boardGame.positions[i][0];
}
if(this.boardGame.positions[0][i].equals(this.boardGame.positions[1][i]) && this.boardGame.positions[1][i].equals(this.boardGame.positions[2][i]))
{
this.isWinner = true;
return this.boardGame.positions[0][i];
}
i++;
if(this.boardGame.positions[i][0].equals(this.boardGame.positions[i][1]) && this.boardGame.positions[i][1].equals(this.boardGame.positions[i][2]))
{
this.isWinner = true;
return this.boardGame.positions[i][0];
}
if(this.boardGame.positions[0][i].equals(this.boardGame.positions[1][i]) && this.boardGame.positions[1][i].equals(this.boardGame.positions[2][i]))
{
this.isWinner = true;
return this.boardGame.positions[0][i];
}

if(this.boardGame.positions[0][0].equals(this.boardGame.positions[1][1]) && this.boardGame.positions[1][1].equals(this.boardGame.positions[2][2]))
{
this.isWinner = true;
return this.boardGame.positions[0][0];
}
if(this.boardGame.positions[0][2].equals(this.boardGame.positions[1][1]) && this.boardGame.positions[1][1].equals(this.boardGame.positions[2][0]))
{
this.isWinner = true;
return this.boardGame.positions[0][2];
}
else
return null;
}



}
Loading