UniS 1 Object-Oriented Software Engineering CS288.

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Basics of Classes in Java.
Advertisements

Color Templates Software Engineering Module: Core of Java Topic: Constructors TALENTSPRINT | © Copyright 2012.
Fundamentals of Software Development 1Slide 1 Recap: Constructors, the new operator and the this object Three ideas:Three ideas: –How to write a constructor.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
UniS 1 Object-Oriented Software Engineering CS288.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Object-Oriented Software Engineering CS Multiple Classes Contents Defining multiple classes Using objects of one class in another Multiple constructor.
Object Oriented Paradigm Programming Paradigms En Mohd Norafizal A.Aziz.
Arrays and Objects OO basics. Topics Basic array syntax/use OO Vocabulary review Simple OO example Instance and Static methods Static vs. instance vs.
1 Object-Oriented Software Engineering CS Multiple Classes Contents Lists of objects Vectors Growing and shrinking Vectors Iteration over Vector.
Writing Classes (Chapter 4)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
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!
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
1 Software Construction Lab 3 Classes and Objects in Java Basics of Classes in Java.
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Objects and Classes Mostafa Abdallah
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
PART 1 Part 1: The Material. Whether you knew it or not, all the programming that you have performed until now was in the boundaries of procedural programming.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CLASSES AND OBJECTS Object-Oriented Basics. Vocabulary Review – C++ Class Object Instance this new Constructor Default constructor Access (private/public/protected)
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
1 Object-Oriented Software Engineering CS Java OO Fundamentals Contents Classes and Objects Making new objects Method declarations Encapsulating.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Classes - Intermediate
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Methods.
Topics Instance variables, set and get methods Encapsulation
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Class Fundamentals BCIS 3680 Enterprise Programming.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Topic: Classes and Objects
Static data members Constructors and Destructors
Yanal Alahmad Java Workshop Yanal Alahmad
Object Oriented Programming
Classes and Objects in Java
Classes and Objects in Java
Classes and Objects in Java
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
Java LESSON 7 Objects, Part 1
Classes & Objects: Examples
Classes, Encapsulation, Methods and Constructors (Continued)
Classes Lecture 7 from Chapter /1/11.
Dr. R Z Khan Handout-3 Classes
Corresponds with Chapter 5
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

UniS 1 Object-Oriented Software Engineering CS288

UniS 2 Java OO Fundamentals Contents Classes and Objects Making new objects Method declarations Encapsulating fields Manipulating object field values

UniS 3 Classes and Objects Class: Template that defines how to make an object. Object: Is an instance of a class. Car int numDoors Double engineSize String make void start ( ) { code } void stop ( ) { code } Car bigFlashJag numDoors = 5 engineSize = 3.5l make = Jaguar start ( ) stop ( ) Car ecoCar numDoors = 2 engineSize = 0.5l make = Smart start ( ) stop ( ) ClassObject

UniS 4 Building Objects Computer Memory Class Definition (acts like template for new objects) Object1Object2Object3Object4 Each object has copies of fields and methods defined by class template, but initialised differently each time

UniS 5 Very Simple Class Example public class SimpleClass { public SimpleClass(String newFieldVal) { uselessField = newFieldVal; } private String uselessField; public static void main(String[ ] args) { /* Code Goes Here */ } Main method executed first. This is where objects can first be set up. Constructor, defines how new objects are initialised.

UniS 6 Syntax for defining new object SimpleClass ob1 = new SimpleClass("Fluffy"); object type object name new keyword class constructor to set up object semicolon terminates ALL statements (just like C++)

UniS 7 Very Simple Class Example public class SimpleClass { public SimpleClass(String newFieldVal) { uselessField = newFieldVal; } private String uselessField; public static void main(String[ ] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass(“I like chocolate"); SimpleClass ob3 = new SimpleClass(“Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); }

UniS 8 Executing the Code Five new objects are created in memory, each with their own copy of ‘uselessField’, and each copy has a different value.

UniS 9 Setter and Getter Methods public class SimpleClass { /* declare class field */ /* define class constructor */ public String getUselessField () { return uselessField; } public void setUselessField (String newUselessField) { uselessField = newUselessField; } /* main method */ }

UniS 10 Getter method for field public String getUselessField () { return uselessField; } public keyword allows this method to be called from other classes type of object returned by method name and parameters of method body of method, what it does

UniS 11 Setter method for field public void setUselessField (String newUselessField) { uselessField = newUselessField; } has String object parameter does not return value changes value of field

UniS 12 Extending main method public static void main(String[ ] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); ob1.setUselessField (aStrVar); }

UniS 13 Execution of main method public static void main(String[] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); ob1.setUselessField (aStrVar); } When execution gets to here objects have these values

UniS 14 Execution of main method public static void main(String[] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); ob1.setUselessField (aStrVar); } When execution gets to here objects have these values

UniS 15 Execution of main method public static void main(String[] args) { SimpleClass ob1 = new SimpleClass("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); SimpleClass ob3 = new SimpleClass("Why cant I go home"); SimpleClass ob4 = new SimpleClass("Flobber Worms Taste Nice"); SimpleClass ob5 = new SimpleClass("Guildford"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = ob2.getUselessField (); ob1.setUselessField (aStrVar); } When execution finishes here objects have these values

UniS 16 Backup Method for Field Value Modify SimpleClass to include new field and methods: private String previousVal; public void setUselessField (String newUselessField) { previousVal = uselessField; uselessField = newUselessField; } public void restoreField () { uselessField = previousVal; }

UniS 17 Experiment with main method public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = "Eat Cabbage"; ob1.setUselessField (aStrVar); ob1.restoreField (); } When execution gets to here objects have these values

UniS 18 Experiment with main method public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = "Eat Cabbage"; ob1.setUselessField (aStrVar); ob1.restoreField (); } When execution gets to here objects have these values

UniS 19 Experiment with main method public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = "Eat Cabbage"; ob1.setUselessField (aStrVar); ob1.restoreField (); } When execution gets to here objects have these values

UniS 20 Experiment with main method public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); ob1.setUselessField ("I wish I'd done C instead"); String aStrVar = "Eat Cabbage"; ob1.setUselessField (aStrVar); ob1.restoreField (); } When execution gets to here objects have these values

UniS 21 Is there a problem with the restore method? public static void main (String[ ] args) { SimpleClass ob1 = new SimpleClass ("Fluffy"); SimpleClass ob2 = new SimpleClass("I like chocolate"); ob1.setUselessField ("I wish I'd done C instead"); ob2.restoreField (); } When execution gets to here objects have these values

UniS 22 Summing Up Every object has copies of the class fields and methods. We have seen example field values for multiple objects. Seen examples of methods for accessing and changing field values. We have seen how objects are created and manipulated during execution of the main method.