Unformatted text preview:

4 2 Anatomy of an Object Recall that an object consists of Instance data Introduction to Programming II Methods Constructors Methods consist of Parameters Chris Brooks Local data Method body Department of Computer Science Return statement University of San Francisco Department of Computer Science University of San Francisco p 1 Department of Computer Science University of San Francisco p 2 4 4 Example 4 3 Constructors A constructor is a method that is called when an object is first created public class Point public int xval public int yval Its responsibility is to initialize an object s instance variables Point int x int y xval x yval y It must have the same name as the class in constructs It has no return type It is called when new is invoked Point p new Point 3 4 Department of Computer Science University of San Francisco p 3 4 5 Multiple Constructors Department of Computer Science University of San Francisco p 4 4 6 Multiple Constructors It s often helpful to be able to specify some instance variables but not all public class Circle private Point center private int radius For example let s say our circle class has a default radius of 1 public Circle Point c center c radius 1 If the radius is something else users can specify it Point p1 new Point 3 3 Circle c new Circle p creates a circle with radius 1 at 3 3 Point p1 new Point 3 3 Circle c new public Circle Point c int r center c radius r Circle p 5 creates a circle with radius 5 at 3 3 Department of Computer Science University of San Francisco p 5 Department of Computer Science University of San Francisco p 6 4 7 Exercise 4 8 Designing a Program Modify the book class from last week s lecture Knowing the syntax of how to build a class is only the Add three constructors Default takes no arguments The bigger challenge is figuring out how to fit the pieces beginning 1 argument title 2 arguments author and title author should be an instance of name Add a constructor to name that takes two arguments first name and last name together The software development process consists of the following steps 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 7 4 9 Requirements Department of Computer Science University of San Francisco p 8 4 10 Design Requirements indicate what a program is supposed to do This is the how part of the program Expressed as a functional specification What is the input like Specifies the classes that are needed and how they interact What methods are called what data is returned What must the output look like Are there other programs it must interact with How quickly must the program run Skipping this step can lead to serious unpleasant bugs A good design should make implementation straightforward Often this comes from a client Usually not as precise as you d like Department of Computer Science University of San Francisco p 9 Department of Computer Science University of San Francisco p 10 4 12 Example student database 4 11 Structure Charts A structure chart is a helpful tool for doing design Let s say we ve been hired by USF to build an application for tracking students Helps to divide and conquer Divide the problem into smaller pieces until you reach pieces that can be tackled directly 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 This is called top down design Print out the average of a student s scores These are our requirements Department of Computer Science University of San Francisco p 11 Department of Computer Science University of San Francisco p 12 4 13 Top down structure chart 4 14 Top down structure chart main is at the top of the chart The structure chart indicates the calling sequence of the Nodes below this represent portsions of the program that are program Serves as a template for the actual program called Arrows indicate input and output of data Annotate parameters as in out or in out Nodes at the next level are then decomposed in the same way Objects called with methods on them are parameters too Eventually we get to methods we know how to implement Department of Computer Science University of San Francisco p 13 Department of Computer Science University of San Francisco p 14 4 16 Bottom up design 4 15 Choosing Objects Each object should have one well defined responsibility A common mistake is to cram too much into a single object or To implement the program in our structure chart start at the leaves and work upward Bottom level methods can be implemented directly method Methods are typically 1 2 screens of code Before making a new class does what you need already exist Methods at the next level up are implemented in terms of those methods Eventually you make it back up to main Department of Computer Science University of San Francisco p 15 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 expected inputs Testing each method seems tedious but it s very important A simple bug missed at a lower level can be very difficult to find later on Good programming practice can help limit the amount of time you spend debugging Department of Computer Science University of San Francisco p 17 Department of Computer Science University of San Francisco p 16


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
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 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?