Creating Objects in a Few Simple Steps

Slides:



Advertisements
Similar presentations
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Advertisements

Written by: Dr. JJ Shepherd
TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
FIT Objectives By the end of this lecture, students should: understand the structure of a class understand the concept of a method understand.
Writing methods and Java Statements. Java program import package; // comments and /* … */ and /** javadoc here */ public class Name { // instance variables.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
Understanding class definitions Looking inside classes.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Introducing Objects. Structure  Objects have two parts: Instance Variables (attributes, adjectives) Instance Variables (attributes, adjectives) private.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Static?. Static Not dynamic class Widget { static int s; int d; // dynamic // or instance // variable }
More on Objects Mehdi Einali Advanced Programming in Java 1.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Written by: Dr. JJ Shepherd
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Java: Base Types All information has a type or class designation
Written by: Dr. JJ Shepherd
Andrew(amwallis) Classes!
Objects Real and Java COMP T1 3
Java: Base Types All information has a type or class designation
OOP Powerpoint slides from A+ Computer Science
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Intro To Classes Review
Advanced Programming in Java
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
CS 302 Week 11 Jim Williams, PhD.
Advanced Programming in Java
Class Structure 16-Nov-18.
Defining Classes and Methods
An Introduction to Java – Part II
Class Structure 28-Nov-18.
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
© A+ Computer Science - OOP © A+ Computer Science -
Today’s topics UML Diagramming review of terms
Defining Classes and Methods
Chapter 4 Topics: class declarations method declarations
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Recap Week 2 and 3.
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
Class Structure 25-Feb-19.
Object-Oriented Programming
Defining Classes and Methods
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
References Revisted (Ch 5)
Review for Midterm 3.
Classes and Methods 15-Aug-19.
Object-Oriented Design AND CLASS PROPERTIES
Presentation transcript:

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

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

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!

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.

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>> { }

Define the Class Example public class Bird { }

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>>;

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

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

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>> }

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

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>> }

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

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>>; }

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

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>> }

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;

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

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>>+“ ”+… }

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

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>>()&& …; }

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

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;