Introduction to Classes and objects - PowerPoint PPT Presentation

1 / 57
About This Presentation
Title:

Introduction to Classes and objects

Description:

A model for this approach. Community of interacting agents called objects ... w. ChangeJob('Ford, Windsor'); Problem 1. Define a class student. Think of the ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 58
Provided by: subirband
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Classes and objects


1
Introduction to Classes and objects
  • Agenda
  • What is an object oriented view of programming?
  • What is a class?
  • How to define a class?
  • Problems to think about
  • creating and using an object

2
What is an object oriented view of programming
Suppose I want to invite my friends Tom, Liz and
Rita to meet me for coffee at Tim Hortons in the
Devonshire Mall at 8PM today. Approach 1 Give a
complete and very detailed set of instructions
on how to go from the University to Tim Hortons.
In this approach, every one exactly follows my
instructions.
3
What is an object oriented view of programming
Approach 2 Simply tell my friends Pl meet me
for coffee at 8 this evening at Tim Hortons in
the Devonshire Mall Here each person follows
his/her own way of interpreting my message and
reacting to it. I dont know and I dont care the
specific mechanism used by each individual in
response to my message.
4
A model for this approach
  • Community of interacting agents called objects
  • Each object provides a service
  • All action is initiated by a message to an object

5
Object oriented view
  • Importance of information hiding
  • Service oriented view of the world
  • Why is this different from invoking a function
    in C?

6
Notion of classes and objects
  • Every time I use the cookie cutter, I get a
    cookie ready for baking.
  • Each cookie looks exactly like the others
  • View my cookie cutter as a template

My Cookie cutter
7
Class - a template for similar objects
  • Class definition
  • What are the properties of the class?
  • What are the capabilities of the class?
  • Worker class
  • How to characterise an object?
  • name, age, place_working
  • What can an object do?
  • change the place of work
  • describe the object as a string

8
Creating an object
  • First step is to define the class if needed
  • Use the class as a template to create objects
  • All objects of the same class has the same set of
    properties and capabilities

9
Class Hierarchies and inheritance
Notion of superclass and subclasses
10
Defining a class
  • public class worker
  • String Name // Example of a variable
  • int age
  • String placeWorking
  • public void ChangeJob(String NewPlace) //
    Method
  • placeWorking NewPlace
  • // Other methods as needed
  • public worker(String WorkerName) //
    Constructor
  • Name WorkerName

11
Static variables and methods
  • Why is it useful to have class level services?
  • Examples
  • Number of workers
  • Average salary

12
How to use the services of a class
  • A service gt a method
  • An object of the class must exist for nonstatic
    methods
  • worker w
  • w new worker( Jim)//use of constructor
  • Use service gt invoke right method
  • w. ChangeJob(Ford, Windsor)

13
Problem 1
  • Define a class student.
  • Think of the
  • properties that characterize a student.
  • capabilities of an object of class student (What
    can this student do?).

14
Properties that characterize a student
  • Name,
  • Stud,
  • DateOfBirth,
  • GradeIn212,
  • NumberOfCoursesTaken,
  • knowsC,
  • and so on

15
Capabilities of objects of class student
  • Change the major
  • retrieve the date of birth
  • compute the major grade average
  • Generate a string describing the object

16
Problem 2
  • Define a class called course. An object of
    such a class would be the course 60-212. Remember
    that this course has been offered many times.
    Each offering has a professor, a list of students
    with their grades and so on.
  • Here we may want to ask questions such as how
    many courses do we have in the University?

17
Your first java application
public class First public static void
main(String args) System.out.println("60
-212 is a\npiece of cake")
Output produced 60-212 is a piece of cake
18
Every application belongs to a class
This is like CLA in C . We will not use it now.
Just include it!!
public class First public static void
main(String args) System.out.println("60
-212 is a\npiece of cake")
This is how you print a message on the screen
just like printf in C
19
Our second java application
import javax.swing.JOptionPane public class
Second public static void main(String
args) JOptionPane.showMessageDialog(null
, "60-212
is a\npiece of cake")
20
Import in Java is just like include in C
import javax.swing.JOptionPane public class
Second public static void main(String args)
JOptionPane.showMessageDialog(null,
"60-212 is a\npiece of
cake")
From the JoptionPane class invoke the method
showMessageDialog
Display the string in the dialog window
21
Points you need to remember
  • General structure of a simple java program
  • import classes
  • public class class_name
  • The name of the java program file must have
    class_name as the file name .
  • The above application must be saved as
    Second.java
  • JOptionPane.showMessageDialog(null, String) to
    display a string in a window

22
Pseudo-program of our third problem
  • main( )
  • Declare variables
  • Read one line of input
  • Read number from the
  • input line
  • if (number is 0 or 1)
  • factorial 1
  • else while (number gt 1)
  • factorial number
  • decrease number by 1
  • Print factorial

Read a number n Calculate n! print n!
23
Our third java Application
24
import javax.swing.JOptionPane public class
Third public static void main(String args)
String input_string // This string holds the
input int factorial 1, number, value
input_string JOptionPane.
showInputDialog("Enter number.

Then press OK")
25
This is how you extract an integer from a string
number Integer.parseInt(input_string)
value number if ((number 0)
(number 1)) factorial 1 else
while (number gt 1) factorial
factorial number number --
JOptionPane.showMessageDialog(null,
value
"! " factorial)
In C we would have written sscanf(input_string,
d, number)
26
These features are just like C
number Integer.parseInt(input_string)
value number if ((number 0)
(number 1)) factorial 1 else
while (number gt 1) factorial
factorial number number --
JOptionPane.showMessageDialog(null,
value
"! " factorial)
27
(No Transcript)
28
Points to remember
  • General structure of a simple program
  • import javax.swing.JOptionPane
  • public class YourClassName
  • public static void main(String args)
  • Body of your program

29
Points to remember
  • How to read an int using dialog windows?
  • Read the string typed by user
  • Extract integer from the string

5234
5234
30
Points to remember
  • How to read an int using dialog windows?
  • Read the string typed by user
  • Extract integer from the string
  • input_string JOptionPane.showInputDialog(String)

Message displayed in input dialog window
number Integer.parseInt(input_string)
31
Points to remember
  • How to display a string using dialog windows?
  • JOptionPane.showMessageDialog(null,
  • String_to_be_displayed")

32
Our fourth JAVA application
  • Until now, we only showed one string. Now we will
    take an application where we show many lines of
    output.
  • Our problem is to generate a list showing degrees
    fahrenheit and the corresponding degrees
    centigrade.
  • The C program to do this is simple.
  • New features shown here are
  • how to specify format for floating point numbers
  • how to display in a textarea

33
import javax.swing. import java.text.DecimalForm
at public class Fourth public static void
main(String args) int
temp_in_fahrenheit double temp_in_celsius
DecimalFormat precisionTwo
new DecimalFormat("0.00")
JTextArea OutputArea new JTextArea(15, 30)

This area will hold lines of output
34
This is how you add to the TextArea
OutputArea.append("Degrees \tDegrees\nfahrenheit\t

centigrade\n") OutputArea.append("_______
____________\n") for (temp_in_fahrenheit 0
temp_in_fahrenheit lt 100
temp_in_fahrenheit 10)
temp_in_celsius 5(temp_in_fahrenheit - 32.)/9
OutputArea.append(temp_in_fahrenheit
"\t" precisionTwo.format(temp_i
n_celsius) "\n")
JOptionPane.showMessageDialog(null, OutputArea)

35
What is a textarea?
Important method append(string) adds string
to the area
36
Points to remember
  • To display many lines of text
  • a) Define a JTextArea as follows
  • JTextArea Areaname new JTextArea(NumLines,
    NumChars)
  • b) Append lines of text to the text area by
  • Areaname.append(A string to be shown)
  • c) Instruct the text area to be displayed by
  • JOptionPane.showMessageDialog(null,

  • Areaname)

37
How to compile and execute a java application?
  • For now, no command line arguments
  • Step 1) type the following
  • javac your_java_program
  • Step 2) type the following
  • java your class name
  • In the case of program 3
  • javac Third.java
  • java Third

38
Features of JAVA to remember
  • Basic structure of our application is
  • import javax.swing.JOptionPane
  • public class Your_Class_name
  • methods as needed
  • public static void main(String args)
  • .
  • Just as you say include in C, here you import
    already existing classes.

39
Problem 1
Write a Java application that will read in two
integers DepositAmount and InterestRate. The
first number represents the amount of money (in
dollars) you wish to invest for a fixed number of
years. The second number represents the interest
rate in percentage. For example, if you wish to
invest 1000 at 8 interest, the two integers
your program will read in will be 1000 and 8
respectively.
40
Your program should display, in a dialog window
the money you will get back after 1, 2, ... 10
years. If P is the money you invest at interest
i for n years, the formula to calculate the money
M you get back after n years is given below M
P (1 i 0.01)n We are not interested in
displaying the fractional part of the money M.
In other words, M is an integer.
41
Step 1 Read in the DepositAmount and the
InterestRate. Step 2 n 1. Step 3 While (n
lt 10), repeat steps 4 - 5 Step 4 Calculate and
display the money M you get back after n
years. Step 5 Increase n by 1.
Note Is a for statement is useful when
implementing the while loop?
42
Decomposition of step 4
Calculate and display the money M you get back
after n years.
If P is the money you invest at interest i for n
years, the formula to calculate the money M you
get back after n years is given below M P
(1 i 0.01)n
43
If n 1, M1 P(1 0.01 i) If n 2, M2 P(1
0.01 i)2 If n 3, M3 P(1 0.01 i)3 If n
4, M4 P(1 0.01 i)4 If n 5, M5 P(1
0.01 i)5 If n 6, M6 P(1 0.01 i)6 If n
7, M7 P(1 0.01 i)7 and so on. How to
calculate P(1 0.01 i)2 ? P(1 0.01 i)2
P(1 0.01 i) (1 0.01 i) M1 (1
0.01 i)
44
If n 3, M3 P(1 0.01 i)3 How to calculate
P(1 0.01 i)3 ? M3 P(1 0.01 i)3 P(1
0.01 i)2 (1 0.01 i) M2 (1 0.01
i) Similarly, M4 M3 (1 0.01 i) and so
on Mn 1 Mn (1 0.01 i) This means we
have to go back and fix the decomposition we have
made earlier
45
Our earlier decomposition
Step 1 Read in the DepositAmount and the
InterestRate. Step 2 n 1. Step 3 While (n
lt 10), repeat steps 4 - 5 Step 4 Calculate and
display the money M you get back after n
years. Step 5 Increase n by 1.
46
Our new decomposition
Step 1 Read in the DepositAmount and the
InterestRate. Step 2 n 1, M
DepositAmount. Step 3 While (n lt 10), repeat
steps 4 - 6 Step 4 Calculate and display the
money Mnew you get back next year using the
formula Mnew M (1 0.01 i). Step 5
Replace M by Mnew. Step 6 Increase n by 1.
47
Reminder
  • General structure of a simple program
  • import javax.swing.
  • public class YourClassName
  • public static void main(String args)
  • Body of your program

This will be replaced by your decomposition
48
Our Java program
import javax.swing. public class
YourClassName public static void main(String
args) Step 1 Read in
DepositAmount and InterestRate. Step 2
n 1, M DepositAmount. Step 3 While
(n lt 10), repeat steps 4 - 6 Step 4
Calculate and display the money Mnew you get back
next year using the
formula Mnew M
(1 0.01 i). Step 5 Replace M by
Mnew. Step 6 Increase n by 1.
49
Reminder
  • How to read an int using dialog windows?
  • Read the string typed by user
  • Extract integer from the string
  • input_string JOptionPane.showInputDialog(String)

Message displayed in input dialog window
number Integer.parseInt(input_string)
50
public static void main(String args)
int DepositAmount, InterestRate String
inputString inputString
JOptionPane.showInputDialog(
Type Deposit amount)
DepositAmount Integer.parseInt(input_string)
Step 1b Read in InterestRate.
Step 2 n 1, M DepositAmount.
Step 3 While (n lt 10), repeat steps 4 - 6
Step 4 Calculate and display the money
Mnew you get back
next year using the formula
Mnew M (1 0.01 i).
Step 5 Replace M by Mnew. Step 6
Increase n by 1.
51
public static void main(String args)
int DepositAmount, InterestRate String
inputString inputString
JOptionPane.showInputDialog(
Type Deposit amount)
DepositAmount Integer.parseInt(input_string)
inputString JOptionPane.showInputDialog(
Type
Interest rate) InterestRate
Integer.parseInt(input_string) Step 2 n
1, M DepositAmount. Step 3 While
(n lt 10), repeat steps 4 - 6 Step 4
Calculate and display the money Mnew you get back
next year using the
formula Mnew M
(1 0.01 i). Step 5 Replace M by
Mnew. Step 6 Increase n by 1.
52
public static void main(String args) int
DepositAmount, InterestRate, n, M, Mnew
String inputString inputString
JOptionPane.showInputDialog(Type Deposit
amount) DepositAmount
Integer.parseInt(input_string)
inputString JOptionPane.showInputDialog(Type
Interest rate) InterestRate
Integer.parseInt(input_string) Step 2 n
1, M DepositAmount. Step 3 While (n
lt 10), repeat steps 4 - 6 Step 4
Calculate and display the money Mnew you
get back next year using the formula
Mnew M (1 0.01 i).
Step 5 Replace M by Mnew. Step 6
Increase n by 1.
Should we use a for loop??
53
public static void main(String args) int
DepositAmount, InterestRate, n, M, Mnew
String inputString inputString
JOptionPane.showInputDialog(Type Deposit
amount) DepositAmount
Integer.parseInt(input_string)
inputString JOptionPane.showInputDialog(Type
Interest rate) InterestRate
Integer.parseInt(input_string) for ( n
1, M DepositAmount n lt 10 n) Step 4
Calculate and display the money Mnew
you get back next year using the formula
Mnew M (1
0.01 i). Step 5 Replace M by Mnew.

54
public static void main(String args) int
DepositAmount, InterestRate, n, M, Mnew
String inputString inputString
JOptionPane.showInputDialog(Type Deposit
amount) DepositAmount
Integer.parseInt(input_string)
inputString JOptionPane.showInputDialog(Type
Interest rate) InterestRate
Integer.parseInt(input_string) for ( n
1, M DepositAmount n lt 10 n)
Mnew M (1 0.01 i). Step 4
Display the money Mnew M Mnew.

55
Reminder
  • To display many lines of text
  • a) Define a JTextArea as follows
  • JTextArea Areaname new JTextArea(NumLines,
    NumChars)
  • b) Append lines of text to the text area by
  • Areaname.append(A string to be shown)
  • c) Instruct the text area to be displayed by
  • JOptionPane.showMessageDialog(null,

  • Areaname)

56
public static void main(String args) int
DepositAmount, InterestRate, n, M, Mnew
String inputString JTextArea OutArea
new JTextArea(15, 20) inputString
JOptionPane.showInputDialog(Type Deposit
amount) DepositAmount
Integer.parseInt(input_string)
inputString JOptionPane.showInputDialog(Type
Interest rate) InterestRate
Integer.parseInt(input_string) for ( n
1, M DepositAmount n lt 10 n)
Mnew M (1 0.01
InterestRate) OutArea.append(n "\t"
Mnew "\n") M Mnew
JOptionPane.showMessageDialog(
null,
OutArea)
We will now compile and run the program!
57
public static void main(String args) int
DepositAmount, InterestRate, n, M, Mnew
String inputString JTextArea OutArea
new JTextArea(15, 20) inputString
JOptionPane.showInputDialog(Type Deposit
amount) DepositAmount
Integer.parseInt(input_string)
inputString JOptionPane.showInputDialog(Type
Interest rate) InterestRate
Integer.parseInt(input_string) for ( n
1, M DepositAmount n lt 10 n)
Mnew M (1 0.01
InterestRate) OutArea.append(n "\t"
Mnew "\n") M Mnew
JOptionPane.showMessageDialog(
null,
OutArea)
Is there a mistake in the program?
Write a Comment
User Comments (0)
About PowerShow.com