Results 41 to 60 of 83
Thread: Illegal expression
-
1) Please use code tags when posting code (please see my signature below).
2) The error comes with a line number and you didn't post the offending code or indicate which line throws the error, so still no way of guessing what the problem is.
- 12-09-2009, 08:14 PM #42
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
can anyone give me guidance as to why the setLength is'nt working? when executing Square s = new Square(); and Circle c1 = new Circle(); the circle is the same size as the square when it's supposed to look like a diceJava Code:public Dice(Square bg, Circle tL, Circle tM, Circle tR, Circle mid, Circle bL, Circle bM, Circle bR) { bg.setLength(75); this.background = bg; this.topLeftSpot = tL; this.topMiddleSpot = tM; this.topRightSpot = tR; this.middleSpot = mid; this.bottomLeftSpot = bL; this.bottomMiddleSpot = bM; this.bottomRightSpot = bR; this.reset();
- 12-09-2009, 10:16 PM #43
Member
- Join Date
- Dec 2009
- Posts
- 24
- Rep Power
- 0
yeah dont think i'll be submitting a very good assignment. don't expect any help from the tutor, recieved more help off my 7 month old spaniel. The course units are not much help either
- 12-09-2009, 10:19 PM #44
Member
- Join Date
- Dec 2009
- Posts
- 24
- Rep Power
- 0
The code is
The code can be compiled without any errors but when the aforementioned statement is executed, an error is displayedJava Code:public class Dice { /* instance variables */ private Square background; private Circle topLeftSpot; private Circle topMiddleSpot; private Circle topRightSpot; private Circle middleSpot; private Circle bottomLeftSpot; private Circle bottomMiddleSpot; private Circle bottomRightSpot; /** * Constructor for objects of class Dice */ public Dice(Square bg, Circle tL, Circle tM, Circle tR, Circle mid, Circle bL, Circle bM, Circle bR) { this.background = bg; this.topLeftSpot = tL; this.topMiddleSpot = tM; this.topRightSpot = tR; this.middleSpot = mid; this.bottomLeftSpot = bL; this.bottomMiddleSpot = bM; this.bottomRightSpot = bR; this.reset(); bg.setLength(75); }
-
Sigh,...
You still never post the exact error message nor indicate which line is causing it. I'm not sure how many times I can request this. I'm afraid that I'm out of here. Best of luck.
- 12-09-2009, 10:36 PM #46
Member
- Join Date
- Dec 2009
- Posts
- 20
- Rep Power
- 0
Hellppp!!
Hi, really stuck does this look right or am I off the right track. its buggin me know.
any help would do kyussy.Java Code:public void changeFace() { int n; n = this.getFace(); if ((n >= 6) || (n <= 0)) { OUDialog.alert("Argument to changeFace() message " + (n) +" is out of range"); } else { return this.getFace(n); } } [
Oli
- 12-09-2009, 11:41 PM #47
Member
- Join Date
- Dec 2009
- Posts
- 24
- Rep Power
- 0
Excuse me fobarable
I posted the error message if you bothered to look properly
-
Yes, you post an error saying incorrect cast, but you never showed the line of code in its context that's throwing that error. Again, we can't help you without more code. Period.
- 12-10-2009, 01:14 PM #49
Member
- Join Date
- Dec 2009
- Posts
- 24
- Rep Power
- 0
Like i said, the code is compiling without an error. its when the code is executed in bluej that the error is displayed.
- 12-10-2009, 02:12 PM #50
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Post the stack trace, not just the error name, and the code in and around where it occurs, and an indication of exactly which line it happens on.
The line you gave us could not have caused that exception. Pure and simple. Either you were running some other code, or that wasn't the line.
-
- 12-10-2009, 02:39 PM #52
Member
- Join Date
- Dec 2009
- Posts
- 24
- Rep Power
- 0
Tolls
Like i have said twice before, i'm new to java, the code is compiling without errors, just will not do as intended when executed in bluej
- 12-10-2009, 03:02 PM #53
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
We're not talking about compiling.
When the program fails while running it will throw an exception. According to your earlier post it was a ClassCastException. That exception will contain what's called a stack trace, which will be a list of classname, method name and line number. Like this:
In this case it's an array out of bounds.Java Code:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at uk.co.hinterland.dt_maven_test.App.deliberateException(App.java:41) at uk.co.hinterland.dt_maven_test.App.main(App.java:36)
We need that, and at least the code for the method it failed in, and an indicator of the line (in my case line 41).
That's a minimum.Java Code:private static void deliberateException() { int[] blah = new int[4]; int i = blah[4]; // FAILED HERE! }
- 12-10-2009, 03:15 PM #54
Member
- Join Date
- Dec 2009
- Posts
- 24
- Rep Power
- 0
you probably are getting slightly annoyed by my lack of knowledge, i do apologise for this, but thats why i have come to this forum. i have posted the code i have written. i have to write certain statements that stem from this code then executed them in a workspace called bluej, the statements are supposed to show a square with a circle in it, it doesn't, so thats why i asked what the error message indicated
and the statement isJava Code:import ou.*; import java.util.Random; /** * Instances of class Dice simulate a playing dice. From outside an instance * of the class, the only messages that should be sent to a Dice object * in normal usage is rollDice() and reset(). * * @author M255 CT * @version 1.0 */ public class Dice { /* instance variables */ private Square background; private Circle topLeftSpot; private Circle topMiddleSpot; private Circle topRightSpot; private Circle middleSpot; private Circle bottomLeftSpot; private Circle bottomMiddleSpot; private Circle bottomRightSpot; /** * Constructor for objects of class Dice */ public Dice(Square bg, Circle tL, Circle tM, Circle tR, Circle mid, Circle bL, Circle bM, Circle bR) { super(); this.background = bg; this.topLeftSpot = tL; this.topMiddleSpot = tM; this.topRightSpot = tR; this.middleSpot = mid; this.bottomLeftSpot = bL; this.bottomMiddleSpot = bM; this.bottomRightSpot = bR; this.reset(); bg.setLength(75); } /* instance methods */ /** * Returns the receiver's background */ public Square getBackground() { return background; } /** * Returns the receiver's topLeftSpot */ public Circle getTopLeftSpot() { return topLeftSpot; } /** * Returns the receiver's topMiddleSpot */ public Circle getTopMiddleSpot() { return topMiddleSpot; } /** * Returns the receiver's topRightSpot */ public Circle getTopRightSpot() { return topRightSpot; } /** * Returns the receiver's middleSpot */ public Circle getMiddleSpot() { return middleSpot; } /** * Returns the receiver's bottomLeftSpott */ public Circle getBottomLeftSpot() { return bottomLeftSpot; } /** * Returns the receiver's bottomMiddleSpot */ public Circle getBottomMiddleSpot() { return bottomMiddleSpot; } /** * Returns the receiver's bottomRightSpot */ public Circle getBottomRightSpot() { return bottomRightSpot; } /** * Sets the x position of the background and the x/y positions of the * seven spots so that they are distributed over the square in the * familiar dice pattern. Finally the method sets the face * of the dice to 1 spot. */ public void reset() { this.getBackground().setXPos(0); this.getTopLeftSpot().setXPos(3); this.getTopLeftSpot().setYPos(3); this.getTopMiddleSpot().setXPos(25); this.getTopMiddleSpot().setYPos(3); this.getTopRightSpot().setXPos(48); this.getTopRightSpot().setYPos(3); this.getMiddleSpot().setXPos(25); this.getMiddleSpot().setYPos(25); this.getBottomLeftSpot().setXPos(3); this.getBottomLeftSpot().setYPos(48); this.getBottomMiddleSpot().setXPos(25); this.getBottomMiddleSpot().setYPos(48); this.getBottomRightSpot().setXPos(48); this.getBottomRightSpot().setYPos(48); this.face1(); } /** * Sets the face of the receiver to 1 spot */ public void face1() { this.getTopLeftSpot().setColour(OUColour.ORANGE); this.getTopMiddleSpot().setColour(OUColour.ORANGE); this.getTopRightSpot().setColour(OUColour.ORANGE); this.getMiddleSpot().setColour(OUColour.BLUE); this.getBottomLeftSpot().setColour(OUColour.ORANGE); this.getBottomMiddleSpot().setColour(OUColour.ORANGE); this.getBottomRightSpot().setColour(OUColour.ORANGE); } /** * Sets the face of the receiver to 2 spots */ public void face2() { this.getTopLeftSpot().setColour(OUColour.ORANGE); this.getTopMiddleSpot().setColour(OUColour.ORANGE); this.getTopRightSpot().setColour(OUColour.BLUE); this.getMiddleSpot().setColour(OUColour.ORANGE); this.getBottomLeftSpot().setColour(OUColour.BLUE); this.getBottomMiddleSpot().setColour(OUColour.ORANGE); this.getBottomRightSpot().setColour(OUColour.ORANGE); } /** * Sets the face of the receiver to 3 spots */ public void face3() { this.getTopLeftSpot().setColour(OUColour.ORANGE); this.getTopMiddleSpot().setColour(OUColour.ORANGE); this.getTopRightSpot().setColour(OUColour.BLUE); this.getMiddleSpot().setColour(OUColour.BLUE); this.getBottomLeftSpot().setColour(OUColour.BLUE); this.getBottomMiddleSpot().setColour(OUColour.ORANGE); this.getBottomRightSpot().setColour(OUColour.ORANGE); } /** * Sets the face of the receiver to 4 spots */ public void face4() { this.getTopLeftSpot().setColour(OUColour.BLUE); this.getTopMiddleSpot().setColour(OUColour.ORANGE); this.getTopRightSpot().setColour(OUColour.BLUE); this.getMiddleSpot().setColour(OUColour.ORANGE); this.getBottomLeftSpot().setColour(OUColour.BLUE); this.getBottomMiddleSpot().setColour(OUColour.ORANGE); this.getBottomRightSpot().setColour(OUColour.BLUE); } /** * Sets the face of the receiver to 5 spots */ public void face5() { this.getTopLeftSpot().setColour(OUColour.BLUE); this.getTopMiddleSpot().setColour(OUColour.ORANGE); this.getTopRightSpot().setColour(OUColour.BLUE); this.getMiddleSpot().setColour(OUColour.BLUE); this.getBottomLeftSpot().setColour(OUColour.BLUE); this.getBottomMiddleSpot().setColour(OUColour.ORANGE); this.getBottomRightSpot().setColour(OUColour.BLUE); } /** * Sets the face of the receiver to 6 spots */ public void face6() { this.getTopLeftSpot().setColour(OUColour.BLUE); this.getTopMiddleSpot().setColour(OUColour.BLUE); this.getTopRightSpot().setColour(OUColour.BLUE); this.getMiddleSpot().setColour(OUColour.ORANGE); this.getBottomLeftSpot().setColour(OUColour.BLUE); this.getBottomMiddleSpot().setColour(OUColour.BLUE); this.getBottomRightSpot().setColour(OUColour.BLUE); } /** * Causes execution to pause by time number of milliseconds */ private void delay(int time) { try { Thread.sleep(time); } catch (Exception e) { System.out.println(e); } } }
square s = new square();
- 12-10-2009, 03:36 PM #55
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
But that line isn't in the code you just posted.
Or if it is, I can't spot it.
Do you get the stack trace? That really is incredibly important. Even BlueJ must give you the stack trace when an exception is thrown.
What is the class Square?
- 12-10-2009, 03:52 PM #56
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
The problem i'm having is that the code is'nt doing what it should, it doesn't produce errors. It should set the squares length to 75, but it doesnt. the code is
the statement is Square s = new Square();Java Code:import ou.*; import java.util.Random; /** * Instances of class Dice simulate a playing dice. From outside an instance * of the class, the only messages that should be sent to a Dice object * in normal usage is rollDice() and reset(). * * @author M255 CT * @version 1.0 */ public class Dice { /* instance variables */ private Square background; private Circle topLeftSpot; private Circle topMiddleSpot; private Circle topRightSpot; private Circle middleSpot; private Circle bottomLeftSpot; private Circle bottomMiddleSpot; private Circle bottomRightSpot; /** * Constructor for objects of class Dice */ public Dice(Square bg, Circle tL, Circle tM, Circle tR, Circle mid, Circle bL, Circle bM, Circle bR) { background = bg; bg.setLength(75); background = bg; topLeftSpot = tL; topMiddleSpot = tM; topRightSpot = tR; middleSpot = mid; bottomLeftSpot = bL; bottomMiddleSpot = bM; bottomRightSpot = bR; reset(); } /* instance methods */ /** * Returns the receiver's background */ public Square getBackground() { return background; } /** * Returns the receiver's topLeftSpot */ public Circle getTopLeftSpot() { return topLeftSpot; } /** * Returns the receiver's topMiddleSpot */ public Circle getTopMiddleSpot() { return topMiddleSpot; } /** * Returns the receiver's topRightSpot */ public Circle getTopRightSpot() { return topRightSpot; } /** * Returns the receiver's middleSpot */ public Circle getMiddleSpot() { return middleSpot; } /** * Returns the receiver's bottomLeftSpott */ public Circle getBottomLeftSpot() { return bottomLeftSpot; } /** * Returns the receiver's bottomMiddleSpot */ public Circle getBottomMiddleSpot() { return bottomMiddleSpot; } /** * Returns the receiver's bottomRightSpot */ public Circle getBottomRightSpot() { return bottomRightSpot; } /** * Sets the x position of the background and the x/y positions of the * seven spots so that they are distributed over the square in the * familiar dice pattern. Finally the method sets the face * of the dice to 1 spot. */ public void reset() { this.getBackground().setXPos(0); this.getTopLeftSpot().setXPos(3); this.getTopLeftSpot().setYPos(3); this.getTopMiddleSpot().setXPos(25); this.getTopMiddleSpot().setYPos(3); this.getTopRightSpot().setXPos(48); this.getTopRightSpot().setYPos(3); this.getMiddleSpot().setXPos(25); this.getMiddleSpot().setYPos(25); this.getBottomLeftSpot().setXPos(3); this.getBottomLeftSpot().setYPos(48); this.getBottomMiddleSpot().setXPos(25); this.getBottomMiddleSpot().setYPos(48); this.getBottomRightSpot().setXPos(48); this.getBottomRightSpot().setYPos(48); this.face1(); } /** * Sets the face of the receiver to 1 spot */ public void face1() { this.getTopLeftSpot().setColour(OUColour.ORANGE); this.getTopMiddleSpot().setColour(OUColour.ORANGE); this.getTopRightSpot().setColour(OUColour.ORANGE); this.getMiddleSpot().setColour(OUColour.BLUE); this.getBottomLeftSpot().setColour(OUColour.ORANGE); this.getBottomMiddleSpot().setColour(OUColour.ORANGE); this.getBottomRightSpot().setColour(OUColour.ORANGE); } /** * Sets the face of the receiver to 2 spots */ public void face2() { this.getTopLeftSpot().setColour(OUColour.ORANGE); this.getTopMiddleSpot().setColour(OUColour.ORANGE); this.getTopRightSpot().setColour(OUColour.BLUE); this.getMiddleSpot().setColour(OUColour.ORANGE); this.getBottomLeftSpot().setColour(OUColour.BLUE); this.getBottomMiddleSpot().setColour(OUColour.ORANGE); this.getBottomRightSpot().setColour(OUColour.ORANGE); } /** * Sets the face of the receiver to 3 spots */ public void face3() { this.getTopLeftSpot().setColour(OUColour.ORANGE); this.getTopMiddleSpot().setColour(OUColour.ORANGE); this.getTopRightSpot().setColour(OUColour.BLUE); this.getMiddleSpot().setColour(OUColour.BLUE); this.getBottomLeftSpot().setColour(OUColour.BLUE); this.getBottomMiddleSpot().setColour(OUColour.ORANGE); this.getBottomRightSpot().setColour(OUColour.ORANGE); } /** * Sets the face of the receiver to 4 spots */ public void face4() { this.getTopLeftSpot().setColour(OUColour.BLUE); this.getTopMiddleSpot().setColour(OUColour.ORANGE); this.getTopRightSpot().setColour(OUColour.BLUE); this.getMiddleSpot().setColour(OUColour.ORANGE); this.getBottomLeftSpot().setColour(OUColour.BLUE); this.getBottomMiddleSpot().setColour(OUColour.ORANGE); this.getBottomRightSpot().setColour(OUColour.BLUE); } /** * Sets the face of the receiver to 5 spots */ public void face5() { this.getTopLeftSpot().setColour(OUColour.BLUE); this.getTopMiddleSpot().setColour(OUColour.ORANGE); this.getTopRightSpot().setColour(OUColour.BLUE); this.getMiddleSpot().setColour(OUColour.BLUE); this.getBottomLeftSpot().setColour(OUColour.BLUE); this.getBottomMiddleSpot().setColour(OUColour.ORANGE); this.getBottomRightSpot().setColour(OUColour.BLUE); } /** * Sets the face of the receiver to 6 spots */ public void face6() { this.getTopLeftSpot().setColour(OUColour.BLUE); this.getTopMiddleSpot().setColour(OUColour.BLUE); this.getTopRightSpot().setColour(OUColour.BLUE); this.getMiddleSpot().setColour(OUColour.ORANGE); this.getBottomLeftSpot().setColour(OUColour.BLUE); this.getBottomMiddleSpot().setColour(OUColour.BLUE); this.getBottomRightSpot().setColour(OUColour.BLUE); } /** * Causes execution to pause by time number of milliseconds */ private void delay(int time) { try { Thread.sleep(time); } catch (Exception e) { System.out.println(e); } } }
the square comes up about 20 not 75, so when i try to create circles, they just obscure the square
- 12-10-2009, 04:03 PM #57
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
You both go on about the Square class, and yet you both post the Dice class, and give us no idea how this is being run (there's no main() method here).
And posting huge slabs of code really doesn't help...any chance of narrowing it down a bit? We have a very low signal to noise ratio so far.
- 12-10-2009, 04:07 PM #58
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
Sorry, im new to this.. the code that i have created is in the dice class as shown below.
I think the bit in red is wrong, as the square being created is'nt the right size.Java Code:public class Dice { /* instance variables */ private Square background; private Circle topLeftSpot; private Circle topMiddleSpot; private Circle topRightSpot; private Circle middleSpot; private Circle bottomLeftSpot; private Circle bottomMiddleSpot; private Circle bottomRightSpot; /** * Constructor for objects of class Dice */ public Dice(Square bg, Circle tL, Circle tM, Circle tR, Circle mid, Circle bL, Circle bM, Circle bR) { [COLOR="Red"]background = bg; bg.setLength(75);[/COLOR] background = bg; topLeftSpot = tL; topMiddleSpot = tM; topRightSpot = tR; middleSpot = mid; bottomLeftSpot = bL; bottomMiddleSpot = bM; bottomRightSpot = bR; reset(); }
Here is part of the question that the above is supposed to be.
The constructor should do the following. First it should send a message to the Square object referenced by the argument bg to set its length to 75. Then the all arguments of the constructor should be directly assigned to the appropriate instance variables. Finally a reset() message should be sent to this (the receiver).
- 12-10-2009, 04:16 PM #59
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
"message"?
I'm not sure they're being overly helpful using that terminology, but hey ho.
Anyway, you're doing exactly what's being asked of you. That the value of length in the Square object isn't being set is nothing to do with that bit of code in your post.
- 12-10-2009, 04:22 PM #60
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
Similar Threads
-
Illegal Start of expression
By Macca07 in forum New To JavaReplies: 3Last Post: 11-23-2009, 08:43 AM -
Illegal start of expression
By Basit56 in forum New To JavaReplies: 2Last Post: 08-18-2009, 09:12 AM -
Illegal Start of an Expression
By David55 in forum CLDC and MIDPReplies: 8Last Post: 11-02-2007, 09:11 PM -
Illegal start of expression
By gabriel in forum New To JavaReplies: 2Last Post: 08-01-2007, 05:09 PM -
Illegal Start of an Expression
By David55 in forum CLDC and MIDPReplies: 0Last Post: 04-20-2007, 05:59 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks