© A+ Computer Science - www.apluscompsci.com Classes And Objects © A+ Computer Science - www.apluscompsci.com.

Slides:



Advertisements
Similar presentations
Looking inside classes Fields, Constructors & Methods Week 3.
Advertisements

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
Road Map Introduction to object oriented programming. Classes
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
Understanding class definitions Looking inside classes.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Composition details Recall, composition involves 3 things: –Declaration of instance variable of.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
© A+ Computer Science - Chicken yeller = new Chicken();
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
© A+ Computer Science - import java.util.Scanner; Try to be as specific as possible when using an import.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
© A+ Computer Science - Visit us at Full Curriculum Solutions M/C Review Question Banks.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
© A+ Computer Science - Scanner frequently used methods NameUse nextInt()returns the next int value nextDouble()returns the next.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
3 Introduction to Classes and Objects.
OOP Powerpoint slides from A+ Computer Science
Java Programming: Guided Learning with Early Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Yanal Alahmad Java Workshop Yanal Alahmad
Intro To Classes Review
Chapter 3: Using Methods, Classes, and Objects
Something about Java Introduction to Problem Solving and Programming 1.
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
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.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Multiple Choice -answer the easiest question 1st
Lesson A4 – Object Behavior
A+ Computer Science INPUT.
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
A+ Computer Science METHODS.
More on Classes and Objects
© A+ Computer Science - OOP © A+ Computer Science -
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Introduction to Java Brief history of Java Sample Java Program
A+ Computer Science INPUT.
A+ Computer Science METHODS.
See requirements for practice program on next slide.
Object Oriented Programming in java
A+ Computer Science PARAMETERS
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
A+ Computer Science AP Review 2019 AP CS A EXAM
Review for Midterm 3.
Basic Exception Handling
Corresponds with Chapter 5
Presentation transcript:

© A+ Computer Science - www.apluscompsci.com Classes And Objects © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Objects © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Object Instantiation new Scanner(System.in); new Monster(); When the reserved word new is combined with a constructor call, a new Object is created in memory. This process of creating a new Object in memory is called instantiation. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Objects © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Object Instantiation Chicken yeller = new Chicken(); yeller is a Chicken reference. new Chicken() creates a new Chicken Object out in memory. yeller stores the location of that new Chicken Object. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Object Instantiation Chicken yeller = new Chicken(); yeller 0x234 0x234 Chicken yeller is a Chicken reference. new Chicken() creates a new Chicken Object out in memory. yeller stores the location of that new Chicken Object. yeller is a reference variable that refers to a Chicken object. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com References Scanner keyboard = new Scanner(System.in); Monster chuck = new Monster(); Typically, a reference is used to store the location of the new Object. keyboard is a Scanner reference that is storing the location of the new Scanner. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Constructors © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Constructors very similar to methods have the same name as the class have no return type – no void,int,etc. initialize all instance variables Constructors are used to initialize all of the data. Typically, all variables are assigned a value in the constructor. A constructor is very similar to a method, but it is technically not a method. Constructors have no return type and are named the same as the class. This is slightly different from a method. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Constructors reference variable Scanner keyboard = new Scanner(System.in); Scanner is a class which must be instantiated before it can be used. In other words, you must make a new Scanner if you want to use Scanner. A reference must be used to store the location in memory of the Scanner object created. System.in is the parameter passed to the Scanner constructor so that Java will know to connect the new Scanner to the keyboard. keyboard is a reference that will store the location of newly created Scanner object. object instantiation / constructor call © A+ Computer Science - www.apluscompsci.com

Constructors VS. Methods © A+ Computer Science - www.apluscompsci.com access name params code Constructors and methods are more similar than different and sometimes constructors are referred to as constructor methods. Most of the differences are not visible. The only real visible differences exist in that the constructor has to be named the same name as the class and that the constructor has no return type. Methods and constructors both have a name, a list of parameters, and a block of code to execute. When you call a method or a constructor, the block of code associated with the name will be executed. method access return type name params code © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com public class Triangle { private int sideA, sideB, sideC; public Triangle() sideA=0; sideB=0; sideC=0; } Default Constructor Default constructors have no parameter list. When a default constructor is called, all instance variables / data fields are set to a zero value. If no constructors are provided for a class, Java will provide a default constructor that will initialize all data to a zero value. Triangle triangle = new Triangle(); © A+ Computer Science - www.apluscompsci.com

Initialization Constructor public Triangle(int a, int b, int c) { sideA=a; sideB=b; sideC=c; } Constructors often have parameters. The parameters allow data to be passed into the class so that it can be assigned to the instance variables / data fields. Initialization constructors have a parameter list and will receive parameters when called. The number of parameter and types passed in must match up with the parameter list following the method name. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Initialization Constructor class Triangle { private int sideA, sideB, sideC; public Triangle(int a, int b, int c) sideA=a; sideB=b; sideC=c; } Initialization constructors have a parameter list and will receive parameters when called. The number of parameter and types passed in must match up with the parameter list following the method name. Triangle triangle = new Triangle(3,4,5); © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Variable Scope © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Scope { int fun = 99; } Any variable defined inside of braces, only exists within those braces. That variable has a scope limited to those braces. When a variable is defined within a set of braces, that variable can only be accessed inside those braces. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Instance Variables When you need many methods to have access to the same variable, you make that variable an instance variable. The scope of an instance variable is the entire class where that variable is defined. An instance variable is a variable tied to an instance of a class. Each time an Object is instantiated, it is given its own set of instance variables. Monster x = new Monster(); x would refer to a new Monster that contains its own set of Monster instance variables. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Defining VS. Assigning int num; int num = 99; num = 56; definition only definition and assignment When defining a variable, the type must be listed and the name. When defining and assigning a variable, the type, name, and value must be listed. When assigning a variable only, the name and value must be listed. assignment only © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Local Variables When you need only one method to have access to a variable, you should make that variable a local variable. The scope of a local variable is limited to the method where it is defined. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com return methods © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Return Methods Return methods perform some action and return a result back to the calling location. int num = keyboard.nextInt(); Return methods typically take in some type of data, do something to the data, and then send back a result. nextInt() is a Scanner return method. nextInt() retrieves the next integer value from the keyboard and sends it back to num. nextInt() returns an int back to the calling location. The value returned is assigned to num. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Return Methods Scanner keyboard = new Scanner(System.in); int num = keyboard.nextInt(); out.println(num); INPUT 1 OUTPUT1 nextInt() gets the next int entered on the keyboard and returns it. The int returned by nextInt() is placed in num. return method num 1 © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Return Method access return type name params code Return methods have a return type specified before the method name. public int getNum() getNum() returns an integer value. public double getStuff() getStuff() returns a double/decimal value. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com constructors public Triangle() { sideA=0; sideB=0; sideC=0; } Default Constructor Constructors are similar to methods. Constructors set the properties of an object to an initial state. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com constructors public Triangle(int a, int b, int c) { sideA=a; sideB=b; sideC=c; } Initialization Constructor Constructors are similar to methods. Constructors set the properties of an object to an initial state. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com modifier methods public void setSides(int a, int b, int c) { sideA=a; sideB=b; sideC=c; } Modifier methods are methods that change the properties of an object. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com accessor methods public void print() { out.println(sideA + " " + sideB + " " + sideC); } Accessor methods are methods that retrieve or grant access to the properties of an object, but do not make any changes. © A+ Computer Science - www.apluscompsci.com