Java Error unreachable statement - java

Im trying to create a class that has a base object. The base object will be used to create a few objects and be made to 'fight' in another class based on strength and powerups.
I have got this error when compiling 'Error, unreachable statement' and it points to line 27 pointing to the return, can someone help me out please?
public class Superhero {
private String superheroName;
private int superheroStrength;
public int powerUp;
public Superhero (String superheroName, int superheroStrength, int powerUp){
this.superheroName = superheroName;
this.superheroStrength = superheroStrength;
System.out.println("Superhero: " + superheroName);
System.out.println("Strength: " + ( superheroStrength + powerUp));
}
public Superhero (String superheroName, int powerUp){
this.superheroName = superheroName;
superheroStrength = 10;
System.out.println("Strength: " + ( superheroStrength+powerUp));
}
public int getStrength(){
return superheroStrength += powerUp;
}
public void powerUp (int powerUp){
this.powerUp += powerUp;
}
public Superhero battle(Superhero1 opponent){
if (this.getStrength()>opponent.getStrength());
return this;
return opponent;
}
public String toString(){
return this.superheroName;
}
}

An extra ; caused all the mess
if (this.getStrength()>opponent.getStrength()); <--
That semicolon terminates the statement there ,And assuming it as a new block stating from there.
Hence the code
public Superhero battle(Superhero1 opponent){
if (this.getStrength()>opponent.getStrength());
return this;
return opponent;
}
equals to
public Superhero battle(Superhero1 opponent){
if (this.getStrength()>opponent.getStrength()){
}
return this;
return opponent;
}
Remove that extra ; (hope that is not intentionally typed), codes will be fine then.
As someone commented already, that's the reason, use always curly braces to avoid situations like this.

Related

Why is it saying no arguments given

Here is my code. I think the error is coming from the fight() method. It is supposed to use whatever the attack is given EX. "#" and print that for the number of levels like this "attack=#, level=5" and it would print "#####"
public class Dragon
{
private String attack;
private int level;
public Dragon (int level, String attack)
{
this.level = level;
this.attack = attack;
}
public String getAttack()
{
return attack;
}
public int getLevel()
{
return level;
}
// Put other methods here
private void fight(int level, String attack)
{
if (level < 0)
{
System.out.print(attack);
}
}
// String representation of the object
public String toString()
{
return "Dragon is at level " + level + " and attacks with " + attack;
}
}
Grader.java: Line 57: Method is expecting type int, String and type "no arguments" was given.
You are saying you don't have access to the Grader class...
Maybe the fight method doesn't need any arguments?
Looks like the constructor is already setting those variables.
Remove the arguments from your flight method and check for this.level < 0 instead?

Getting previous return value with new value concatenated using recursion

I'm working a problem on recursion. After writing the code requested, the site I'm working from runs the code with different values as input. However, the first run works properly, but all subsequent runs concatenate the return value of the first run with the values of each of the subsequent runs.
I am also getting a stack overflow error at the end.
I need help!
Here is the code:
package com.company;
import static java.lang.System.*;
public class Main {
public static String returnValue="";
public static void main(String[] args) {
repeat("this is fun", 1);
out.println(returnValue);
}
public static String repeat(String s, int i){
if (i==0) {
return returnValue;
}
else{
returnValue+=s;
repeat(s,i-1);
}
return returnValue;
}
}
Any help is greatly appreciated.
You need to move the static returnValue into the method. You then need to control the result by capturing the string returned by the inner recursive call.
Something like:
public static String repeat(String s, int i){
String returnValue="";
if (i==0) {
return returnValue;
}
else{
returnValue+=s + repeat(s,i-1);
}
return returnValue;
}
NB: This may not be the equivalent algorithm to what you intend but it should demonstrate the technique.
If that is the correct solution then you can tidy up with:
public static String repeat(String s, int i){
if (i==0) {
return "";
} else {
return s + repeat(s,i-1);
}
}
If you are comfortable with expressions:
public static String repeat(String s, int i) {
return i <= 0
? ""
: s + repeat(s, i - 1);
}
and you can get rid of the static attribute!

Java counter not counting properly

Hi I'm very new to Java and have this HW problem, we are asked to build Class LTile, a tile in the game of Scrabble.
The ID number of a tile should be assigned by a class/static data member, which keeps track of the number of LTile objects produced.
My ID output didn't not print from 1 to 26, instead, they are all assign 26.
I suspect my ID attribute must be wrong, but couldn't figure out the exactly error. Any help is greatly appreciated! Thanks!
package hw2;
public class LTile {
char letter;
private int value;
private static int ID=0;
public LTile(){
this.letter = '?';
this.value = 0;
LTile.ID++;
}
public LTile(char letter, int value){
this.letter=letter;
this.value=value;
LTile.ID++;
}
public char getLetter(){
return this.letter;
}
public int getValue(){
return this.value;
}
public int getID(){
return LTile.ID;
}
public boolean equals(Object obj){
if(this ==obj){
return true;}
else{
return false;
}
}
public String toString(){
return "["+ID+":" + letter+","+value+"]";
}
//**
//** main() for testing LTile
//**
public static void main(String[] args)
{
final String letters = "EAIONRTLSUDGBCMPFHVWYKJXQZ";
final int[] values = {1,1,1,1,1,1,1,1,1,1,2,2,3,3,3,3,4,4,4,4,4,5,8,8,10,10};
java.util.List<LTile> lst = new java.util.ArrayList<LTile>();
for (int i = 0; i < letters.length(); ++i)
lst.add(new LTile(letters.charAt(i), values[i]));
for (LTile tile : lst)
System.out.println(tile);
System.out.println();
// test for equals
boolean found = false;
for (int i = 0; i < lst.size()-1; ++i) {
for (int j = i+1; j < lst.size(); ++j) {
if (lst.get(i).equals(lst.get(j))) {
System.out.println("ERROR in equals() found for "
+ lst.get(i) + " and " + lst.get(j));
found = true;
}
}
}
if (!found)
System.out.println("No error in equals().");
}
}
My output is:
[26:E,1]
[26:A,1]
[26:I,1]
[26:O,1]
[26:N,1]
[26:R,1]
[26:T,1]
[26:L,1]
[26:S,1]
[26:U,1]
[26:D,2]
[26:G,2]
[26:B,3]
[26:C,3]
[26:M,3]
[26:P,3]
[26:F,4]
[26:H,4]
[26:V,4]
[26:W,4]
[26:Y,4]
[26:K,5]
[26:J,8]
[26:X,8]
[26:Q,10]
[26:Z,10]
No error in equals()
**The correct output should be:**
[1: E,1]
[2: A,1]
[3: I,1]
[4: O,1]
[5: N,1]
[6: R,1]
[7: T,1]
[8: L,1]
[9: S,1]
[10: U,1]
[11: D,2]
[12: G,2]
[13: B,3]
[14: C,3]
[15: M,3]
[16: P,3]
[17: F,4]
[18: H,4]
[19: V,4]
[20: W,4]
[21: Y,4]
[22: K,5]
[23: J,8]
[24: X,8]
[25: Q,10]
[26: Z,10]
No error in equals().
The ID number of a tile should be assigned by a class/static data member, which keeps track of the number of LTile objects produced.
This means that the id value should come from a static data member, not that it should be a static data member. So you need two fields: an instance id field to keep the object's id and a static CURRENT_ID field to keep track of how many objects you have created so far.
public class LTile {
char letter;
private int value;
private int id; // instance id
private static int CURRENT_ID = 0; // static counter from where the instance ids will be drawn
public LTile() {
// Call the other constructor to avoid unnecessary repetition
this('?', 0);
}
public LTile(char letter, int value) {
this.letter = letter;
this.value = value;
this.id = LTile.CURRENT_ID++;
}
// rest of the code
public int getID() {
return this.id;
}
public String toString() {
return "["+this.id+":" + this.letter+","+this.value+"]";
}
}
Note you also need to change getId() and toString() to use the instance id instead of the static counter.

Error in my Word program in Java

This is my code and it compiles fine but when I try to create a string it says
Error: cannot find symbol - variable racer
public class Word {
private String original;
public Word(String s) {
original = s;
}
public String reverse () {
String reverse= "";
int x = 1;
int length = original.length();
while (length - x >= 0) {
reverse = reverse + original.substring(length -x);
x++;
}
return reverse;
}
public boolean isPalindrome() {
if(original.equals(reverse()))
return true;
else
return false;
}
}
The stated problem is not in the code posted - my guess is irrelephant's comment is correct, ie change new Word(racer) --> new Word("racer").
But I offer this to eliminate any chance of any errors in your code by basically eliminating your code:
public class Word {
private String original;
public Word(String s) {
original = s;
}
public boolean isPalindrome()
return new StringBuilder(original).reverse().toString().equals(original);
}
}
or if you must expose a reverse() method:
public class Word {
private String original;
public Word(String s) {
original = s;
}
public String reverse () {
return new StringBuilder(original).reverse().toString();
}
public boolean isPalindrome()
return reverse().equals(original);
}
}
I don't see the variable racer anywhere, but since you're using reverse inside a method, I'd recommend making it
Most likely, racer was never defined
Either that or the method was called w/o quotes
isPalindrome(racer)//note the lack of quotes
change reverse() to this
private() String reverse () {
String reverse= "";
int x = 1;
int length = original.length();
while (length - x >= 0) {
reverse = reverse + original.substring(length -x);
x++;
}
return reverse;

Null pointer exception for Array of Objects

I am new to using arrays of objects but can't figure out what I am doing wrong and why I keep getting a Null pointer exception. I am trying to create an Theatre class with an array of spotlight objects that are either set to on or off. But - whenever I call on this array I get a null pointer exception.
package theatreLights;
public class TheatreSpotlightApp {
public static void main(String[] args) {
Theatre theTheatre = new Theatre(8);
System.out.println("element 5 " + theTheatre.arrayOfSpotlights[5].toString());
}
}
package theatreLights;
public class Theatre {
spotlight[] arrayOfSpotlights;
public Theatre(int N){
arrayOfSpotlights = new spotlight[N];
for (int i = 0; i < arrayOfSpotlights.length; i++) {
arrayOfSpotlights[i].turnOn();
}
}
}
package theatreLights;
public class spotlight {
int state;
public spotlight(){
state = 0;
}
public void turnOn(){
state = 1;
}
void turnOff(){
state = 0;
}
public String toString(){
String stringState = "";
if(state == 0){
stringState = "is off";
}
else if(state==1){
stringState = "is on";
}
return stringState;
}
}
I must be doing something basic wrong in creating the array but can't figure it out.
replace
arrayOfSpotlights[i].turnOn();
with
arrayOfSpotLights[i] = new Spotlight();
arrayOfSpotlights[i].turnOn();
The line
arrayOfSpotlights = new spotlight[N];
will create an array of spotlights. It will however not populate this array with spotlights.
When you do "arrayOfSpotlights = new spotlight[N];" you init an array of length N, what you need to do is also init each object in it:
for i=0; i<N; i++
arrayOfSpotlights[i] = new spotlight();
arrayOfSpotlights[i].turnOn();
Hope I'm correct :)
You are not creating an spotlight objects.
arrayOfSpotlights = new spotlight[N];
This just creates an array of references to spotlights, not the objects which are referenced.
The simple solution is
for (int i = 0; i < arrayOfSpotlights.length; i++) {
arrayOfSpotlights[i] = new spotlight();
arrayOfSpotlights[i].turnOn();
}
BTW You should use TitleCase for class names.
You could write your class like this, without using cryptic code like 0 and 1
public class Spotlight {
private String state;
public Spotlight() {
turnOff();
}
public void turnOn() {
state = "on";
}
void turnOff() {
state = "off";
}
public String toString() {
return "is " + state;
}
}
You declared the array arrayOfSpotlights, but didn't initialize the members of the array (so they are null - and you get the exception).
Change it to:
public class Theatre {
spotlight[] arrayOfSpotlights;
public Theatre(int N){
arrayOfSpotlights = new spotlight[N];
for (int i = 0; i < arrayOfSpotlights.length; i++) {
arrayOfSpotlights[i]=new spotlight();
arrayOfSpotlights[i].turnOn();
}
}
}
and it should work.

Categories