H
h2o pete
VIP
VIP
Retired
Thanks! How do I put that into a new project to test it out?
public class Tester{
public static void main(String [] args){
int i= //getinput
while(i> 0){
System.out.println(i + " green bottles hanging on the wall \n"
+ i + " green bottles hanging on the wall \n"
+ "and if one green bottle should accidentally fall");
i--;
System.out.println("there'd be " + i + " green bottles hanging on the wall \n");
}//loop
}//main
}//class
import java.util.*;
public class Tester{
public static void main(String [] args){
Scanner inScan = new Scanner(System.in);
System.out.print("Enter the starting number of bottles: ");
int i = inScan.nextInt();
while(i > 0){
System.out.println(i + " green bottles hanging on the wall \n"
+ i + " green bottles hanging on the wall \n"
+ "and if one green bottle should accidentally fall");
i--;
System.out.println("there'd be " + i + " green bottles hanging on the wall \n");
}
}
}
Since we need to use equals() to compare object values, what does the '==' operator do when used with objects?
http://www.coventry.ac.uk/swift/~csx067/110ct/portfolio09/lecture7.htm
http://www.coventry.ac.uk/swift/~csx067/110ct/portfolio09/lecture8.htm
http://www.coventry.ac.uk/swift/~csx067/110ct/portfolio09/lecture9.htm
Circle pen;
pen = new Circle(10) //10 being the int parameter
// assign objects
mySecondHobby = myFirstHobby;
Amurka said:In the trial method, the line of code below makes it so that mySecondHobby references the same object as myFirstHobby. Therefore, by changing the date to myFirstHobby, the date to mySecondHobby is also the same. It's the same idea as the last picture with the color changing thing. They are both referencing the same object, so you do something to one of them, it's like you did it to both of them. If you need a better explanation, let me know...
/**
* Plays scissors/paper/stone against the computer's collection
*
* Rules are:
* scissors beats paper (cuts it)
* paper beats stone (wraps it)
* stone beats scissors (blunts them)
* Code as:
* scissors = 1
* paper = 2
* stone = 3
*
* @author Lisa Payne
* @version Nov 2007
*/
public class Game
{
// A Die object is used to create a random value
private Die opponentSelector;
// opponent is the opponent's guess 1-3
private int opponent;
// player is the player's guess 1-3
private int player;
/**
* Constructor initialises all attributes
*/
public Game()
{
// opponentSelector creates random value 1-3
opponentSelector = new Die(3);
opponent = 0;
player = 0;
}
/**
* Player chooses scissors
*/
public void playScissors()
{
player = 1;
opponent = this.opponentGuess();
this.calcResult();
}
/**
* Player chooses paper
*/
public void playPaper()
{
player = 2;
opponent = this.opponentGuess();
this.calcResult();
}
/**
* Player chooses stone
*/
public void playStone()
{
player = 3;
opponent = this.opponentGuess();
this.calcResult();
}
/**
* opponentGuess generates a random guess from the opponent
*/
private int opponentGuess()
{
return opponentSelector.roll();
}
/**
* calculates the game's winner
*/
private void calcResult()
{
switch (player) {
case 1:
switch (opponent) {
case 1: this.displayResult("scissors", "scissors", "draw");
break;
case 2: this.displayResult("scissors", "paper", "win");
break;
case 3: this.displayResult("scissors", "stone", "loose");
break;
default: System.out.println("Invalid opponent choice");
}
break;
case 2:
switch (opponent) {
case 1: this.displayResult("paper", "paper", "draw");
break;
case 2: this.displayResult("paper", "stone", "win");
break;
case 3: this.displayResult("paper", "scissors", "loose");
break;
default: System.out.println("Invalid opponent choice");
}
break;
case 3:
switch (opponent) {
case 1: this.displayResult("stone", "stone", "draw");
break;
case 2: this.displayResult("stone", "scissors", "win");
break;
case 3: this.displayResult("stone", "paper", "loose");
break;
default: System.out.println("Invalid opponent choice");
}
break;
default: System.out.println("invalid player choice");
}
}
/**
* displayResult
*/
private void displayResult(String playChoice, String oppChoice, String result)
{
System.out.println();
System.out.println("=====================================================");
System.out.print("You chose: " + playChoice + " ");
System.out.println("and the computer chose: " + oppChoice + " ");
System.out.println("So you **" + result + "** this game");
System.out.println("=====================================================");
}
}
/**
* Plays scissors/paper/stone against the computer's collection
*
* Rules are:
* scissors beats paper (cuts it)
* paper beats stone (wraps it)
* stone beats scissors (blunts them)
* Code as:
* scissors = 1
* paper = 2
* stone = 3
*
* @author Lisa Payne
* @version Nov 2007
*/
public class Game
{
// A Die object is used to create a random value
private Die opponentSelector;
// opponent is the opponent's guess 1-3
private int opponent;
// player is the player's guess 1-3
private int player;
//-----------------------------------
private int gamesPlayed, gamesWon;
//-----------------------------------
/**
* Constructor initialises all attributes
*/
public Game()
{
// opponentSelector creates random value 1-3
opponentSelector = new Die(3);
opponent = 0;
player = 0;
//-----------------------------------
gamesPlayed = 0;
gamesWon = 0;
//-----------------------------------
}
/**
* Player chooses scissors
*/
public void playScissors()
{
player = 1;
opponent = this.opponentGuess();
this.calcResult();
}
/**
* Player chooses paper
*/
public void playPaper()
{
player = 2;
opponent = this.opponentGuess();
this.calcResult();
}
/**
* Player chooses stone
*/
public void playStone()
{
player = 3;
opponent = this.opponentGuess();
this.calcResult();
}
/**
* opponentGuess generates a random guess from the opponent
*/
private int opponentGuess()
{
return opponentSelector.roll();
}
/**
* calculates the game's winner
*/
private void calcResult()
{
//-----------------------------------
gamesPlayed++;
//-----------------------------------
switch (player) {
case 1:
switch (opponent) {
case 1: this.displayResult("scissors", "scissors", "draw");
break;
case 2: this.displayResult("scissors", "paper", "win");
//-----------------------------------
gamesWon++;
//-----------------------------------
break;
case 3: this.displayResult("scissors", "stone", "loose");
break;
default: System.out.println("Invalid opponent choice");
}
break;
case 2:
switch (opponent) {
case 1: this.displayResult("paper", "paper", "draw");
break;
case 2: this.displayResult("paper", "stone", "win");
//-----------------------------------
gamesWon++;
//-----------------------------------
break;
case 3: this.displayResult("paper", "scissors", "loose");
break;
default: System.out.println("Invalid opponent choice");
}
break;
case 3:
switch (opponent) {
case 1: this.displayResult("stone", "stone", "draw");
break;
case 2: this.displayResult("stone", "scissors", "win");
//-----------------------------------
gamesWon++;
//-----------------------------------
break;
case 3: this.displayResult("stone", "paper", "loose");
break;
default: System.out.println("Invalid opponent choice");
}
break;
default: System.out.println("invalid player choice");
}
}
/**
* displayResult
*/
private void displayResult(String playChoice, String oppChoice, String result)
{
System.out.println();
System.out.println("=====================================================");
System.out.print("You chose: " + playChoice + " ");
System.out.println("and the computer chose: " + oppChoice + " ");
System.out.println("So you **" + result + "** this game");
//-----------------------------------
System.out.println("Games Played: " + gamesPlayed + ", Games Won: " + gamesWon);
//-----------------------------------
System.out.println("=====================================================");
}
}
don't forget the import statement needed for Scanner:Scanner inScan = new Scanner(System.in);
System.out.println("Choose your play choice: (1 = Scissors; 2 = Paper; 3 = Stone)");
player = inScan.nextInt();
while(player < 1 || player > 3){
System.out.println("Invalid input. Please re-enter: (1 = Scissors; 2 = Paper; 3 = Stone)");
player = inScan.nextInt();
}
import java.util.Scanner;
import java.util.ArrayList;
/**
* A class to maintain an arbitrarily long list of notes.
* Notes are numbered for external reference by a human user.
* In this version, note numbers start at 0.
*
* @author David J. Barnes and Michael Kolling.
* @version 2006.03.30
*/
public class Notebook
{
// Storage for an arbitrary number of notes.
private ArrayList<String> notes;
/**
* Perform any initialization that is required for the
* notebook.
*/
public Notebook()
{
notes = new ArrayList<String>();
}
/**
* Store a new note into the notebook.
* @param note The note to be stored.
*/
public void storeNote(String note)
{
notes.add(note);
}
/**
* @return The number of notes currently in the notebook.
*/
public int numberOfNotes()
{
return notes.size();
}
/**
* Show a note.
* @param noteNumber The number of the note to be shown.
*/
public void showNote(int noteNumber)
{
if(noteNumber < 0) {
System.out.println("Note number entered is not valid");
}
else if(noteNumber < numberOfNotes()) {
// This is a valid note number, so we can print it.
System.out.println(notes.get(noteNumber));
}
else {
System.out.println("Note number entered is not valid");
}
}
/**
* Remove a note.
*/
public void removeNote(int noteNumber)
{
if(noteNumber < 0) {
System.out.println("Note number entered is not valid");
}
else if(noteNumber < numberOfNotes()) {
// This is a valid note number, so we can remove it.
notes.remove(noteNumber);
}
else {
System.out.println("Note number entered is not valid");
}
}
/**
* List all notes in the notebook.
*/
public void listNotes()
{
for(int i = 0; i < notes.size(); i++)
System.out.println(i + ": " + notes.get(i));
}
/**
* Checks for duplicates
*/
public boolean checkDuplicates(int i, int j)
{
String duplicate = "";
if ( notes.get(i).equals( notes.get(j) )) {
duplicate = notes.get(i);
System.out.println("Duplicate found : " + duplicate);
return true;
}
return false;
}
}
public void play()
{
Scanner inScan = new Scanner(System.in);
System.out.println("Choose your play choice: (1 = Scissors; 2 = Paper; 3 = Stone)");
player = inScan.nextInt();
while(player < 1 || player > 3)
{
System.out.println("Invalid input. Please re-enter: (1 = Scissors; 2 = Paper; 3 = Stone)");
player = inScan.nextInt();
}
{
if (player == 1) {
playScissors();
}
}
}
public void play()
{
Scanner inScan = new Scanner(System.in);
System.out.println("Choose your play choice: (1 = Scissors; 2 = Paper; 3 = Stone)");
player = inScan.nextInt();
while(player < 1 || player > 3)
{
System.out.println("Invalid input. Please re-enter: (1 = Scissors; 2 = Paper; 3 = Stone)");
player = inScan.nextInt();
}
[COLOR="Red"]{[/COLOR]
if (player == 1) {
playScissors();
}
[COLOR="red"]}[/COLOR]
}
public void play()
{
Scanner inScan = new Scanner(System.in);
System.out.println("Choose your play choice: (1 = Scissors; 2 = Paper; 3 = Stone)");
player = inScan.nextInt();
while(player < 1 || player > 3)
{
System.out.println("Invalid input. Please re-enter: (1 = Scissors; 2 = Paper; 3 = Stone)");
player = inScan.nextInt();
}
if (player == 1) {
playScissors();
}
if (player == 2) {
playPaper();
}
else
playStone();
}
/**
* opponentGuess generates a random guess from the opponent
*/
private int opponentGuess()
{
return opponentSelector.roll();
}