Types of classes, part 2

Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don't hold it against me. One of the perils of having a 20+ year old website!

Today I will continue my series on classes in Java. To review where we are, you can read this post.

First of all, we need to add a couple of methods to our class. There are two other types of methods in classes: reporters and mutators. A reporter does exactly that. As you might notice, our instance variables are private, meaning that only that class can see it. So if we want to know certain information about the student, we must write a method that looks at the variable in question and reports it. A mutator method changes the object in some way. So, that being said, lets write a couple of these methods.

………..

public String tellName(){
return name;
}

public void computeGPA(float newAvg){
float oldAvg= gpa;
gpa= (oldAvg+newAvg)/2;
}

Here, we have added two methods to our class- a reporter and a mutator. Our reporter, tellName, looks at the current value of String name and returns it. Since we are returning a value, the method must be of the type we are returning. It is public so it can be accessed from outside our class. Now, here I just wrote one for name, but to get the other values (age, gpa, status), you must write a reporter method for each variable, as it’s respective type (an age reporter would be of type int).

Our next method is a mutator that figures out a new gpa. This is a simple one that accepts a new GPA from the client and averages the new and old one together. This method is not returning anything, just doing a calculation, so it is a void method. It is accepting a variable of type float, which we declare in the parentheses before the do work in the method. Then we perform the calculation. We declare a new variable called oldAvg that gets the value of gpa, and then we assign the new value to gpa.

That is all there is to other types of methods. They can be as simple or as complicated as you see fit. Next time, I will talk about inheritance and extensions. Later!

Similar Posts

  • Quoteables

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!I like writing down some funny quotes from professors, and this first week was full of them… “What kind of place…

  • |

    Teach yourself how to program

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!As some of you may know, I have taught myself a lot of the programming I know. It started with HTML,…

  • |

    Stu.dicio.us

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!Stu.dicio.us is a social note-taking website that looks really nice and handy. It has your schedule, todo list for each class…

  • The first weekend…

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!Im fucking exhausted already. I got here friday- leadership dinner. Sat- Move in frreshmen on 3rd floors in hot sun. Then…

  • | |

    What Developers can learn from Home Improvement

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website! It’s no secret that I love the show Home Improvement, especially if you know me personally or follow me on…

  • Another Year Gone By

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!Well today (or really yesterday) I had my last final of my junior year, which more or less makes me a…