java setter / getter

06/26/2014 20:31 fear-x#1
Code:
import java.util.Scanner;

class TeamName {
	private String name; //private hides from other classes in the package
	private String NameGet;
	//public shows to all package
	
	//this will run without any argument passed to it
	public TeamName(){
		System.out.println("cunstructor without argument");
	}
	//prints team name // new TeamName("team");
	public TeamName(String name){
		String nameGetter;
		Scanner in = new Scanner(System.in);
		System.out.println("Please enter a team name");
		nameGetter = in.nextLine();
		System.out.println("Welcome " + nameGetter);
		in.close();
	}
}
class GetName {
	public String defaultName;
	
	public GetName(String defaultName) {
		System.out.println(defaultName); //display current team name
	}
}
class  SetName {
	public SetName(String defaultName) {
		System.out.println(defaultName); //print result
	}
}
public class Team {
	public String nameSetter;
	public static void main(String[]args){
		new TeamName();
		new TeamName("team");
		
		
		String defaultName = "unknown Team";
		GetName theNewName = new GetName(defaultName);
		
		
		//String defaultName;
		Scanner in = new Scanner(System.in);
		System.out.println("Please enter a team name");
		defaultName = in.nextLine();
		
		
		System.out.println("Welcome " + defaultName);
		in.close();
		
		
	}
	public String getNameSetter() {
		return nameSetter;
	}
	public void setNameSetter(String nameSetter) {
		this.nameSetter = nameSetter;
	}
}
ive been trying to create getters /setters for a week now omg ... everyday few hours.. but i end up quitting coz it annoys the shit out of me, and i dont wana smash my comp into pieces... can anyone help and explain please?
06/26/2014 20:50 th0rex#2
Getters and setters should not be in an extra class. Your class should have an private member and the Getter and Setter should either return that member or set that member to a new value.
06/26/2014 21:07 fear-x#3
i have no idea how to do the setting / getting ... been reading too much my brain isnt thinking straight anymore
06/26/2014 21:34 XxharCs#4
omitma said it already.

An example:
Code:
public class Team {

	private String teamName;
	private int teamID;
	
	public Team() {
	
		this.teamName = "Team1";
		this.teamID = 123;
	}
	
	public void setTeamName(String teamName) {
	
		this.teamName = teamName;
	}
	
	public String getTeamName() {
	
		return this.teamName;
	}
	
	public void setTeamID(int teamID) {
	
		this.teamID = teamID;
	}
	
	public int getTeamID() {
	
		return teamID;
	}
}
And you other class which wants to use it:
Code:
public class Test {

	public static void main(String[] args) {
	
		Team t = new Team();
		
		// now, because you can't use t.teamName = "something" or t.teamID = something, you need to use the setter Method
		t.setTeamName("Team epvp");
		t.setTeamID(1337);
		
		// now, you can't use System.out.println("" + t.teamName + " " + t.teamID) you need to use the getter Methods to get the values of the variables
		System.out.println("" + t.getTeamName() + " " + t.getTeamID());
	}
}
I hope you can understand it better now.
06/29/2014 00:29 fear-x#5
this example is perfect ! ive looked for one so many days all show nothing really... thank you ! where could i find more of such examples for java? im on an online course but the course website and examples and help is all total SHIT.

PS. sorry for late reply ! forgot to check back ! i got cought up in this new game Banished... you should check it out too(not advertising) this game is REALLY! REALLY! addictive...4 days now i have not layed my eyes off it !

and btw can you explain @Override?
06/29/2014 12:37 strubelz#6
@Override is just for you as Programmer, it shows you that you override a emthode from a Superclass, but the Compiler don't compile it. So you can do it to keep your code clean and avoid mistakes but you don't need to do it.
06/29/2014 13:15 fear-x#7
Code:
Write a Team class to encapsulate the concept of a Team.
The team has only one attribute, the team name.
Include the following:
Constructors: One that takes no arguments and one that takes the Team name.
Add a getter/assessor method and a mutator/setter method that follow the standard naming conventions based on the field name you use to store the team name.
Add an override of the toString method (You must use the @Override annotation).
Add an override of the equals method (You must use the @Override annotation).
Add any other methods that should be in this class.
this is what i have to do for a project... im only a beginner with java... and i have no clues of what where and how to use @Override really... they say got examples in their website but that website is built by a bunch of 10 year olds i think because i cant find anything... not only me but alot of other students ...
06/29/2014 16:29 XxharCs#8
The lesson/excercise wants propably to write your own toString, equals methods and so on, thats why u need to use the @Override annotation.
With this annotation you just override the method from the superclass. If such a method doesn't exist in the superclass, you will get an error.

When you are overriding the equals method, then you need to override also the hashCode() method.
If you are overriding the equals() method without overriding the hashCode() method, may cause unexpected behaviour for classes/methods which use hashcode as a short cut to assessing equality (eg the HashSet). A good IDE will (prompt to) override the HashCode for you.

If you just take my code from above, for the lesson you just need to add the constructor which takes an argument, and also the override methods. Don't forget to remove the int variable.
I will just write it down because it's not much code.
Code:
public class Team {

	private String teamName;
	
	public Team() {
	
		this.teamName = "Team1";
	}
	
	public Team(String teamName) {
	
		this.teamName = teamName;
	}
	
	public void setTeamName(String teamName) {
	
		this.teamName = teamName;
	}
	
	public String getTeamName() {
	
		return this.teamName;
	}
	
	@Override
	public String toString() {
		
		return "Team name: " + this.teamName;
	}
	
	@Override
	public boolean equals(Object o) {
		
		if(this.teamName == ((Team)o).getTeamName()){
			
			return true;
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		
		final int prime = 31;
		int result = 1;
		
		result = prime * result + ((this.teamName == null) ? 0 : this.teamName.hashCode());
		
		return result;
	}
	
	/*
	// Or you can generate the hashcode using Apache HashCodeBuilder
	@Override
	public int hashCode() {
		HashCodeBuilder builder = new HashCodeBuilder();
		builder.append(this.getTeamName());
		return builder.toHashCode();
	}
	*/
}
Have Fun!
And yeah, i am just using for Java just Oracle: [Only registered and activated users can see links. Click Here To Register...]
I started with Java in school, so i had a german book but there is no english version of it, sry :/, on english it would be Java Head First
06/30/2014 01:07 fear-x#9
ty man . :) uno i read every single word you wrote but i dont understand any furhter to this part of overrides and shit :D ... it was easy till this part though.. and kinda liked it ! but now ... seeing it like this with so much nonsense... makes me dizzy :D
06/30/2014 13:36 XxharCs#10
I will try to explain it in other way. :D

Method overriding happens when you redefine a method with the same signature in a sublcass.

For example, we have an Animal class, which has the method move()
Code:
public class Animal {
	
	public Animal() {}
	
	public void move() {
		
		System.out.println("Animal moves!");
	}
}
Then we take a subclass of Animal, for example Cat. The Cat class has also the method move() which we are overriding. Plus we will add the miau() method for the Cat class.
Code:
public class Cat extends Animal {

	public Cat() {}
	
	public void move() {
		
		System.out.println("Cat moves!");
	}
	
	public void miau() {
		
		System.out.println("miauuuu");
	}
}
And now lets test it.
Code:
public class Test {
	
	public static void main(String[] args) {
		
		Animal a = new Animal();
		Animal c = new Cat();
		
		a.move();
		c.move();
	}
}
So what is the output here?
Code:
Animal moves!
Cat moves!
We have successfully overriden the move() method.

But what happens if we want to use the miau() method?
Code:
public class Test {
	
	public static void main(String[] args) {
		
		Animal a = new Animal();
		Animal c = new Cat();
		
		a.move();
		c.move();
		c.miau(); // we add this here
	}
}
We are getting an error! =>
Code:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The method miau() is undefined for the type Animal

The @Override annotation is optional (but a very good thing) and indicates that this is expected to be overriding. If you misspell something or have a wrongly-typed parameter, the compiler will warn you. ==>

Code:
public class Cat extends Animal {

	public Cat() {}
	
	@Override
	public void move() {
		
		System.out.println("Cat moves!");
	}
	
	@Override
	public void miau() { // now you get here an error because this method doesn't exist in Animal
		
		System.out.println("miauuuu");
	}
}
Output:
Code:
The method miau() of type Cat must override or implement a supertype method
06/30/2014 15:01 fear-x#11
so you use override when you have 2 same methos? like you have move() in more than one class?
06/30/2014 15:09 strubelz#12
Yes, but only if one class extend the other.
06/30/2014 22:20 fear-x#13
ok i think i got it ...
@override is basically used when you want to use that move() but you want it to do something else inside the { } brackets ... correct? (hoping for a yes here) and that prevents the code from being messy and full of errors ... ? :P

btw i really appreciate all the help ! nice to see people helping others!