Ok, so I'm trying to make a constructor for a text-based game for a monster. Here is the code for it:

public class Methods
{
    String name; //characteristics of monsters
    int bodyPoints, mindPoints, attack, defense;

    public Monster(String name, int bodyPoints, int mindPoints, int attack, int defense)
    {
        this.name=name;
        this.bodyPoints=bodyPoints;
        this.mindPoints=mindPoints;
        this.attack=attack;
        this.defense=defense;
    }
}

However, it gives me a "return type required error" on line 8. I'm not sure why, because in a method for a different game that I got from my teacher, he uses the same code and it works. Here's the entire code for that class:

import java.util.Random;

public class Fighter
{
	String name;
	int numAttacks, hitDamage, lifePoints, armor;
	Random g;
	public Fighter(String name, int numAttacks, int hitDamage, int lifePoints, int armor)
	{
		g=new Random();
		this.name=name;
		this.numAttacks=numAttacks;
		this.hitDamage=hitDamage;
		this.lifePoints=lifePoints;
		this.armor=armor;
	}
	public String toString()
	{
		return name +" "+ lifePoints;
	}
	public boolean isAlive()
	{
		boolean alive=false;
		if(lifePoints>0)alive=true;
		return alive;
	}
	public boolean wasHit(int amount)
	{
		boolean hit;
		if(amount>armor)hit=true;
		else hit=false;
		return hit;
	}
	public String getName()
	{
		return name;
	}
	public void isAttacked(int damage)
	{
		lifePoints=lifePoints-damage;
		if(lifePoints<0)
		{
			lifePoints=0;
		}
	}
	public void attack(Fighter f)
	{
		int temp1, temp2;
		System.out.println(""+name+" is attacking "+f.getName()+".");
		for(int i=0;i<numAttacks;i++)
		{
			temp1=g.nextInt(20)+1;
			if(f.wasHit(temp1))
			{
				System.out.println(""+f.getName()+" was hit by "+name+".");
				temp2=g.nextInt(hitDamage)+1;
				f.isAttacked(temp2);
			}
			else 
			{
				System.out.println(""+name+" missed.");
			}
		}
	}
		
	//    so f is the Fighter that this Fighter is attacking 
	// the method works as follows
	//    generate a random number from 1-20
	//    if this penetrates f's armor, then we have a hit, otherwise a miss, test this with
	//          if(f.wasHit(...))  where ... is the variable storing your random number
	//    if a hit, generate a new random number from 1-hitDamage
	//    tell f that he was attacked by that amount (e.g., f.isAttacked(amount);)
	//    output a message that says this Fighter (name) hit f (f.getName()) for amount damage
	//    if not a hit, indicate that the attack was a miss
}

The only think I can think of that might be wrong is that I may need something in the rest of my class (the constructor is the only part I have written so far) but I don't know what that would be... any ideas?

Here's the full error message, in case it helps:
invalid method declaration; return type required
public Monster(String name, int bodyPoints, int mindPoints, int attack, int defense)
1 error

Recommended Answers

All 2 Replies

The constructor must have the SAME name as the class. Line 8 has a different name.
Lines 3 & 8 have the same names in second code.

Awesome, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.