© A+ Computer Science - www.apluscompsci.com OOP Pieces © A+ Computer Science - www.apluscompsci.com.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Chapter 3 Introduction to Classes, Objects, Methods, and Strings
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
{ int fun = 99; } Scope refers to whether the variable is seen within a certain context Any variable defined inside of curly brackets, only exists within.
Road Map Introduction to object oriented programming. Classes
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
Writing Classes (Chapter 4)
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Chapter 4 Introduction to Classes, Objects, Methods and strings
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Working With Objects Tonga Institute of Higher Education.
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
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.
Topics Instance variables, set and get methods Encapsulation
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.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes (Part 1) Lecture 3
3 Introduction to Classes and Objects.
OOP Powerpoint slides from A+ Computer Science
Examples of Classes & Objects
Java Programming: Guided Learning with Early Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3: Using Methods, Classes, and Objects
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
Chapter 3 Introduction to Classes, Objects Methods and Strings
Multiple Choice -answer the easiest question 1st
Lesson A4 – Object Behavior
A+ Computer Science INPUT.
Classes & Objects: Examples
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
A+ Computer Science METHODS.
Chapter 4 Writing Classes.
© A+ Computer Science - OOP © A+ Computer Science -
Classes, Objects, Methods and Strings
© A+ Computer Science - Classes And Objects © A+ Computer Science -
A+ Computer Science METHODS.
See requirements for practice program on next slide.
Object Oriented Programming in java
A+ Computer Science AP Review 2019 AP CS A EXAM
Introduction to C Programming
Corresponds with Chapter 5
Presentation transcript:

© A+ Computer Science - www.apluscompsci.com OOP Pieces © 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 AplusBug(); 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. Instantiation creates a spot for an object and sets the initial state of the object. Object state is determined by the objects instance variables. © A+ Computer Science - www.apluscompsci.com

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

© A+ Computer Science - www.apluscompsci.com References Scanner keyboard = new Scanner(System.in); AplusBug dude; dude = new AplusBug(); 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 References AplusBug dude = new AplusBug(); dude 0x234 0x234 AplusBug dude is an AplusBug reference. new AplusBug() creates a new AplusBug Object out in memory. dude stores the location of that new AplusBug Object. dude is a reference variable that refers to an AplusBug object. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Instance Variables © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Instance Variables When you need multiple methods to have access to the same variable, make the variable an instance variable. The scope of an instance variable is the entire class where the 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. Instance variables are defined at the top of the class. Instance variable can be used by all methods within the class. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Private All members with private access can be accessed or modified only inside the class where they are defined. All members of a class with private access can be accessed or modified within the class where they are defined only. Private members cannot be accessed outside of the class. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Encapsulation All data members should have private access. A set of public methods should be provided to manipulate the private data. Data should be declared with private access and public methods should be provided to manipulate the private data. © 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 Instance Variables public class Calc { private int one, two; private int answer; public void add(){ answer = one + two; } public int getAnswer(){ return answer; Instance variables are shared by all methods in a class. Instance variables are defined at the top of the class. Instance variable can be used by all methods within the class. © 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

Constructors vs. Methods © A+ Computer Science - www.apluscompsci.com access name params code method 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. access return type name params code © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Constructors public Triangle() { sideA=0; sideB=0; sideC=0; } 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 Default Constructor class Triangle { private int sideA, sideB, sideC; public Triangle() sideA=0; sideB=0; sideC=0; } 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

© A+ Computer Science - www.apluscompsci.com Instance Variables class Triangle { private int sideA, sideB, sideC; public Triangle(int a, int b, int c) { sideA=a; sideB=b; sideC=c; } public String toString() return sideA + " " + sideB + " " + sideC; Instance variables are shared by all methods in a class. They are initialized in the constructor. Instance variables are defined at the top of the class. Instance variable can be used by all methods within the class. © 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; } Constructors are similar to methods. Constructors set the properties of an object to an initial state. © 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

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

Modifier & Accessor Methods © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Modifier Methods Modifier methods are methods that change the properties of an object. Modifier methods make changes to the instance variables of the class. © 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 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 class Triangle { private int sideA, sideB, sideC; public Triangle(int a, int b, int c) { sideA=a; sideB=b; sideC=c; } public String toString() return sideA + " " + sideB + " " + sideC; return type return method toString() is used to display an Object. print() and println() automatically call toString() when displaying an Object reference. toString() typically sends back all data/properties from an Object as one String. © 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 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 Local Variables public class LocalVars { private int fun; //instance variable public void change() { int fun = 99; //local variable } public void print() { System.out.println(fun); public static void main(String args[]) LocalVars test = new LocalVars(); test.change(); test.print(); OUTPUT Class LocalVars has an one instance variable named fun. Method change contains a local variable named fun. Local variables take precedence over instance variables. Local variables can only be used inside the method in which they are defined. © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com LocalVars fun 0 change fun = 99 print Class LocalVars has an one instance variable named fun. Instance variable fun can be used anywhere in class LocalVars. Method change contains a local variable named fun. Method change does not make any changes to the instance variable fun. Local variable fun can only be used in method change. © A+ Computer Science - www.apluscompsci.com

Formatting Numbers

© A+ Computer Science - www.apluscompsci.com Formatting Output How to format What to format out.printf( " %.2f " , 9.237284 ); Method printf() is used to format output. printf() is most commonly used to set the number of decimal places when displaying a real number. printf() can also be used to align output to the left or to the right. The % sign is used to indicate that a value needs to be displayed. The value will be found in the comma separated list. %f – real / decimal value %d – integer value %c – character value %s – string value - left aligned OUTPUT9.24 © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Formatting Output OUTPUT dec == 9.2 dec == 9.23 dec == 9.231 dec == 9.2315 dec == 9.23148 double dec = 9.231482367; out.printf("dec == %.1f\n",dec); out.printf("dec == %.2f\n",dec); out.printf("dec == %.3f\n",dec); out.printf("dec == %.4f\n",dec); out.printf("dec == %.5f\n",dec); Method printf() is used to format output. printf() is most commonly used to set the number of decimal places when displaying a real number. printf() can also be used to align output to the left or to the right. The % sign is used to indicate that a value needs to be displayed. The value will be found in the comma separated list. %f – real / decimal value %d – integer value %c – character value %s – string value - left aligned © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Formatting Output Column size # of decimals out.printf( " %9.2f " , 8.25612 ); Method format() is used to format output. format() is most commonly used to set the number of decimal places when displaying a real number. format() differs from printf() in that format() is a return method and printf() is a void method. The % sign is used to indicate that a value needs to be displayed. The value will be found in the comma separated list. %f – real / decimal value %d – integer value %c – character value %s – string value - left aligned OUTPUT ? type of data © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Formatting Output double dec = 5.3423; out.println(String.format("%.3f",dec)); out.println(String.format("%12.3f",dec)); out.println(String.format("%-7.3fx",dec)); OUTPUT ? Method format() is used to format output. format() is most commonly used to set the number of decimal places when displaying a real number. format() differs from printf() in that format() is a return method and printf() is a void method. The % sign is used to indicate that a value needs to be displayed. The value will be found in the comma separated list. %f – real / decimal value %d – integer value %c – character value %s – string value - left aligned © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Formatting Output int num = 923; out.printf("%d\n", num); out.printf("%6d\n", num); out.printf("%-6d\n", num); out.printf("%06d\n", num); OUTPUT ? Method printf() is used to format output. printf() is most commonly used to set the number of decimal places when displaying a real number. printf() can also be used to align output to the left or to the right. The % sign is used to indicate that a value needs to be displayed. The value will be found in the comma separated list. %f – real / decimal value %d – integer value %c – character value %s – string value - left aligned © A+ Computer Science - www.apluscompsci.com

© A+ Computer Science - www.apluscompsci.com Formatting Output int num = 567; out.println(String.format("%d",num)); out.println(String.format("%6d",num)); out.println(String.format("%-6d",num)); out.println(String.format("%06d",num)); OUTPUT ? Method format() is used to format output. format() is most commonly used to set the number of decimal places when displaying a real number. format() differs from printf() in that format() is a return method and printf() is a void method. The % sign is used to indicate that a value needs to be displayed. The value will be found in the comma separated list. %f – real / decimal value %d – integer value %c – character value %s – string value - left aligned © A+ Computer Science - www.apluscompsci.com