Why is it saying no arguments given - java

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?

Related

Can't figure out how to return a string composed of two variables, a string and a integer?- repost, updated

This program is using a get/set method for both, but I just can't figure out how!
Thanks to a helpful user recommending string.valueOf, I now have this
public int getSeatNumber() {
String output = "" + seatLetter + String.valueOf(seatNumber);
return output;
}
but it still has the same error,
"Incompatible types, string cannot be converted to an int".
Here is the full object, although not all variables are set, as this is recycled code.
public class Ticket {
private int seatNumber, phoneNumber;
private double price;
private String seatLetter, name;
//default constructor
public Ticket() {
price = 300;
seatNumber = 1;
name = "John Doe";
seatLetter = "A";
}
public double getPrice() {
return price;
}
public int getSeatNumber() {
String output = "" + seatLetter + String.valueOf(seatNumber);
return output;
}
public String getModel() {
return model;
}
public void setPrice(double _price) {
price = _price;
}
public void setSeatNumber(int _seatNumber) {
seatNumber = _seatNumber;
}
public void setSeatName(String _seatLetter) {
seatLetter = _seatLetter;
}
#Override
public String toString() {
String output = "";
output += "Model = " + model + "\n";
output += "Price = $" + price + "\n";
output += "Horsepower = " + horsePower + "HP";
return output;
}
}
You've declared a data type of String output and returning a string when the method is completed, but if you take a closer look at your method declaration, it is declaring that your method will return an int.
public int getSeatNumber() <-- need to return an int type.
public String getSeatNumber() should work without throwing errors.
Change your method to this:
public String getSeatNumber() {
String output = "" + seatLetter + String.valueOf(seatNumber);
return output;
}
Notice how I changed the method return type to String.
I advice you use String.format instead, as it will make your code cleaner (I didn't use it since I don't know the types of your values).

How Can I calculate the difference of age between 2 objects?

I am learning JAVA OOP, I have to compare the age between 2 objects:
In java Procedural, I will have done:
public static int calculateDifferenceAge(int agePlayer1, int agePlayer2){
int differenceAge = agePlayer1 - agePlayer2;
if(differenceAge < 0){
differenceAge = -differenceAge;
}
return differenceAge;
}
Then
public static void displayDifferenceAge(String namePlayer1, String namePlayer2, int agePlayer1, int agePlayer2){
System.out.println("Age difference between " + namePlayer1 + " and " + namePlayer2 + " is of" + calculateDifferenceAge(agePlayer1, agePlayer2) + " year(s).");
}
}
I don't understand how to create my calculateDifferenceAge() method in OOP ?
In my main file I have this:
List<Player> players = new ArrayList <Player>();
players.add(new Player("Eric", 31, true));
players.add(new Player("Juliette", 27, false));
I am stuck into my 2 methods:
How to subtract the age of 2 objects?
public static int calculateAgeDifference(List <Player> players){
Player differenceAge = (players.get(0) - players.get(1));
return differenceAge;
}
public static void displayCalculateAgeDifference(List <Player> players){
System.out.println(calculateAgeDifference().age);
}
Class Player
public class Player {
public String name;
public int age;
public boolean sex;
public Player(String name, int age, boolean sex){
this.name = name;
this.age = age;
this.sex = sex;
}
you're only missing a little step in your code. The steps to extract the ages of the list should be:
1.- Extract the object from the list
2.- Extract the age of that object (or player, in this case)
3.- Substract the ages
There's some ways to do it, but I would do it this way:
public static int calculateAgeDifference(List<Player> players) {
int age1= players.get(0).age;
int age2= players.get(1).age;
int differenceAge = age1 - age2;
if(differenceAge < 0){
differenceAge = -differenceAge;
}
return differenceAge;
}
I hope that helps. What i've done there is extract the objects player from the list: players.get(0) extracts the first object inside the list, which is a Player. Now that I have a player and it has an age variable i have to extract it with player.age. I collapsed those steps, if you have any questions I can explain you further
Display method:
public static int displayCalculateAgeDifference (List<Player> players){
String name1= players.get(0).name;
String name2= players.get(1).name;
//as you know this method return the difference in a number form
int difference= calculateAgeDifference(players);
System.out.println("Age difference between " + name1 + " and " + name2 + " is of" + difference + " year(s).");
}
Let's start with a class Player. Let's give it a name and an age, and a calculateAgeDifference method. It should look something like,
public class Player {
private int age;
private String name;
public Player(String name, int age) {
this.name = name;
this.age = age;
}
public int calculateAgeDifference(Player player2) {
return Math.abs(this.age - player2.age);
}
}
Then you can call it like
Player a = new Player("Eric", 40);
Player b = new Player("Sally", 51);
System.out.println(a.calculateAgeDifference(b));
You must have a similar Player class. Yours appears to also have a boolean field. It isn't clear why. So I can't speak to that.
Why did your method interface change from two parameters to a list? You can still pass two instances of the object. You can still return the integer age value from the method, no need to create a Frankenstein's Player instance only to hold the age.
I am assuming your Player class has a method getAge() to extract the age value which was passed in in the constructor:
public static int calcAgeDiff(final Player player1, final Player player2) {
int age1 = player1.getAge();
int age2 = player2.getAge();
return Math.abs(age2 - age1);
}
Alternatively, you can add an instance method to your Player class itself to calculate the age difference to a different player:
public class Player {
// fields
// constructor
// getters
public int ageDiffTo(final Player otherPlayer) {
return Math.abs(this.age - otherPlayer.age); // <- a class can always access its private fields, even of other instances
}
}
then call as player1.ageDiffTo(player2)

Java Error unreachable statement

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.

Don't understand why I receive null

So here is my Superhero class:
public class Superhero {
public int strength;
public int powerUp;
public int defaultStrength = 10;
public String name;
public Superhero(String name) {
this.strength = 10;
System.out.println("The Superheroes available are :" + name);
}
public Superhero(String name, int strength) {
if (strength >= 0) {
this.strength = strength;
System.out.println("The Superheroes available are :" + name);
} else {
System.out.println("Error. Strength cannot be < 0");
}
}
public void setStrength( int strength ) {
this.strength = strength;
}
public int getStrength() {
return strength;
}
public void powerUp(int powerUp) {
this.strength += powerUp;
}
}
Here is my Fight class the problem here is when I run it I get back that the winner result is null and I don't understand why it is doing that.
import java.io.*;
public class Fight {
public static void main (String args[]) {
Superhero gambit = new Superhero( "Gambit" );
Superhero groot = new Superhero( "Groot", 79);
System.out.println( "Gambit's strength is: " + gambit.strength);
System.out.println( "Groot's strength is: " + groot.strength);
System.out.println("The winner of the fight is: " + fight(gambit, groot));
}
static String fight(Superhero a, Superhero b)
{
if (a.strength > b.strength)
{
return a.name;
} else
{
return b.name;
}
}
}
Look at your constructor:
public Superhero(String name) {
this.strength = 10;
System.out.println("The Superheroes available are :" + name);
}
That sets the instance field strength, but doesn't do anything with the name instance field. Your other constructor is the same. You need to include:
this.name = name;
to copy the value from the parameter into the instance variable. Do this in both constructors. Otherwise you just end up with the default value for name, which is a null reference.
As an aside, I'd strongly recommend making your fields private and adding a getName() method to retrieve the name from else your fight method. I'd also throw an exception instead of just printing out an error message if the strength is below 0, and also I'd make the constructor which doesn't take a strength parameter just chain to the one that does:
public Superhero(String name) {
this(name, 10);
}
public Superhero(String name, int strength) {
if (strength < 0) {
throw new IllegalArgumentException("strength cannot be negative");
}
this.strength = strength;
this.name = name;
System.out.println("The Superheroes available are :" + name);
}
(The message displayed by the constructor is a bit odd, given that it's only listing a single name, but that's a different matter.)
The problem is in your constructors:
public Superhero(String name) {
this.strength = 10;
System.out.println("The Superheroes available are :" + name);
}
public Superhero(String name, int strength) {
if (strength >= 0) {
this.strength = strength;
System.out.println("The Superheroes available are :" + name);
} else {
System.out.println("Error. Strength cannot be < 0");
}
}
Your constructors have an argument of a String name, but you never set the instance variable to a value. The default value of an uninitialized variable that contains an object is null.
You never set the name in any of the constructors of Superhero. To fix the first constructor, for example:
public Superhero(String name) {
this.name = name;
this.strength = 10;
System.out.println("The Superheroes available are :" + name);
}
You are never assigning the "incoming" name to the attribute field "name" of your class. So the later one stays null.
Btw: you really want to read those exception traces carefully. They provide all the information that you need to have in order to figure whats going on.
Final note: consider using the keyword final for the attributes of your class. That way you would have not run into this problem; as the compiler would have told you that the field "name" isn't initialized in your code.
Make a getter method that returns the String name in your superhero class, then call that method within your Fight class. I would also suggest changing your global variables in your superhero class from public to private, so they are only accessible within that class.
EDIT: As stated in another answer, your constructor that takes name as an argument is never assigned to a variable.
You are not setting the values of name variable in the constructor, use this
public Superhero(String name) {
this.strength = 10;
this.name = name;
System.out.println("The Superheroes available are :" + name);
}
public Superhero(String name, int strength) {
this.name = name;
if (strength >= 0) {
this.strength = strength;
System.out.println("The Superheroes available are :" + name);
} else {
System.out.println("Error. Strength cannot be < 0");
}
}

How to return string and int type in a method?

Write a class for a video game character. The character should have a
name, a type (scout, soldier, medic, etc.) and current health.
Therefore it needs three attributes:
String name, String type, int health
This class should have the following methods:
GameCharacter( String newName, String newType, newCurHealth )
Constructor that takes three inputs.
changeHealth( int change ) A method that changes the health of the
character. The character’s health will change by change amount, so it
will go down if change is negative, and up if it’s positive. If the
health goes below 0, changeHealth should return the String "Your
character is dead".
Here is my code so far. Is there anything I can do to make it better? & a way to return a string in my second method?
public class GameCharacter {
private String name;
private String type;
private int health;
public GameCharacter(String newName, String newType, int newCurHealth){
name = newName;
type = newType;
health = newCurHealth;
}
public int changeHealth (int change){
if (change < 0){
return health - change;
} else if (change > 0){
return health + change;
} else if (health < 1){
// string that character is dead
}
}
public static void main(String[] args){
GameCharacter Mario = new GameCharacter ("Mario", "Hero", 100);
GameCharacter Luigi = new GameCharacter ("Luigi", "Sidekick", 100);
GameCharacter Bowser = new GameCharacter ("Bowser", "Villian", 100);
}
}
You cannot return either an int or a String. You have to pick one type and you should re-think your design, if you want to output a message. E.g. just check the return value of changeHealth() after calling the method.
Or you could define a custom exception (or use an existing one). Just to get you started:
public int changeHealth(int change) {
int result = health;
if (health < 1) {
throw new IllegalStateException("Cannot change health, character is dead already.");
}
// Calculate health change (if any)
health += change;
// Return new health
return health;
}
Humbly, I think what you want isn't a good way.
Your methods should be so semantics as possible.
A better approach would be return a negative int and your class GameCharacter can have a method isDead or isAlive that will give you this state.
public class GameCharacter {
private String name;
private String type;
private int health;
public boolean isAlive(){ return health>0; }
public boolean isDead(){ !isAlive(); }
}
It would make more sense to always return the same thing, regardless of whether your character is dead or not.
For example:
public class GameCharacter {
private String name;
private String type;
private int health;
public GameCharacter(String newName, String newType, int newCurHealth){
name = newName;
type = newType;
health = newCurHealth;
}
public String changeHealth (int change){
// Adding positive number will increase health.
// Adding negative number will decrease health.
health += change;
if (health > 0){
return "Your character now has " + health + " health.";
} else {
return "Your character is dead.";
}
}
public static void main(String[] args){
GameCharacter Mario = new GameCharacter ("Mario", "Hero", 100);
GameCharacter Luigi = new GameCharacter ("Luigi", "Sidekick", 100);
GameCharacter Bowser = new GameCharacter ("Bowser", "Villain", 100);
}
}
I think you misunderstood the assignment. The method is not supposed to return the new health, but to change the health of that character, i.e. just update this.health "in place" and change the returned type to String.
Also, no need to check whether change is positive or negative; just add it to health!
public String changeHealth (int change) {
this.health += change
if (health < 1) {
return "Your character is dead";
} else {
return null; // or whatever
}
}
Edit: While other answers propose some good alternatives and additions to the Character API, given that this looks like an assignment, I think you should stick to the description of the method, i.e. change the health, and return a string.
If you declare a method to return an int you cannot make it return a String as a "special result".
There are several ways to solve your problem. You can add a method like checkAlive() which returns true if the current health is greater than 0 and false otherwise, or make the caller check the returned value (which should be the health after the change) and print the string if that value is smaller than or equal to 0.
Also I think you have some bugs in your concept: first, your method doesn't change the health value inside your class; second, the code inside the last if, where you want to return the string, will be executed only when 0 is passed as parameter to the method. That's probably not what you want. To follow my suggestion edit the method like this:
public int changeHealth(int change) {
health += change;
return health;
}
Few things,
first, dont worry, your character will never die
if (change < 0){
return health - change;
} else if (change > 0){
return health + change;
}
you are adding positive change value to health and substracting negative value from it. Mathematics 101 x - (-y) == x+y
second, your character might dead, but i dont think any action related to him being dead should be happened inside `GameCharacter' class. i suggest you to return true/false value which indicate is character still alive. or create enum. if you go that way, you culd have multiple states (alive, almost_dead, dead)
I would rethink the design, particularly around the changeHealth method. However, if you really want to have a combined return type of an int and an optional string, you could create a new class, say, HealthStatus, that actually contains an Optional<String>:
public class HealthStatus {
public final int health;
public final Optional<String> message;
public HealthStatus(int health, String message) {
this.health = health;
this.message = Optional.of(message);
}
public HealthStatus(int health) {
this.health = health;
this.message = Optional.empty();
}
public int getHealth() {
return health;
}
public Optional<String> getMessage() {
return message;
}
}
Then, in your changeHealth method, you can optionally return a message:
public HealthStatus changeHealth(int change) {
health += change;
if (health < 1) {
return new HealthStatus(health, "Your character is dead");
} else {
return new HealthStatus(health);
}
}
And at the point where you call it, you can print the message, if there is one (or do whatever else is appropriate with it):
// take 7 points of damage:
HealthStatus status = changeHealth(-7);
hitPoints = status.getHealth();
// print the message, if there is one
status.getMessage().ifPresent(System.out::println);
public String changeHealth(int change) {
health += change;
return health < 0 ? "Your character is dead" : null;
}

Categories