Register for your free account! | Forgot your password?

Go Back   elitepvpers Coders Den Java
You last visited: Today at 01:49

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



java setter / getter

Discussion on java setter / getter within the Java forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,234
Received Thanks: 1,095
java setter / getter

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 **** out of me, and i dont wana smash my comp into pieces... can anyone help and explain please?
fear-x is offline  
Old   #2
 
elite*gold: 46
Join Date: Oct 2010
Posts: 782
Received Thanks: 525
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.
th0rex is offline  
Old   #3
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,234
Received Thanks: 1,095
i have no idea how to do the setting / getting ... been reading too much my brain isnt thinking straight anymore
fear-x is offline  
Old   #4
 
XxharCs's Avatar
 
elite*gold: 34
Join Date: Apr 2011
Posts: 1,475
Received Thanks: 1,228
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.
XxharCs is offline  
Thanks
3 Users
Old   #5
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,234
Received Thanks: 1,095
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 ****.

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?
fear-x is offline  
Old   #6
 
strubelz's Avatar
 
elite*gold: 31
Join Date: Jan 2014
Posts: 310
Received Thanks: 55
@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.
strubelz is offline  
Old   #7
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,234
Received Thanks: 1,095
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 ...
fear-x is offline  
Old   #8
 
XxharCs's Avatar
 
elite*gold: 34
Join Date: Apr 2011
Posts: 1,475
Received Thanks: 1,228
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:
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
XxharCs is offline  
Thanks
1 User
Old   #9
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,234
Received Thanks: 1,095
ty man . uno i read every single word you wrote but i dont understand any furhter to this part of overrides and **** ... it was easy till this part though.. and kinda liked it ! but now ... seeing it like this with so much nonsense... makes me dizzy
fear-x is offline  
Old   #10
 
XxharCs's Avatar
 
elite*gold: 34
Join Date: Apr 2011
Posts: 1,475
Received Thanks: 1,228
I will try to explain it in other way.

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
XxharCs is offline  
Thanks
1 User
Old   #11
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,234
Received Thanks: 1,095
so you use override when you have 2 same methos? like you have move() in more than one class?
fear-x is offline  
Old   #12
 
strubelz's Avatar
 
elite*gold: 31
Join Date: Jan 2014
Posts: 310
Received Thanks: 55
Yes, but only if one class extend the other.
strubelz is offline  
Thanks
2 Users
Old   #13
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,234
Received Thanks: 1,095
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!
fear-x is offline  
Reply


Similar Threads Similar Threads
[Release] Gomomos SP Setter
01/07/2013 - Soldier Front - 1 Replies
Just download this file : Download sphack.exe @ UppIT Or download this one: : Download SPHack.Gomomo.exe @ UppIT Click on run and start sf press on insert and you can select SP each game enjoy :-)
Mains3rv3r's X/Y/Z Setter
05/27/2011 - WarRock - 4 Replies
Credits goes to Mains3rv3r
group setter bukkit?
03/18/2011 - Minecraft - 0 Replies
Wie hiess das plugin um die gruppen ig mit bukkit zu setzen? mfg
Private Server Datenbank Item Setter
08/01/2008 - Cabal Private Server - 17 Replies
Wie viell schon viele mitbekommen haben oder auch nicht, kann man sich Waffen/Items per hexcode ins inventar setzen: http://img3.imagebanana.com/img/seivcmsb/thumb/in v.JPG hier im screen: 0x440047000028000000000000896B4685 0x steht für hex code 4400 ist der itemcode (z.b Osmium Blade) wenn mir wer ne liste mit den ganzen item codes geben kann z.b: (0001 = Martial TitaniumBoots, 0002 = Martial TitaniumHelm, ... , 00FA = Mithril FB Suit , ...)
CO Font Setter *UPDATED - Works with 4347*
05/05/2007 - CO2 Exploits, Hacks & Tools - 24 Replies
Thanks to r34p3rex (Original Post "http://www.elitepvpers.com/forum/index.php?ac t=ST&f=108&t=47805&s=") I made this little CO Font tool. Just Select what font you want to use and then run Conquer (leave CO Fonts running). Thats all there is to it. Just thought I'd make editing the fonts a bit easier for those of you who don't want to touch their registries. Don't worry, once you close CO Fonts it sets the Courier New font back to the default. *EDIT - If you download this please come back and...



All times are GMT +2. The time now is 01:49.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.