What's new

Java ERROR!!! HELP :(

  • Thread starter h2o pete
  • Start date
  • Views 4,423
H

h2o pete

VIP
VIP
Retired
Messages
4,507
Reaction score
531
Points
485
Sin$
0
Thanks! How do I put that into a new project to test it out?
 
Amurka

Amurka

Getting There
Messages
1,617
Reaction score
349
Points
190
Sin$
0
Code:
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

you know how to do user input?
 
H

h2o pete

VIP
VIP
Retired
Messages
4,507
Reaction score
531
Points
485
Sin$
0
No I don't think we've been taught that lol :frown:

I think I tried it like that earlier but I couldn't get it to work I must have been doing something wrong lol. I'll try again tomorrow beacuse it's late here now.
 
Amurka

Amurka

Getting There
Messages
1,617
Reaction score
349
Points
190
Sin$
0
this works, i just tried it:
Code:
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");
}
}
}
 
H

h2o pete

VIP
VIP
Retired
Messages
4,507
Reaction score
531
Points
485
Sin$
0
That's awesome thanks! :biggrin:

I've got a week left to hand in the project now.. I think I've got most of it done with a lot of help for you haha! I do actually understand it now though unlike when she was teaching us...


You got any ideas on this one?

Code:
Since we need to use equals() to compare object values, what does the '==' operator do when used with objects?
 
Amurka

Amurka

Getting There
Messages
1,617
Reaction score
349
Points
190
Sin$
0
lol idk how to say it. its something like:
== checks to see if the reference is the same
.equals checks to see if the two things it is comparing are the same, but like different references. so it checks the things that the reference points to and sees if they are the same, it doesnt check to see if the reference is the same.
 
H

h2o pete

VIP
VIP
Retired
Messages
4,507
Reaction score
531
Points
485
Sin$
0
i dont get what you mean by reference! lol

can you give me like an example or something? :biggrin:
 
H

h2o pete

VIP
VIP
Retired
Messages
4,507
Reaction score
531
Points
485
Sin$
0
ahh thanks i think i get it now!

that was awesome haha! :thumbup:
 
Amurka

Amurka

Getting There
Messages
1,617
Reaction score
349
Points
190
Sin$
0
glad you liked it lol...i just forgot I had an assignment due at midnight for my cs class haha just banged out over 2000 lines of it out today.
 
H

h2o pete

VIP
VIP
Retired
Messages
4,507
Reaction score
531
Points
485
Sin$
0
I should probably link you to the questions it's easier than copying the code haha :tongue:

Code:
http://www.coventry.ac.uk/swift/~csx067/110ct/portfolio09/lecture7.htm
I'm stuck on 7.4 and 7.5 and then I'm done with 7 :biggrin: thanks againnn

Also if you could help with 8.4 from this one then I'm totally done with 8 as well :biggrin: that would be awesome!

Code:
http://www.coventry.ac.uk/swift/~csx067/110ct/portfolio09/lecture8.htm

And again lol if I can do 9.5 from this one then I'm done with all of 9

Code:
http://www.coventry.ac.uk/swift/~csx067/110ct/portfolio09/lecture9.htm

If you could give me some tips or something that would be great!

It's just the last few questions now and then I'm nearly ready to hand this in :smile:

I hate to ask you all these questions lol it's just you help me understand so much better than I did in the lectures!

For this one we have to say what would be returned by objectA:getColour(); at the end of the sequence and then explain why that would be returned. Have I got this totally wrong lol because I just think it's red but that makes it seem a bit easy :?

objecttypesdemo.jpg
 
Amurka

Amurka

Getting There
Messages
1,617
Reaction score
349
Points
190
Sin$
0
no, itd be green lol. Its the same reference so it would set the new color to to the same object twice, making it red, then making it green. hope i explained that well, if not ill explain more...and ill help with the others later when i get back
 
H

h2o pete

VIP
VIP
Retired
Messages
4,507
Reaction score
531
Points
485
Sin$
0
Ah I think I get it lol thanks! :smile:

And yeah any help with the others would be great I'm stuck on them lol :biggrin:

Any ideas?? :biggrin:
 
Amurka

Amurka

Getting There
Messages
1,617
Reaction score
349
Points
190
Sin$
0
7.4:
Code:
Circle pen;
pen = new Circle(10) //10 being the int parameter
7.5:
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...
Code:
// assign objects
mySecondHobby = myFirstHobby;
8.4:
I could do it, but I don't have the original project or don't see where I could get it...?

9.5:
Again, can't do it without an "earlier portfolio"
 
H

h2o pete

VIP
VIP
Retired
Messages
4,507
Reaction score
531
Points
485
Sin$
0
thanks man!!!!!! :biggrin:

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...

Yep I get it now thanks!! It's easier than I thought, I guess I just didn't spot the statement when I looked through it lol :frown:. Thanks!!

the 'original project' is the same for each one - the scissors, paper, stone game.

here's my code for it so far:

BTW i noticed a while ago that my teacher has spelt 'lose' as 'loose' LOL so basically whenever you see 'loose' it means 'lose' :tongue:

Code:
/**
* 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("=====================================================");
}

}
 
Amurka

Amurka

Getting There
Messages
1,617
Reaction score
349
Points
190
Sin$
0
it's rock, not stone! Rock paper scissors, not scissors paper stone?!?!:cursing: lol
but...

8.4:
anything I added or changed is between
//--------------------
blah
//--------------------
It's only asking to add two instance variables basically, gamesPlayed and gamesWon. I didn't add getter methods, because it didn't ask for them, but you could easily do that if you need to.
Code:
/**
* 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("=====================================================");
}

}

9.5:
What they want is basically just a user input way of starting the game instead of a random generator like the Dice class does. The input would look something like this:
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();
}
don't forget the import statement needed for Scanner:
Code:
import java.util.Scanner;
 
H

h2o pete

VIP
VIP
Retired
Messages
4,507
Reaction score
531
Points
485
Sin$
0
Thanks man!

I'm gonna look at this in the morning and make sure I understand them :biggrin:

And I know I call it rock, paper, scissors too lol but with her spelling 'loose' instead of 'lose' I'm starting to think she's stupid :rolleyes:

I understand all of the ones you went through now :biggrin:! Thanks man!

Any ideas on 10.6?

110CT: Lecture 10 Portfolio

This notebook one is really confusing :frown:

What I've got so far:

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

I can't get 9.5 to work now either lol :frown:

Do I have to add an if statement or something?

Code:
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();
}
}
}
 
Amurka

Amurka

Getting There
Messages
1,617
Reaction score
349
Points
190
Sin$
0
I can't get 9.5 to work now either lol :frown:

Do I have to add an if statement or something?

Code:
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]
}
[/QUOTE]

get rid of them brackets idk why they are there and then just add if statements for each of the three player types like you did. i guess thatd work. i think it just wants to make sure you can do user input.
 
H

h2o pete

VIP
VIP
Retired
Messages
4,507
Reaction score
531
Points
485
Sin$
0
I'm using this:

Code:
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();
}

But when I try to play using that, it lets me give an option but then brings me back to the code and says there's a problem with this:

Code:
/**
*  opponentGuess generates a random guess from the opponent
*/
private int opponentGuess()
{
return opponentSelector.roll();
}

java.lang.NullPointerException
at Game.opponentGuess(Game.java:90)
at Game.playPaper(Game.java:71)
at Game.play(Game.java:49)

:?
 
Amurka

Amurka

Getting There
Messages
1,617
Reaction score
349
Points
190
Sin$
0
show me all your code. Is there another class that goes with this?
 
Top Bottom
Login
Register