Bobby D. Gerardo - PowerPoint PPT Presentation

About This Presentation
Title:

Bobby D. Gerardo

Description:

Programming and Problem Solving Using Java. Chapter 3 ... int i = double r. Write an assignment in java: x = (a b)2(c d) Evaluate the following: ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 51
Provided by: Bob7158
Category:
Tags: bobby | doubler | gerardo

less

Transcript and Presenter's Notes

Title: Bobby D. Gerardo


1
Programming and Problem Solving Using Java
  • Bobby D. Gerardo
  • Kunsan National University
  • Summer 2004

2
Chapter 3Object Oriented Design and Writing
Worker Class
3
Review
4
Class Definition
  • A Class Definition has the following form
  • A header declaration
  • The class body
  • The data field declaration of the class
  • The method definition of the class
  • Form visibility class classname
  • Example public class PiglatinApp

5
Definition of Method main()
  • Method main() has always the following header
  • public static void main(String args)
  • public can be accessed outside of the class
    main()
  • static means that main() is not applied to an
    object when it is invoked. (Applied for a
    class)
  • void means that main() does not return a result
    but is executed for its effect

6
Case Study Solution Format
  1. Problem specify the problem requirement
  2. Analysis analyze the problem an identify the
    classes that will be needed
  3. Design design the classes to solve the problem.
    Locate the relevant classes in libraries. Modify
    existing classes if necessary. Design new classes
    where necessary
  4. Implementation implement the new and modified
    classes.
  5. Testing test and verify the completed program.

7
Review Exercise
8
Review Exercise
  • List 4 kinds of primitive data
  • Indicate whether each assignment is valid or not
    valid.
  • r 3.5 5.0
  • i 2 5
  • char c my name
  • int i double r
  • Write an assignment in java x (ab)2(cd)
  • Evaluate the following
  • Value of x is 57
  • Value of x is 5 7
  • This String.charAt(1)

9
Selected Programming Solution1. Math Class and
Pow Method2. Solution of a Distance Problem3.
Demo of House Cost
10
The Math Class and Pow Method
Bank Calculator example Formula, balanceN
balance(1InterestRate)2 In Java, balanceN
balance( (1InterestRate) (1InterestRate)
Modified Bank Calculator using pow(x, y)
method Formula, balanceN balance(1InterestRat
e)2 In Java, R 1 InterestRate balanceN
Balance Math.pow(R, 2)
11
Solution of a Distance Problem
Java Code X (X2-X1) Y (Y2-Y1)
D Math.sqrt(Math.pow(X, 2) Math.pow(Y, 2))
12
Chapter 3Object Oriented Design and Writing
Worker Class
13
Object Oriented Design-outline
  • Object oriented design write classes that
    define methods and perform different kinds of
    operation.
  • Worker classes are also called support classes
    because they support application class or client
    class.
  • Java Abstract Window Toolkit (AWT)
  • Incorporate Java Applet in HTML file.

14
3.1. A First Worker Class Class FoodItem
  • The data field of a class (also called instance
    variables) store the information associated with
    an object of that class.
  • The fields maybe referenced through out the class
  • The values stored in an objects data fields
    represent the state of that object.

15
3.1. Class FoodItem (cont)
  • Declare Class FoodItem and its three data fields.
  • Giving data fields initial value.
  • Method main definitions.
  • Methods for the class FoodItem.
  • Constructor- create new FoodItem objet
  • Object mutators- change the descriptors, size,
    price
  • Accesors- return the description, size, price
  • Method that returns the object state as a string
  • A method that calculates the unit price of an
    item

16
3.1. Class FoodItem (cont)
  • Table 3.1 Method headers for FoodItem
  • Constructor method
  • void Methods- mutator methods are all void
    methods.
  • The return statement method that is not a void
    method or constructor returns a single result.
  • Post conditions a condition that must be true
    after the method executes and is part of the
    class documentation.
  • Note We will demo the class FoodItem and
    TestFoodItem

17
Arguments are Passed by a value
  • In Java, primitive type arguments are passed by
    value.
  • This means that the value of each argument is
    passed to each corresponding parameter and a
    separate copy of that value is stored locally in
    the method parameter.
  • Note see Fig. 3.4

18
Transfer of Control during Method Call and Return
  • During compilation of a class, each method is
    translated into a sequence of byte code
    instruction.
  • Each call to a method is translated as an
    instruction that transfers control to the byte
    code instruction for that method.
  • When the compiler reaches the end of method
    definition, it inserts an instruction returning
    control from he method back to the calling
    instruction.
  • Note Fig. 3.5 show the transfer of control that
    occurs when the main method in Fig. 3.2 executes
    that statement.

19
Encapsulation
  • Because all data fields have private visibility
    (FoodItem Example), another class that is a
    client (user) of the class FoodItem cannot access
    the field directly.
  • To change the data field value, the client must
    invoke one of the mutator methods.
  • To retrieve a data field value, the client must
    invoke one of the accesor methods.
  • This is the principle of encapsulation.
  • Note see Fig. 3.6

20
Case Study 1 Finding the Words in a Sentence
21
Case Study 1 Finding the Words in a Sentence
  • 1. Problem Write a program that gets a sentence
    and displays the first three words in the
    sentence on separate lines
  • 2. Analysis analyze the problem an identify the
    classes that will be needed
  • 3. Design design the classes to solve the
    problem. Locate the relevant classes in
    libraries. Modify existing classes if necessary.
    Design new classes where necessary
  • 4. Implementation implement the new and
    modified classes.
  • 5. Testing test and verify the completed
    program.

22
Modified Finding the Words in a Sentence
  • Modify the case study Finding the Words in a
    Sentence to show the output on a dialog window.
    Note, this is to use java.swing.

23
Case Study 2 Designing a Coin Changer
24
Case Study2 Designing a Coin Changer
  • 1. Problem Determine the value of collection
    coins. Our goal is to write a class that
    simulates the behavior of a coin changer. Instead
    of pouring a container of coins into a hopper,
    the user will provide the number of each kind of
    coin as input data.
  • 2. Analysis analyze the problem an identify the
    classes that will be needed
  • 3. Design design the classes to solve the
    problem. Locate the relevant classes in
    libraries. Modify existing classes if necessary.
    Design new classes where necessary
  • 4. Implementation implement the new and
    modified classes.
  • 5. Testing test and verify the completed
    program.

25
Modified Designing a Coin Changer
  • Modify the case study Finding the Words in a
    Sentence to add the method to a class
    CoinChanger that dispenses change using the
    fewest coins. For example, if the value computed
    for change is 92 cents, this method should return
    a String object that contains the characters.
  • quarter 3, dimes 1, nickels 1, pennies 2

26
Programming Projects(1) Case 1 Finding the
Words in a Sentence, P.120(2) Modified
Finding the Words in a Sentence(3) Case 2
Coin Changer, P. 127 (4) Modified Coin Changer,
Prog. 1, P.134Note Morning Programming
Exercises
27
Case Study 3 Computing the Weight of Flat
Washers
28
Case Study 3 Computing the Weight of Flat
Washers
  • 1. Problem You work for a company that
    manufactures flat washers. To estimate shipping
    costs, you need a program that computes the
    weight of a specified quantity of flat washers.
    As part of this process, you need to compute the
    weight of a single washer.
  • 2. Analysis analyze the problem an identify the
    classes that will be needed
  • 3. Design design the classes to solve the
    problem. Locate the relevant classes in
    libraries. Modify existing classes if necessary.
    Design new classes where necessary
  • 4. Implementation implement the new and
    modified classes.
  • 5. Testing test and verify the completed
    program.

29
Modified Computation of the Weight of Flat
Washers
  • Modify the case study Computing the Weight of
    Flat Washers by including the following
  • Data inputs shall be in radius rather than in
    diameter.
  • Put a dialog box prompting a message that the
    input inner radius is larger than the outer
    radius.

30
Summary of Programming Projects(1) Case 1
Finding the Words in a Sentence, P.120(2)
Modified Finding the Words in a
Sentence(3) Case 2 Coin Changer, P. 127 (4)
Modified Coin Changer, Prog. 1, P.134(5) Case 3
Computing the Weight of Flat Washers (6)
Modified Computation of the weight of
Flat washers
31
Review of Methods
  • Constructor Methods always has the same name as
    the class. It should have the public visibility
    and does not have a result type because it does
    not return a value. Fig. 3.17
  • Accessor Methods accessor methods are sometimes
    called getter methods and should begin with the
    word get. Fig. 3.18
  • Mutator Methods Just as we need to access an
    objects private o protected data from another
    object, we sometimes need to store data in an
    objects private or protected fields. We use
    mutator or modifier methods to do this. Fig. 3.19

32
String Objects are Immutable
  • Unlike the classes that we define ourselves,
    String objects are immutable which means that we
    cannot change the characters stored in a String
    object.
  • If we do so Java creates a new String object that
    store the modified string. Fig. 3.20

33
Table 3.5 Some formatting patterns for class
DecimalFormat
34
Table 3.6 Class DecimalFormat methods
35
Table 3.7 Class NumberFormat Methods
36
Table 3.8 Class KeyIn methods for reading data
37
Applets, AWT, and the Graphics Class
  • Java provides a collection of graphics methods
    that enable you to draw pictures or graphical
    patterns on a drawing surface such as a computer
    screen.
  • You can draw lines and geometric shapes.
  • You can specify the position of each shape and
    also its color.

38
The Drawing Surface
  • In graphics programming you can control the
    location on a drawing surface of each line or
    shape that you draw.
  • You must know the size of your drawing surface
    and how to reference picture elements (pixels) on
    it.
  • Fig. 3.32

39
The AWT Class Library
  • Abstract Window Toolkit (AWT) to facilitate
    graphics and GUI programming.
  • If you wish to draw you must import the AT
    package java.awt.
  • AWT contains a hierarchy of classes for
    displaying windows and GUI components.
  • An application class is completely self
    sufficient entity that may rely on support
    objects to do its job.

40
The AWT Class Library (cont)
  • An application class is completely self
    sufficient entity that may rely on support
    objects to do its job.
  • An applet is not self-sufficient. It is intended
    as a support object in a larger context such as
    web browser like IE or Netscape.
  • Fig. 3.33., two files play important role,
    htmlFileName and className.

41
Graphics programming Exercises
  • Figure 3.34 P. 159
  • Modify Fig. 3.34 to draw Red and blue lines at
    position 240X340

42
Drawing Rectangles
  • You can use method drawRect() (defined in class
    Graphics) to draw rectangle on the screen. i.e.
  • g.drawRect(x1, y1, 100, 50)
  • Fig. 2.36 Rectangle with (x1, y1) as top to left
    corner.

43
Graphics programming Exercises
  • Figure 3.37 P. 162- Class House
  • Modify Fig. 3.37 to use brown lines and red door.

44
Drawing Arcs and Circles
  • Method drawArc() draws an arc. The method call is
  • g.drawARc(x, y, 100, 100, 0, 90)
  • Fig. 3.40 show the Arc drawn by the previous
    statement
  • Circle
  • g.drawOval(x, y, 100, 100)
  • g.drawArc(x, y, 100, 100, 0, 360)

45
Circle with Point (x, y)
  • A circle with center at point (x, y) and radius r
    could be drawn inside a square whose top-left
    corner is at point (x-r, y-r).
  • g.drawOval (x-r, y-r, 2r, 2r)
  • Fig. 3.41

46
Graphics programming Exercises
  • Figure 3.42 P. 166- Class HappyFace
  • Modify Fig. 3.42 with eyes and nose filled with
    yellow color.

47
Drawing Pie Slices
  • Java Provides methods to draw pie slices
  • A pie slice is filled segment of a circle. The
    method call is
  • g.fillArc(x-r, y-r, 2r, 2r, 20, 30)
  • Fig. 3.44 A Pie Slice

48
Programming Projects
  1. No.1 P. 177 Projectile Problem
  2. No. 7 P. 179 Bar Graph.

49
Summary of Programming Exercises and Projects
  • Exercises
  • Figure 3.34 P. 159
  • Modify Fig. 3.34 to draw Red and blue lines at
    position 240X340
  • Figure 3.37 P. 162- Class House
  • Modify Fig. 3.37 to use brown lines and red door.
  • Figure 3.42 P. 166- Class HappyFace
  • Modify Fig. 3.42 with eyes and nose filled with
    yellow color.
  • Projects
  • No.1 P. 177 Projectile Problem
  • No. 7 P. 179 Bar Graph.

50
? End of Chapter
Write a Comment
User Comments (0)
About PowerShow.com