COMP 14 Introduction to Programming - PowerPoint PPT Presentation

1 / 55
About This Presentation
Title:

COMP 14 Introduction to Programming

Description:

Create class-containing application program by extending definition of class JFrame. ... Use the same technique that is used to create JLabel and JTextField. ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 56
Provided by: jasonj3
Category:

less

Transcript and Presenter's Notes

Title: COMP 14 Introduction to Programming


1
COMP 14Introduction to Programming
  • Graphical User Interfaces
  • July 6, 2005

2
Quiz
  • Doh!

3
Announcements
  • Turn in Assignment 3 if you have not done so yet
  • Mid term Tuesday, July 11th
  • In class review Monday, July 10th
  • Quiz!
  • Tic-tac-toe (assignment 4)
  • Demo
  • Questions
  • Due Monday, July 10th at 1159 pm

4
COMP 14 So Far...
  • Problem Solving
  • Expressions
  • Java syntax and usage
  • Input/Output (I/O)
  • User Input
  • File I/O
  • Control Structures
  • Selection (if, if-else, switch)
  • Loops (while, do...while, for)

5
COMP 14 Next...
  • Object-oriented programming and design
  • GUIs
  • Example of object-oriented programming (today)
  • Writing methods
  • Pieces of code that we give a name
  • Writing classes
  • Organizing related pieces of information
  • Inheritance and polymorphism
  • Object-oriented design is one big happy family
  • Exceptions
  • Handling problems in your code
  • Events
  • Communication
  • Arrays
  • access multiple pieces of information with a
    single identifier
  • Sorting

6
GUIs
  • We will look at GUIs as an example to introduce
    object-oriented programming concepts

7
Graphical User Interface (GUI) Components
  • View inputs and outputs simultaneously.
  • One graphical window.
  • Input values in any order.
  • Change input values in window.
  • Click buttons to get output.

8
Java GUI Components
9
Graphical User Interface (GUI) Components
  • GUI components placed in content pane.
  • GUI components
  • Windows
  • Labels
  • Text areas
  • Buttons

10
GUI Components
  • Added to content pane of window.
  • Not added to window itself.
  • Pixel A picture element.

11
Windows
  • Can be created using a Frame object.
  • The class Frame provides various methods to
    control attributes of a window.
  • Measured in pixels of height and width.
  • Attributes associated with windows
  • Title
  • Width
  • Height

12
class JFrame
  • GUI window instance created as instance of Frame.
  • Provides various methods to control window
    attributes.

13
Constructor
  • A special method of a class used to instantiate
    an object
  • A constructor is invoked when a new object is
    created with new
  • The method name is the same as the class name
  • A class can provide several possible constructors
  • each with different parameters

14
Methods Provided by the class JFrame
Constructors
15
Methods Provided by the class JFrame
16
Two Ways to Create a Window
  • First way
  • Declare object of type JFrame.
  • Instantiate object.
  • Use various methods to manipulate window.
  • Second way
  • Create class-containing application program by
    extending definition of class JFrame.
  • Utilize mechanism of inheritance.

17
Inheritance
  • Derives a new class based an existing parent
    (superclass) class
  • The new child (subclass) inherits features such
    as methods and definitions from the parent class
  • The child class only describes things that are
    different than the parent class

18
Content Pane
  • Inner area of GUI window (below title bar, inside
    border).
  • To access content pane
  • Declare reference variable of type Container.
  • Use method getContentPane of class JFrame.

19
Methods Provided by the class Container
20
class JLabel
  • Labels Objects of a particular class type.
  • class JLabel Used to create labels.
  • Label attributes
  • Title
  • Width
  • Height
  • To create a label
  • Instantiate object of type JLabel.
  • Modify attributes to control display of labels.

21
Methods Provided by the class JLabel
22
Methods Provided by the class JLabel
23
class JTextField
  • Text fields Objects belonging to class
    JTextField.
  • To create a text field
  • Declare reference variable of type JTextField.
  • Instantiate object.

24
Methods Provided by the class JTextField
25
class JButton
  • Provided to create buttons in Java.
  • To create a button
  • Use the same technique that is used to create
    JLabel and JTextField.

26
Methods Provided by the class JButton
27
Methods Provided by the class JButton
28
Handling an Event
  • Action event An event created when JButton is
    clicked.
  • Event listener An object that receives a message
    when JButton is clicked.
  • In Java, you must register the listener.

29
Handling an Event
  • class ActionListener
  • Handles action event.
  • Part of package java.awt.Event.
  • The class ActionListener is a special type of
    class (interface).
  • Must contain the actionPerformed method.

30
Rectangle Program Sample Run
31
Programming Example Temperature Conversion
  • Input Temperature in Fahrenheit or Celsius.
  • Output Temperature in Celsius if input is
    Fahrenheit temperature in Fahrenheit if input is
    Celsius.

32
Programming Example Temperature Conversion
  • Solution
  • Create the appropriate JLabels, JTextFields,
    JButtons.
  • Add them to the created content pane.
  • Calculate the appropriate conversions when the
    buttons are clicked and an event is triggered.

33
Sample Run for TempConversion
34
To do
  • Finish reading chapter 6
  • Assignment 4
  • Due Monday, July 10th
  • Start reviewing for exam
  • Exam is Tuesday, July 11th
  • Forward me the names of concepts you would like
    to review Monday

35
(No Transcript)
36
Additional Slides
37
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
38
ObjectsVCR Example
  • Use it without knowing how it's made
  • Internal parts are hidden -- only interact with
    the provided buttons
  • Can't modify the functions of a VCR -- record
    button always records, play button always plays

Same is true for objects (like Strings) that are
provided by Java
39
Objects
  • Consist 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.
40
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

41
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

42
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 operations

43
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

44
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

45
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

46
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
47
class Rectangle Data Members and Operations
class name
data members
operations (methods)
Last Step design and implement an algorithm for
each operation
48
Anatomy of a Class
  • A class contains data declarations and method
    declarations

int width int length
Data declarations
Method declarations (operations)
49
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
50
Object-Oriented Design Example 2
  • A place to buy candy is from a candy machine.
    A new candy machine is bought for the gym, but it
    is not working properly. The candy machine has
    four dispensers to hold and release items sold by
    the candy machine and a cash register. The
    machine sells four products candies, chips, gum,
    and cookieseach stored in a separate dispenser.
    You have been asked to write a program for this
    candy machine so that it can be put into
    operation.

51
Object-Oriented Design Example 2
  • The program should do the following
  • Show the customer the different products sold by
    the candy machine.
  • Let the customer make the selection.
  • Show the customer the cost of the item selected.
  • Accept money from the customer.
  • Return change.
  • Release the item, that is, make the sale.

52
Object-Oriented Design Example 2
53
Object-Oriented Design Example 2
54
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

55
Next in Comp14
  • Tomorrow writing methods
  • Monday review for mid-term
  • Bring laptops
  • Write questions before coming to class
Write a Comment
User Comments (0)
About PowerShow.com