Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.

Similar presentations


Presentation on theme: "CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009."— Presentation transcript:

1 CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009

2 Question Writing Methods Write a method called sum100 that returns the sum of the integers from 1 to 100, inclusive. Steps: –write the method header public static returnType methodName (formal parameters) –think about the problem and develop an algorithm for solving the problem –write the method body

3 Solution Writing Methods { int sum = 0; for (int num = 1; num<=100; num++) { sum += num; } return sum; } public static int sum100 ()

4 Question Writing Methods Write a method called larger that accepts two double parameters and returns true if the first parameter is greater than the second and false otherwise.

5 Solution Writing Methods { if (num1 > num2) { return true; } return false; } public static boolean larger (double num1, double num2)

6 Question Writing Methods Write a method called average that accepts two integer parameters and returns their average as a double. { int sum = num1 + num2; return (sum / 2.0); } public static double average (int num1, int num2)

7 Question Overloading Methods Overload the average method such that if three integers are provided as parameters, the method returns the average of all three. Write another method with the same name that has a different parameter list. { int sum = num1 + num2 + num3; return (sum / 3.0); } public static double average (int num1, int num2, int num3)

8 Object-Oriented Design What is it? Designing a solution to a problem by first identifying components called objects, and determining how the objects interact with each other

9 Objects Consists of data and operations on the data Data - descriptive characteristics Operations - what it can do (or what can be done to it) Example A coin that can be flipped so that its face shows either "heads" or "tails" –data: its current face (heads or tails) –operations: it can be flipped Operations can change data.

10 Classes and Objects Rectangle A class (the concept) length = 15, width = 3 An object (the realization) length = 20, width = 6 length = 15, width = 15 Multiple objects from the same class

11 Objects And Methods and Classes We represent operations with methods –group of statements that are given a name We can use objects and their methods without knowing exactly how the methods work An object is an instance of a class. A class is the blueprint of an object. –the class provides the methods that can operate on an object of that class

12 Classes A class contains data declarations and method declarations A class is a description of an object –just a model, not an actual object –you can think of the concept of a book without thinking of a particular book A class is no more an object than a blueprint is an actual house

13 Object-Oriented Design Simplified Methodology 1. Write down detailed description of problem 2. Identify all (relevant) nouns and verbs 3. From list of nouns, select objects 4. Identify data components of each object 5. From list of verbs, select operation

14 Object-Oriented Design Example 1 Problem Statement –Write a program to input the length and width of a rectangle and calculate and print the perimeter and area of the rectangle

15 Example 1 Building a Rectangle Identify nouns –length, width, rectangle, perimeter, area Identify each class –length of a rectangle –width of a rectangle –perimeter of a rectangle –area of a rectangle

16 Example 1 Building a Rectangle Identify data members for each class –nouns: length, width, area, perimeter –what are the essential nouns for describing the rectangle? –area and perimeter can be computed if we know the length and width

17 Example 1 Building a Rectangle Identify operations for each class –input, calculate, print –setLength –setWidth –computePerimeter –computeArea –print –getLength –getWidth directly from problem statement customary to include operations to get the value of the data members

18 class Rectangle Data Members and Operations Last Step: design and implement an algorithm for each operation class name data members operations (methods)

19 Anatomy of a Class A class contains data declarations and method declarations int width; int length; Data declarations Method declarations (operations)

20 Classes and Objects Rectangle A class (the concept) length = 15, width = 3 An object (the realization) length = 20, width = 6 length = 15, width = 15 Multiple objects from the same class

21 Implementing Rectangle... { length = l; } public void setLength (int l) public void setWidth (int w) { width = w; } Remember: according to scope rules, the methods in Rectangle can directly access width and length.

22 Implementing Rectangle... public int getLength() { return length; } { return width; } public int getWidth()

23 Implementing Rectangle... public int computePerimeter() { return (width*2 + length*2); } { return (width * length); } public int computeArea()

24 Implementing Rectangle... public void print() { System.out.print ("The perimeter of the " + length + "x" + width); System.out.print (" rectangle is " + computePerimeter()); System.out.println (" and the area is " + computeArea()); }

25 Creating an Object Before we can access the members (variables and methods) of a class, we have to instantiate, or create, an object Rectangle r = new Rectangle(); class name variable name reserved word constructor method use empty parentheses when no parameter to method

26 Constructors A constructor is a special method that is used to initialize a newly created object When writing a constructor, remember that: –it has the same name as the class –it does not return a value –it has no return type, not even void –it typically sets the initial values of instance variables The programmer does not have to, but usually should, define a constructor for a class

27 Constructor public Rectangle (int l, int w) { length = l; width = w; } public class Rectangle { private int length; private int width; Rectangle r2 = new Rectangle (5, 10); public Rectangle () { length = 0; width = 0; }

28 Rectangle.java Typical Order in the Java Source File: –data members –constructor(s) –other methods

29 Instance Data The length and width variables in the Rectangle class are called instance data –each instance (object) of the Rectangle class have its own width and length variables Every time a Rectangle object is created, new width and length variables are created as well The objects of a class share the method definitions, but each has its own data space –the only way two objects can have different states -- two different memory locations

30 Rectangles in Memory Rectangle r2 = new Rectangle (20, 30); 3800 4500 r1 r2 3800 4500 5 10 20 30 Rectangle r1 = new Rectangle (5, 10);

31 r 2500 2 3 Using the Rectangle Class Create an object: Rectangle r; r = new Rectangle(2, 3); OR Rectangle r = new Rectangle(2, 3); Use the object and the dot operator to access methods: r.setLength(5); r.setWidth(10); r 2 3 2500 510

32 Visibility Modifiers public visibility –can be accessed from anywhere private visibility –can only be accessed from inside the class (inside the same Java source file) default visibility –members declared without a visibility modifier –can be accessed by any class in the same package public class Rectangle { private int length; private int width; } public Rectangle () { length = 0; width = 0; }...

33 Visibility Modifiers Guidelines Usually declare data members with private visibility Declare methods that clients (other classes) are supposed to call with public visibility –service methods Declare methods that only other methods in the class are supposed to call with private visibility –support methods

34 UML Diagram Top box: name of class Middle box: data members and their data types Bottom box: member methods’ names, parameter list, return type of method + means public - means private Rectangle -length: int -width: int +Rectangle() +Rectangle(int, int) +setLength(int): void +setWidth(int): void +getLength(): int +getWidth(): int +computePerimeter(): int +computeArea(): int +print(): void

35 Driver Programs Classes containing the main method that we use to test our classes It's very useful to first write your classes and write a driver program to test them.

36 computeArea(); computeAreaprint Method Control Flow The called method can be within the same class as the caller, in which case only the method name is needed

37 print computeArea computeArea(); obj.print(); main Method Control Flow The called method can be part of another class or object

38 RectangleTester.java Rectangle r1 = new Rectangle(); Rectangle r2 = new Rectangle (20, 30); r1.setWidth(5); r1.setLength(10); r1.print(); r2.print(); Must be looking at class with main method to run the program Exception in thread "main" java.lang.NoSuchMethodError: main Don't forget to re-compile files after making changes

39 Thought Exercise Write a method for the Rectangle class called printBox that will print the rectangle as a box made of % example: length = 3, width = 5 %%% % %%%

40 Non-Concrete Objects Objects in programs don't always have real-world analogs Example object: error message data: text of the error message operation: print the text of the error message to the screen


Download ppt "CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009."

Similar presentations


Ads by Google