Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 4: September 12-16, 2011 Aditya Mathur/Tim Korb Department of Computer.

Similar presentations


Presentation on theme: "CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 4: September 12-16, 2011 Aditya Mathur/Tim Korb Department of Computer."— Presentation transcript:

1 CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 4: September 12-16, 2011 Aditya Mathur/Tim Korb Department of Computer Science Purdue University West Lafayette, IN, USA http://www.cs.purdue.edu/homes/apm/courses/CS180Fall2011/ This week: 1.Review 2.Types: conversion 3.Type mismatch 4.Writing simple programs to solve simple problems 5.Random numbers 6.Exceptions: Overflow/underflow 7.Concurrency: task level

2 Readings and Exercises for Week 4 Readings: Chapter 3: All Exercises: 3.14, 3.15, 3.19, 3.20 9/14/2011©Aditya Mathur. CS 180. Fall 2011. Week 4

3 Quiz 9/14/2011©Aditya Mathur. CS 180. Fall 2011. Week 4

4 Q1. In p = 1.2 * 3, the type of p must be a float or else Java will announce an error. (a)true (b)false 9/14/2011©Aditya Mathur. CS 180. Fall 2011. Week 4 Correct answer: b (type can also be double)

5 Q2. If int p = 5 % 3, then the value of p is, (a)2 (b)1 (c)0 9/14/2011©Aditya Mathur. CS 180. Fall 2011. Week 4 Correct answer: a

6 Q3. If int p = 5 / 3, then the value of p is, (a)2 (b)1 (c)0 9/14/2011©Aditya Mathur. CS 180. Fall 2011. Week 4 Correct answer: b

7 Q4. If float p = 5 / 3, then the value of p is, (a)2.0 (b)1.0 (c)0.0 (d)1.666666 9/14/2011©Aditya Mathur. CS 180. Fall 2011. Week 4 Correct answer: b

8 Q5. In p = 2 * 3, the type of p must be an int or long. (a)true (b)false 9/14/2011©Aditya Mathur. CS 180. Fall 2011. Week 4 Correct answer: b (can be float also)

9 “There are 10 types of people in the world: Those who understand binary and those who don’t.” Q6: This joke is funny because: (a)It fails to state what the other 8 types are (b)10 is hexadecimal for 16 (c)10 is binary for 2 (d)Technical jokes are inherently funny (e)Both (c) and (d) 9/14/2011©Aditya Mathur. CS 180. Fall 2011. Week 4 Correct answer: c

10 Announcements 9/14/2011©Aditya Mathur. CS 180. Fall 2011. Week 4 Feast with faculty: Today at 6:30pm in Ford Dining Hall. Somewhere on the second floor. Thursday 12:30, 1:30 and 4:30: an additional instructor has been assigned starting next week (week 5). End of week 4: What should you be able to do with Java? Homework and lab grading: make sure you understand why any points have been taken off.

11 Review 9/14/2011©Aditya Mathur. CS 180. Fall 2011. Week 4

12 Java program structure 9/14/2011 public class Class1{ } ©Aditya Mathur. CS 180. Fall 2011. Week 4 A Java program consists of one or more classes. public class Class2{ } public class ClassN{ }

13 Class 9/14/2011 public class ClassName{ } ©Aditya Mathur. CS 180. Fall 2011. Week 4 A class is a template for creating objects.

14 Class: Constructor 9/14/2011 public class ClassName{ public ClassName ( ){ } ©Aditya Mathur. CS 180. Fall 2011. Week 4 A constructor is used for creating new objects from a class.

15 Class: Constructor: Parameters 9/14/2011 public class ClassName{ public ClassName ( parameters ){ } ©Aditya Mathur. CS 180. Fall 2011. Week 4 A constructor may have zero or more parameters. Parameters are used to provide initial values of instance variables at the time of constructing an object.

16 Class: “getter” methods 9/14/2011 public class ClassName{ public ClassName ( parameters ){ } public type getSomething(){ return something; } ©Aditya Mathur. CS 180. Fall 2011. Week 4 A class may have 0 or more “getter” methods. Each such method is used for extracting the value of an instance variable inside an object. Each “getter” method has a return type which is the type of the value returned.

17 Class: “setter” methods 9/14/2011 public class ClassName{ public ClassName ( parameters ){ } public type getSomething(){ return something; } public void setSomething( type val){ something=val; } ©Aditya Mathur. CS 180. Fall 2011. Week 4 A class may have 0 or more “setter” methods. Each such method is used for setting the value of an instance variable inside an object.

18 Class: main() method 9/14/2011 public class ClassName{ public ClassName ( parameters ){ } public type getSomething(){ return something; } public void setSomething( type val){ something=val; } public static void main(String [] args { ……… } ©Aditya Mathur. CS 180. Fall 2011. Week 4 At least one class in your program must have a method named main(). Program execution begins at the first statement in main().

19 Methods: 9/14/2011 public type methodName(){ Declarations statements } ©Aditya Mathur. CS 180. Fall 2011. Week 4 Execution happens inside methods. Each method contains declarations and statements. Each method has a name and the type of the return value. Recall: a constructor is a special method.

20 Methods: Parameters 9/14/2011 public type methodName(parameters){ Declarations statements } ©Aditya Mathur. CS 180. Fall 2011. Week 4 A method may have zero or more parameters.

21 Object: requests 9/14/2011 objectName.methodName(parameters); ©Aditya Mathur. CS 180. Fall 2011. Week 4 An object performs an operation when a request is sent to it. A request is sent to an object by placing the name of the method provided by the object. The method name is placed to the right of the object name separated by the dot (.) sign. Random r=new Random(); int age=r.nextInt(100);

22 Declarations 9/14/2011 type name; int age; double speed; Car momsCar; ©Aditya Mathur. CS 180. Fall 2011. Week 4 Any variable or object name must be declared before it is used. A declaration contains a type followed by the name of a variable or object being declared.

23 Declaration: primitive types 9/14/2011 short, int, long, float, double char boolean ©Aditya Mathur. CS 180. Fall 2011. Week 4 Variables of primitive type are used to hold numbers, truth values, and characters.

24 Declaration: String 9/14/2011 String name; ©Aditya Mathur. CS 180. Fall 2011. Week 4 String is a class in Java. Objects of type String hold strings as their values. Many operations can be performed on objects of type String. For example, you can concatenate two strings, or obtain any specific character within a string.

25 Declaration: Other classes 9/14/2011 Math Scanner ©Aditya Mathur. CS 180. Fall 2011. Week 4 Java has a huge library of classes that you may use. Math and Scanner are two classes that you will use quite often. You will be introduced to many more Java classes in the near future.

26 Statements 9/14/2011 Assignment: variable=expression; ©Aditya Mathur. CS 180. Fall 2011. Week 4 An assignment statement is used for computing the value of a variable. The expression on the right of the = is evaluated and its value assigned to the variable on the left of =. Next week we will learn more about statements.

27 Scanner: methods Type match/mismatch 9/14/2011©Aditya Mathur. CS 180. Fall 2011. Week 4

28 Scanner: methods: Examples 9/14/2011 int age; age = source.nextInt(); float price; price = source.nextFloat(); String name; name = source.nextLine(); String name; name = source.next(); boolean alive; alive = source.nextBoolean(); ©Aditya Mathur. CS 180. Fall 2011. Week 4

29 Formatting a string: Example 9/14/2011 public class FormatTest { public static void main(String[] args) { double price = 4.977798; String formattedPrice = String.format("%8.2f", price); System.out.println(formattedPrice); } Output: ? ©Aditya Mathur. CS 180. Fall 2011. Week 4

30 Random numbers 9/14/2011 Useful in many situations, mostly in simulation (e.g., roll dice), video games…. ©Aditya Mathur. CS 180. Fall 2011. Week 4

31 Random numbers: Create a generator object 9/14/2011 Random generator = new Random(); ©Aditya Mathur. CS 180. Fall 2011. Week 4

32 Random numbers: How to generate? 9/14/2011 Any Java integer: int r = generator.nextInt(); An integer between 0 and (n-1): int r = generator.nextInt(n); A double between 0 and 1 (exclusive): double r = generator.nextDouble(); A boolean: boolean r = generator.nextBoolean(); ©Aditya Mathur. CS 180. Fall 2011. Week 4

33 Random numbers: Scaling 9/14/2011 Integer between 11 and 21 (inclusive) int r = generator.nextInt(11); int num = r + 11; Double between 0 and 90 (in/exclusive) double r = generator.nextDouble(); angle = r * 90; ©Aditya Mathur. CS 180. Fall 2011. Week 4

34 Exceptions 9/14/2011©Aditya Mathur. CS 180. Fall 2011. Week 4

35 Overflow and underflow in arithmetic operations 9/14/2011 int x = y / z; // What if z is 0? float p = q * r; // What if the product is larger // than maximum float? ©Aditya Mathur. CS 180. Fall 2011. Week 4

36 Concurrency Revisited 9/14/2011©Aditya Mathur. CS 180. Fall 2011. Week 4

37 Specific problem 9/14/2011 Create two RaceCar objects as threads. Each object has a current position in miles and a randomly generated speed between 40 and 75 miles per hour. When started, each thread computes the new position of the car assuming that the car has traveled for 5 minutes and that the car is moving at a constant speed in a straight line. Get the new position of each car and display on the screen. ©Aditya Mathur. CS 180. Fall 2011. Week 4 Car 1 Car 2

38 Concurrency: Live program development. 9/14/2011©Aditya Mathur. CS 180. Fall 2011. Week 4

39 Concurrency: task level 9/14/2011 In some problems independent tasks can be executed in parallel. This may decrease the time to solve the problem. ©Aditya Mathur. CS 180. Fall 2011. Week 4

40 Concurrency revisited: A generic problem 9/14/2011 Given functions f() and g() Compute sum = f(a) + g(b, c) When f() and g() are time consuming operations, concurrency might speed up the task of computing the sum by using independent threads to compute f() and g(). ©Aditya Mathur. CS 180. Fall 2011. Week 4

41 Concurrency: Program design: Controller 9/14/2011 Create thread to compute f() Create thread to compute g() Start thread to compute f() Start thread to compute g() Wait for the two threads to complete Get data from the two threads and compute sum. Display sum Execution begins here Execution ends here ©Aditya Mathur. CS 180. Fall 2011. Week 4

42 Concurrency revisited: Threads 9/14/2011 Start execution Save input parameters Begin computation of f() End Computation of f() When asked, return value of f() Start execution Save input parameters Begin computation of g() End Computation of g() When asked, return value of g() ComputeFComputeG ©Aditya Mathur. CS 180. Fall 2011. Week 4

43 Week 4: September 12-16, 2011 Hope you enjoyed this week! Questions? Contact your recitation instructor. Make full use of our office hours. 9/14/2011©Aditya Mathur. CS 180. Fall 2011. Week 4


Download ppt "CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 4: September 12-16, 2011 Aditya Mathur/Tim Korb Department of Computer."

Similar presentations


Ads by Google