Advertising (This ad goes away for registered users. You can Login or Register)

Java Class Problem

Programming on your favorite platform, for your favorite platform? Post here
User avatar
KaptainBio
Posts: 30
Joined: Sun Jul 07, 2013 11:02 am
Location: C:\Music\Metal

Java Class Problem

Post by KaptainBio » Tue Jul 16, 2013 6:41 pm

Hi all

Im learning Java at the moment, and I wanted to practise the classes and i got a problem.

First I will show you what i have maid.

[spoiler]
package Instanzen;


class Weapon{
String m_WeaponName;
String m_WeaponType;
int m_WeaponDMG;

Weapon (String Weaponname, String Weapontype, int Weapondamage){
m_WeaponName=Weaponname;
m_WeaponType=Weapontype;
m_WeaponDMG=Weapondamage;

}
}


class Armor{
String m_ArmorName;
String m_ArmorType;
int m_ArmorDEF;

Armor(String Armorname, String Armortype, int Armordefence){
m_ArmorName = Armorname;
m_ArmorType = Armortype;
m_ArmorDEF = Armordefence;

}
}


class Warrior2{
String m_PlayerName;
int m_PlayerLevel;
Weapon m_Weapon;
Armor m_Armor;
int m_Health;

Warrior2(String Playername, int Playerlevel, int Playerhealth){
m_PlayerName=Playername;
m_PlayerLevel=Playerlevel;
m_Weapon= new Weapon("Ironsword","Sword",75);
m_Armor= new Armor("Ironarmor","Chain",75);
m_Health= Playerhealth+Playerlevel*8;
}
void datenAusgeben(){
System.out.println("Player: "+m_PlayerName);
System.out.println("Level: "+m_PlayerLevel);
System.out.println("Weapon: "+m_Weapon);
System.out.println("Armor: "+m_Armor);
System.out.println("Health: "+m_Health);

}
}

public class Krieger2 {
public static void main(String[]args){

Warrior2 KaptainBio= new Warrior2 ("KaptainBio",2,100);
KaptainBio.datenAusgeben();

}

}
[/spoiler]

I wanted to "create" a Warrior.
What I want Java to do is to say what Weapon and Armor the Player wears through system.out.println();
But I get only this, and it does not look like what I want to get.
[spoiler]

Code: Select all

Player: KaptainBio
Level: 2
Weapon: Instanzen.Weapon@b162d5
Armor: Instanzen.Armor@1cfb549
Health: 116
[/spoiler]
And I am using Java Eclipse on German.

Hope you can help me.
Advertising

User avatar
Kankertje
Moderator
Posts: 831
Joined: Mon Apr 23, 2012 12:22 pm
Contact:

Re: Java Class Problem

Post by Kankertje » Tue Jul 16, 2013 6:54 pm

try it like this

System.out.println("Weapon: "+m_Weapon.m_WeaponName)
Advertising

User avatar
Xian Nox
Retired Mod
Posts: 2749
Joined: Fri Nov 05, 2010 5:27 pm
Location: Over the hills and far away

Re: Java Class Problem

Post by Xian Nox » Tue Jul 16, 2013 6:56 pm

Kankertje wrote:try it like this

System.out.println("Weapon: "+m_Weapon.m_WeaponName)
No. This will work, but only if used from the same package.

Okay, first of all, there's code tags for code, use those instead of quotes.
Your problem is this: you're passing an Object, not a String to System.out.println(). What you're seeing is the object instance name as expected. What you should do is override toString, and use it.

Code: Select all

class Weapon {
    private String name;
    private int type;
    private int damage;

    public Weapon (String name, int type, int damage) {
        this.name = name;
        this.type = type;
        this.damage = damage;
    }

    @Override
    public String toString() {
        return name + " #" + type + " | " + damage;
    }
}
and to print it

Code: Select all

Weapon w = ...
System.out.println(w.toString());

User avatar
KaptainBio
Posts: 30
Joined: Sun Jul 07, 2013 11:02 am
Location: C:\Music\Metal

Re: Java Class Problem

Post by KaptainBio » Tue Jul 16, 2013 7:04 pm

Ok first: THX A LOT!

but when I insert this:

Code: Select all

Weapon w = ...
System.out.println(w.toString());
BTW what to do with the "..."

Eclipse marks this as wrong:

Code: Select all

m_Weapon= new Weapon("Ironsword","Sword",75);
:/

User avatar
Xian Nox
Retired Mod
Posts: 2749
Joined: Fri Nov 05, 2010 5:27 pm
Location: Over the hills and far away

Re: Java Class Problem

Post by Xian Nox » Tue Jul 16, 2013 7:11 pm

KaptainBio wrote:BTW what to do with the "..."
create a new instance, like new Weapon(stuff here)
KaptainBio wrote:Eclipse marks this as wrong:

Code: Select all

m_Weapon= new Weapon("Ironsword","Sword",75);
Yes, because I changed the type of the second parameter. Here you go, all's chewed up:

Code: Select all

class Weapon {
    private String m_WeaponName;
    private String m_WeaponType;
    private int m_WeaponDMG;

    public Weapon (String Weaponname, String Weapontype, int Weapondamage){
        m_WeaponName=Weaponname;
        m_WeaponType=Weapontype;
        m_WeaponDMG=Weapondamage;
    }

    public String toString() {
        return m_WeaponName + " " + m_WeaponType + " " + m_WeaponDMG;
    }
}

User avatar
KaptainBio
Posts: 30
Joined: Sun Jul 07, 2013 11:02 am
Location: C:\Music\Metal

Re: Java Class Problem

Post by KaptainBio » Tue Jul 16, 2013 7:16 pm

OK, fixed it now, and now he marks

Code: Select all

	public static void main(String[]args){
}
this as wrong >.<
Why?

//EDIT: to be correct only

Code: Select all

main(String[]args)

User avatar
Xian Nox
Retired Mod
Posts: 2749
Joined: Fri Nov 05, 2010 5:27 pm
Location: Over the hills and far away

Re: Java Class Problem

Post by Xian Nox » Tue Jul 16, 2013 7:20 pm

Your brackets seem may be wrong somewhere, the problem is not exactly with that line.
Also, are all of those classes in one file?

User avatar
KaptainBio
Posts: 30
Joined: Sun Jul 07, 2013 11:02 am
Location: C:\Music\Metal

Re: Java Class Problem

Post by KaptainBio » Tue Jul 16, 2013 7:23 pm

All classes which i use are in this file,

should i copy paste this all again here?

Found the mistake, "}" missed, but now he marks

Code: Select all

public static class Krieger2
means here "Krieger2"...

Eclipses comment is:
"Illegal modifier for the class Krieger2; only public, abstract & final are permitted"
Here the "programm"
[spoiler]

Code: Select all

package Instanzen;


class Weapon {
    private String name;
    private String type;
    private int damage;

    public Weapon (String name, String type, int damage) {
        this.name = name;
        this.type = type;
        this.damage = damage;
    }

    @Override
    public String toString() {
        return name + " #" + type + " | " + damage;
    }
}

class Armor{
	String m_ArmorName;
	String m_ArmorType;
	int m_ArmorDEF;
	
	Armor(String Armorname, String Armortype, int Armordefence){
		m_ArmorName = Armorname;
		m_ArmorType = Armortype;
		m_ArmorDEF = Armordefence;
		
	}
}


class Warrior2{
	String m_PlayerName;
	int m_PlayerLevel;
	Weapon m_Weapon;
	Armor m_Armor;
	int m_Health;
	
	Warrior2(String Playername, int Playerlevel, int Playerhealth){
		m_PlayerName=Playername;
		m_PlayerLevel=Playerlevel;
		m_Armor= new Armor("Ironarmor","Chain",75);
		m_Health= Playerhealth+Playerlevel*8;
	}
		void datenAusgeben(){
			System.out.println("Player: "+m_PlayerName);
			System.out.println("Level: "+m_PlayerLevel);
			Weapon w = new Weapon("Ironsword","Sword",75);
					System.out.println(w.toString());
			System.out.println("Armor: "+m_Armor);
			System.out.println("Health: "+m_Health);
			
		}
}

public static class Krieger2 {
	public static void main(String[]args){
	
		Warrior2 KaptainBio= new Warrior2 ("KaptainBio",2,100);
		KaptainBio.datenAusgeben();
		
	}
}
[/spoiler]

//EDIT2

took the "static" out.
I Would like to say that it is working :3 THX @ All
Last edited by KaptainBio on Tue Jul 16, 2013 7:47 pm, edited 1 time in total.

User avatar
Xian Nox
Retired Mod
Posts: 2749
Joined: Fri Nov 05, 2010 5:27 pm
Location: Over the hills and far away

Re: Java Class Problem

Post by Xian Nox » Tue Jul 16, 2013 7:46 pm

Remove static. btw, have you learned Visual C++ before?

User avatar
KaptainBio
Posts: 30
Joined: Sun Jul 07, 2013 11:02 am
Location: C:\Music\Metal

Re: Java Class Problem

Post by KaptainBio » Tue Jul 16, 2013 7:52 pm

No i have started with Java, bought a book and try to learn it in the holidays. or go further into it

Should I learn Visual C++?

Post Reply

Return to “Programming”