Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating Objects in a Few Simple Steps

Similar presentations


Presentation on theme: "Creating Objects in a Few Simple Steps"— Presentation transcript:

1 Creating Objects in a Few Simple Steps
Written by Dr. JJ Shepherd

2 Overview These slides will go over the very basics of creating a class with an accompanying example. The following steps are a suggested way to create classes, but may not be the most appropriate for all situations. Whenever the enclosing symbol “<<>>” is used this indicates “Fill in the rest” or “substitute with something”. Whenever the symbol “…” is used it indicates there is additional code that has been omitted

3 The Steps Define the Class Create the Instance Variables (attributes)
Make their scope private Create the Constructors Default Parameterized for each attribute Create the Accessors for every attribute Create the Mutators for every attribute Create the other Methods Equals() and toString() are both good ones to have Profit!

4 The Example Create a class called Bird
Bird has the following attributes Name: A string representing the bird’s name Weight: A decimal number indicating the bird’s weight Age: A whole number indicating the bird’s age Bird also has the following methods Equals: A method that takes in another instance of Bird and returns true of false depending on if the attributes of one are equal to the other toString: A method that takes nothing and returns a String composed of its attributes.

5 Define the Class Classes are used to create instance of objects. They are a way of grouping together common attributes and actions into a single type. Defining the class takes the following formula public class <<Name of Class>> { }

6 Define the Class Example
public class Bird { }

7 Create Instance Variables
These are the properties / attributes that are important to each instance of the class. Defining these take the following formula for each attribute private <<type>> <<identifier>>;

8 Create Instance Example
public class Bird { private String name; private double weight; private int age; }

9 Create Constructors Constructors are used to create the object in memory. There are two types of constructors that are focused on in this course Default Parameterized

10 Create Default Constructor
The default constructor is used to create the object and set all the attributes to some default value. They follow this formula public <<Class’ Name>>() { <<Set default values>> }

11 Create Default Constructor Example
public Bird() { this.name = “none”; this.weight = 0.0; this.age = 0; }

12 Create Parameterized Constructor
The parameterized (param) constructor is used to create the object and set all the attributes to values passed via a parameter. Strong advisement for calling the classes Mutators (discussed later) to set the values while also checking for correctness. They follow this formula public <<Class’ Name>>(<<Param for each attribute>>) { <<Set values using mutators>> }

13 Create Parameterized Constructor Example
public Bird(String aName, double aWeight, int anAge) { this.setName(aName); this.setWeight(aWeight); this.setAge(anAge); }

14 Create Accessors Accessors give access to attributes outside of the class, and should be created for each attribute. They follow this formula public <<attribute type>> get<<attribute identifier>>() { return this.<<attribute>>; }

15 Create Accessors Example
public String getName() { return this.name; } public double getWeight() return this.weight; public int getAge() return this.age;

16 Create Mutators Mutators are used to set attribute values and should be created for each attribute. They also check for potential errors in assigning values They follow this formula public void set<<attribute identifier>>(<<the value to be set>>) { <<Check for errors>> this.<<attribute>> = <<the value to be set>> }

17 Create Mutators Example
public void setName(String aName) { //We are not checking for errors on name’s this.name = aName; } public void setWeight(double aWeight) if(aWeight >= 0)//Assuming weights are non-negative this.weight = aWeight; public void setAge(int anAge) if(anAge >= 0)//Assuming ages are non-negative this.age = anAge;

18 Create Other Methods Other methods are dependent on the object; as they represents the object’s actions. Despite this there are two very common other methods that are not usually required, but are strongly recommended toString equals

19 Create toString This method should return a string that represents the class and it’s attributes. This is very useful for debugging as if the object is placed in a System.out.println() statement then it will print its toString’s String They follow this formula public String toString() { return <<attribute 1>>+“ ”+… }

20 Create toString Example
public String toString() { return this.name+“ ”+ this.weight+“ ”+ this.age; }

21 Create equals This is another useful method that will help determine if all of the attributes of one instance of an object is equal to the attributes of another. Remember to use “==” for primitive types and “.equals” for object types It follows this formula public boolean equals(<<Another instance>>) { return <<Another Instance>> != null && this.<<attribute>> <<either == or .equals) <<Another instance>>.get<<attribute>>()&& …; }

22 Create equals Example public boolean equals(Bird aBird) { return aBird != null && this.name.equals(aBird.getName()) && this.weight == aBird.getWeight() && this.age == aBird.getAge(); }

23 Putting it all together Example
Here is the full definition of the Bird class example public void setWeight(double aWeight) public class Bird if(aWeight >= 0)//Assuming weights are non-negative { private String name; this.weight = aWeight; private double weight; private int age; public Bird() public void setAge(int anAge) this.name = "none"; if(anAge >= 0)//Assuming ages are non-negative this.weight = 0.0; this.age = 0; this.age = anAge; } public Bird(String aName, double aWeight, int anAge) public String toString() this.setName(aName); this.setWeight(aWeight); return this.name+" "+ this.setAge(anAge); this.weight+" "+ this.age; public String getName() public boolean equals(Bird aBird) return this.name; return aBird != null && public double getWeight() this.name.equals(aBird.getName()) && this.weight == aBird.getWeight() && return this.weight; this.age == aBird.getAge(); public int getAge() }//end class return this.age; public void setName(String aName) //We are not checking for errors on name’s this.name = aName;


Download ppt "Creating Objects in a Few Simple Steps"

Similar presentations


Ads by Google