Creating Methods

Tutorial Description:
Understanding and using user-created java methods in your scripts.
Some of you may want to know about creating methods in java. I went threw weeks of studying and finally found out, or really understand how methods work and how to create them.
Lets start off with a regular class called Course.java Here is my code for this class:
public class Course {
public static void main (String[] args) {
Course course1 = new Course("ComputerScience", 1, 100.0);
Course course2 = new Course("English", 2, 76.0);
Course course3 = new Course("PE", 3, 96.0);
course1.ridPoints(12.2, 100.0);
System.out.println(course1);
course2.addPoints(2.0, 76.0);
System.out.println(course2);
course3.addPoints(3.0, 96.0);
System.out.println(course3);
System.out.println(course3.courseName());
System.out.println(course2.grade());
}
}
Ok, now you may wonder, WTF is this new method addPoints and ridPoints??? Well, I am using my objects (course1,2,3) and implementing my methods on them.
Course course1 = new Course("ComputerScience", 1, 100.0);
Course course2 = new Course("English", 2, 76.0);
Course course3 = new Course("PE", 3, 96.0);
This creates 3 courses for me to play with. Inside of each object’s parenthesis, there are 3 different things that define it. It’s Name, its Period, and its Current Grade.
course1.ridPoints(12.2, 100.0);
System.out.println(course1);
course2.addPoints(2.0, 76.0);
System.out.println(course2);
These 2 pretty much work alike, except in the first one, It gets rid of 12.2 points from a score of 100.0, and the value is stored in course1.
In the second one, we add 2.0 points to 76, and is stored in course2.
Now, of course we can’t just make methods and have no background information right? Hell yea thats right.
Here is the method code:
public class Course {
private int period;
private double grade;
private String courseName;
//Set up a course, period, and grade
public Course (String nameOfCourse, int periodOfCourse, double gradeofCourse) {
courseName = nameOfCourse;
period = periodOfCourse;
grade = gradeofCourse;
}
public String courseName() {
return courseName;
}
public int period() {
return period;
}
public double grade(){
return grade;
}
public double addPoints (double pointsAdded, double originalPoints) {
grade = pointsAdded + originalPoints;
return grade;
}
public double ridPoints (double pointsRidded, double originalPoints) {
grade = originalPoints - pointsRidded;
return grade;
}
public String toString(){
return (courseName + "\t\tPeriod= " + period + "\tGrade= " + grade);
}
}
Lets start with our simple methods such as:
public double ridPoints (double pointsRidded, double originalPoints) {
grade = originalPoints - pointsRidded;
return grade;
}
Here we know that this will be public, and will return double values, and this method’s name is ridPoints. Inside the paranthesis, is the data we have to type in order for any calculations to occur.
So now we can just add our objects name a (dot) and then out method.
Example : course1.ridPoints(12.0, 80.0);
The more simple methods like:
public int period() {
return period;
These just return the periods/grades/names of our objects. For instance we can try this: System.out.println(“Course 1 Period: ” + course1.period)
and this wil be printed
Course 1 Period: 1
public Course (String nameOfCourse, int periodOfCourse, double gradeofCourse) {
courseName = nameOfCourse;
period = periodOfCourse;
grade = gradeofCourse;
}
This declares what will be inside of our objects values.
Notice it’s name is the same as its class.
private int period;
private double grade;
private String courseName;
These private variables tell us, and the compiler that the only person/thing in the world that can modify them, is anything in the Course class.
In simpler terms, we can create a WhyAmIFailing.java file, and use our course.java methods.
But inside of our WhyAmIFailing.java, we cannot change any of the private values.
If these would have been declared public, you could have. Though this is not safe, and should not become a habit.
Good luck.

