Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.

Slides:



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

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Fields, Constructors, Methods
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Road Map Introduction to object oriented programming. Classes
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
Introduction to Java, OO and IDEs ICW Lecture 2 Errol Thompson Room 134.
More on Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lectures 2 & 3 Wanda M. Kunkle.
Understanding class definitions Looking inside classes 3.0.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle.
Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CS 106 Introduction to Computer Science I 03 / 19 / 2008 Instructor: Michael Eckmann.
Object Interaction 1 Creating cooperating objects.
Understanding class definitions – Part II –. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle.
File Input and Output CSC 1401: Introduction to Programming with Java Week 4 – Lecture 2 Wanda M. Kunkle.
The Java String Type CSC 1401: Introduction to Programming with Java Week 7 – Lecture 1 Wanda M. Kunkle.
COMP Classes Yi Hong May 22, Announcement  Lab 2 & 3 due today.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages
Classes, Objects, and Methods
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 12.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
5.0 Objects First with Java A Practical Introduction using BlueJ Introduction to Computer Science I Instructor: Allyson Anderson.
Introduction to Object-Oriented Programming
Object Oriented Design: Identifying Objects
CSC 212 Object-Oriented Programming and Java Part 1.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
1 COS 260 DAY 2 Tony Gauvin. 2 Agenda Questions? Class roll call Blackboard Web Resources Objects and classes 1 st Mini quiz on chap1 terms and concepts.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Chapter 1 Object Orientation: Objects and Classes.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Welcome to AP Computer Science JAVA Programming in.
CS100A, Fall 1997, Lecture 91 CS100A, Fall 1997 Lecture 9, Tuesday, 30 September Input/Output & Program Schema System.in, class Text, Some basic data processing,
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Understanding class definitions
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CS 106 Introduction to Computer Science I 10 / 29 / 2007 Instructor: Michael Eckmann.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
OOP with Java, David J. Barnes/Eric Jul Defining Classes1 Object State and Complexity Objects maintain a state. State is represented by a set of attributes.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Chapter 1 Object Orientation: Objects and Classes.
Objects as a programming concept
3 Introduction to Classes and Objects.
Trainings 11/4 Intro to Java.
An Introduction to Java – Part II
Object-Oriented Programming
Understanding class definitions
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming Language
Introduction to Object-Oriented Programming
COS 260 DAY 6 Tony Gauvin.
Presentation transcript:

Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle

2 Data Types in Java Recall from week 2’s lecture notes that there are two main kinds of types in Java: Recall from week 2’s lecture notes that there are two main kinds of types in Java: Primitive type Primitive type Stores a single piece of data of a specific type Stores a single piece of data of a specific type Some primitive types and the kinds of data they store are: Some primitive types and the kinds of data they store are: int – a 32-bit integer (A bit is a 0 or 1.) int – a 32-bit integer (A bit is a 0 or 1.) float – a 32-bit floating point (decimal) number float – a 32-bit floating point (decimal) number double – a 64-bit floating point (decimal) number double – a 64-bit floating point (decimal) number char – a single character (e.g., ‘Y’) char – a single character (e.g., ‘Y’)

3 Data Types in Java Class type Class type Stores multiple pieces of data and provides methods for manipulating that data Stores multiple pieces of data and provides methods for manipulating that data Some class types are: Some class types are: String – stores and manipulates a string of characters contained between double quotation marks, e.g., “Susan” String – stores and manipulates a string of characters contained between double quotation marks, e.g., “Susan” input – stores and manipulates text entered at the keyboard input – stores and manipulates text entered at the keyboard Student – a programmer-defined class that allows us to store and manipulate basic information about a student (Source: Objects First with Java: A Practical Introduction using BlueJ, by David J. Barnes & Michael Kölling) Student – a programmer-defined class that allows us to store and manipulate basic information about a student (Source: Objects First with Java: A Practical Introduction using BlueJ, by David J. Barnes & Michael Kölling) Variables that are class types are called objects. Variables that are class types are called objects.

4 Object-Oriented Programming (OOP) Java is an object-oriented programming (OOP) language. Java is an object-oriented programming (OOP) language. OOP is an approach to programming that views a computer program as consisting of abstract objects that interact with one another in much the same way that real-world objects (people, automobiles, etc.) interact with one another. OOP is an approach to programming that views a computer program as consisting of abstract objects that interact with one another in much the same way that real-world objects (people, automobiles, etc.) interact with one another. OOP has its own terminology. OOP has its own terminology.

5 Object-Oriented Programming (OOP) In OOP, objects interact with one another via methods, a programming construct with which you are already familiar. In OOP, objects interact with one another via methods, a programming construct with which you are already familiar. Objects can be of the same or different types. Objects can be of the same or different types. Objects of the same type are said to be in the same class. Objects of the same type are said to be in the same class.

6 Object-Oriented Programming (OOP) Objects in the same class have the same methods, but they generally have different characteristics, or attributes. Objects in the same class have the same methods, but they generally have different characteristics, or attributes. An object’s attributes are stored in fields, or instance variables. (The latter term derives from the fact that creating an object from a specific class type is often referred to as instantiating the object.) An object’s attributes are stored in fields, or instance variables. (The latter term derives from the fact that creating an object from a specific class type is often referred to as instantiating the object.) A special kind of method called a constructor is required to create an object. A special kind of method called a constructor is required to create an object.

7 Object-Oriented Programming (OOP) Constructor Constructor Called to construct, or initialize, an object Called to construct, or initialize, an object Has the same name as the class Has the same name as the class Provides the object’s instance variables (fields) with initial values Provides the object’s instance variables (fields) with initial values May be more than one in a class May be more than one in a class

8 Basic Structure of a Class (from which objects can be created) Contents of the class: public class ClassName { Fields Constructors Methods [Optional “main” method] } Contents of the class: public class ClassName { Fields Constructors Methods [Optional “main” method] }

9 Example Class: Student Fields: public class Student { // Student's full name private String name; // Student’s ID private String id; // Number of credits taken so far private int credits; // Remaining class code omitted } Fields: public class Student { // Student's full name private String name; // Student’s ID private String id; // Number of credits taken so far private int credits; // Remaining class code omitted } The “private” specifier limits the visibility of the variable.

10 Example Class: Student Constructor: public class Student { // Fields omitted /** * Create a new student with a given name and ID number. */ public Student(String fullName, String studentID) { name = fullName; id = studentID; credits = 0; } // Remaining class code omitted } Constructor: public class Student { // Fields omitted /** * Create a new student with a given name and ID number. */ public Student(String fullName, String studentID) { name = fullName; id = studentID; credits = 0; } // Remaining class code omitted } The constructor provides the fields with initial values.

11 Example Class: Student Sample method: public class Student { // Fields and constructor omitted /** * Return the full name of this student. */ public String getName() { return name; } // Remaining class code omitted } Sample method: public class Student { // Fields and constructor omitted /** * Return the full name of this student. */ public String getName() { return name; } // Remaining class code omitted } Method to retrieve the value of the “name” field

12 Example Class: LabClass Sample “main” method: public class LabClass { // Fields, constructor, and non-main methods // omitted public static void main (String[] args) { // Create a new lab class LabClass myClass = new LabClass(25); // Remaining code omitted } } Sample “main” method: public class LabClass { // Fields, constructor, and non-main methods // omitted public static void main (String[] args) { // Create a new lab class LabClass myClass = new LabClass(25); // Remaining code omitted } } Creates a new LabClass object called myClass

13 Sample Classes Now let’s take a look at the Student and LabClass classes: Now let’s take a look at the Student and LabClass classes: Student.java Student.java Student.java LabClass.java LabClass.java LabClass.java

14 Topics for Friday’s Lecture Last lab Last lab Review for final exam Review for final exam