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;
}
}

Wednesday, June 20, 2012

Height Estimation Program, JAVA

/**
  * This program gives an estimate of a child's height using the formula:
* H(male) = (H(mother)*13/12 + H(father))/2
* H(female) = (H(father)*12/13 + H(mother))/2
* By allowing the user to input the height in feet and inches, conversions are possible.
*/

public class Username {

public static void main(String[] args) {
// Variable declarations

int mother_feet, mother_inches;
int father_feet, father_inches;
int child_total_inches;
Scanner scan = new Scanner(System.in);
String gender;
String male;
String female;
do{
Scanner keyboard=new Scanner(System.in);
System.out.println("Enter the gender of the child. ");
gender = keyboard.nextLine();

System.out.println("Enter the height in feet then the height " +
"in inches of the mother.");
mother_feet = keyboard.nextInt();
mother_inches = keyboard.nextInt();

System.out.println("Enter the height in feet then the height " +
"in inches of the father.");
father_feet = keyboard.nextInt();
father_inches = keyboard.nextInt();

// Convert input to inches and estimate the adult height of the child
int mother_height = (mother_feet*12)+mother_inches;
int father_height = (father_feet*12)+father_inches;
if (gender.equals("male"))
{
// Male child formula
child_total_inches = ((mother_height * 13 / 12)
+ father_height)/2;
}
else
{
// Female child formula
child_total_inches = ((father_height * 12 / 13)
+ mother_height)/2;
}
// Output the estimated height
System.out.println("Your child is estimated to grow to " +
child_total_inches / 12 + " feet and " +
child_total_inches % 12 + " inches.");
System.out.println("Do you want to continue?");
String yes=keyboard.next("yes");
if (yes.equals(yes))
continue;
else
break;

}while(true);
}
} // Height

Tuesday, May 8, 2012

Rotate an image in JAVA

//write one program that will rotate an image 180 degrees in a clockwise direction (or counter clockwise
//direction since both give the same result) and one program that will rotate an image 90 degrees in a
//counter-clockwise direction.








public class Lab10a
{

public static void main (String[] args)
{

System.out.println("bilal abuzenah");

// Access and open the picture
String filename = FileChooser.pickAFile ();
Picture p = new Picture (filename);

// call the method to modify the Pictture
Picture p2;
p2 = modifyPicture (p);

// explore (display) the picture
//p2.explore();
p2.show();
String saveFilename = FileChooser.pickAFile ();
p2.write (saveFilename);
// get a new filename and save the picture
//String saveFilename = FileChooser.pickAFile ();
//p2.write (saveFilename);

} // end of main

public static Picture modifyPicture (Picture p)
{
// get the width and height of the picture
int width = p.getWidth();
int height = p.getHeight();
//System.out.println ("The picture is " + width + " pixels wide and " + height + " pixel high.");

System.out.println ("Width: " + width + ", Height: " + height);

// create the new Picture that will be returned
Picture resultPicture;
resultPicture = new Picture (width, height);

// Set up a loop to access all the X position
int xPos;
int yPos;
int xNew;
int yNew;
Pixel pix;
Pixel pix2;
int red;
int green;
int blue;

for ( xPos = 0 ; xPos < width ; ++xPos )
{
for ( yPos = 0 ; yPos < height ; ++yPos )
{
// access the pixel to be modifed
pix = p.getPixel (xPos, yPos);

xNew = (width - 1) - xPos;
yNew = (height - 1) - yPos;
pix2 = resultPicture.getPixel (xNew, yNew);



// get the colr values from the original pixel
red = pix.getRed();
green = pix.getGreen();
blue = pix.getBlue();

// set those color values into the result pixel
pix2.setRed(red);
pix2.setGreen(green);
pix2.setBlue(blue);

//System.out.println ("The picture is " + xNew + " pixels wide and " + yNew + " pixel high.");


}
}

return resultPicture;
} // end of modifyPicture

} // end of class

cropping an image using java

//This lab assignment will have you write at least two methods in java that will create a new image that will
 //show a portion from another image. This technique is called cropping.
//** a specific image was used for this, u need to change the values!!






import java.awt.Color;

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

// use the string files to get the first picture,
String filename = FileChooser.pickAFile();
Picture p1 = new Picture (filename);

Picture p2 ;
int x1, x2;
int y1, y2;
// determine the values by using p1.explore()

//Input your values of X, Y of the part you want to crop from the image.

x1 =123 ;
x2 = 621;
y1 = 134;
y2 = 292;

// call method
p2 = cropImg (p1,x1,y1,x2,y2);

//use explore to determine the part you want crop
//p1.explore();

//display the picture
p2.show();

// show the original image
//p1.show();
// get a new filename and save the picture
String saveFilename = FileChooser.pickAFile ();
//p2.write (saveFilename);

} // end of main

// alternate way to access the pixels from three different pictures
public static Picture cropImg (Picture p1,
int x1, int y1,
int x2, int y2)
{
// set up my return variable
Picture p2;

// set dimensions of the new created picture and copy pixels over to p2
p2 = new Picture ((x2-x1),(y2-y1));

// access pixels.
Pixel pixelArray[] = p1.getPixels();

for (int i = 0 ; i < pixelArray.length ; i++)
{
// access the original pixel and information from that pixel
Pixel pix1 = pixelArray [i];
int red = pix1.getRed ();
int green = pix1.getGreen ();
int blue = pix1.getBlue ();
int xPos = pix1.getX();
int yPos = pix1.getY();

// access the portion of the picture to be cropped
if ( ((xPos >= x1) &&
(xPos < x2)) &&
((yPos >= y1) &&
(yPos < y2)) )
{
// detirmine the new location for the pixels
int X3;
X3 = xPos-x1;
int Y3;
Y3 = yPos-y1;

Pixel pix2 = p2.getPixel (X3, Y3);

// Set colors
pix2.setRed (red);
pix2.setGreen (green);
pix2.setBlue (blue);

}

} // end of for loop

return p2;

} // end of cropImg method

} // end of class

combine two sound files to add a "background" soundtrack to a foreground sound.

//This lab will have you combine two sound files to add a "background" soundtrack to a foreground sound.
 //The following files of the Gettysburg Address and the Preamble of the U.S. Constitution are great
 //foreground sound files.






public class Lab13
{

public static void main (String args[])
{
System.out.println("Begin Java Execution");
System.out.println("");

// put your Java Program here

// pick the first file.
String filename = FileChooser.pickAFile();
Sound foregroundSound = new Sound (filename);

// pick the background sound file.
String filename2 = FileChooser.pickAFile();
Sound backgroundSound = new Sound (filename2);

// combine the sounds
// Call a method to combine the two sounds
Sound s3;
s3 = combinesound (foregroundSound, backgroundSound);
// Play the sound
//s3.play();

s3.explore();

// save the new sound file

String saveFilename = FileChooser.pickAFile ();
s3.write (saveFilename);


System.out.println("");
System.out.println("End Java Execution");

}
// Start a combinesound method.

public static Sound combinesound (Sound foregroundSound, Sound backgroundSound)
{
SoundSample[] fg = foregroundSound.getSamples();
SoundSample[] bg = backgroundSound.getSamples();

// set length3 to the length of the shorter sound
int length3;
if ( fg.length < bg.length )
length3 = fg.length;
else
length3 = bg.length;

Sound s3 = new Sound (length3);
SoundSample[] ssarr3 = s3.getSamples();

int pos;

for ( pos = 0 ; pos < ssarr3.length ; pos++ )
{
int amplitude1 = fg[pos].getValue();
int bgp = pos;

while ( bgp >= bg.length )
{
bgp = bgp - bg.length ;
}

int amplitude2 = (bg[bgp].getValue())/3;

// combine the two amplitudes (add them together)
int amplitude3 = amplitude1 + amplitude2;

// check for clipping and resolve
if ( amplitude3 > 32767 )
{
amplitude3 = 32767;
}
if ( amplitude3 < -32768 )
{
amplitude3 = -32768;
}

// store the new amplitude value into sounnd s3
ssarr3[pos].setValue (amplitude3);
}

return s3;
}


} // end of Template class