Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values.

Slides:



Advertisements
Similar presentations
Introduction to classes Sangeetha Parthasarathy 06/11/2001.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Objects, Methods, Parameters, Input Lecture 5, Thu Jan
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter Day 7. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 7 Questions from last Class?? Problem set 1 Corrected  Good results 3.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Introduction to Java Java SE 8 for Programmers Paul Deitel Harvey Deitel Deitel Developer Series 2014.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
10-Nov-15 Java Object Oriented Programming What is it?
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
Objects & Classes Weiss ch. 3. So far: –Point (see java.awt.Point) –String –Arrays of various kinds –IPAddress (see java.net.InetAddress) The Java API.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Wednesday –POD –I have updated grades in powerschool. If you have a zero for a lab grade, it probably means you didn’t DropItToMe. Please do so. –Slides.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Access Control to Class Members tMyn1 Access Control to Class Members In its support for encapsulation, the class provides two major benefits. First, it.
Programming in Java (COP 2250) Lecture 10 Chengyong Yang Fall, 2005.
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 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.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
יסודות מדעי המחשב – תרגול 6
Variables in Java A variable holds either
Examples of Classes & Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3: Using Methods, Classes, and Objects
Classes A class is a blueprint of an object
Chapter 4: Writing Classes
Object Based Programming
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
Classes & Objects: Examples
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Anatomy of a Method.
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Packages & Random and Math Classes
Chapter 9 Objects and Classes
Outline Creating Objects The String Class The Random and Math Classes
Using java libraries CGS3416 spring 2019.
OO Programming Concepts
ITE “A” GROUP 2 ENCAPSULATION.
CSG2H3 Object Oriented Programming
Presentation transcript:

Classes/Objects

Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values –can be initialized to null Use new to instantiate new objects/initialize object variables –String name = new String(“Mickey”); –invokes Constructor of String class

Example int num = 5; String name = new String(“Mickey”); String name2 = “Minnie”; //String shortcut 5 num name Mickey name2 Minnie

Dot Operator Use dot operator to access methods of an object –name.length()

Aliases Two object variables can point to the same object With primitive types, a copy is made int a = 5; int b = 12; a = b; 12 a b

Aliases String name = “Mickey”; String name2 = “Mickey”; name Mickey name2 Mickey

Aliases name = name2; name name2 Mickey

Strings Strings are immutable String methods –char charAt(int index) –String replace(char oldChar, char newChar)

Strings String s = “Hello” String t = s.replace('l', 'p'); s Hello t Heppo

Packages Classes in the standard Java library are grouped into packages –java.util –java.math –java.text Using the API

import To use a class from the library you must either use the fully-qualified name or import the class java.util.Scanner s = new java.util.Scanner(System.in) import java.util.Scanner; import java.util.*; //import all classes in java.util.

Exercises Complete PP 3.1 –Write an application that prompts for and reads the user's first and last name (separately). Then, print a string composed of the first letter of the user's first name, followed by (no more than) the first five characters of the user's last name, followed by a random number in the range 10 to 99. Hint: You will need to use java.util.Random

Java Classes Contain data and methods Methods enable user to access and modify data

Encapsulation Data should be hidden from the user –Access to/modification of data is controlled by methods Modifiers –Enable programmer to specify which data and methods are private and which are public private are accessible within the class public are accessible from anywhere

Name.java public class Name { private String first; private String last; //Constructor public Name(String thefirst, String thelast) { first = thefirst; last = thelast; } public void printName() { System.out.println("First: " + first + " Last: " + last); }

Driver Classes The driver typically contains the main method –this is the starting point for the program The driver creates instances of other classes

NameTest.java public class NameTest { public static void main(String[] args) { Name n = new Name("Sami", "Rollins"); n.printName(); }

Methods public void printName() {... } modifier return_type name(type name,...) You must specify the return type and the type of each parameter

More Method Headers For each data member, consider whether you should create setter and getter methods public String getFirstName() public void setFirstName(String newname) public String getLastName() public void setLastName(String newname)

Constructors Called when a new object is created Like a method with no return type Should initialize all relevant data Should take as input initial values for any variables that do not have a logical default value A default constructor takes no parameters public Name(String thefirst, String thelast) { first = thefirst; last = thelast; }

Scope public Name(String first, String last) { this.first = first; this.last = last; } this provide access to class variable first/last are local to the constructor

Exercises Complete exercise PP4.4 –Design and implement a class called Book that contains instance data for the title, author, publisher, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Create a driver class called BookShelf, whose main method instantiates and updates several Book objects.