Title: COMP 14 Introduction to Programming
1COMP 14Introduction to Programming
- Graphical User Interfaces
- July 6, 2005
2Quiz
3Announcements
- 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
4COMP 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)
5COMP 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
6GUIs
- We will look at GUIs as an example to introduce
object-oriented programming concepts
7Graphical 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.
8Java GUI Components
9Graphical User Interface (GUI) Components
- GUI components placed in content pane.
- GUI components
- Windows
- Labels
- Text areas
- Buttons
10GUI Components
- Added to content pane of window.
- Not added to window itself.
- Pixel A picture element.
11Windows
- 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
12class JFrame
- GUI window instance created as instance of Frame.
- Provides various methods to control window
attributes.
13Constructor
- 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
14Methods Provided by the class JFrame
Constructors
15Methods Provided by the class JFrame
16Two 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.
17Inheritance
- 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
18Content 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.
19Methods Provided by the class Container
20class 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.
21Methods Provided by the class JLabel
22Methods Provided by the class JLabel
23class JTextField
- Text fields Objects belonging to class
JTextField. - To create a text field
- Declare reference variable of type JTextField.
- Instantiate object.
24Methods Provided by the class JTextField
25class JButton
- Provided to create buttons in Java.
- To create a button
- Use the same technique that is used to create
JLabel and JTextField.
26Methods Provided by the class JButton
27Methods Provided by the class JButton
28Handling 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.
29Handling 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.
30Rectangle Program Sample Run
31Programming Example Temperature Conversion
- Input Temperature in Fahrenheit or Celsius.
- Output Temperature in Celsius if input is
Fahrenheit temperature in Fahrenheit if input is
Celsius.
32Programming 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.
33Sample Run for TempConversion
34To 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)
36Additional Slides
37Object-Oriented Design
Designing a solution to a problem by first
identifying components called objects, and
determining how the objects interact with each
other
38ObjectsVCR 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
39Objects
- 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.
40ObjectsAnd 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
41Classes
- 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
42Object-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
43Object-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
44Example 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
45Example 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
46Example 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
47class Rectangle Data Members and Operations
class name
data members
operations (methods)
Last Step design and implement an algorithm for
each operation
48Anatomy of a Class
- A class contains data declarations and method
declarations
int width int length
Data declarations
Method declarations (operations)
49Classes 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
50Object-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.
51Object-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.
52Object-Oriented Design Example 2
53Object-Oriented Design Example 2
54Non-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
55Next in Comp14
- Tomorrow writing methods
- Monday review for mid-term
- Bring laptops
- Write questions before coming to class