1 Attachment(s)
Someone help me with my code it has some errors? Thanks!
Attachment 1511
Errors in order:
Syntax error on tokens, delete these tokens
Syntax error on tokens, Expression expected instead
Syntax error on tokens, Expression expected instead
Syntax error on tokens, delete these tokens
This is my worker class:
package HW07;
//Bulb.java
public class Bulb
{
//Instance Data
private boolean bulbLit;
//Constructor
public Bulb (boolean bulbLitIn)
{
bulbLit = bulbLitIn;
}
//bulb setter off
public void turnOff()
{
bulbLit = false;
}
//bulb setter on
public void turnOn ()
{
bulbLit = true;
}
public String toString()
{
String result = ;
result += This bulb is currently ;
if(bulbLit)
result += on;
else
result += off
return result;
}
}
This is my driver class:
package HW07;
public class HW07
{
public static void main (String[] args)
{
//Instantiate 2 bulb objects.
Bulb bulb1 = new Bulb (false);
Bulb bulb2 = new Bulb (false);
//Bulb1 is turned on
bulb1.turnOn();
//Print out each bulbs original information.
System.out.println ("Bulb 1: " +bulb1);
System.out.println ("Bulb 2: " +bulb2);
//Turn off bulb1 turn on bulb2
bulb1.turnOff();
bulb2.turnOn();
//Print out each bulbs original information.
System.out.println ("Bulb 1: " +bulb1);
System.out.println ("Bulb 2: " +bulb2);
}
}
This is the output:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on tokens, delete these tokens
Syntax error on tokens, Expression expected instead
Syntax error on tokens, Expression expected instead
Syntax error on tokens, delete these tokens
at HW07.Bulb.toString(Bulb.java:35)
at java.lang.String.valueOf(String.java:2826)
at java.lang.StringBuilder.append(StringBuilder.java: 115)
at HW07.HW07.main(HW07.java:20)
Re: Someone help me with my code it has some errors? Thanks!
Quote:
Originally Posted by
skitheeast8
String result = ;
On which line is the error occuring? Is it the above line? Those quotations look like special word processor characters and I think the compiler does not know what they are.
Re: Someone help me with my code it has some errors? Thanks!
Oh wow, I'm an idiot.
Well, thankfully someone picked up on my little mistake! Thank you!
Re: Someone help me with my code it has some errors? Thanks!
Quote:
Originally Posted by
skitheeast8
Attachment 1511
Errors in order:
Syntax error on tokens, delete these tokens
Syntax error on tokens, Expression expected instead
Syntax error on tokens, Expression expected instead
Syntax error on tokens, delete these tokens
This is my worker class:
I think it is rejecting the quote marks that you have in Bulb.java.
Code java:
package HW07;
//Bulb.java
public class Bulb
{
//Instance Data
private boolean bulbLit;
//Constructor
public Bulb (boolean bulbLitIn)
{
bulbLit = bulbLitIn;
}
//bulb setter off
public void turnOff()
{
bulbLit = false;
}
//bulb setter on
public void turnOn ()
{
bulbLit = true;
}
public String toString()
{
String result = ; //<---From Zaphod_b Bad quote marks.
result += This bulb is currently ;//<---From Zaphod_b Bad quote marks.
if(bulbLit)
result += on;//<---From Zaphod_b Bad quote marks.
else
result += off//<---From Zaphod_b Bad quote marks. (Also missing semicolon)
return result;
}
}
What happens if you try
Code java:
package HW07;
//Bulb.java
public class Bulb
{
//Instance Data
private boolean bulbLit;
//Constructor
public Bulb (boolean bulbLitIn)
{
bulbLit = bulbLitIn;
}
//bulb setter off
public void turnOff()
{
bulbLit = false;
}
//bulb setter on
public void turnOn()
{
bulbLit = true;
}
public String toString()
{
String result = " ";
result += "This bulb is currently ";
if(bulbLit)
result += "on";
else
result += "off";
return result;
} // End toString
} // End class definition
Cheers!
Z
Re: Someone help me with my code it has some errors? Thanks!
Quote:
Originally Posted by
Zaphod_b
I think it is rejecting the quote marks that you have in Bulb.java.
I wish I had thought of that!
Re: Someone help me with my code it has some errors? Thanks!
Quote:
Originally Posted by
Junky
I wish I had thought of that!
I apologize for stepping on your toes with my redundant post.
I am trying to break myself of the habit of spending a few minutes crafting a response and then forgetting to see if someone else has already answered before I post. I used to do an immediate delete of the post when I discovered that I had done that, but people complained about that too.
Bottom line: I'll try to do better.
Cheers!
Z
Re: Someone help me with my code it has some errors? Thanks!
People just love to whine. ;)
Re: Someone help me with my code it has some errors? Thanks!
That reminds me of what I sometimes do. I reply to a post without realising there is a second page and people have already responded.