CS1101: Programming Methodology Aaron Tan.

Slides:



Advertisements
Similar presentations
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
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.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown,
Chapter 41 Defining Classes and Methods Chapter 4.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Software Development Software Life Cycle UML Diagrams.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
29-Jun-15 Using Objects. 2 Classes and objects The type of an object is the class that describes that object If we say int count, the type of count is.
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology Aaron Tan.
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
CS1101: Programming Methodology Aaron Tan.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology
Recitation 2 Main Method, API & Packages, Java Basics.
Introduction to Object-Oriented Programming
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Unit 3: Java Data Types Math class and String class.
Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
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.
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!
CS1101: Programming Methodology Aaron Tan.
Classes CS 21a: Introduction to Computing I First Semester,
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology Aaron Tan.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
10-Nov-15 Java Object Oriented Programming What is it?
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
CS1101: Programming Methodology
CS1101: Programming Methodology Aaron Tan.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
2.3 The Java API Documentation. What follows are the contents of the Java Application Programming Interface (API) Documentation for the system supplied.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
CS1101X: Programming Methodology Recitation 4 Design Issues and Problem Solving.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CS1101: Programming Methodology
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS1101: Programming Methodology
CS1101: Programming Methodology
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Building Java Programs
Implementing Non-Static Features
More on Classes and Objects
Building Java Programs
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Lecture 8-2: Object Behavior (Methods) and Constructors
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

CS1101: Programming Methodology Aaron Tan

2 How was your mid-term test?  Which of the following best describes your feeling about the test?

3 This is Week 8  Week 7:  Revision on Modular programming  Chapter 6: Object-Oriented Programming  Chapter 7: OOP Additional Details  Survey  This week:  Chapter 7: OOP Additional Details (cont.)  About String  Exercises  Chapter 8: Software Engineering  Chapter 9: Classes with Class Members  Exercises

4 Chapter 7 OOP Additional Details  Let’s continue chapter 7 from where we left off last week…

5 Writing good programs  Now that we know basic programming, we should not be contended with merely writing correct programs.  We have to write good programs  By following good programing practice and methodology  By writing elegant and efficient programs (this requires more practice and is the subject for follow-up modules, but no harm starting early!)

6 Overloaded Constructors  Refer to Fraction.java in the textbook.  We can write more constructors. Examples: public Fraction( ) { this(0, 1); // creates a fraction 0/1 } public Fraction(Fraction frac) { this(frac.numerator, frac.denominator); // copies frac } public Fraction(int n) { this(n, 1); // creates a fraction n/1 } public Fraction(int n, int d) { this.numerator = n; this.denominator = d; this.quotient = (double) this.numerator / this.denominator; } Given New

7 String class  After the tracing exercises last week, some students tried question 4 on strings, and found that the result doesn’t match my explanation.  Why? The reason is that String is a very special class.  Example: We do not need the “new” statement to create a String object!

8 String is immutable In Java, a String object is immutable  This means once a String object is created, it cannot be changed, such as replacing a character with another character or removing a character  The String methods we have used so far do not change the original string. They create a new string from the original. For example, substring creates a new string from a given string. The String class is defined in this manner for efficiency reason.

9 Effect of Immutability We can do this because String objects are immutable.

10 String: Examples Example 1: String s = "abc"; s = s + "def";  A new string "abcdef" is created for s in the second statement. Example 2: String s1 = "abc"; String s2 = "abc"; s1 = "xyz";  s2 still refers to "abc". (Imagine the horror if this is not so!) Hence, strings behave differently from what I explained last week, about reference variable of an object or an array.

11 Class Activity: Ball class  Last week, we created a Ball class with these members:  Instance variables (data members)  colour (String)  radius (double)  Methods: constructors, accessors and mutators  Overloaded constructors: Ball() and Ball(String, double)  Accessors: getColour() and getRadius()  Mutators: setColour(String) and setRadius(double)  Let’s download the files Ball.java and BallDriver.java and study them

12 Ball: Writing equals() method  Run BallDriver and enter the same data for both objects myBall1 and myBall2  What is the truth value of (myBall1 == myBall2) ?  Why is it so?  The equals() method isn’t working because we haven’t written it! Write the equals() method.  Where should it be? In Ball.java or BallDriver.java?  Write the equals() method now.

13 Ball: Writing toString() method (1/2)  The following is a possible output of the program: 1st ball: [blue, 2.5] 2nd ball: [orange, 3.1]  Can you replace the output statements in BallDriver.java with the following? System.out.print("1st ball: "); System.out.println(myBall1); System.out.print("2nd ball: "); System.out.println(myBall2);  What’s the output? (Actual output may differ from below.) 1st ball: 2nd ball: Hashcodes (page 514)

14 Ball: Writing toString() method (2/2)  To make it work, you need to write the toString() method.  The toString() method returns a string, which is the string representation of the data in an object.  Note that after the toString() method is written, you may print myBall1 object in any of these two ways: System.out.println( myBall1 ); or System.out.println( myBall1.toString() );  Write the toString() method now.

15 Modularisation (1/2)  Observe that there are duplicate codes in the input section of BallDriver.java. The blue statements and red statements are almost the same. If we need to create more Ball objects, the code gets even longer. System.out.print("Enter 1st ball's colour: "); colour = stdIn.next(); System.out.print("Enter 1st ball's radius: "); radius = stdIn.nextDouble(); Ball myBall1 = new Ball(colour, radius); System.out.print("Enter 2nd ball's colour: "); colour = stdIn.next(); System.out.print("Enter 2nd ball's radius: "); radius = stdIn.nextDouble(); Ball myBall2 = new Ball(colour, radius);

16 Modularisation (2/2)  Can you ‘modularise’ your BallDriver.java program so that you call a method each time you want to create a Ball object?  You can then replace the code in the previous slide with this: Ball myBall1 = readBall(stdIn, "1st"); Ball myBall2 = readBall(stdIn, "2nd");  You need to write the readBall() method.

17 Chapters 8 and 9  We’ll take a break from the Ball class and return to it later.  Let’s go to Chapters 8 and 9 now.

18 Point class (1/3)  I want to introduce the Point class to you because…  it appears quite often in past year’s papers  I want to add an instance variable for a Point object inside the Ball class (so that we have an object within an object)  Refer to API: Field Summary int x x The x coordinate. int y y The y coordinate. Constructor Summary PointPoint() Constructs and initializes a point at the origin (0, 0) of the coordinate space. PointPoint(int x, int y) Constructs and initializes a point at the specified ( x, y) location in the coordinate space. PointPoint(Point p) Constructs and initializes a point with the same location as the specified Point object.Point

19 Point class (2/3) Method Summary boolean equalsequals(Object obj) Determines whether or not two points are equal.Object Point getLocationgetLocation() Returns the location of this point. double getXgetX() Returns the X coordinate of the point in double precision. double getYgetY() Returns the Y coordinate of the point in double precision. void movemove(int x, int y) Moves this point to the specified location in the ( x, y) coordinate plane. void setLocationsetLocation(double x, double y) Sets the location of this point to the specified double coordinates. void setLocationsetLocation(int x, int y) Changes the point to have the specified location. void setLocationsetLocation(Point p) Sets the location of the point to the specified location.Point String toStringtoString() Returns a string representation of this point and its location in the ( x, y) coordinate space. This is not a complete list. Refer to the API page.

20 Point class (3/3)  Let’s do some programs on Point class so that you are familiar with it.  Download PointDriver.java and study it. Note how a Point object is displayed.  Now, let’s do something more complex. Download MyRectangle.java, MyRectangleDriver.java and MyRectangleDriverV2.java and study them.  The computeArea() method in MyRectangle.java is a stub. Complete it.  Should we make area another data member of the MyRectangle class? Think about it. If we do make it a data member, what changes do we need to make in MyRectangle.java? We’ll discuss this next week.

21 Other Point related classes  Check out other related classes on the API  Point2D.Double  Point2D.Float

22 Ball class: centre (1/2)  Now that we understand the Point class, let’s include a new data member for the Ball class  centre: of type Point, to represent the centre of a Ball object  Centre is supposed to be a 3D point, since balls are 3D objects, but some students are frightened of 3D, so we shall just make centre a 2D point  You need to update/add constructors,add appropriate accessor and mutator methods, and update the toString() method in Ball.java.

23 Ball class: centre (2/2)  In your BallDriver.java program, create two Ball objects, and check whether they overlap each other. (Two balls overlap when their centres are too close to each other.)  You need to add an appropriate method to do the above.  What should be the name of the method?  Where should that method be, in Ball.java or BallDriver.java?  What is its return type?  What is/are its parameter(s)?  This is your take-home exercise

24 Next week  Next week, I will present BallV2.java and BallV2Driver.java that include all the things we’ve discussed today

25 Announcement/Reminder  Lab #3  Deadline: 8 October (Wednesday), 2359hr.  Identical codes  Please do not share codes for your lab assignments!  Mid-term test  Results will be released on the IVLE gradebook. Watch out for the announcement.  Your answer sheets will be returned to you next week in your discussion session.

26 This is Week 8  Next week?  Chapter 10 Arrays and ArrayList(sections 10.7 – 10.14)

27 End of file