Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 3: September 6-10, 2010 Aditya Mathur Department of Computer Science Purdue.

Similar presentations


Presentation on theme: "CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 3: September 6-10, 2010 Aditya Mathur Department of Computer Science Purdue."— Presentation transcript:

1 CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 3: September 6-10, 2010 Aditya Mathur Department of Computer Science Purdue University West Lafayette, IN, USA http://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/ This week: 1.Feedback for Week 2 2.Review 3.Primitive types 4.Writing simple programs to solve simple problems

2 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 2©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010

3 Special help sessions Sundays: 2-4pm LWSN B158 3©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010

4 Feedback for Week 2 4©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010

5 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 5©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010

6 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 6©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010

7 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 7©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010

8 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 8©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010

9 Q5. I understand the need for import statements. (a)8-10 (b)4-7 (c)1-3 (d)Missed week 2 lecture 9©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010

10 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 10©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010

11 Review 11©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010

12 The edit, compile, execute cycle 12©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 Edit a Java program Compile your program Execute your program Syntax Error Run time Error or Incorrect Output No syntax error Correct program 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..java file(s).class file(s) (byte code)

13 Classes and Objects 13©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 Set of real or virtual objects animal vehicle student flower Class AnimalClass VehicleClass StudentClass Flower Template in Java dog Class DogmyDogmarysDog Objects created truckstudent RepresentCreate

14 String 14©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 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

15 String 15©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 Expressions: String firstName, middleInitial, lastName; String fullName; fullName=firstName+middleInitial+lastName; String message=“Please enter the price:”;

16 Strings: Other operations You may apply a variety of operations to strings. Examples follow. 16©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 StatementOperation used String commend=“Bently,”+ “ good girl!”;Catenation char firstChar=commend.charAt(0);Character extraction movieName.equals(“Fugitive”)Comparison String.valueOf(29)Conversion to String

17 Types 17©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010

18 Types 18©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 Set of valuesSet of Operations x x x x x a b c

19 Primitive types: short, int, long 19©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 -14 Set of integersSet of Operations 12 + - * 180 2010 1751 % Integer.MAX_VALUE: 2 31 -1Integer.MIN_VALUE: -2 31 Long.MAX_VALUE: 2 63 -1Integer.MIN_VALUE: -2 63 short: 2 bytes int: 4 bytes long: 8 bytes

20 Primitive types: short, int, long: Examples 20©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 Real world entity or expressionTypePossible name in Java Population of a countryintcountryPopulation Age of a patient (in years)shortpatientAge Number of different ways to arrange 15 books in a bookshelf longbookArrangementCount Difference between two integersint or long diff Number of web siteslongnumberOfWebSites

21 Primitive types: float, double 21©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 3.14 Set of integersSet of Operations (sample) 12.77 + - * 180.0 2010.98135 -1751.0 > Float.MAX_VALUE: 3.40282347e+38fFloat.MIN_VALUE: 1.40239846e-45f Double.MAX_VALUE: 1.79769313486231570e+308 Double.MIN_VALUE: 4.94065645841246544e-324.2010E4 == Infinity -Infinity NaN float: 4 bytes double: 8 bytes

22 Primitive types: float, double: Examples 22©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 Real world entity or expressionTypePossible name in a Java program Height of a personfloatheight Voting percentagefloatvotePercent Wavelength of green lightdoublewavelengthLIght Price of a ticketfloatticketPrice πdoublepi (Note: PI is a constant in Java)

23 Primitive types: boolean 23©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 Set of logical valuesSet of Operations (sample) != == true false || && | boolean: 1 bit; size not defined

24 Primitive types: boolean: Examples 24©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 Real world entity or expressionTypePossible name in a Java program Value of x<y;booleanresult she/he drives a carbooleancanDriveCar Class endedbooleanclassEnded

25 Primitive types: char 25©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 Set of characters (sample values shown) Set of Operations (sample) != == ‘a’ ‘&’ || && | ‘$’ ‘+’ char: 2 bytes, unicode character

26 Primitive types: char: Examples 26©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 Real world entity or expressionTypePossible name in a Java program Middle initialcharmiddleInitial Letter of the alphabetcharletter US currency signcharusCurrency

27 Names 27©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 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.

28 Constants 28©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 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”

29 Named Constants 29©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 A constant can be named and the name used instead of the constant itself. Examples: final float pi=3.14159; final boolean dogsExist=true;

30 Variables 30©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 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.

31 Declarations 31©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 int age; float height, area; String name boolean int x=1, y=0; String firstName=“Harry”;

32 Simple expressions 32©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 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!

33 Assignment statement 33©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 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!

34 Let us write a simple Java program: The problem 34©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 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.

35 Problem: Understanding 35©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 This is an easy problem!

36 Problem: Design of solution 36©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 Keep this step independent of Java! Step 1: Get data Step 2: Compute total sale Step 3: Display total sale

37 Problem: Refine solution: Get data 37©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 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

38 Problem: Refine solution: Compute total sale 38©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010 Keep this step independent of Java! Step 2: Total sale=price of a ticket * number of tickets sold

39 Code the solution in Java. Test it. 39©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010

40 Week 3: September 3-10, 2010 Hope you enjoyed this week! Questions? Contact your recitation instructor. Make full use of our office hours. 40©Aditya Mathur. CS 180. Fall 2010. Week 39/8/2010


Download ppt "CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 3: September 6-10, 2010 Aditya Mathur Department of Computer Science Purdue."

Similar presentations


Ads by Google