NotesWeek3.Sept 6-10..

advertisement
CS 180 Problem Solving and Object Oriented
Programming
Fall 2010
http://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/
Notes for Week 3:
September 6-10, 2010
Aditya Mathur
Department of Computer Science
Purdue University
West Lafayette, IN, USA
This week:
1.
2.
3.
4.
Feedback for Week 2
Review
Primitive types
Writing simple programs
to solve simple problems
Readings and Exercises for Week 3
Readings:
Chapter 1: 1.7
Chapter 2: 2.1, 2.2, 2.3, 2.4
Exercises:
2.18, 2.19, 2.20
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
2
Special help sessions
Sundays: 2-4pm LWSN B158
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
3
Feedback for Week 2
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
4
Q1.
The lab exercises were useful
(Use scale of 1-10: 10 most useful, 1 not
useful at all)
(a) 8-10
(b) 4-7
(c) 1-3
(d) Missed Week 2 lab
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
5
Q2.
The recitation exercises were useful
(Use scale of 1-10: 10 most useful, 1 not
useful at all)
(a) 8-10
(b) 4-7
(c) 1-3
(d) Missed week 2 recitation
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
6
Q3.
The recitation instructor was helpful.
(Use scale of 1-10: 10 most helpful, 1 not
helpful at all)
(a) 8-10
(b) 4-7
(c) 1-3
(d) Missed week 2 recitation
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
7
Q4.
I understand how to assign a string to a
string variable.
(a) 8-10
(b) 4-7
(c) 1-3
(d) Missed week 2 lecture
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
8
Q5.
I understand the need for import statements.
(a) 8-10
(b) 4-7
(c) 1-3
(d) Missed week 2 lecture
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
9
Q6.
So far I am liking the course (10 liking a lot,
1 not liking at all).
(a) 8-10
(b) 4-7
(c) 1-3
(d) Missed week 2 lecture
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
10
Review
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
11
The edit, compile, execute cycle
.java file(s)
Edit a
Java program
.class file(s)
(byte code)
No syntax
Compile your error
Execute your
program
program
Syntax
Error
Correct
program
Run time
Error or
Incorrect Output
In CS 180 we shall use DrJava for editing, compiling and execution. DrJava is an
Integrated Development Environment also known as an IDE. Eclipse, JBuilder, and
IntelliJ IDEA are a few other Java IDEs. For programming the RidgeSoft robot we
shall use RoboJDE.
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
12
Classes and Objects
Set of real or
virtual objects
Represent
Template
in Java
Create
Objects
created
Class Animal
animal
Class Vehicle
truck
Class Student
student
vehicle
student
flower
dog
Class Flower
myDog
Class Dog
marysDog
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
13
String
Is a sequence of zero or more Unicode characters.
Examples:
“Greetings!”
“Your total tax is:”
“Please enter the price:”
““
“”
Declaration:
String name=“Beatles”; // name is an object of type String
String store=“Macy’s”; // store is an object of type String
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
14
String
Expressions:
String firstName, middleInitial, lastName;
String fullName;
fullName=firstName+middleInitial+lastName;
String message=“Please enter the price:”;
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
15
Strings: Other operations
You may apply a variety of operations to strings. Examples follow.
Statement
Operation used
String commend=“Bently,”+ “ good girl!”;
Catenation
char firstChar=commend.charAt(0);
Character
extraction
movieName.equals(“Fugitive”)
Comparison
String.valueOf(29)
Conversion to
String
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
16
Types
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
17
Types
Set of values
x
Set of Operations
a
b
9/8/2010
c
©Aditya Mathur. CS 180. Fall 2010. Week 3
18
Primitive types: short, int, long
Set of integers
2010
+
12
-
-14
180
9/8/2010
Set of Operations
*
%
short: 2 bytes
int: 4 bytes
long: 8 bytes
1751
Integer.MAX_VALUE: 231 -1
Integer.MIN_VALUE: -231
Long.MAX_VALUE: 263 -1
Integer.MIN_VALUE: -263
©Aditya Mathur. CS 180. Fall 2010. Week 3
19
Primitive types: short, int, long: Examples
Real world entity or expression
Type
Possible name in Java
Population of a country
int
countryPopulation
Age of a patient (in years)
short
patientAge
Number of different ways to
arrange 15 books in a bookshelf
long
bookArrangementCount
Difference between two integers
int or
long
diff
Number of web sites
long
numberOfWebSites
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
20
Primitive types: float, double
Set of integers
Set of Operations
(sample)
2010.98135
12.77
Infinity
-
3.14
*
-Infinity
.2010E4
180.0
==
+
float: 4 bytes
double: 8 bytes
>
NaN
-1751.0
Float.MAX_VALUE: 3.40282347e+38f
Float.MIN_VALUE: 1.40239846e-45f
Double.MAX_VALUE: 1.79769313486231570e+308
Double.MIN_VALUE: 4.94065645841246544e-324
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
21
Primitive types: float, double: Examples
Real world entity or expression
Type
Possible name in a Java
program
Height of a person
float
height
Voting percentage
float
votePercent
Wavelength of green light
double wavelengthLIght
Price of a ticket
float
π
double pi (Note: PI is a constant
in Java)
9/8/2010
ticketPrice
©Aditya Mathur. CS 180. Fall 2010. Week 3
22
Primitive types: boolean
Set of logical values
Set of Operations
(sample)
==
true
||
false
|
&&
!=
boolean: 1 bit; size not defined
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
23
Primitive types: boolean: Examples
Real world entity or expression
Type
Possible name in a Java
program
Value of x<y;
boolean
result
she/he drives a car
boolean
canDriveCar
Class ended
boolean
classEnded
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
24
Primitive types: char
Set of characters
(sample values shown)
Set of Operations
(sample)
==
‘a’
||
‘$’
‘&’
|
&&
!=
‘+’
char: 2 bytes, unicode character
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
25
Primitive types: char: Examples
Real world entity or expression
Type
Possible name in a Java
program
Middle initial
char
middleInitial
Letter of the alphabet
char
letter
US currency sign
char
usCurrency
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
26
Names
Used to denote classes, objects, data
Contain characters; must start with a letter, or a $
sign or an underscore.
Examples: height, area1, Dog, $great
Length unlimited, case sensitive.
Dog and dog are different names.
Convention: All class names begin with an
uppercase letter; all other names begin with
a lower case letter.
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
27
Constants
A constant is something that cannot change during program
execution.
Examples:
Integer constants: 0, 1, -1, +24, 29, 300009998, O14, 0x1B
Floating point constants: 0.0, -2.345e28, -0.000976512
Boolean constants: true, false
Character constants: ‘ ‘, ‘a’, ‘A’, ‘$’
String constants: “”, “ “, “Hi!”, “Alice in Wonderland”
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
28
Named Constants
A constant can be named and the name used instead of the
constant itself.
Examples:
final float pi=3.14159;
final boolean dogsExist=true;
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
29
Variables
A variable is something whose value may change during
program execution.
Every variable has a name and a type.
Every variable must be declared before it is used.
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
30
Declarations
int age;
float height, area;
String name
boolean
int x=1, y=0;
String firstName=“Harry”;
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
31
Simple expressions
Expressions are used to compute “something”.
float x, y, z;
x*y+z; // Arithmetic expression, results in float value
x<y; // Boolean expression, results in boolean value
String firstName=“Mary”, lastName= “Jones”;
firstName+” “+lastName; // Results in a string
More in Chapter 2! And yet more to come!
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
32
Assignment statement
An assignment statement allows assigning the value of an expression
to a variable.
float p=x*y+z; // p gets the value of x*y+z
boolean q=x<y; // q gets the value of x<y
String firstName=“Mary”, lastName= “Jones”;
String name= firstName+” “+lastName;
More in Chapter 2! And yet more to come!
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
33
Let us write a simple Java program:
The problem
Write a Java program to compute the
net sale in dollars given the price of
each ticket in dollars and the number
of tickets sold.
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
34
Problem: Understanding
This is an easy problem!
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
35
Problem: Design of solution
Keep this step independent of Java!
Step 1: Get data
Step 2: Compute total sale
Step 3: Display total sale
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
36
Problem: Refine solution: Get data
Keep this step independent of Java!
Step 1.1: Prompt for price
Step 1.2: Read price
Step 1.3: Prompt for number of tickets sold
Step 1.4: Read number of tickets sold
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
37
Problem: Refine solution: Compute total sale
Keep this step independent of Java!
Step 2: Total sale=price of a ticket * number of
tickets sold
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
38
Code the solution in Java.
Test it.
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
39
Week 3: September 3-10, 2010
Hope you enjoyed this week!
Questions?
Contact your recitation instructor. Make
full use of our office hours.
9/8/2010
©Aditya Mathur. CS 180. Fall 2010. Week 3
40
Download