import java.util.*;
public class Main {
public static void main(String[] args) {
Alice alice = new Alice( "Alice"," The Red Queen", 180);
Chesire chesire = new Chesire( "Chesire"," Jabberwocky", 150, true);
Hatter hatter = new Hatter( "Hatter"," Jabberwocky", 200);
ArrayList<String> cookies = new ArrayList<>();
cookies=cookies;
cookies.add("Eat me");
cookies.add("Try me");
try{
int x = 0;
int y = 10;
int z = y/x;
}catch(Exception ex){
System.out.println("Example of exception");
}
finally{
System.out.println("Who will win the battle? The white queen or the red! Lets play shall we?");
}
Scanner choice = new Scanner(System.in);
System.out.println("Choose a number, has to be between 1-7. Whoever is strong enough to make it to the end wins the war!");
int aliceInWonderland = choice.nextInt();
while(aliceInWonderland != 0){
switch(aliceInWonderland) {
case 1: alice.hitByRedqueen();
break;
case 2: hatter.hitByJabberwocky();
break;
case 3: chesire.hitByJabberwocky();
break;
case 4: chesire.heal();
break;
case 5: hatter.heal();
break;
case 6: alice.heal();
break;
case 7: alice.eatCookies();
break;
default: System.out.println("To begin you have to select between 1-7");
}
System.out.println("Want to play again? Just pick a number between 1-7 again!");
}
}
}
Having errors trying to extend a class in one of my seperate classes. I need to be able to extend a class as part of the project and I thought I’ve done it correctly but i keep getting errors.
Alice Class
import java.util.*;
public class Alice implements Heal{
private int health;
private String name;
private String weapon;
private ArrayList<String> cookies = new ArrayList<>();
public Alice(String name,String weapon, int health){
this.name=name;
this.weapon=weapon;
this.health=health;
this.cookies=cookies;
cookies.add("Eat me");
cookies.add("Try me");
}
Scanner a = new Scanner(System.in);
public void eatCookie(ArrayList<String> cookies){
System.out.println("Alice has a choice between two cookies! Which cookie should she try 1 or 2?");
int choiceCookie = a.nextInt();
switch(choiceCookie){
case 1: System.out.println(" Wow you made a good call! The eat me cookie made you grow into a GIANT. This will definetly help in your fight against the red queen. Health goes up by 50! New Status:" + (this.health+=50));
break;
case 2: System.out.println("OH NO! The try me cookie was a mistake, it made you shrink almost rendering you useless for battle! Health goes down 60 points. New Status: " + (this.health-=80));
break;
default: System.out.println("We only have two cookies to choose from, make a good choice!");
}
}
public void hitByRedqueen(){
this.health-=20;
if(this.health<=0){
this.health=0;
}
System.out.println("Oh man! + getName() + got hit by the the Red Queen and loses 30 points from health. New status:" + this.health);
if(this.health==0){
System.out.println(getName() + "is too weak to keep fighting, without her the fight against the red queen is over. THE BLOODY RED QUEEN HAS WON!");
}
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getWeapon(String weapon){
return weapon;
}
public void setWeapon(String weapon){
this.weapon=weapon;
}
public int getHealth(int health){
return health;
}
public int setHealth(int health){
return this.health = health;
}
@Override
public void heal(){
this.health+=100;
System.out.println(getName() + " realized her courage and imagination are in her favor against the queen! New status: " + this.health);
}
public void eatCookies() {
}
}
Chesire Class
import java.util.*;
public class Chesire extends Hatter implements Heal {
private int health;
private boolean vanishing;
public Chesire(String name, String weapon, int health, boolean vanishing){
super(name,weapon,health);
this.vanishing = vanishing;
this.health = health;
}
@Override
public void hitByJabberwocky(){
if(vanishing){
this.health-=20;
if(this.health<=0) this.health=0;
System.out.println(getName()+ "tried to vanish, but wasn't quite quick enough to miss the full swing of the " + getWeapon() + "! Health went down by 20! Status update: " + this.health);
}
if(!vanishing){
this.health-=50;
if(this.health<=0) this.health=0;
System.out.println(getName() + "was too weak to vanish before the next attack of the " + getWeapon() + ". Health went down by 50!" + this.health);
}
if(health==0){
System.out.println(getName() + "is no longer in the fight against the red queen...");
}
}
}
Hatter Class
public class Hatter implements Heal {
private String name;
private String weapon;
private int health;
public Hatter(String name, String weapon, int health){
this.name = name;
this.weapon = weapon;
this.health = health;
}
public void hitByJabberwocky(){
this.health-=25;
if(this.health<=0){
this.health=0;
}
System.out.println("OH NO, " + getName(name) +"got hurt by a " + getWeapon(weapon) + ". Health has lowered by 25. New status: " + this.health );
if(this.health==0){
System.out.println(getName(name) + " is no longer in the fight against the bloody red queen...");
}
}
public String getName(String name){
return name;
}
public void setName(String name){
this.name = name;
}
public String getWeapon(String weapon){
return weapon;
}
public void setWeapon(String weapon){
this.weapon = weapon;
}
public int getHealth(int health){
return health;
}
public void setHealth(int health){
this.health = health;
}
@Override
public void heal(){
this.health+=100;
System.out.println(getName(name) + "found strength from Alice! New status: " + this.health);
}
}
I was expecting the class Chesire to implement the Hatter class, and it does to a certain extent I believe but I am getting these errors which in turn isn’t allowing me to run the code.
javac -classpath .:/run_dir/junit-4.12.jar:target/dependency/* -d . Alice.java Chesire.java Hatter.java Heal.java Main.java
Chesire.java:17: error: method getName in class Hatter cannot be applied to given types;
System.out.println(getName()+ "tried to vanish, but wasn't quite quick enough to miss the full swing of the " + getWeapon() + "! Health went down by 20! Status update: " + this.health);
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
Chesire.java:17: error: method getWeapon in class Hatter cannot be applied to given types;
System.out.println(getName()+ "tried to vanish, but wasn't quite quick enough to miss the full swing of the " + getWeapon() + "! Health went down by 20! Status update: " + this.health);
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
javac -classpath .:/run_dir/junit-4.12.jar:target/dependency/* -d . Alice.java Chesire.java Hatter.java Heal.java Main.java
Chesire.java:17: error: method getName in class Hatter cannot be applied to given types;
System.out.println(getName()+ "tried to vanish, but wasn't quite quick enough to miss the full swing of the " + getWeapon() + "! Health went down by 20! Status update: " + this.health);
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
Chesire.java:17: error: method getWeapon in class Hatter cannot be applied to given types;
System.out.println(getName()+ "tried to vanish, but wasn't quite quick enough to miss the full swing of the " + getWeapon() + "! Health went down by 20! Status update: " + this.health);
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
javac -classpath .:/run_dir/junit-4.12.jar:target/dependency/* -d . Alice.java Chesire.java Hatter.java Heal.java Main.java
Chesire.java:2: error: Chesire is not abstract and does not override abstract method heal() in Heal
public class Chesire implements Heal {
^
Chesire.java:8: error: constructor Object in class Object cannot be applied to given types;
super(name,weapon,health);
^
required: no arguments
found: String,String,int
reason: actual and formal argument lists differ in length
Chesire.java:12: error: method does not override or implement a method from a supertype
@Override
^
Chesire.java:17: error: cannot find symbol
System.out.println(getName()+ "tried to vanish, but wasn't quite quick enough to miss the full swing of the " + getWeapon() + "! Health went down by 20! Status update: " + this.health);
^
symbol: method getName()
location: class Chesire
Chesire.java:17: error: cannot find symbol
System.out.println(getName()+ "tried to vanish, but wasn't quite quick enough to miss the full swing of the " + getWeapon() + "! Health went down by 20! Status update: " + this.health);
javac -classpath .:/run_dir/junit-4.12.jar:target/dependency/* -d . Alice.java Chesire.java Hatter.java Heal.java Main.java
Chesire.java:17: error: method getName in class Hatter cannot be applied to given types;
System.out.println(getName()+ "tried to vanish, but wasn't quite quick enough to miss the full swing of the " + getWeapon() + "! Health went down by 20! Status update: " + this.health);
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
Chesire.java:17: error: method getWeapon in class Hatter cannot be applied to given types;
System.out.println(getName()+ "tried to vanish, but wasn't quite quick enough to miss the full swing of the " + getWeapon() + "! Health went down by 20! Status update: " + this.health);
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
Chesire.java:23: error: method getName in class Hatter cannot be applied to given types;
System.out.println(getName() + "was too weak to vanish before the next attack of the " + getWeapon() + ". Health went down by 50!" + this.health);
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
Chesire.java:23: error: method getWeapon in class Hatter cannot be applied to given types;
System.out.println(getName() + "was too weak to vanish before the next attack of the " + getWeapon() + ". Health went down by 50!" + this.health);
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
Chesire.java:26: error: method getName in class Hatter cannot be applied to given types;
System.out.println(getName() + "is no longer in the fight against the red queen...");
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
5 errors
exit status 1