1 / 40

CSCI 51 Introduction to Programming

Learn about classes, objects, methods, and designing solutions using OOP principles. Explore implementing algorithms in Java.

colinj
Download Presentation

CSCI 51 Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. QuestionWriting 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 returnTypemethodName (formal parameters) • think about the problem and develop an algorithm for solving the problem • write the method body

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

  4. QuestionWriting 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. SolutionWriting Methods { if (num1 > num2) { return true; } return false; } public static boolean larger (double num1, double num2)

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

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

  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 A class (the concept) An object (the realization) length = 15, width = 3 Rectangle length = 20, width = 6 Multiple objects from the same class length = 15, width = 15

  11. ObjectsAnd 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 DesignSimplified Methodology • Write down detailed description of problem • Identify all (relevant) nouns and verbs • From list of nouns, select objects • Identify data components of each object • 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 1Building 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 1Building 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 1Building 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 RectangleData Members and Operations class name data members Last Step: design and implement an algorithm for each operation 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 A class (the concept) An object (the realization) length = 15, width = 3 Rectangle length = 20, width = 6 Multiple objects from the same class length = 15, width = 15

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

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

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

  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(); use empty parentheses when no parameter to method variable name constructor method class name reserved word

  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 Rectangle r2 = new Rectangle (5, 10); public class Rectangle { private int length; private int width; public Rectangle () { length = 0; width = 0; } public Rectangle (int l, int w) { length = l; width = w; }

  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); Rectangle r1 = new Rectangle (5, 10); 3800 r1 10 3800 5 4500 r2 30 4500 20

  31. r 2500 2500 3 2 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 2500 2500 3 2 5 10

  32. 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 Visibility Modifiers public class Rectangle { private int length; private int width; } public Rectangle () { length = 0; width = 0; } ...

  33. Visibility ModifiersGuidelines • 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. Method Control Flow The called method can be within the same class as the caller, in which case only the method name is needed print computeArea computeArea();

  37. main print computeArea obj.print(); computeArea(); 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

More Related