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.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
CS 180 Problem Solving and Object Oriented Programming Spring 2011 February 16, 2011 Aditya Mathur Department of Computer Science Purdue University West.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 5: September 19-23, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 13: Nov 14-18, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 10: Oct 24-28, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 8: Oct 10-14, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 13: Nov 15-19, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 16: Dec 6-10, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 9: Oct 17-21, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for the Final Lecture Dec 7, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 3: September 5-9, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Some basic I/O.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 20, 2004 Last update:
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 11: Oct 31-Nov 4, 2011 Aditya Mathur Department of Computer Science Purdue.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
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.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 5: September 20-24, 2010 Aditya Mathur Department of Computer Science Purdue.
1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 11: Nov 1-5, 2010 Aditya Mathur Department of Computer Science Purdue University.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 14: Nov 21-25, 2011 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 9: Oct 18-22, 2010 Aditya Mathur Department of Computer Science Purdue.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 10: Oct 25-29, 2010 Aditya Mathur Department of Computer Science Purdue.
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 6: September 27-Oct 1, 2010 Aditya Mathur Department of Computer Science.
Repetition Statements
CompSci 230 S Programming Techniques
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 21, 2011
Building Java Programs
Yanal Alahmad Java Workshop Yanal Alahmad
Multiple variables can be created in one declaration
Java so far Week 7.
Building Java Programs
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Building Java Programs
Names of variables, functions, classes
Building Java Programs
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

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 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

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

Quiz 9/14/2011©Aditya Mathur. CS 180. Fall Week 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 Week 4 Correct answer: b (type can also be double)

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 Week 4 Correct answer: a

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 Week 4 Correct answer: b

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

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 Week 4 Correct answer: b (can be float also)

“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 Week 4 Correct answer: c

Announcements 9/14/2011©Aditya Mathur. CS 180. Fall 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.

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

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

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

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

Class: Constructor: Parameters 9/14/2011 public class ClassName{ public ClassName ( parameters ){ } ©Aditya Mathur. CS 180. Fall 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.

Class: “getter” methods 9/14/2011 public class ClassName{ public ClassName ( parameters ){ } public type getSomething(){ return something; } ©Aditya Mathur. CS 180. Fall 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.

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 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.

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 Week 4 At least one class in your program must have a method named main(). Program execution begins at the first statement in main().

Methods: 9/14/2011 public type methodName(){ Declarations statements } ©Aditya Mathur. CS 180. Fall 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.

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

Object: requests 9/14/2011 objectName.methodName(parameters); ©Aditya Mathur. CS 180. Fall 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);

Declarations 9/14/2011 type name; int age; double speed; Car momsCar; ©Aditya Mathur. CS 180. Fall 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.

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

Declaration: String 9/14/2011 String name; ©Aditya Mathur. CS 180. Fall 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.

Declaration: Other classes 9/14/2011 Math Scanner ©Aditya Mathur. CS 180. Fall 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.

Statements 9/14/2011 Assignment: variable=expression; ©Aditya Mathur. CS 180. Fall 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.

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

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 Week 4

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

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

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

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 Week 4

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 Week 4

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

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 Week 4

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

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 Week 4 Car 1 Car 2

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

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 Week 4

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 Week 4

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 Week 4

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 Week 4

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 Week 4