Tuesday, July 17, 2012

Coin Game in Java

import java.util.Scanner;
import java.util.Random;
public class CoinGame
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter '0' if you want to play against a computer or '1' if you want to play against another player.");
int start = keyboard.nextInt();
int score = 0;
int n = 1;
String a;
String b;
String ht = "ht";
Random coin = new Random();
if (start == 1) {
System.out.println("Welcome to the coin-guessing game.");
System.out.println("Player 1's objective is to guess te opposite of what Player 2 will guess.");
System.out.println("Player 2's objective is to guess what Player 1 has guess that Player 2 will not guess.");
System.out.println(" In other words, Player 2 wants to guess Player 1's input.");
System.out.println("Every set that goes in Player 1's favor will decrease the score by 1.");
System.out.println("Every set that goes in Player 2's favor will increase the score by 1.");
System.out.println("Player 1 wins if the score reaches -5 while Player 2 wins if the score reaches +5.");
while( score*score < 25) {
System.out.println(" ");
System.out.println("Set " + n);
System.out.println("Now, Player 1, with Player 2 not looking, enter 'h' (for heads) or 't' (for tails).");
a = keyboard.next();
char c = a.charAt(0);
while (c != 'h' & c != 't'){
System.out.println("Player 1, you did not enter 'h' (for heads) or 't' (for tails).");
System.out.println("Please try again.");
a = keyboard.next();
c = a.charAt(0);
}
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println("40 blank lines were just printed so that Player 2 should not see Player 1's input.");
System.out.println("Now, Player 2, guess Player 1's input by entering 'h' (for heads) or 't' (for tails).");
b = keyboard.next();
char d = b.charAt(0);
System.out.println("Player 1 had entered " + c + " while Player 2 had entered " + d + ".");
n = n+1;
if (c == d)
score = score + 1;
else
score = score - 1;
if (score > 0)
System.out.println("The score is now " + score + " in Player 2's favor.");
if (score < 0)
System.out.println("The score is now " + score + " in Player 1's favor.");
if (score == 0)
System.out.println("The score is now tied at " + score + ".");
if (score == 4)
System.out.println("Uh-oh, looks like this game is about to be over. Match Point!");
if (score == -4)
System.out.println("Uh-oh, looks like this game is about to be over. Match Point!");
}
if(score == 5)
System.out.println("Player 2 Wins!");
else
System.out.println("Player 1 Wins!");
}
if (start == 0) {
System.out.println("Welcome to the coin-guessing game.");
System.out.println("Your objective is to guess te opposite of what the computer will guess.");
System.out.println("The computer's guesses will be randomly generated.");
System.out.println("Every set that goes in your favor will decrease the score by 1.");
System.out.println("Every set that goes in the computer's favor will increase the score by 1.");
System.out.println("You win if the score reaches -5 while the computer wins if the score reaches +5.");
while( score*score < 25) {
System.out.println(" ");
System.out.println("Set " + n);
System.out.println("Please enter 'h' (for heads) or 't' (for tails).");
a = keyboard.next();
char c = a.charAt(0);
while (c != 'h' & c != 't'){
System.out.println("You did not enter 'h' (for heads) or 't' (for tails).");
System.out.println("Please try again.");
a = keyboard.next();
c = a.charAt(0);
}
int f = coin.nextInt(2);
String e = ht.substring(f,f+1);
char d = e.charAt(0);
System.out.println("You entered " + c + " while the computer had entered " + d + ".");
n = n+1;
if (c == d)
score = score + 1;
else
score = score - 1;
if (score > 0)
System.out.println("The score is now " + score + " in the computer's favor.");
if (score < 0)
System.out.println("The score is now " + score + " in your favor.");
if (score == 0)
System.out.println("The score is now tied at " + score + ".");
if (score == 4)
System.out.println("Uh-oh, looks like this game is about to be over. Match Point!");
if (score == -4)
System.out.println("Uh-oh, looks like this game is about to be over. Match Point!");
}
if(score == 5)
System.out.println("Sorry, You Lost.");
else
System.out.println("You Win!");
}
}
}

Monday, July 2, 2012

box of produce JAVA CODE


Description:
* This program reads in three random vegetables from a predefined list and asks if the user would like
* to substitute any produce. The user can substitute produce as many times as they like.
* Once the user confirms the content of the box, the contents of the final box is output.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TestBoxOfProduce
{
public static void main(String[] args)
{

// reads in the ListOfProduce file
Scanner file = null;
try {
file = new Scanner(new FileInputStream("text"));
}
catch (FileNotFoundException e) {}

String item1 = file.next();
String item2 = file.next();
String item3 = file.next();
String item4 = file.next();
String item5 = file.next();
Scanner user = new Scanner(System.in);

BoxOfProduce1 CSA = new BoxOfProduce1();

System.out.println("Your box of produce contains: ");
System.out.println("[ "+CSA.getProduce1() + ", "
+ CSA.getProduce2() + ", " + CSA.getProduce3()+" ]");

String loop = "n";
System.out.println("\nAre you happy with your selection? (Y/N)\n>");
loop = user.next();

//begins loop if user is not satisfied with their selection
while (loop.equalsIgnoreCase("n"))
{
Scanner user2 = new Scanner(System.in);
Scanner user3 = new Scanner(System.in);

System.out.println("\nYou can choose from the following items: \n(1)" + item1 +" \n(2)" + item2+" \n(3)"+item3+" \n(4)"+item4+" \n(5)"+item5);
System.out.println("Which item do you prefer? Please select an item number.\n>");
int choice = user2.nextInt();
String item = null;

//given user input for their choice, sets the item to this new choice
if (choice==1)
item =item1;
if (choice==2)
item =item2;
if (choice==3)
item=item3;
if (choice==4)
item =item4;
if (choice==5)
item =item5;
System.out.println("You prefer "+item.toLowerCase());

System.out.println("\nWhich item would you like to replace?");
System.out.println("(1)"+CSA.getProduce1() + " "
+ "(2)" + CSA.getProduce2() + " " + "(3)"+CSA.getProduce3()+"\n>");
int replace = user3.nextInt();

if (replace == 1)
{
CSA.setProduce1(item);
}
if (replace == 2)
{
CSA.setProduce2(item);
}
if (replace == 3)
{
CSA.setProduce3(item);
}


System.out.println("\nYour new selection contains: ");
System.out.println(CSA.getProduce1() + ", "
+ CSA.getProduce2() + ", " + CSA.getProduce3());

System.out.println("\nAre you happy with your selection? (Y/N)");
loop = user.next();

}


System.out.println("Enjoy your selection.");

}
}

*************************************

file #2

Description:
* This program defines the class, accessor, and mutator variables.
* It takes values from the file, "ListOfProduce," puts them into an array with numbers corresponding to each item.
* It produces three random numbers and accesses the corresponding value of the array.
*


import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.io.*;


public class BoxOfProduce1
{
private String produce1, produce2, produce3 ;

public BoxOfProduce1()
{
this.produce1 = "";
this.produce2 = "";
this.produce3 = "";

//creates an array from the read in file (ListOfProduce)
ArrayList<String> produce = new ArrayList<String>();

Scanner file = null;
try {
file = new Scanner(new FileInputStream("text"));
}
catch (FileNotFoundException e)
{
}
String item1 = file.next();
String item2 = file.next();
String item3 = file.next();
String item4 = file.next();
String item5 = file.next();

produce.add(item1);
produce.add(item2);
produce.add(item3);
produce.add(item4);
produce.add(item5);

// creates 3 random variables from a possible 5 which correspond to the values in the array
Random r= new Random();
int x1 = r.nextInt(5);
int x2 = r.nextInt(5);
int x3 = r.nextInt(5);

this.produce1 = produce.get(x1);
this.produce2 = produce.get(x2);
this.produce3 = produce.get(x3);


}
public BoxOfProduce1 (String newProduce1, String newProduce2, String newProduce3)
{
this.produce1 = newProduce1;
this.produce2 = newProduce2;
this.produce3 = newProduce3;
}

//accessor1
public String getProduce1()
{
return this.produce1;
}

//mutator1
public void setProduce1 (String newProduce1)
{
this.produce1 = newProduce1;
}
//accessor2
public String getProduce2()
{
return this.produce2;
}
//mutator2
public void setProduce2(String newProduce2)
{
this.produce2 = newProduce2;
}
//accessor3
public String getProduce3()
{
return this.produce3;
}
//mutator3
public void setProduce3(String newProduce3)
{
this.produce3 = newProduce3;
}
public String toString()
{
return "Produce 1: " + this.produce1 + "Produce 2: " + this.produce2 +" Produce 3: " + this.produce3;
}
}