Mutators & Accessors

Can someone explain them better than my book did for me?

Page 1 of 1

14 Replies - 3980 Views - Last Post: 21 September 2009 - 09:32 AM Rate Topic: -----

#1 Jrdpa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 16-February 09

Mutators & Accessors

Posted 18 September 2009 - 09:37 AM

Hi all,

I am trying to do my next Java homework, but I'm still a bit lost on mutators. Can anyone give me a better, perhaps less jargony, answer? The textbook we are using takes jargon to the extreme. I know some jargon is needed, but this is ridiculous. I did a search on here, but the topics I read didn't explain mutator methods, just mentioned them in the code or said something was similar to them. If I missed one that explains them, please point it out to me! I didn't do a search for accessors, but I'm just as lost.

If only my professor would provide more help than basically, here's the outline and book, e-mail me if you have questions. Covering 2 chapters a week doesn't help either. Urgh...

Not asking you to do it for me, but the current problem is below. I'm going to have to look up how to do fractions. It has been too long!

Quote

Define a class called Fraction. This class is used to represent a ratio of two integers. Include mutator methods that allow the user to set the numerator and the denominator. Also include a method that displays the fraction on the screen as a ratio (e.g., 5/9). This method does not need to reduce the fraction to lowest terms.

Include an additional method, equals, that takes as input another Fraction and returns true if the two fractions are identical and false if they are not. This method should treat the fractions reduced to lowest terms; that is, if one fraction is 20/60 and the other is 1/3, then the method should return true.

Embed your class in a test program that allows the user to create a fraction. Then the program should loop repeatedly until the user decides to quit. Inside the body of the loop, the program should allow the user to enter a target fraction into an anonymous object and learn whether the fractions are identical.


Thanks,

JRDPA

Is This A Good Question/Topic? 0
  • +

Replies To: Mutators & Accessors

#2 thezboe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 18-September 09

Re: Mutators & Accessors

Posted 18 September 2009 - 09:45 AM

Mutators and Accessors are generally known as Setters and Getters. Mutators methods will take an argument and change the variable to whatever the argument is. Accessor methods will simply just return the value of the variable in question.

Note that Mutators and Accessors are generally easier to comprehend when dealing with primitive types, as they get a bit more tricky when talking about Objects.
Was This Post Helpful? 0
  • +
  • -

#3 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Mutators & Accessors

Posted 18 September 2009 - 09:47 AM

Accessors are known as getters, while mutators are known as setters.
Take this as example:
public class Circle{

private double radius = 1.0;

public int getRadius(){
	return radius;
}

}



So in your main method, you can use getRadius() method to get the current radius of the circle.
Example:
public static void main(String[] args){
	Circle circle1 = new Circle();
	circle1.getRadius();
}



This would return the radius of the circle1 you declared.

As for mutator(setter):
public class Circle{

private double radius = 1.0;

public void setRadius(double newRadius){
	radius = newRadius;
}

}



So in your main method, you can use setRadius() method to change the current radius of your circle.
Example:
public static void main(String[] args){
	Circle circle1 = new Circle();
	circle1.setRadius(5);
}


Now, the radius of the circle1 will be 5.

I hope this is clear enough for you to understand what getters and setters are for.

This post has been edited by AntonWebsters: 18 September 2009 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#4 Jrdpa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 16-February 09

Re: Mutators & Accessors

Posted 18 September 2009 - 10:39 AM

View PostAntonWebsters, on 18 Sep, 2009 - 08:47 AM, said:

Accessors are known as getters, while mutators are known as setters.
Take this as example:
public class Circle{

private double radius = 1.0;

public int getRadius(){
	return radius;
}

}



So in your main method, you can use getRadius() method to get the current radius of the circle.
Example:
public static void main(String[] args){
	Circle circle1 = new Circle();
	circle1.getRadius();
}



This would return the radius of the circle1 you declared.

As for mutator(setter):
public class Circle{

private double radius = 1.0;

public void setRadius(double newRadius){
	radius = newRadius;
}

}



So in your main method, you can use setRadius() method to change the current radius of your circle.
Example:
public static void main(String[] args){
	Circle circle1 = new Circle();
	circle1.setRadius(5);
}


Now, the radius of the circle1 will be 5.

I hope this is clear enough for you to understand what getters and setters are for.


Do accessors and mutators have to have another file to test things out? We are having to do that and I think that is part of what is throwing me. I had an A in the class last semester, but this semester, as a fellow classmate said, I feel like the village idiot!
Was This Post Helpful? 0
  • +
  • -

#5 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Mutators & Accessors

Posted 18 September 2009 - 10:48 AM

Well it depends on where you put your test driver class. I've seen people put their Circle class and test driver class in the same .java file...but I never put them in the same . java file...just in case I need to reuse the Circle class...so I always separate the test driver class and the Circle class. They are in two different .java files.
Was This Post Helpful? 0
  • +
  • -

#6 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2637
  • View blog
  • Posts: 15,663
  • Joined: 24-September 08

Re: Mutators & Accessors

Posted 18 September 2009 - 11:03 AM

I personally create classes for EVERYTHING, so my main method for a project in Netbeans, which is supposed to be used to tie the classes together into a working program, is used as a tester class. For example, I create a new class that extends JFrame when I work with GUI's just so it's as easy as possible. I set the constants that determine the size and such, then call an instance of my class in the main method just as a tester.

As far as mutators go, you have to understand the concept of a method.

[access] [return] [name] ([parameters)] {
//c0d3z
}

In a class, your setters could be private, depending on your constructors, and should be void. Getters, however, should be the return type of what the variable is you are returning and generally have no parameters. Their purpose is just for instantiating a classes variables, and could also be used to internally manipulate them.

Hope this helps a little.
Was This Post Helpful? 0
  • +
  • -

#7 Jrdpa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 16-February 09

Re: Mutators & Accessors

Posted 18 September 2009 - 11:39 AM

Am I even on the right track with this? I still feel lost. See the program instructions in the original post. I am using examples from the book and web (looked up fractions because the book never says how to do them) to try to figure this out. The book talks about math, but never says how to do fractions, then the assignment is fractions. :blink: Of course anything that involves math automatically scrambles my brain.

import java.math.*;

public class Fraction {
	Fraction (int n, int d) { 
		numerator = n; denominator = d;
		setFraction(n +  "/" + d);
	}
   
	public Fraction() {
	}
		
	public static void main(String[] args) {
	
	}
	
	// Converts fraction to a string format n/d 
	 public String toString()   {
		int Gcd = gcd(n, d);
		return (n/Gcd + "/" + d/);
	}
	 
	// mutator
	public void setFraction(int newFraction) {
	input = newFraction;
}

}

 


//public class fraction {

//private double n ();

//public void setNumerator(double newNumberator){
  //  n = newNumerator;
//}





Thanks,

JRDPA
Was This Post Helpful? 0
  • +
  • -

#8 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Mutators & Accessors

Posted 18 September 2009 - 12:00 PM

Are you sure the setFraction(n + "/" + d) in the constructor is gonna work?
Get rid of it...
I think you should make a displayFraction() method for that...
public String displayFraction(){
	String fraction = (n + "/" + d);
	return fraction;
}



Meanwhile, I'll try and modify your code to make it work.

This post has been edited by AntonWebsters: 18 September 2009 - 12:01 PM

Was This Post Helpful? 0
  • +
  • -

#9 Jrdpa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 16-February 09

Re: Mutators & Accessors

Posted 18 September 2009 - 12:04 PM

View PostAntonWebsters, on 18 Sep, 2009 - 11:00 AM, said:

Are you sure the setFraction(n + "/" + d) in the constructor is gonna work?
Get rid of it...
I think you should make a displayFraction() method for that...
public String displayFraction(){
	String fraction = (n + "/" + d);
	return fraction;
}



Meanwhile, I'll try and modify your code to make it work.



Not really. It's something I found here which is a program on fractions. It involves a toString, which I need, so I took that little bit to try to figure out my own program.
Was This Post Helpful? 0
  • +
  • -

#10 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Mutators & Accessors

Posted 18 September 2009 - 12:17 PM

First of all, in my opinion, I think a setter is for users to change the value of numerator and denominator in the main method...you shouldn't use it in your constructor that way.
Secondly, the setFraction() method you made takes in int newFraction as argument...but the way you used it in the constructor,setFraction(n + "/" + d), it's taking in String as argument.
And um, yeah, you can use toString() method to display the output as well.
Was This Post Helpful? 0
  • +
  • -

#11 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2637
  • View blog
  • Posts: 15,663
  • Joined: 24-September 08

Re: Mutators & Accessors

Posted 18 September 2009 - 12:19 PM

Try:

public class Fraction {

private int n;
private int d;

public Fraction(int n, int d) {

this.n = n;
this.d = d;

}

private String toString() {

return (d + "/" + n);

}

}




And in main you can just call Fraction f = new Fraction(3,5); or whatever and say double d = f.toString(); System.out.println(d) and it should display 3/5 I think. It's been a while lol.
Was This Post Helpful? 0
  • +
  • -

#12 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Mutators & Accessors

Posted 18 September 2009 - 12:38 PM

View PostNeoTifa, on 18 Sep, 2009 - 11:19 AM, said:

Try:

public class Fraction {

private int n;
private int d;

public Fraction(int n, int d) {

this.n = n;
this.d = d;

}

private String toString() {

return (d + "/" + n);

}

}




And in main you can just call Fraction f = new Fraction(3,5); or whatever and say double d = f.toString(); System.out.println(d) and it should display 3/5 I think. It's been a while lol.


Ma'am, you're assigning String to variable "d" of type double in your double d = f.toString()? @.@
toString() method returns String...
Shouldn't it be like this?
String dStr = f.toString();
double d = Double.parseDouble(dStr);



Other than the minor mistake, NeoTifa's example is a very nice one. =)

This post has been edited by AntonWebsters: 18 September 2009 - 12:53 PM

Was This Post Helpful? 0
  • +
  • -

#13 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2637
  • View blog
  • Posts: 15,663
  • Joined: 24-September 08

Re: Mutators & Accessors

Posted 18 September 2009 - 02:24 PM

LOL! I was half asleep XD I was thinking of actually assigning the value for some reason....

This post has been edited by NeoTifa: 18 September 2009 - 02:25 PM

Was This Post Helpful? 0
  • +
  • -

#14 Jrdpa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 16-February 09

Re: Mutators & Accessors

Posted 21 September 2009 - 09:00 AM

I am trying bits of what you guys have, but am still getting errors. My tutor is lost at this point as well. This is Chapter 5 and I still have to do 6 and then 7/8 are due Sat at midnight...doing 2 chapters a week and seeming to not be able to understand mutators/accessors is just killing me! Not to mention I have Visual Basic and Networking on top of this.

import java.math.*;


public class Fraction {

int d, n;

	//public String toString();
	Fraction (int n, int d) { 
		// numerator = n; denominator = d;
		getFraction(n +  "/" + d);
	}
   
   
	public Fraction() {
	}
		
	public static void main(String[] args) {
	
	}
	
	// Converts fraction to a string format n/d 
	public String displayFraction(){
	String fraction = (n + "/" + d);
	return fraction;
}

	// mutator
	public String setFraction(int newFraction) {
	return newFraction.toString();
}

}

 


//public class fraction {

//private double n ();

//public void setNumerator(double newNumberator){
  //  n = newNumerator;
//}





Errors:
Fraction.java:19: cannot find symbol
symbol : method getFraction(java.lang.String)
location: class Fraction
getFraction(n + "/" + d);
^
Fraction.java:38: int cannot be dereferenced
return newFraction.toString();
^
2 errors

I haven't even started on the Test program for it yet. :/
Was This Post Helpful? 0
  • +
  • -

#15 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Mutators & Accessors

Posted 21 September 2009 - 09:32 AM

You haven't defined getFraction() method yet...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1