Home

Programming skills: Java ✍️

C++
Java
Python
JavaScript
Java:Vending Machine
import java.util.Scanner;
public class VendingMachine
{ public static void main(String[] args)
{ final int rateChip=125;
final int rateCookie=85;
final int rateCandy=95;
final int quarter= 25;
final int dimes=10;
final int nickels=5;
int buyerMoney, change;
//create a menu
System.out.println ("Here is a selection menu for the food:");
System.out.println ("1) Potato Chips");
System.out.println ("2) Cookies");
System.out.println ("3) Candies");
Scanner input = new Scanner(System.in);
System.out.print ("Please select the food:");
int foodSelection = input.nextInt();
// if the food doesn't exist
if (foodSelection != 1 && foodSelection != 2 && foodSelection != 3)
{
System.out.print("Sorry, your selection doesn't exist!");
return;
}
System.out.print ("Please enter the amount of food:");
int foodQuantity = input.nextInt();
// if the food sold out
if (foodQuantity > 1)
{
System.out.print("Sold out, please make another choice!");
return;
}
if (foodQuantity == 0)
{
System.out.print("Please go away, don't waste my time!");
return;
}
System.out.print ("Please enter the amount of quarter:");
int numQuarters = input.nextInt();
System.out.print ("Please enter the amount of dimes:");
int numDimes = input.nextInt();
System.out.print ("Please enter the amount of nickels:");
int numNickles = input.nextInt();
buyerMoney=numQuarters * quarter + numDimes * dimes + numNickles * nickels;
// assume you only choose one quantity
if (foodQuantity == 1)
{
if (foodSelection == 1)
{
// if the amount is exactly the same the cost
if (buyerMoney == rateChip * foodQuantity )
System.out.print("Please take your food, NO change!");
// if the amount is less than the the cost
if (buyerMoney < rateChip * foodQuantity )
System.out.print("Sorry, you don't have enough money!");
// if the amount is more than the cost
if (buyerMoney > rateChip * foodQuantity )
{
change = buyerMoney - rateChip * foodQuantity;
System.out.print("Please take your food and your change: "+ change + " cents.");
}
}
else if (foodSelection == 2)
{
// if the amount is exactly the same the cost
if (buyerMoney == rateCookie * foodQuantity )
System.out.print("Please take your food, NO change!");
// if the amount is less than the the cost
if (buyerMoney < rateCookie * foodQuantity )
System.out.print("Sorry, you don't have enough money!");
// if the amount is more than the cost
if (buyerMoney> rateCookie * foodQuantity )
{
change = buyerMoney - rateCookie * foodQuantity;
System.out.print("Please take your food and your change: "+ change + " cents.");
}
}
else if (foodSelection == 3)
{
// if the amount is exactly the same the cost
if (buyerMoney == rateCandy * foodQuantity )
System.out.print("Please take your food, NO change!");
// if the amount is less than the the cost
if (buyerMoney < rateCandy * foodQuantity )
System.out.print("Sorry, you don't have enough money!");
// if the amount is more than the cost
if (buyerMoney>rateCandy * foodQuantity )
{
change = buyerMoney - rateCandy * foodQuantity;
System.out.print("Please take your food and your change: "+ change + " cents."); } } } }
}





























































Java: Tic Tac Toe
import java.util.Scanner;
public class TicTacToe
{
private char[][] board;
private char player; // 'X' or 'O'
/* Instantiate board to be a 3 by 3 char array of spaces. Set player to be 'X' */
public TicTacToe() {
/*Step 1: create an empty board, with an initial value of a space (' ')*/
board = new char [3][3];
board[0][0] = ' ';
board[0][1] = ' ';
board[0][2] = ' ';
board[1][0] = ' ';
board[1][1] = ' ';
board[1][2] = ' ';
board[2][0] = ' ';
board[2][1] = ' ';
board[2][2] = ' ';
player = 'X'; }
/*If s represents a valid move, add the current player's symbol to the board
* and return true. Otherwise return false.*/
public boolean play(String s) {
/* Step 2: Fill in here with your own play logic, and replace the return with you own.*/
char rowInput = s.charAt(0);
char colInput = s.charAt(1);
int row;
if (rowInput == 'A') {
row = 0;
} else if (rowInput == 'B') { row = 1;
} else if (rowInput == 'C') {
row = 2;
} else
{ return false;
}
int column;
if (colInput == '1')
{ column = 0;
} else if (colInput == '2')
{ column = 1;
} else if (colInput == '3')
{ column = 2;
} else { return false;
} if (board[row][column] == ' ')
{ board[row][column] = player;
return true;
} else
{ return false;
}
}
/* Switches the current player from X to O, or O to X.*/
public void switchTurn()
{ // Step 3: Fill in with your code to toggle between
// 'X' and 'O'
// Switch from 'X' to 'O'
if (player == 'X')
player = 'O';
// Switch from 'O' and 'X'
else if (player == 'O')
player = 'X';
}
/* Returns true if the current player has won the game. Three in a row, column * or either diagonal. Otherwise, return false.
*/ public boolean won()
{ /* Step 5: Fill in the code for the won method. This method should return true * if the current player has 3 in-a-row in any row, column or diagonal. * Otherwise, return false.*/
if (board[0][0] == player) {
if (board[0][1] == player && board[0][2] == player)
return true;
if (board[1][0] == player && board[2][0] == player)
return true;
if (board[1][1] == player && board[2][2] == player)
return true; }
if (board[0][1] == player) {
if (board[1][1] == player && board[2][1] == player)
return true; }
if (board[0][2] == player) {
if (board[1][2] == player && board[2][2] == player)
return true;
if (board[1][1] == player && board[2][0] == player)
return true;
}
if (board[1][0] == player) {
if (board[1][1] == player && board[1][2] == player)
return true;
}
if (board[2][0] == player) {
if (board[2][1] == player && board[2][2] == player)
return true;
}
return false; // TODO: replace with your own return statement. }
/* Returns true if there are no places left to move*/
ublic boolean stalemate() {
/*Step 4: Fill in the code for the stalemate method. It should return true if * there are no more places to move on the board. Otherwise, return false;*/
for (int row = 0; row <= 2; row++) {
for (int column = 0; column <= 2; column++) {
if (board[row][column] == ' ')
return false;
} }
return true; // replace with your own return }
public char getPlayer() {
return player; }
public void print() {
System.out.println();
System.out.println("\t 1 2 3");
System.out.println();
System.out.println("\tA " + board[0][0] + "|" + board[0][1] + "|" + board[0][2]);
System.out.println("\t -----");
System.out.println("\tB " + board[1][0] + "|" + board[1][1] + "|" + board[1][2]);
System.out.println("\t " + "-----");
System.out.println("\tC " + board[2][0] + "|" + board[2][1] + "|" + board[2][2]);
System.out.println(); }
/* Step 6: Main Method for Final Step - Delete your main method and uncomment * this one. Runs the game by getting input from the user, making the * appropriate moves, and prints who won or if it was a stalemate.*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
TicTacToe game = new TicTacToe();
System.out.println("Welcome to tic-tac-toe");
System.out.println("Enter coordinates for your move following the X and O prompts");
while (!game.stalemate())
{ // Print the game
game.print();
// Prompt player for their move
System.out.print("Please type the position where you want to move \n" + game.player +":");
// Loop while the method play does not return true when given their move.
while(true) {
String s = in.next();
boolean validMove = game.play(s);
if (validMove)
break;
// Body of loop should ask for a different move
else
System.out.println("Your move is not valid, please enter another move");
} // If the game is won, call break;
if (game.won())
break;
// Switch the turn game.switchTurn();
} game.print();
if (game.won()) {
System.out.println("Player " + game.getPlayer() + " Wins!!!!");
} else {
System.out.println("Stalemate"); }
in.close();} }
counter free