NotesWeek2.Aug 30-Sept 3

NotesWeek2.Aug 30-Sept 3 - CS 180 Problem Solving and...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 180 Problem Solving and Object Oriented Programming Fall 2010 hLp://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/ Notes for Week 2: August 30September 3, 2010 Aditya Mathur Department of Computer Science Purdue University West LafayeLe, 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 8/30/10 CS 180. Fall 2010. Week 2 2 Feedback for Week 1 8/30/10 CS 180. Fall 2010. Week 2 3 Q1. The lab exercises were useful (Use scale of 110: 10 most useful, 1 not useful at all) (a) 810 (b) 47 (c) 13 (d) Missed Week 1 lab 8/30/10 CS 180. Fall 2010. Week 2 4 Q2. The recita]on exercises were useful (Use scale of 110: 10 most useful, 1 not useful at all) (a) 810 (b) 47 (c) 13 (d) Missed week 1 recita]on 8/30/10 CS 180. Fall 2010. Week 2 5 Q3. The recita]on instructor was helpful. (Use scale of 110: 10 most helpful, 1 not helpful at all) (a) 810 (b) 47 (c) 13 (d) Missed week 1 recita]on 8/30/10 CS 180. Fall 2010. Week 2 6 Q4. I understand the difference between "sequen]al solu]on" and "Concurrent solu]on". (a) Yes (b) No (c) Not sure (d) Missed week 1 lecture(s) 8/30/10 CS 180. Fall 2010. Week 2 7 Q5. I understand the difference between "Data Parallelism" and "Task parallelism". (a) Yes (b) No (c) Not sure (d) Missed week 1 lecture(s) 8/30/10 CS 180. Fall 2010. Week 2 8 Q6. So far I am liking the course (10 liking a lot, 1 not liking at all). (a) 810 (b) 47 (c) 13 (d) Could not aLend classes during week 1 8/30/10 CS 180. Fall 2010. Week 2 9 Dissec]ng a Java Program: Preliminaries 8/30/10 CS 180. Fall 2010. Week 2 10 The edit, compile, execute cycle .java file(s) .class file(s) (byte code) Correct No syntax program error Compile your Execute your program program Syntax Error Run ]me Error or Incorrect Output Edit a Java program In CS 180 we shall use DrJava for edi]ng, compiling and execu]on. 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 RidgeSoe robot we shall use RoboJDE. 8/30/10 CS 180. Fall 2010. Week 2 11 Classes and Objects Set of real or virtual objects Represent Template in Java Create Objects created Class Animal animal vehicle student Class Student flower dog Class Flower myDog student Class Vehicle truck Class Dog marysDog 8/30/10 CS 180. Fall 2010. Week 2 12 Classes and Objects Class: Contains proper]es and opera]ons 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 proper]es and opera]ons 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] Class [Data and zero or more methods] Data Data represents proper]es of a real, virtual or a Java object. E.g., breed, age, color Methods are opera]ons that can be performed on an object created from a class. E.g., run(), sit(), down(), no(), goodBoy() 14 Method Data and statements At least one class must have a method named main(). 8/30/10 CS 180. Fall 2010. Week 2 Java program: Classes and Objects class Automobile Data create camry make model maxSpeed start() make model Method Data and statements create mazdaRX7 make model maxSpeed start() Objects derived from Class Automobile 8/30/10 CS 180. Fall 2010. Week 2 15 Elements of a Sequen]al Java Program Program to be dissected: Program 1.4 BouncingBall.java in Chapter 1 pages 1213. Strategy: Go through this program line by line and aLempt to understand the meaning of each line. It is likely this exercise will generate more ques]ons than answers. 8/30/10 CS 180. Fall 2010. Week 2 16 Elements of a Concurrent Java Program Program to be dissected: Program 1.7 AreaMeasuringRobot.java in Chapter 1 pages 2930. Strategy: Go through this program line by line and aLempt to understand the meaning of each line. It is likely this exercise will generate more ques]ons than answers. 8/30/10 CS 180. Fall 2010. Week 2 17 Types Set of values Set of Opera]ons x a b c 8/30/10 CS 180. Fall 2010. Week 2 18 Primi]ve types: int, long Set of integers Set of Opera]ons 12 2010 14 + * % 180 1751 Integer.MAX_VALUE: 231 1 Long.MAX_VALUE: 263 1 8/30/10 Integer.MIN_VALUE: 231 Integer.MIN_VALUE: 263 19 CS 180. Fall 2010. Week 2 Primi]ve types: float, double Set of integers 2010.98135 12.77 Infinity 3.14 Infinity NaN + == * > Set of Opera]ons (sample) .2010E4 180.0 1751.0 Float.MAX_VALUE: 3.40282347e+38f Float.MIN_VALUE: 1.40239846e45f Double.MAX_VALUE: 1.79769313486231570e+308 Double.MIN_VALUE: 4.94065645841246544e324 8/30/10 CS 180. Fall 2010. Week 2 20 Primi]ve types: boolean Set of logical values Set of Opera]ons (sample) == || false | && != true 8/30/10 CS 180. Fall 2010. Week 2 21 Primi]ve types: char Set of characters (sample values shown) Set of Opera]ons (sample) `a' `&' `+' `$' == || | && != 8/30/10 CS 180. Fall 2010. Week 2 22 Names Used to denote classes, objects, data Contain characters; must start with a leLer, or a $ sign or an underscore. Examples: height, area1, Dog, $great Length unlimited, case sensi]ve. Dog and dog are different names. Conven]on: All class names begin with an uppercase leLer; all other names begin with a lower case leLer. 8/30/10 CS 180. Fall 2010. Week 2 23 Constants A constant is something that cannot change during program execu]on. Examples: Integer constants: 0, 1, 1, +24, 29, 300009998, O14, 0x1B Floa]ng point constants: 0.0, 2.345e28, 0.000976512 Boolean constants: true, false Character constants: ` `, `a', `A', `$' String constants: "", " ", "Hi!", "Alice in Wonderland" 8/30/10 CS 180. Fall 2010. Week 2 24 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/30/10 CS 180. Fall 2010. Week 2 25 Variables A variable is something whose value may change during program execu]on. Every variable has a name and a type. Every variable must be declared before it is used. 8/30/10 CS 180. Fall 2010. Week 2 26 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/30/10 CS 180. Fall 2010. Week 2 27 Strings: assignment You may assign a value to a string object. Examples follow. myDogsName="Bently"; 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/30/10 CS 180. Fall 2010. Week 2 28 Strings: Other opera]ons You may apply a variety of opera]ons to strings. Examples follow. String commend="Bently,"+ " good girl!; // String catena]on String myCar="It's a Porsche"+ ", and I love it!" +"but maintenance is expensive." // String catena]on String firstChar=commend.charAt(0); // Extract character at posi]on 0 8/30/10 CS 180. Fall 2010. Week 2 29 Strings: Other opera]ons You may apply a variety of opera]ons to strings. Examples follow. Statement Opera+on used String commend="Bently,"+ " good girl!"; Catena]on char firstChar=commend.charAt(0); movieName.equals("Fugi]ve") String.valueOf(29) Character extrac]on Comparision Conversion to String 8/30/10 CS 180. Fall 2010. Week 2 30 Declara]ons int age; float height, area; String name boolean int x=1, y=0; String firstName="Harry"; 8/30/10 CS 180. Fall 2010. Week 2 31 Simple expressions Expressions are used to compute "something". float x, y, z; x*y+z; // Arithme]c 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/30/10 CS 180. Fall 2010. Week 2 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! 8/30/10 CS 180. Fall 2010. Week 2 33 Week 2: August 30September 3, 2010 Hope you enjoyed this week! Ques]ons? Contact your recita]on instructor. Make full use of our office hours. 8/30/10 CS 180. Fall 2010. Week 2 34 ...
View Full Document

Ask a homework question - tutors are online