340 likes | 472 Views
CS 180 Problem Solving and Object Oriented Programming Fall 2010. http://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/. Notes for Week 2: August 30-September 3, 2010. Aditya Mathur Department of Computer Science Purdue University West Lafayette, IN, USA.
E N D
CS 180 Problem Solving and Object Oriented Programming Fall 2010 http://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/ Notes for Week 2: August 30-September 3, 2010 Aditya Mathur Department of Computer Science Purdue University West Lafayette, IN, USA
Readings and Exercises for Week 2 Readings: Chapter 1: 1.3, 1.4, 1.5, 1.6, 1.7 Chapter 2: 2.1, 2.2, 5.4, 5.5 Exercises: 2.1, 2.3, 2.4, 2.7, 2.9, 2.12, 2.14 CS 180. Fall 2010. Week 2
Feedback for Week 1 CS 180. Fall 2010. Week 2
Q1. The lab exercises were useful (Use scale of 1-10: 10 most useful, 1 not useful at all) 8-10 4-7 1-3 Missed Week 1 lab CS 180. Fall 2010. Week 2
Q2. The recitation exercises were useful (Use scale of 1-10: 10 most useful, 1 not useful at all) 8-10 4-7 1-3 Missed week 1 recitation CS 180. Fall 2010. Week 2
Q3. The recitation instructor was helpful.(Use scale of 1-10: 10 most helpful, 1 not helpful at all) 8-10 4-7 1-3 Missed week 1 recitation CS 180. Fall 2010. Week 2
Q4. I understand the difference between “sequential solution” and “Concurrent solution”. Yes No (c) Not sure (d) Missed week 1 lecture(s) CS 180. Fall 2010. Week 2
Q5. I understand the difference between “Data Parallelism” and “Task parallelism”. Yes No (c) Not sure (d) Missed week 1 lecture(s) CS 180. Fall 2010. Week 2
Q6. So far I am liking the course (10 liking a lot, 1 not liking at all). 8-10 4-7 1-3 Could not attend classes during week 1 CS 180. Fall 2010. Week 2
Dissecting a Java Program: Preliminaries CS 180. Fall 2010. Week 2
The edit, compile, execute cycle .java file(s) .class file(s) (byte code) Correct program No syntax error Edit a Java program Compile your program Execute your program Syntax Error 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. CS 180. Fall 2010. Week 2
Classes and Objects Represent Create Set of real or virtual objects Template in Java Objects created Class Animal Class Flower Class Vehicle Class Dog Class Student animal truck myDog marysDog vehicle student student flower dog CS 180. Fall 2010. Week 2
Classes and Objects Class: Contains properties 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 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.
Java program: Structure Package [Contains one or more classes] Data represents properties of a real, virtual or a Java object. E.g., breed, age, color Class [Data and zero or more methods] Data Methods are operations that can be performed on an object created from a class. E.g., run(), sit(), down(), no(), goodBoy() Method Data and statements At least one class must have a method named main(). CS 180. Fall 2010. Week 2
Java program: Classes and Objects class Automobile make model maxSpeed make model maxSpeed Data create camry make model start() start() Method Data and statements create mazdaRX7 Objects derived from Class Automobile CS 180. Fall 2010. Week 2
Elements of a Sequential Java Program Program to be dissected: Program 1.4 BouncingBall.javain Chapter 1 pages 12-13. 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. CS 180. Fall 2010. Week 2
Elements of a Concurrent Java Program Program to be dissected: Program 1.7 AreaMeasuringRobot.java in Chapter 1 pages 29-30. 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. CS 180. Fall 2010. Week 2
Types Set of values Set of Operations x a x x c b x x CS 180. Fall 2010. Week 2
Primitive types: int, long Set of integers Set of Operations -14 2010 + 12 * - % 1751 180 Integer.MAX_VALUE: 231 -1 Integer.MIN_VALUE: -231 Long.MAX_VALUE: 263 -1 Integer.MIN_VALUE: -263 CS 180. Fall 2010. Week 2
Primitive types: float, double Set of integers Set of Operations (sample) 3.14 2010.98135 == + Infinity * 12.77 - > -Infinity .2010E4 NaN 180.0 -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 CS 180. Fall 2010. Week 2
Primitive types: boolean Set of logical values Set of Operations (sample) == true || && | != false CS 180. Fall 2010. Week 2
Primitive types: char Set of characters (sample values shown) Set of Operations (sample) == ‘a’ || && ‘$’ | != ‘&’ ‘+’ CS 180. Fall 2010. Week 2
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. CS 180. Fall 2010. Week 2
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” CS 180. Fall 2010. Week 2
Named Constants A constant can be named and the name used instead of the constant itself. Examples: final float pi=3.14159; final booleandogsExist=true; CS 180. Fall 2010. Week 2
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. CS 180. Fall 2010. Week 2
Strings: basics A string is any sequence of Unicode characters You may name a string as in the following: StringmyDogsName; 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”? CS 180. Fall 2010. Week 2
Strings: assignment You may assign a value to a string object. Examples follow. myDogsName=“Bently”; StringmyCarColor=“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. CS 180. Fall 2010. Week 2
Strings: Other operations You may apply a variety of operations to strings. Examples follow. String commend=“Bently,”+ “ good girl!; // String catenation StringmyCar=“It’s a Porsche”+ “, and I love it!” +”but maintenance is expensive.” // String catenation StringfirstChar=commend.charAt(0); // Extract character at position 0 CS 180. Fall 2010. Week 2
Strings: Other operations You may apply a variety of operations to strings. Examples follow. CS 180. Fall 2010. Week 2
Declarations int age; float height, area; String name boolean intx=1, y=0; StringfirstName=“Harry”; CS 180. Fall 2010. Week 2
Simple expressions Expressions are used to compute “something”. floatx, y, z; x*y+z; // Arithmetic expression, results in float value x<y; // Boolean expression, results in boolean value StringfirstName=“Mary”, lastName= “Jones”; firstName+” “+lastName; // Results in a string More in Chapter 2! And yet more to come! CS 180. Fall 2010. Week 2
Assignment statement An assignment statement allows assigning the value of an expression to a variable. floatp=x*y+z; // p gets the value of x*y+z booleanq=x<y; // q gets the value of x<y StringfirstName=“Mary”, lastName= “Jones”; String name= firstName+” “+lastName; More in Chapter 2! And yet more to come! CS 180. Fall 2010. Week 2
Week 2: August 30-September 3, 2010 Hope you enjoyed this week! Questions? Contact your recitation instructor. Make full use of our office hours. CS 180. Fall 2010. Week 2