Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 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 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 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 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 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 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 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 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 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 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 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 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 14 Topics for Friday’s Lecture Last lab Last lab Review for final exam Review for final exam


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

Similar presentations


Ads by Google