Unformatted text preview:

small lecturenumber - hepage : Anatomy of an Objectsmall lecturenumber - hepage : Constructorssmall lecturenumber - hepage : Examplesmall lecturenumber - hepage : Multiple Constructorssmall lecturenumber - hepage : Multiple Constructorssmall lecturenumber - hepage : Exercisesmall lecturenumber - hepage : Designing a Programsmall lecturenumber - hepage : Requirementssmall lecturenumber - hepage : Designsmall lecturenumber - hepage : Structure Chartssmall lecturenumber - hepage : Example: student databasesmall lecturenumber - hepage : Top-down structure chartsmall lecturenumber - hepage : Top-down structure chartsmall lecturenumber - hepage : Choosing Objectssmall lecturenumber - hepage : Bottom-up-designsmall lecturenumber - hepage : Unit testingIntroduction to Programming IIConstructorsChris BrooksDepartment of Computer ScienceUniversity of San FranciscoDepartment of Computer Science — University of San Francisco – p.1/??4-2: Anatomy of an Object•Recall that an object consists of:◦Instance data◦Methods•Methods consist of:◦Parameters◦Local data◦Method body◦Return statementDepartment of Computer Science — University of San Francisco – p.2/??4-3: Constructors•A constructor is a method that is called when an object is firstcreated.•Its responsibility is to initialize an object’s instance variables.•It must have the same name as the class in constructs.•It has no return type.•It is called when ’new’ is invoked.Department of Computer Science — University of San Francisco – p.3/??4-4: Examplepublic class Point {public int xval;public int yval;Point(int x, int y) {xval = x;yval = y;}}Point p = new Point(3,4);Department of Computer Science — University of San Francisco – p.4/??4-5: Multiple Constructors•It’s often helpful to be able to specify some instance variables,but not all.•For example, let’s say our circle class has a default radius of 1.•If the radius is something else, users can specify it.•Point p1 = new Point(3,3); Circle c = newCircle(p); creates a circle with radius 1 at (3,3).•Point p1 = new Point(3,3); Circle c = newCircle(p,5); creates a circle with radius 5 at (3,3).Department of Computer Science — University of San Francisco – p.5/??4-6: Multiple Constructorspublic class Circle {private Point center;private int radius;public Circle(Point c) {center = c;radius = 1;}public Circle (Point c, int r) {center = cradius = r;}}Department of Computer Science — University of San Francisco – p.6/??4-7: Exercise•Modify the ’book’ class from last week’s lecture.•Add three constructors:◦Default: takes no arguments.◦1 argument: title◦2 arguments: author and title•author should be an instance of ’name’•Add a constructor to ’name’ that takes two arguments: firstname and last name.Department of Computer Science — University of San Francisco – p.7/??4-8: Designing a Program•Knowing the syntax of how to build a class is only thebeginning.•The bigger challenge is figuring out how to fit the piecestogether.•The software development process consists of the followingsteps:◦Establishing requirements◦Creating a design◦Implementing the design◦Testing•Usually, this is an iterative, repeated process.Department of Computer Science — University of San Francisco – p.8/??4-9: Requirements•Requirements indicate what a program is supposed to do.•Expressed as a functional specification.◦What is the input like?◦What must the output look like?◦Are there other programs it must interact with?◦How quickly must the program run?•Often, this comes from a client.•Usually not as precise as you’d like.Department of Computer Science — University of San Francisco – p.9/??4-10: Design•This is the ’how’ part of the program.•Specifies the classes that are needed and how they interact.•What methods are called, what data is returned.•Skipping this step can lead to serious, unpleasant bugs.•A good design should make implementation straightforward.Department of Computer Science — University of San Francisco – p.10/??4-11: Structure Charts•A structure chart is a helpful tool for doing design.•Helps to divide and conquer•Divide the problem into smaller pieces until you reach piecesthat can be tackled directly.•This is called top-down design.Department of Computer Science — University of San Francisco – p.11/??4-12: Example: student database•Let’s say we’ve been hired by USF to build an application fortracking students.•It should be able to do the following:◦Add new students to the database◦Delete a student from the database◦Enter a student’s test scores◦Print out the average of a student’s scores•These are our requirementsDepartment of Computer Science — University of San Francisco – p.12/??4-13: Top-down structure chart•main is at the top of the chart•Nodes below this represent portsions of the program that arecalled.•Arrows indicate input and output of data.•Nodes at the next level are then decomposed in the same way.•Eventually, we get to methods we know how to implement.Department of Computer Science — University of San Francisco – p.13/??4-14: Top-down structure chart•The structure chart indicates the calling sequence of theprogram.•Serves as a template for the actual program.•Annotate parameters as in, out, or in-out•Objects called with methods on them are parameters too!Department of Computer Science — University of San Francisco – p.14/??4-15: Choosing Objects•Each object should have one well-defined responsibility.•A common mistake is to “cram too much” into a single object ormethod.•Methods are typically 1-2 screens of code.•Before making a new class, does what you need already exist?Department of Computer Science — University of San Francisco – p.15/??4-16: Bottom-up-design•To implement the program in our structure chart, start at theleaves and work upward.•Bottom-level methods can be implemented directly.•Methods at the next level up are implemented in terms of thosemethods.•Eventually, you make it back up to main.Department of Computer Science — University of San Francisco – p.16/??4-17: Unit testing•As you code each method, you should test it in isolation.•Write a small program that calls this method with all expectedinputs.•Testing each method seems tedious, but it’s very important.◦A simple bug missed at a lower level can be very difficult tofind later


View Full Document

USF CS 112 - Constructors

Documents in this Course
Structs

Structs

4 pages

Trees

Trees

25 pages

Strings

Strings

27 pages

Queues

Queues

3 pages

Trees

Trees

24 pages

Arrays

Arrays

5 pages

ArrayList

ArrayList

24 pages

Stacks

Stacks

2 pages

Stacks

Stacks

8 pages

Trees

Trees

24 pages

Stacks

Stacks

8 pages

Queues

Queues

16 pages

Queues

Queues

17 pages

Queues

Queues

17 pages

Load more
Download Constructors
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Constructors and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Constructors 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?