Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.

Slides:



Advertisements
Similar presentations
Programming for Beginners
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Modules and Objects Introduction to Computing Science and Programming I.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Primitive Data Types byte, short, int, long float, double char boolean Are all primitive data types. Primitive data types always start with a small letter.
CS 201 Functions Debzani Deb.
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Classes, Objects, and Methods
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Java Classes Using Java Classes Introduction to UML.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
The Java Programming Language
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Lecture 2: Classes and Objects, using Scanner and String.
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
CIT 590 Intro to Programming First lecture on Java.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 11: Handling Errors; File Input/Output.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
Object Orientation “Thinking” Object Oriented, Further Constraints, and Documentation.
Getting Started with C# August 29, NET Concepts Language Independence Language Integration  your C# program can use a class written in VB Program.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Lecture 9 Using Objects. Remember: 3 Different Kinds of Classes 1.Application Class – what we've been doing – Has public static void main ( String []
CSE 1301 Lecture 5 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Introduction to Computer Programming CS 126 Lecture 2 Zeke Maier.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 7: Methods & User Input.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
ECE 122 Feb. 1, Introduction to Eclipse Java Statements Declaration Assignment Method calls.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
School of Computer Science & Information Technology G6DICP - Lecture 11 Classes & Objects.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Working With Objects Tonga Institute of Higher Education.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
Chapter 1 Object Orientation: Objects and Classes.
The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Introduction to Computing Science and Programming I
Andrew(amwallis) Classes!
Concepts of Object Oriented Programming
Something about Java Introduction to Problem Solving and Programming 1.
Documentation and Style
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Java for Beginners University Greenwich Computing At School DASCO
Week 7 - Monday CS 121.
Presentation transcript:

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming

Revision of Sessions 1-5 So far, we’ve covered: Data types Flow control (if, for, while, do-while) Software Design Software Testing Common syntax (programming grammar) Good coding style (comments, conventions, indents) These ideas apply to nearly all programming languages.

Session 6 - aims & objectives Feedback from evaluation forms Introduction to Object Oriented Programming Find out what Classes and Objects are Learn the difference between an Object and a Class Understand why Classes and Objects are important in object-oriented programming Learn how to create your own Classes in Java

Object-Oriented Programming Object-orientation is an important programming paradigm Programs are assembled into smaller elements called Objects, which represent something in the real world. Objects can contain both data and functionality. The source code responsible for defining an object as called a Class. An object is a specific instance of a class. Object-oriented code should be highly re-usable. Provides a “construction kit” approach to programming. Most OO programs will contain at least 2 classes.

Classes A class could be thought of as a user-defined data type, perhaps holding many pieces of information. For example, we could create a Date class which contains: day (String) date (int) month (String) year (int) An object is an instance of a class: ‘Today’ is an object of class Date. ‘Last Saturday’ is an object of class Date.

Classes We might want to create a class to represent a Person: The class might include... forename (String) surname (String) age, in years (int) birthday (class Date) weekday (String) day (int) month (String) year (int) One class can re-use another! “Construction kit” approach to programming!

Naming Convention Classes are usually named with a capital letter. Primitive variables are named with lower case letters. This makes it really easy to keep track of what is a class/object and what isn’t. Ever wondered why String has a capital S, while int, double, boolean are in lower case? String is actually a class. It is quite unusual as it can be used like a primitive variable – no other class can do this. More about Strings next week...

Using Classes – 1 Define your class: Classes can be defined in the same file as your application, or in their own file. For now, we will put the class definition above the application class, in the same file. class Date { // Define class variables... String weekday; int day; String month; int year; } class myprog { public static void main(String[] args) { // My application code... }

Using Classes – 2 Now to create an object. This is called instantiating the class. Step 1: Declare an object called ‘today’. This tells the compiler that ‘today’ is an object of class Date. Date today; Step 2: Instantiate the class using the reserved word ‘new’. This creates the object in the computer’s memory. today = new Date(); Or, you can do both steps together if you want... Date today = new Date();

Using Classes – 3 You can access an object’s member variables using dot syntax. So, we could set up the Today object as follows: Date today = new Date(); today.weekday = “Tuesday”; today.day = 1; today.month = “March”; today.year = 2010; We could print today’s day variable: System.out.println(today.weekday);

Using Classes – 4 What if we have an object inside an object? Imagine we have a class called Person, and an object of class Person called einstein. Person einstein = new Person(); Let’s set Einstein’s birthday... einstein.birthday.weekday=“Friday”; einstein.birthday.day=14; einstein.birthday.month=“March”; einstein.birthday.year=1879;

The purpose of OO programming Classes should model the real world – thus leading to efficient coding Classes should be reusable ‘Construction kit’ approach to programming Many useful classes have already been created. Available in libraries ready for you to use. Complex code can be created easily. We’ll talk about how to use the libraries in Session 8...

Coming up in Session 7... Adding functionality to classes (methods) How to get input from the user.