CompSci 230 S Programming Techniques

Slides:



Advertisements
Similar presentations
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look 1 Xiang Lian The University of Texas – Pan American Edinburg,
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Object-Based Programming Outline Introduction Implementing a Time Abstract Data Type with a Class Class Scope Controlling Access to Members Creating Packages.
More on Objects CS 102 More, more, more on Objects.
 2002 Prentice Hall. All rights reserved. 1 Object Oriented Programming Revisited Object Orientation –Classes Encapsulate data –Attributes Encapsulate.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look - modified by Eileen Kraemer.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
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.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
 2002 Prentice Hall. All rights reserved. 1 Chapter 8 – Object-Based Programming Outline 8.1 Introduction 8.2 Implementing a Time Abstract Data Type with.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
1 Chapter 3 – Object-Based Programming 2 Initializing Class Objects: Constructors Class constructor is a specical method to initialise instance variables.
Object Oriented Programming
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 7: Classes Part II Outline 7.1 Introduction 7.2 const (Constant) Objects and const Member Functions.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 12.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Jozef Goetz Credits: Copyright  Pearson Education, Inc. All rights reserved. expanded by J. Goetz, 2016.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. Këpuska Summer 2004 from Dr. S. Kozaitis Spring 2003 slides 1 Summary of Chapter 6: Classes.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 8 – Object-Based Programming Part II 8.9 Composition 8.10 Garbage Collection 8.11 Static Class.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Chapter 10 Thinking in Objects
Classes (Part 1) Lecture 3
Chapter 10 Thinking in Objects
CSC241: Object Oriented Programming
Chapter 10 Thinking in Objects
Chapter 7 – Object-Based Programming
Classes and Objects: A Deeper Look (Chapter 8)
Object Oriented Programming using Java - Class Instance Variables
The University of Texas Rio Grande Valley
Object Based Programming
Inheritance 2nd Lecture
Chapter 9 Thinking in Objects
Chapter 9 Object-Oriented Programming: Inheritance
Chapter 6 Methods: A Deeper Look
Phil Tayco Slide version 1.0 Created Oct 9, 2017
IFS410: Advanced Analysis and Design
Lecture 22 Inheritance Richard Gesick.
Classes and Objects: A Deeper Look
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Packages From Deitel & Deitel.
Object Oriented Programming in java
Week 4 Object-based Programming (2) Classes and Objects: A Deeper Look
Chapter 9 Thinking in Objects
Inheritance 2nd Lecture
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look UTPA – Fall 2012 This set of slides is revised from lecture.
CS360 Client/Server Programming Using Java
Classes and Objects: A Deeper Look
Chapter 7: Classes Part II
Classes & Objects A deeper Look Chapter 10 & 11
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 8 Classes and Objects: A Deeper Look
Classes and Objects Systems Programming.
CS 240 – Advanced Programming Concepts
Presentation transcript:

CompSci 230 S2 2017 Programming Techniques Classes and Objects: A Deeper Look

Agenda & Reading Topics: Reading Case study: Time class Composition: Employee & Date Reading Java how to program Late objects version (D & D) Chapter 7 & 8 The Java Tutorial Classes: http://docs.oracle.com/javase/tutorial/java/javaOO/classes.html Objects http://docs.oracle.com/javase/tutorial/java/javaOO/objects.html Lecture09

1.Case Study: Time1 class Class Time1 represents the time of day. Time1.java Class Time1 represents the time of day. private int instance variables hour, minute and second represent the time in universal-time format (24-hour clock format in which hours are in the range 0–23, and minutes and seconds are each in the range 0–59). public methods setTime, toUniversalString and toString. Class Time1 does not declare a constructor, so the compiler supplies a default constructor. Each instance variable implicitly receives the default int value. public class Time1 { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 – 59 public void setTime( int h, int m, int s ) { ... } public String toUniversalString() { ... } public String toString() { ... } } Lecture09

1.Time1 class Instance Variables & Methods Time1Test.java The instance variables hour, minute and second are each declared private private instance members are not accessible outside the class. Instance Methods: toUniversalString and toString Time1 time = new Time1(); System.out.println( time.toString() ); The initial standard time is: 12:00:00 AM System.out.println( time.hour ); %02d:%02d:%02d The initial universal time is: 00:00:00 The initial standard time is: 12:00:00 AM System.out.println( time.toUniversalString() ); System.out.println( time.toString() ); Complete the toUniversalString method public String toString() { return String.format( "%d:%02d:%02d %s", ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ), minute, second, ( hour < 12 ? "AM" : "PM" ) ); } Lecture09

1.Time1 class Instance Variables & Methods Method setTime declares three int parameters and uses them to set the time. test each argument to determine whether the value is outside the proper range. If it is out of range, set the value to zero time.setTime( 13, 27, 6 ); Universal time after setTime is: 13:27:06 time.setTime( 99, 99, 99 ); Universal time: 00:00:00 ! public void setTime( int h, int m, int s ) { hour = = ( ( h >= 0 && h < 24 ) ? ……………. } Complete the setTime method Lecture09

2.Case Study: Time2 Note: Case Study: Time2 class public class Time2 { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 – 59 ... Note: No constructor has been defined in Time1 class. We can only use the default one Case Study: Time2 class Add 5 overloaded constructors Overloaded constructors enable objects of a class to be initialized in different ways. To overload constructors, simply provide multiple constructor declarations with different signatures. Recall that the compiler differentiates signatures by the number of parameters, the types of the parameters and the order of the parameter types in each signature. Add getHour, getMinute, getSecond methods Add setHour, setMinute, setSecond methods Modify the toString() and toUniversalString() methods Time2.java Lecture09

2.Case Study: Time2 Overloaded Constructors Time2Test.java Five overloaded constructors that provide convenient ways to initialize objects. The compiler invokes the appropriate constructor by matching the number, types and order of the types of the arguments specified in the constructor call with the number, types and order of the types of the parameters specified in each constructor declaration. Time2 t1 = new Time2(); // 00:00:00 Time2 t2 = new Time2( 2 ); // 02:00:00 Time2 t3 = new Time2( 21, 34 ); // 21:34:00 Time2 t4 = new Time2( 12, 25, 42 ); // 12:25:42 Time2 t5 = new Time2( 27, 74, 99 ); // 00:00:00 Time2 t6 = new Time2( t4 ); // 12:25:42 Lecture09

2.Case Study: Time2 Overloaded Constructors Such a constructor simply initializes the object as specified in the constructor’s body Using this in method-call syntax as the first statement in a constructor’s body invokes another constructor of the same class. Popular way to reuse initialization code provided by another of the class’s constructors rather than defining similar code in the no-argument constructor’s body. Once you declare any constructors in a class, the compiler will not provide a default constructor. Standard constructor: Time2(int h, int m, int s) s: 0 Time2(int h) Time2() Time2(int h, int m) Time2(int h, int m, int s) public Time2(int h, int m, int s){ setTime( h, m, s ); } Time2(Time2 t) time.getHour(), … Lecture09

Exercise 1: Complete all constructors public class Time2 { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 – 59 public void setTime( int h, int m, int s ) { setHour( h ); // set the hour setMinute( m ); // set the minute setSecond( s ); // set the second } public void setHour( int h ){ hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); ... Lecture09

2.Case Study: Time2 Get & Set methods It would seem that providing set and get capabilities is essentially the same as making a class’s instance variables public. A public instance variable can be read or written by any method that has a reference to an object that contains that variable. If an instance variable is declared private, a public get method certainly allows other methods to access it, but the get method can control how the client can access it. A public set method can—and should—carefully scrutinize attempts to modify the variable’s value to ensure valid values. We can check and only modify if the parameter is a valid value Although set and get methods provide access to private data, it is restricted by the implementation of the methods time.hour Advantages public void setHour( int h ){ hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); } Validation Lecture09

3.Composition Date Class Date Instance variables: day, month and year to represent a date The constructor receives three int parameters. It also validate day if it’s out of range or invalid The toString method return the object’s string representation. public class Time2 { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 – 59 public void setTime( int h, int m, int s ) { setHour( h ); // set the hour setMinute( m ); // set the minute setSecond( s ); // set the second } public void setHour( int h ){ hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); ... Lecture09

3.Composition EmployeeTest.java A class can have references to objects of other classes as members. This is called composition and is sometimes referred to as a has-a relationship. Example: Employee and hire date (Date) Class Employee Instance variables: firstName, lastName, birthDate and hireDate Members firstName and lastName are references to String objects Members birthDate and hireDate are references to Date objects Blue, Bob; Hired: 3/12/1988 Birthday: 7/24/1949 Date birth = new Date(7, 24, 1949); Date hire = new Date(3,12,1988); Employee bob = new Employee("Bob", "Blue", birth, hire); System.out.println(bob); firstName lastName birthDate hireDate bob Bob Date year: 1949 month: 7 day: 24 Date year: 1988 month: 12 day: 3 Blue Lecture09

3.Composition Employee Employee & Date : has-a relationship Employee Class Date class public class Employee { private String firstName; private String lastName; private Date birthDate; private Date hireDate; ... class Date { private int month; // 1-12 private int day; // 1-31 based on month private int year; public Date( int theMonth, int theDay, int theYear ){ ... } public Employee(String first, String last, Date birth, Date hire ) { firstName = first; lastName = last; this.birthDate = birth; hireDate = hire; } Lecture09

3.Composition Java Source Files When you compile a .java file containing more than one class, the compiler produces a separate class file with the .class extension for every compiled class. When one source-code (.java) file contains multiple class declarations, the compiler places both class files for those classes in the same directory. A source-code file can contain only one public class—otherwise, a compilation error occurs. class Date { ... } class Employee { ... public class EmployeeTest { public static void main( String args[] ) { EmployeeTest.java Lecture09

Exercise 2 A 3x3 matrix is represented by the following array: { {29, 28, 27}, {16, 15, 14}, {3, 2, 1} }; Complete the following MyMatrix class class MyMatrix { private int[][] data; private final static int SIZE=3; public MyMatrix() { //complete this } public MyMatrix(int x11, int x12, int x13, int x21, int x22, ... public String toString() { StringBuffer sb = new StringBuffer(""); for (int[] row: data) sb.append(String.format("%s%n", _______________________)); return sb.toString(); } [1, 2, 3] [4, 5, 6] [7, 8, 9] MyMatrix myMatrix2 = new MyMatrix(1, 2, 3, 4, 5, 6, 7, 8, 9); System.out.println(myMatrix2 ); Lecture09

Exercise 3 Complete the add method. This method takes two MyMatrix as parameters, adds and returns a new MyMatrix object. public MyMatrix add(MyMatrix other) { MyMatrix result = new MyMatrix(); return result; } [30, 30, 30] [20, 20, 20] [10, 10, 10] MyMatrix myMatrix2 = new MyMatrix(1, 2, 3, 4, 5, 6, 7, 8, 9); MyMatrix myMatrix1 = new MyMatrix(29, 28, 27, 16, 15, 14, 3, 2, 1); MyMatrix result = myMatrix1.add(myMatrix2); System.out.println(result); Lecture09