1 CSCE 1030 Computer Science 1 Introduction to Object Oriented Programming.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Advertisements

Lecture 2: Object Oriented Programming I
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
CS 106 Introduction to Computer Science I 03 / 19 / 2008 Instructor: Michael Eckmann.
Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –
Fall 2005CSE 115/503 Introduction to Computer Science I1 Composition details Recall, composition involves 3 things: –Declaration of instance variable of.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Introduction to Object-Oriented Programming
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
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.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects.
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.
CS 106 Introduction to Computer Science I 10 / 29 / 2007 Instructor: Michael Eckmann.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
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.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Modern Programming Tools And Techniques-I
GC211 Data structure Lecture 3 Sara Alhajjam.
The need for Programming Languages
3 Introduction to Classes and Objects.
University of Central Florida COP 3330 Object Oriented Programming
Objects as a programming concept
Yanal Alahmad Java Workshop Yanal Alahmad
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3: Using Methods, Classes, and Objects
University of Central Florida COP 3330 Object Oriented Programming
Programming Language Concepts (CIS 635)
CS 302 Week 11 Jim Williams, PhD.
Dr Shahriar Bijani Winter 2017
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Lecture 22 Inheritance Richard Gesick.
Classes & Objects: Examples
Today’s topics UML Diagramming review of terms
Simple Classes in Java CSCI 392 Classes – Part 1.
Introduction to Object-Oriented Programming
Dr. R Z Khan Handout-3 Classes
TCSS 143, Autumn 2004 Lecture Notes
[Robert W. Sebesta, “Programming the World Wide Web
Presentation transcript:

1 CSCE 1030 Computer Science 1 Introduction to Object Oriented Programming

The Basic Idea 2 In the real world, objects have certain attributes and certain tasks you can perform with or on them. A car may have attributes that describe the car such as: Color Manufacturer Maximum Speed etc … or even attributes that describe the current state of the car such as: Current Speed Is Running Lights On etc You may be able to perform the following tasks with a car: Turn on headlights Start the car Accelerate etc Let us use a car as an example of an object:

A Car as an Object 3 Attributes: Color Manufacturer Maximum Speed Current Speed Is Running Lights On etc Tasks: Turn on headlights Start the car Accelerate etc In Object Oriented Programming, we can represent a car as an Object. Attributes such as Color, Current Speed, Manufacturer, etc. may be held by variables. Tasks such as Accelerate or Start the car may be implemented using methods.

Object as an Instance of a Class 4 An object is an instance of a class. The same attributes of a car listed on the previous slides can be used to describe any of a wide variety of cars. This is useful because it allows us to reuse much of the same code to create multiple car objects in our programs. To create an object that is a car in your program, you must first specify a class for Car. This class will specify which attributes and tasks should be included to describe or use a car. Once the Car class is created, you may create instances of this class. Likewise, the same tasks are also relevant to any of a wide variety of cars.

Class Car and Car Objects Pseudocode 5 Variables: color manufacturer maximumSpeed currentSpeed isRunning lightsOn Methods: startCar() turnOnHeadlights() accelerate() class Car Create new object of class Car called martysCar Create new object of class Car called ryansCar martysCar.startCar() ryansCar.turnOnHeadlights() Creating and using Car objects In this example, two Car objects are created: martysCar ryansCar Next, we call the method startCar() on martysCar. Finally, we call the method turnOnHeadlights() on ryansCar. After the code to the right is executed, martysCar is running and the headlights on ryansCar are turned on.

Class Car and Car Objects In Java 6 public class Car { private String color; private String manufacturer; private double currentSpeed; private boolean isRunning; private boolean lightsOn; public Car(String setColor, String setManufacturer) { color = setColor; manufacturer = setManufacturer; isRunning = false; lightsOn = false; currentSpeed = 0; } public void startCar() { isRunning = true; } public void turnOnHeadlights() { lightsOn = true; } public void accelerate(double newSpeed) { currentSpeed = newSpeed; } Car.java

Class Car and Car Objects In Java public class Car { private String color; private String manufacturer; private double currentSpeed; private boolean isRunning; private boolean lightsOn; public Car(String setColor, String setManufacturer) { color = setColor; manufacturer = setManufacturer; isRunning = false; lightsOn = false; currentSpeed = 0; } public void startCar() { isRunning = true; } public void turnOnHeadlights() { lightsOn = true; } public void accelerate(double newSpeed) { currentSpeed = newSpeed; } 7 Access Modifiers public – available everywhere private – available only within the class Access Modifiers public – available everywhere private – available only within the class Note: If the class itself is declared as private, then this overrides the public declarations of any variables or methods within that class! Car.java

Class Car and Car Objects In Java public class Car { private String color; private String manufacturer; private double currentSpeed; private boolean isRunning; private boolean lightsOn; public Car(String setColor, String setManufacturer) { color = setColor; manufacturer = setManufacturer; isRunning = false; lightsOn = false; currentSpeed = 0; } public void startCar() { isRunning = true; } public void turnOnHeadlights() { lightsOn = true; } public void accelerate(double newSpeed) { currentSpeed = newSpeed; } 8 Constructor A classs constructor is called when creating a new object of that class. Note: In this example, the constructor accepts two variables as arguments ( setColor and setManufacturer ). When this constructor is called with these two arguments, it uses them to set the values of color and manufacturer. It also then initializes the values of isRunning, lightsOn, and currentSpeed. Also: If you do not provide a constructor in your class, the Java compiler will provide a default constructor at compile time that includes no arguments. Car.java

Class Car and Car Objects In Java public class TestingCar { public static void main(String args[]) { Car martysCar = new Car(blue, Hyundai); Car ryansCar = new Car(red, Cadillac); martysCar.startCar(); ryansCar.turnOnHeadlights(); } 9 TestingCar.java Create new object of class Car called martysCar Create new object of class Car called ryansCar martysCar.startCar() ryansCar.turnOnHeadlights() Recall from an earlier slide: Note: We also used class Cars constructor to initialize the values of color and manufacturer on both Car objects.