CS 106 Introduction to Computer Science I 03 / 19 / 2008 Instructor: Michael Eckmann.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Advertisements

1 CSCE 1030 Computer Science 1 Introduction to Object Oriented Programming.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 04 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 29 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 26 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 24 / 2010 Instructor: Michael Eckmann.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
CS 106 Introduction to Computer Science I 04 / 21 / 2010 Instructor: Michael Eckmann.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 18 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Chapter 6: A First Look at Classes
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 25 / 2010 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
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.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
CS 106 Introduction to Computer Science I 10 / 29 / 2007 Instructor: Michael Eckmann.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 01 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 22 / 2007 Instructor: Michael Eckmann.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
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.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 10 / 2007 Instructor: Michael Eckmann.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Today’s topics UML Diagramming review of terms
Presentation transcript:

CS 106 Introduction to Computer Science I 03 / 19 / 2008 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS Spring 2008 Today’s Topics Comments and/or Questions? Object oriented programming

Michael Eckmann - Skidmore College - CS Spring 2008 Program 1 Comments about the extra credit method. This method is supposed to ignore case and ignore some punctuation and whitespace to determine if a string is a palindrome. A man, a plan, a canal, Panama. –This is a palindrome if we ignore case of letters, spaces, commas and periods. The only additional method needed, besides what we learned, may be to call the toUpperCase() method in the Character class. No other methods in the String class besides.charAt are needed.

Michael Eckmann - Skidmore College - CS Spring 2008 Program 1 toUpperCase() method of the Character class takes in a character as an argument and returns a characer. The character returned is: –the uppercase version of that character if the argument was a lowercase letter –the original character if the argument was not a lowercase letter Examples: –Character.toUpperCase('c') returns 'C' –Character.toUpperCase('C') returns 'C' –Character.toUpperCase('#') returns '#' –Character.toUpperCase('\n') returns '\n' –Character.toUpperCase('z') returns 'Z'

Object orientation Object orientation is a programming paradigm that views the pieces of large programs as objects with “attributes” and “behaviors”. Java is an object-oriented language. C++ is another widely used object-oriented language. Other languages with which you may be familiar, like C, Pascal, and Fortran, are not object-oriented. These are what are called procedural languages. So far in this course, we have used Java in a very procedural way and did not use it to its potential as an object-oriented language.

Object orientation An example of an object. If we wanted to write a program that worked with rectangles: – The attributes of a rectangle include its length and width. – Other attributes might be its color,... – Behaviors of the rectangle could include: changing its size, computing its area, etc...

Object orientation So, in our example, a rectangle would have as its data: – length – width – color As its methods we would have things like: – set_length – set_width – set_color – calculate_area

Object orientation This translates to creating a class called Rectangle which has length, width and color as instance variables. This class will represent a generic rectangle. The methods in the class Rectangle would be: – set_length – set_width – set_color – calculate_area – among other possible methods

Class Rectangle The Rectangle class would be defined in its own file called Rectangle.java The Rectangle class represents what it means to be a rectangle -- it means having a length width and color and it means that we can change it's length width and color. We can also calculate the area of a rectangle. If we wanted to be able to do more things to a Rectangle then we could add more methods such as calculating the perimeter of the rectangle.

class definition of Rectangle Let's partially create the class Rectangle. Essentially we are creating a new data type named Rectangle to represent rectangles.

Object of type Rectangle An object of type Rectangle is ONE rectangle with a specific length, width, and color. An object of type Rectangle must be created to use the data and methods in the Rectangle class. To do this, in some other class, we could instantiate an object of type Rectangle, by declaring a variable of type Rectangle (and calling it's constructor – we'll get to that shortly). Then, through this object reference, we would have access to the methods within the Rectangle class to set its length, set its width, compute its area, etc... for that particular instance of the class. We might want multiple variables of type rectangle, each with their own width, length and color. These would each be objects of type Rectangle.

Constructor methods A constructor method is a special method of a class that is called when an object of the class is instantiated (created.)‏ Constructor methods usually initialize the instance variables. Constructor methods have the same name as the class and are defined WITHOUT a return type. A constructor for our class Rectangle should contain code that sets the values of length, width and color.

Constructor methods for example, we might have a constructor that looks like the following: public Rectangle ( double len, double wid, String col )‏ { length = len; width = wid; color = col; } Recall that the instance variables were named length, width and color that is why they are on the left side of the assignment statements --- they're the variables being set.

Constructor methods We might also want a default constructor (one that takes no arguments) so that we can create an object of type Rectangle without specifying its length, width and color. public Rectangle ( )‏ { length = 1.0; width = 1.0; color = “Green”; } This says the default rectangle is 1 by 1 and Green.

Constructor methods Assuming that the two constructors just described are defined within the Rectangle class, to instantiate an object of type Rectangle we would do one of the following: // this would create a rectangle with the specified values. Rectangle some_rect = new Rectangle ( 3.2, 5.1, “Red”); // this would create a “Green” rectangle with length = 1 // and width = 1 Rectangle another_rect = new Rectangle ( );

example Let’s create a program containing two classes –Rectangle let's add the code from the previous slides into Rectangle and also provide a method that prints the length, width and color of the rectangle. –RectangleTester will contain the main method and instantiate a couple of objects of type Rectangle with which we'll call the methods of Rectangle.

class / object / constructor terminology Programmer defined classes can be used as types, just like classes in the Java API can be used as types (e.g. String.)‏ Objects have attributes (data) and behaviors (methods.)‏ The data are the instance variables in a class. An object of a class is instantiated by declaring a variable of that type and initializing the object by calling a constructor of that class with the new operator. – e.g. Time time_object = new Time( ); here, we are creating an object (time_object), of a programmer defined class (Time).

Let's create a class Time What instance variables might we have to represent a time? What types will they be? What are the valid values of each of these variables? We can make sure that these variables ALWAYS have valid data in them –make the instance variables private –and in any method that can change the value of an instance variable make sure to not allow it to have an invalid value