diff --git a/ProgExer1 - Prime Numbers/PrimeNumbers.java b/ProgExer1 - Prime Numbers/PrimeNumbers.java new file mode 100644 index 0000000..51d33e4 --- /dev/null +++ b/ProgExer1 - Prime Numbers/PrimeNumbers.java @@ -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(); + } + } + } + } +} + diff --git a/ProgExer2 - Quarters, Dimes, and Pennies/QuartersDimesPennies.java b/ProgExer2 - Quarters, Dimes, and Pennies/QuartersDimesPennies.java new file mode 100644 index 0000000..36c2a43 --- /dev/null +++ b/ProgExer2 - Quarters, Dimes, and Pennies/QuartersDimesPennies.java @@ -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; + } +} diff --git a/ProgExer3 - Tic Tac Toe/Tic Tac Toe/GameBoard.java b/ProgExer3 - Tic Tac Toe/Tic Tac Toe/GameBoard.java new file mode 100644 index 0000000..0bc045a --- /dev/null +++ b/ProgExer3 - Tic Tac Toe/Tic Tac Toe/GameBoard.java @@ -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 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; + } + + + +} \ No newline at end of file diff --git a/ProgExer4 - Airline Seats/Airline.java b/ProgExer4 - Airline Seats/Airline.java new file mode 100644 index 0000000..c8c994e --- /dev/null +++ b/ProgExer4 - Airline Seats/Airline.java @@ -0,0 +1,78 @@ +import java.util.*; +public class Airline +{ + + int[][] airlineSeats; + public Airline(int[][] airlineSeats) + { + this.airlineSeats = airlineSeats; + } + + public void displayAirlineSeats() + { + System.out.println(); + for(int i=0; i 7 || seatNumber < 1) || (Character.toUpperCase(seatChar) < 'A' || Character.toUpperCase(seatChar) > 'D')) + { + System.out.println("Error: Invalid input. (Range is 1A - 7D)."); + this.displayAirlineSeats(); + this.inputSeat(); + } + else if(this.isSeatAvailable(seatNumber, seatChar)) + this.airlineSeats[seatNumber-1][letterToArrayNum(seatChar)] = 'X'; + else + { + System.out.println("Error: Invalid seat location, it's occupied."); + this.displayAirlineSeats(); + this.inputSeat(); + } + } + + public int letterToArrayNum(char seatChar) + { + switch(seatChar) + { + case 'A': + return 0; + case 'B': + return 1; + case 'C': + return 2; + case 'D': + return 3; + } + return -1; + } + + public void inputSeat() + { + System.out.print("\nPlease choose an unoccupied seat location (from 1A to 7D): "); + Scanner scanner = new Scanner(System.in); + String seatLocation; + + seatLocation = scanner.next(); + + pickSeat(Character.getNumericValue(seatLocation.charAt(0)), Character.toUpperCase(seatLocation.charAt(1))); + } +} + diff --git a/ProgExer4 - Airline Seats/AirlineTest.java b/ProgExer4 - Airline Seats/AirlineTest.java new file mode 100644 index 0000000..d85098e --- /dev/null +++ b/ProgExer4 - Airline Seats/AirlineTest.java @@ -0,0 +1,25 @@ +import java.util.*; +public class AirlineTest +{ + public static void main(String[] args) + { + int[][] airlineSeats = {{'A','B','C','D'},{'A','B','C','D'},{'A','B','C','D'},{'A','B','C','D'},{'A','B','C','D'},{'A','B','C','D'},{'A','B','C','D'}}; + Scanner input = new Scanner(System.in); + String yOrN, yes = "y"; + + Airline airline = new Airline(airlineSeats); + + do + { + airline.displayAirlineSeats(); + airline.inputSeat(); + airline.displayAirlineSeats(); + + + System.out.print("Get another seat? [y|n] "); + yOrN = input.next(); + + }while(yes.equals(yOrN.toLowerCase())); + + } +} \ No newline at end of file diff --git a/ProgExer5 - Number Counter/NumberCounter.java b/ProgExer5 - Number Counter/NumberCounter.java new file mode 100644 index 0000000..6ea04b7 --- /dev/null +++ b/ProgExer5 - Number Counter/NumberCounter.java @@ -0,0 +1,53 @@ +import java.util.*; +public class NumberCounter +{ + public static void main(String[] args) + { + int[] inputNumbers = new int[16]; + List uniqueNumbers = new ArrayList(); + + Scanner input = new Scanner(System.in); + + int count = 0; + + System.out.println("Enter 16 numbers [duplicates allowed]."); + while(count < 16) + { + inputNumbers[count] = input.nextInt(); + count++; + } + + uniqueNumbers.add(inputNumbers[0]); + + //finding unique elements in inputNumbers and storing it to the uniqueNumbers List + for(int i=1; i<16; i++) + { + if(uniqueNumbers.get(0) != inputNumbers[i]) + { + for(int j=0; j (fraction.getNumerator()*this.denominator)) + return true; + else + return false; + } + + public boolean isGreaterThanOrEqualTo(Rational fraction) + { + if((this.numerator*fraction.getDenominator()) >= (fraction.getNumerator()*this.denominator)) + return true; + else + return false; + } + + public void add(Rational fraction) + { + int addDenominator = this.denominator * fraction.getDenominator(); + int addNumerator = (((addDenominator/this.denominator)*this.numerator)+(((addDenominator/fraction.getDenominator())*fraction.getNumerator()))); + this.denominator = addDenominator; + this.numerator = addNumerator; + this.normalize(); + + } + + public void subtract(Rational fraction) + { + int subDenominator = this.denominator * fraction.getDenominator(); + int subNumerator = (((subDenominator/this.denominator)*this.numerator)-(((subDenominator/fraction.getDenominator())*fraction.getNumerator()))); + this.denominator = subDenominator; + this.numerator = subNumerator; + this.normalize(); + } + + public void multiply(Rational fraction) + { + int multDenominator = this.denominator * fraction.getDenominator(); + int multNumerator = this.numerator * fraction.getNumerator(); + this.denominator = multDenominator; + this.numerator = multNumerator; + this.normalize(); + } + + public void divide(Rational fraction) + { + int divDenominator = this.denominator * fraction.getNumerator(); + int divNumerator = this.numerator * fraction.getDenominator(); + this.denominator = divDenominator; + this.numerator = divNumerator; + this.normalize(); + } + + public void normalize() + { + boolean isNumNegative = false, isDenomNegative = false; + + if(this.numerator < 0) + { + this.numerator *= -1; + isNumNegative = true; + } + + if(this.denominator < 0) + { + this.denominator *= -1; + isDenomNegative = true; + } + + int smallNum = (this.numerator