CS 180 Problem Solving and OO Programming Fall 2010

Download Report

Transcript CS 180 Problem Solving and OO Programming Fall 2010

CS 180 Problem Solving and Object Oriented
Programming
Fall 2011
http://www.cs.purdue.edu/homes/apm/courses/CS180Fall2011/
Notes for Week 2:
August 29-September 2, 2011
Aditya Mathur
Department of Computer Science
Purdue University
West Lafayette, IN, USA
Readings and Self-help Exercises for Week 2
Readings:
Chapter 2: 2.1, 2.2, 2.3, 2.4
Self help exercises (not to be turned in):
2.1, 2.2, 2.3, 2.4, 2.7, 2.9, 2.12, 2.14
8/29/2011
CS 180. Fall 2011. Week 2
2
About Homework
All homework problems will be assigned during recitation.
Please make sure you attend your recitation section.
Homework assigned during week X is due at the start of your
recitation during week X+1.
It is best to use Piazza to ask questions regarding homework. But
please do not post answers to homework problems.
Most, but not necessarily all, homework problems will be from
the textbook.
8/29/2011
CS 180. Fall 2011. Week 2
3
Lab for Week 2
Play with Java programs:
Introduction to Android-based smart phone
Introduction to the Finch robot
8/29/2011
CS 180. Fall 2011. Week 2
4
Feedback for Week 1
8/29/2011
CS 180. Fall 2011. Week 2
5
Q1. I understand the difference between
“sequential solution” and “Concurrent
solution”.
(a) Yes
(b) No
(c) Not sure
(d) Missed week 1 lecture (s)
8/29/2011
CS 180. Fall 2011. Week 2
6
Q2. I understand the difference between
“Data Parallelism” and “Task parallelism”.
(a) Yes
(b) No
(c) Not sure
(d) Missed week 1 lecture(s)
8/29/2011
CS 180. Fall 2011. Week 2
7
Dissecting a Java Program:
Preliminaries
8/29/2011
CS 180. Fall 2011. Week 2
8
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.
8/29/2011
CS 180. Fall 2011. Week 2
9
Classes and Objects
Set of real or
virtual objects
Represent
Template
in Java
Create
Objects
created
Class Animal
animal
Class Car
momsCar
Class Student
amanda
car
student
flower
dog
Class Flower
bruschetta
Class Dog
ziggy
8/29/2011
CS 180. Fall 2011. Week 2
10
Classes and Objects
Class:
Contains attributes and operations related to some real or virtual object.
This object could be abstract or concrete [e.g., a Dog or a Golden Retriever].
Object:
Created from a class.
Contains specialized properties (attributes) and operations related
to a more specific real or virtual object, e.g., object myDog created
from class Dog has breed as a property that might be different from
another object marysDog created from the same class.
8/29/2011
CS 180. Fall 2011. Week 2
11
Java program: Structure
Package [Contains one or more classes]
Class [Data and zero or more methods]
Data (attributes)
Method
Data and
statements
At least one class must have a
method named main().
8/29/2011
CS 180. Fall 2011. Week 2
Data represents
properties of a real,
virtual or a Java
object. E.g., breed,
age, color
Methods are
operations that can
be performed on an
object created from a
class.
E.g., run(), sit(),
down(), no(),
goodBoy()
12
Java program: Classes and Objects
class Automobile
Data
make
model
create camry
make
model
maxSpeed
start()
Method
Data and
statements
create mazdaRX7
make
model
maxSpeed
start()
Objects derived from
Class Automobile
8/29/2011
CS 180. Fall 2011. Week 2
13
Elements of a Sequential Java Program
For practice (try later):
Programs to be dissected:
Program 2.6 ScannerExample.java Chapter 2 pages 55-56.
Program 2.7 BouncingBall.java in Chapter 2 pages 58-59.
Strategy:
Go through this program line by line and attempt to
understand the meaning of each line. It is likely this
exercise will generate more questions than answers.
8/29/2011
CS 180. Fall 2011. Week 2
14
Elements of a Concurrent Java Program
For practice (try later):
Program to be dissected:
Program 2.8 AreaMeasuringRobot.java in Chapter 2 pages 63-64.
Strategy:
Go through this program line by line and attempt to understand
the meaning of each line. It is likely this exercise will generate
more questions than answers.
8/29/2011
CS 180. Fall 2011. Week 2
15
Announcements
Feast with faculty tonight: 6:30pm in Ford Dining Hall, room on the second
floor.
Lab makeup policy at the course web site.
This week’s recitation: you will learn about the Scanner class and
how to map a problem to a Java program. Make sure you attend!
Project 1 description will be released via the Schedule page of the
course site on Sunday (a day before the announced date).
Is Java a pure OO language:? Use Google to find an answer.
8/29/2011
CS 180. Fall 2011. Week 2
16
Binary numbers and Floating point
representation
“I hate floating point and IEEE format!!”
Knowledge useful in
CS 240, CS 314, and several other classes.
Writing programs for engine control, satellite control, aircraft
design, integration, etc. etc.
Understanding why the results of a computation are not what
you expected.
8/29/2011
CS 180. Fall 2011. Week 2
17
Types
Set of values
Set of Operations
a
x
b
8/29/2011
CS 180. Fall 2011. Week 2
c
18
Primitive types: int, long
Set of integers
Set of Operations
2010
+
12
-
-14
180
8/29/2011
/
*
%
1751
Integer.MAX_VALUE: 231 -1
Integer.MIN_VALUE: -231
Long.MAX_VALUE: 263 -1
Long.MIN_VALUE: -263
CS 180. Fall 2011. Week 2
19
Primitive types: float, double
Set of real numbers
Set of Operations
(sample)
2010.98135
12.77
+
Infinity
3.14
-Infinity
.2010E4
180.0
-
==
*
>=
>
NaN
-1751.0
Float.MAX_VALUE: 3.40282347e+38
Float.MIN_VALUE: 1.40239846e-45
Double.MAX_VALUE: 1.79769313486231570e+308
Double.MIN_VALUE: 4.94065645841246544e-324
8/29/2011
CS 180. Fall 2011. Week 2
20
Primitive types: boolean
Set of logical values
==
true
||
false
8/29/2011
Set of Operations
(sample)
|
CS 180. Fall 2011. Week 2
&&
!=
21
Primitive types: char
Set of characters
(sample values shown)
Set of Operations
(sample)
==
‘a’
||
‘$’
‘&’
|
&&
!=
‘+’
8/29/2011
CS 180. Fall 2011. Week 2
22
Operators: Arithmetic, relational, conditional
Arithmetic operators
+
*
/
%
a+b*c-d
a/b
c%d
8/29/2011
Relational operators
==
<
>
<=
>=
!=
Boolean/conditional
operators
||
&&
a==b
a<=b
a!=b
a==b||c<d
a<=b&&c>d
a!=b &&c>d||p+1<q
CS 180. Fall 2011. Week 2
23
Operators: bitwise
Bitwise operators
&: bitwise AND
|: bitwise OR
^: bitwise exclusive OR
~: bitwise complement
Bitwise shift operators
<<: bitwise left shift
>>: bitwise right shift
>>>: unsigned right shift
a & b: logical and of a and b
a|b: logical OR of a and b
a <<3: shift bit pattern of a left by 3 bits
a>>2: shift bit pattern of a to the right by 2 bits
8/29/2011
CS 180. Fall 2011. Week 2
24
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.
8/29/2011
CS 180. Fall 2011. Week 2
25
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”
8/29/2011
CS 180. Fall 2011. Week 2
26
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;
8/29/2011
CS 180. Fall 2011. Week 2
27
Variables
A variable is something whose value may change during
program execution.
Example: int numStudents; denotes the number of
students whose grads have been processed. Its value
changes as each student’s grade is processed by a grade
processing program.
Every variable has a name and a type.
Example: int hurricaneCategory; The name is
hurricaneCategory and its type is int.
Every variable must be declared before it is used.
8/29/2011
CS 180. Fall 2011. Week 2
28
Strings: basics
A string is any sequence of Unicode characters
You may name a string as in the following:
String myDogsName;
myDogsName is an object of type String.
It can take any string as its value. For example,
“Max”, “Bently”, “Jake” and “Raja” are possible values of
myDogsName.
What is the difference between 29 and “29”?
8/29/2011
CS 180. Fall 2011. Week 2
29
Strings: assignment
You may assign a value to a string object.
myDogsName=“Bently”; // Assuming that myDogsName has been declared
String myCarColor=“Black”;
All string objects must be declared before they are used.
Thus it would be incorrect to assign a value to myDogsName before
it has been declared.
8/29/2011
CS 180. Fall 2011. Week 2
30
Strings: Other operations
You may apply a variety of operations to strings. Examples follow.
String commend=“Bently,”+ “ good girl!; // String catenation
String myCar=“It’s a Porsche”+ “, and I love it!” +”but
maintenance is expensive.” // String catenation
String firstChar=commend.charAt(0); // Extract character at
position 0
8/29/2011
CS 180. Fall 2011. Week 2
31
Strings: More operations
There exist a variety of operations on strings. A few are given below.
Statement
Operation used
String commend=“Bently,”+ “ good girl!”;
Catenation
char firstChar=commend.charAt(0);
Character extraction using charAt()
movieName.equals(“Fugitive”)
Comparison using equals()
String.valueOf(29)
Conversion integer 29 to String “29”
8/29/2011
CS 180. Fall 2011. Week 2
32
Declarations
int age;
float height, area;
String name
boolean iAmAlive;
int x=1, y=0;
String firstName=“Harry”;
8/29/2011
CS 180. Fall 2011. Week 2
33
Simple expressions
Expressions are used to compute “something”.
float x, y, z; // Declare x, y, z as variables of type float
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!
8/29/2011
CS 180. Fall 2011. Week 2
34
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!
8/29/2011
CS 180. Fall 2011. Week 2
35
Back to classes and objects
8/29/2011
CS 180. Fall 2011. Week 2
36
Creating an object: Mom’s car
new Car (“Yellow”, “Mom”, 200);
public class Car{
String color;
String owner;
int miles;
String color=“Yellow”;
String owner=“Mom”;
int miles=200;
public int getMiles(){
}
8/29/2011
CS 180. Fall 2011. Week 2
37
Creating an object: Dad’s car
new Car (“Black”, “Dad”, 200000);
public class Car{
String color;
String owner;
int miles;
String color=“Black”;
String owner=“Dad”;
int miles=200000;
public int getMiles(){
}
8/29/2011
CS 180. Fall 2011. Week 2
38
Creating an object: Instance variables
public class Car{
String color;
String owner;
int miles;
Instance variables are copied into the new object.
Each object has its own values for these variables.
Thus, two objects of the same type have
their own copies of instance variables.
public int getMiles(){
}
8/29/2011
CS 180. Fall 2011. Week 2
39
Creating an object: get and set instance
variables
public class Car{
String color;
String owner;
int miles;
public int getMiles(){
return miles;
}
public void setMiles(int m ){
miles=m;
}
8/29/2011
Get the value of miles from the object.
int dadsCarMIles=dadsCar.getMiles();
Set the value of miles in an object.
momsCar.setMiles(300);
CS 180. Fall 2011. Week 2
40
Classes and Objects: Summary
Class:
Template to create objects.
Object:
Created from a class; may denote a real world or an abstract object;
Inherits all instance variables and methods.
Instance
variables:
Variables that become local to an object.
Used for getting data from and into an object.
get and
set methods
Constructor
8/29/2011
Used for creating objects.
CS 180. Fall 2011. Week 2
41
Week 2: August 29-September 2, 2011
Hope you enjoyed this week!
Questions?
Contact your recitation instructor. Make
full use of our office hours.
8/29/2011
CS 180. Fall 2011. Week 2
42