CSC 212 Object-Oriented Programming and Java Part 1.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Lecture 2: Object Oriented Programming I
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Road Map Introduction to object oriented programming. Classes
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
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 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CSC 212 Object-Oriented Programming and Java Part 2.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
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.
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Topics Instance variables, set and get methods Encapsulation
OOP Basics Classes & Methods (c) IDMS/SQL News
Unified Modeling Language (UML)
Comp1004: Building Better Objects II Encapsulation and Constructors.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Objects as a programming concept
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3: Using Methods, Classes, and Objects
Chapter 4: Writing Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
IFS410: Advanced Analysis and Design
Classes & Objects: Examples
Defining Classes and Methods
Object-Oriented Programming
Object Oriented Programming in java
Defining Classes and Methods
Defining Classes and Methods
CSG2H3 Object Oriented Programming
Presentation transcript:

CSC 212 Object-Oriented Programming and Java Part 1

Announcements Watch the CSC212 webpage for announcements No lab section today --- go to Mass If you need additional Java help, speak up and get extra help now. We’re going fast.  The “Getting help” page contains links to several on-line Java tutorials.

A Quick Overview of Java Java is modern object-oriented language  Programs are written via Java classes  Classes define the types for instance variables (“objects”) Classes contain 2 groups of members:  Descriptions of data (fields)  Algorithms for manipulating data (methods)

Classes and Objects: First Look Class : a blueprint for an object Objects : defined by and created from classes (blueprints) House Floor Plan Houses w/same Floor Plan Figure from Lewis and Loftus: Java Software Solutions, p.123 house158 new house158(asphalt, clapboards) new house158(asphalt, stucco) new house158(tile, stucco)

Object Terminology What Are Classes? A class defines a "type" for objects. A class definition includes all of the fields and methods common to all objects of that "type". All Java code is contained within a class. Software classes usually model classes of real- world objects you find in everyday life. What Is an Object? An object is an instance of a class, like a variable is an instance of a primitive type. What Are Fields? Fields define what data can be stored within each object of a class. What Are Methods? Methods define the operations on objects of a class. Typically, methods modify the data stored in an objects’ fields.

Object-Oriented Programming Procedural programming: procedures and functions are active; data are passive. Object-orientation: data - objects - are active; methods serve the data.  Java supports primitive types: ints, floats, Strings, arrays…  BUT can also have Cars, Motorcycles, People and any other type that's important to your problem.

Classes and Objects Classes specify data and behavior of objects. Fields describe what the class is. Methods describe what the class does.

Classes and Objects Objects of the same class must have the same fields…  …but field values may differ. Objects of the same class must have the same methods…  …but may behave differently because of different values in the fields and arguments.

Structure of a Class Definition Class name with class modifiers Fields – typed with field modifiers Constructors – build class instances  Typically how to initialize the instance's data fields Methods – manipulate data to get results  Also have visibility attributes

Student Class public class Student { // declare the fields // define the constructors // define the methods }

Student Class What data defines a student?

The Student Variables public class Student { public String name, studentID; protected int years_attended; private float gpa, credits; public static int total_enrollment; // define the constructors // define the methods } // end of class definition

Constructors for Student Constructors are special methods which create instances. Typically initialize fields values. (They can do more – later) public Student (String sname, long ssn) { name = sname; studentID = Long.toString(ssn); years_attended = 0; gpa = credits = 0; total_enrollment = total_enrollment++; }

Additional Constructors Classes can have several constructors  Must differ in parameter lists (signatures). public Student (String sname, String id) { name = sname; studentID = id; years_attended = 0; gpa = credits = 0; total_enrollment++; } public Student () { name = “J Doe”; studentID = “none”; years_attended = 0; gpa = credits = 0; total_enrollment++; }

The Student Class public class Student { protected String name, studentID; protected int years_attended; private float gpa; public static int total_enrollment; public Student(String sname, long ssn) { … } public Student(String sname, String id) { … } public Student() { … } // define the methods } // end of class definition

Java classes can have many methods. Methods describe what a class does or what can be done to the class  Should be the starting point of defining a class  E.g. Should start by asking: What does this data do? What is the effect of this data? Methods

Methods for Student Class What methods would this class define?

Update a Student's GPA /** Add grades and update GPA*/ void addClass(float creditHrs, float grade) { gpa = (gpa*credits+grade*creditHrs) / (credits+creditHrs); credits += creditHrs; // shorthand: credits = credits +creditHrs }

void setId(String newId) { studentID = newId; } Set and Get Methods Provide set and get methods for each field  Controlling access to fields  Limits errors and problems (or amount of searching when debugging)  Common design pattern public String getId() { return(studentID); }

Why Use Set Methods? public void setId(long new_id) { if (new_id < 0) { studentID=Long.toString(0); System.out.println("Bad value, ID set to 0"); } else studentID = Long.toString(new_id); } Public String name, studentID Two ways to set studentID: s.set_id(150); s.studentID = "150"; Which is better programming practice? (and why)?

Why use get & set methods? What are the advantages of using get & set methods?

Access Protection All classes, methods, and fields can be:  public - Accessible by anyone from anywhere  package [default] - Accessed only by members in the same package Methods and fields can also be:  private - Only class instances can access it  protected - Accessible by instances of the same class, subclasses & package members

Access Protection Public elements enable access to the classes functionality. Private (or protected) elements hide inner workings of the class. Access protection is an important part of proper object-oriented progamming

Access Protection Benefits Enforce constraints on an object's state Provide simpler client interface  Abstraction: Make available only what people must know  Encapsulation: Separate interface from implementation

Rules of Thumb Classes are public Fields are private  Outside access only using “get” and “set” methods Constructors are public Get and set methods (if any) are public Other methods on a case-by-case basis

Instantiation New instances are created (“instantiated”) using new: Student r1 = new Student("Bob", 1050); The variable storing the new object instance. The types must match. Create a new instance of the Student class A call to one of the constructors for the Student class. This instance will have the name "Bob" and a studentID of "1050".

Java Variables and Values Every variable has a type  Primitive boolean char byte, short, int, long float, double  Class Defined by the programmer

Java Variables and Values Variables contain:  Values of primitive type int x = 20; int y = x; y = y + 3; // No change to value in x  References to instances Student s1 = new Student("Bob", 150); Student s2 = s1; //s1 and s2 refer to SAME object s2.add_class(4.0,2.5); // GPA of s1 also updated String str = s1.getId(); // str == "150"!

Formal and Actual Parameters Student s1 = new Student("Bob", 150); Public Student(String sname, long ssn) { name = sname; studentID = Long.toString(ssn); years_attended = 0; gpa = credits = 0; total_enrollment; = total_enrollment++; } In a constructor (or any method) call: In class Student - the constructor for Student Actual Values (could be variables, too) Actual values transmitted to corresponding formal parameters. Alignment is done left to right, one to one. Actual and formal parameters must have matching types. Transmission is done by copying. Formal parameters - values obtained from corresponding actual parameters

Method main() public class Student { … public static void main(String[] args) { Student s1 = new Student("Bob", 1050); s1.add_class(4.0, 3.5); System.out.print(s1.get_id + "\t"); System.out.println(Float.toString(s1.gpa)); } Each program starts at special method called “main” Any class can have a main method Adding main to class can serve as debugging tool

Daily Quiz Do problem R-1.9 from the book (p. 52)  For this quiz, no documentation is required