CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 2: August 29-September 2, 2011 Aditya Mathur Department of Computer Science.

Slides:



Advertisements
Similar presentations
SOPH 303 The Digital World Term 3 Spring 2013 Notes for Week 1. Sessions 2 and 3 Revised: January 28, 2013 Aditya Mathur Head of Pillar Information Systems.
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Lecture 1 Introduction to Java Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
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 2011 Notes for Week 4: September 12-16, 2011 Aditya Mathur/Tim Korb Department of Computer.
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 2011 Notes for Week 3: September 5-9, 2011 Aditya Mathur Department of Computer Science Purdue.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Java Syntax Primitive data types Operators Control statements.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 20, 2004 Last update:
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
1 CS1130 Spring 2011: David Gries & Walker White Transition to Object-Oriented Programming Assumes programming knowledge in a language like Matlab, C,
1 CS1110 Fall 2011: David Gries & Steve Marschner CS1112: Matlab No prior programming experience One semester of calculus Math- & engineering- type problems.
2015/8/221 Data Types & Operators Lecture from (Chapter 3,4)
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 7: Oct 3-7, 2011 Aditya Mathur Department of Computer Science Purdue University.
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.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CS 180 Problem Solving and Object Oriented Programming Fall 2010 Notes for Week 1: August 23-27, 2010 Aditya Mathur Department of Computer Science Purdue.
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.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Java Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
1 CS1110 Fall 2011 David Gries, Steve Marschner Reading for this lecture and previous lecture: Sections 1.1, 1.2, 1.3. Lab 1 will give you practice with.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Fundamental Programming: Fundamental Programming Introduction to C++
CS 1150 – Lab #3 – Representing Numbers TA – Sanjaya Wijeratne – Web Page -
Number Representation, Data Types and Elementary Programming Shirley Moore CS 1401 February 5-7, 2013.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Programming The Language of Now & the Future* Lecture 0 An Introduction to Java Syntax for Non-C Programmers John Morris Department of Electrical.
1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Copyright Curt Hill Variables What are they? Why do we need them?
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
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.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
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.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Fundamentals 2.
Yanal Alahmad Java Workshop Yanal Alahmad
Java Programming: From Problem Analysis to Program Design, 4e
Trainings 11/4 Intro to Java.
Lecture 5 from (Chapter 4, pages 73 to 96)
Building Java Programs Chapter 2
Chapter 2: Java Fundamentals
CS 200 Primitives and Expressions
Summary of what we learned yesterday
Unit 3: Variables in Java
CS Week 2 Jim Williams, PhD.
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
Variables and Constants
CS Problem Solving and Object Oriented Programming Spring 2019
Presentation transcript:

CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 2: August 29-September 2, 2011 Aditya Mathur Department of Computer Science Purdue University West Lafayette, IN, USA

Readings and Self-help Exercises for Week 2 Readings: Chapter 2: 2.1, 2.2, 2.3, 2.4 Self help exercises (not to be turned in): 2.1, 2.2, 2.3, 2.4, 2.7, 2.9, 2.12, CS 180. Fall Week 28/29/2011

About Homework All homework problems will be assigned during recitation. Please make sure you attend your recitation section. Homework assigned during week X is due at the start of your recitation during week X+1. It is best to use Piazza to ask questions regarding homework. But please do not post answers to homework problems. Most, but not necessarily all, homework problems will be from the textbook. 3CS 180. Fall Week 28/29/2011

Lab for Week 2 Play with Java programs: Introduction to Android-based smart phone Introduction to the Finch robot 4CS 180. Fall Week 28/29/2011

Feedback for Week 1 5CS 180. Fall Week 28/29/2011

Q1. I understand the difference between “sequential solution” and “Concurrent solution”. (a)Yes (b)No (c) Not sure (d) Missed week 1 lecture (s) 6CS 180. Fall Week 28/29/2011

Q2. I understand the difference between “Data Parallelism” and “Task parallelism”. (a)Yes (b)No (c) Not sure (d) Missed week 1 lecture(s) 7CS 180. Fall Week 28/29/2011

Dissecting a Java Program: Preliminaries 8CS 180. Fall Week 28/29/2011

The edit, compile, execute cycle 9CS 180. Fall Week 28/29/2011 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)

Classes and Objects 10CS 180. Fall Week 28/29/2011 Set of real or virtual objects animal car student flower Class AnimalClass CarClass StudentClass Flower Template in Java dog Class Dogbruschettaziggy Objects created momsCaramanda RepresentCreate

Classes and Objects Class: Contains attributes 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 (attributes) 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. 8/29/201111CS 180. Fall Week 2

Java program: Structure 12CS 180. Fall Week 28/29/2011 Package [Contains one or more classes] Class [Data and zero or more methods] Data (attributes) Method Data and statements At least one class must have a method named main(). Data represents properties of a real, virtual or a Java object. E.g., breed, age, color Methods are operations that can be performed on an object created from a class. E.g., run(), sit(), down(), no(), goodBoy()

Java program: Classes and Objects 13CS 180. Fall Week 28/29/2011 Data Method Data and statements class Automobile make model create camry Objects derived from Class Automobile make model maxSpeed start() make model maxSpeed start() create mazdaRX7

Elements of a Sequential Java Program Programs to be dissected: Program 2.6 ScannerExample.java Chapter 2 pages Program 2.7 BouncingBall.java in Chapter 2 pages CS 180. Fall Week 28/29/2011 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. For practice (try later):

Elements of a Concurrent Java Program Program to be dissected: Program 2.8 AreaMeasuringRobot.java in Chapter 2 pages CS 180. Fall Week 28/29/2011 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. For practice (try later):

Announcements This week’s recitation: you will learn about the Scanner class and how to map a problem to a Java program. Make sure you attend! 16CS 180. Fall Week 28/29/2011 Project 1 description will be released via the Schedule page of the course site on Sunday (a day before the announced date). Feast with faculty tonight: 6:30pm in Ford Dining Hall, room on the second floor. Is Java a pure OO language:? Use Google to find an answer. Lab makeup policy at the course web site.

Binary numbers and Floating point representation CS 240, CS 314, and several other classes. 17CS 180. Fall Week 28/29/2011 Writing programs for engine control, satellite control, aircraft design, integration, etc. etc. “I hate floating point and IEEE format!!” Understanding why the results of a computation are not what you expected. Knowledge useful in

Types 18CS 180. Fall Week 28/29/2011 Set of valuesSet of Operations x x x x x a b c

Primitive types: int, long 19CS 180. Fall Week 28/29/ Set of integersSet of Operations * % Integer.MAX_VALUE: Integer.MIN_VALUE: Long.MAX_VALUE: Long.MIN_VALUE: /

Primitive types: float, double 20CS 180. Fall Week 28/29/2011 Float.MAX_VALUE: e+38Float.MIN_VALUE: e-45 Double.MAX_VALUE: e+308 Double.MIN_VALUE: e Set of real numbers E4 Infinity -Infinity NaN Set of Operations (sample) + - * > == >=

Primitive types: boolean 21CS 180. Fall Week 28/29/2011 Set of logical valuesSet of Operations (sample) != == true false || && |

Primitive types: char 22CS 180. Fall Week 28/29/2011 Set of characters (sample values shown) Set of Operations (sample) != == ‘a’ ‘&’ || && | ‘$’ ‘+’

Operators: Arithmetic, relational, conditional 23CS 180. Fall Week 28/29/2011 Arithmetic operators + - * / % Relational operators == < > <= >= != Boolean/conditional operators || && a+b*c-d a/b c%d a==b a<=b a!=b a==b||c<d a d a!=b &&c>d||p+1<q

Operators: bitwise 24CS 180. Fall Week 28/29/2011 Bitwise operators &: bitwise AND |: bitwise OR ^: bitwise exclusive OR ~: bitwise complement Bitwise shift operators <<: bitwise left shift >>: bitwise right shift >>>: unsigned right shift a & b: logical and of a and b a|b: logical OR of a and b a <<3: shift bit pattern of a left by 3 bits a>>2: shift bit pattern of a to the right by 2 bits

Names 25CS 180. Fall Week 28/29/2011 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.

Constants 26CS 180. Fall Week 28/29/2011 A constant is something that cannot change during program execution. Examples: Integer constants: 0, 1, -1, +24, 29, , O14, 0x1B Floating point constants: 0.0, e28, Boolean constants: true, false Character constants: ‘ ‘, ‘a’, ‘A’, ‘$’ String constants: “”, “ ”, “Hi!”, “Alice in Wonderland”

Named Constants 27CS 180. Fall Week 28/29/2011 A constant can be named and the name used instead of the constant itself. Examples: final float pi= ; final boolean dogsExist=true;

Variables 28CS 180. Fall Week 28/29/2011 A variable is something whose value may change during program execution. Example: int numStudents; denotes the number of students whose grads have been processed. Its value changes as each student’s grade is processed by a grade processing program. Every variable has a name and a type. Example: int hurricaneCategory; The name is hurricaneCategory and its type is int. Every variable must be declared before it is used.

Strings: basics A string is any sequence of Unicode characters 29CS 180. Fall Week 28/29/2011 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”?

Strings: assignment You may assign a value to a string object. 30CS 180. Fall Week 28/29/2011 myDogsName=“Bently”; // Assuming that myDogsName has been declared 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.

Strings: Other operations You may apply a variety of operations to strings. Examples follow. 31CS 180. Fall Week 28/29/2011 String commend=“Bently,”+ “ good girl!; // String catenation String myCar=“It’s a Porsche”+ “, and I love it!” +”but maintenance is expensive.” // String catenation String firstChar=commend.charAt(0); // Extract character at position 0

Strings: More operations There exist a variety of operations on strings. A few are given below. 32CS 180. Fall Week 28/29/2011 StatementOperation used String commend=“Bently,”+ “ good girl!”;Catenation char firstChar=commend.charAt(0);Character extraction using charAt() movieName.equals(“Fugitive”)Comparison using equals() String.valueOf(29)Conversion integer 29 to String “29”

Declarations 33CS 180. Fall Week 28/29/2011 int age; float height, area; String name boolean iAmAlive; int x=1, y=0; String firstName=“Harry”;

Simple expressions 34CS 180. Fall Week 28/29/2011 Expressions are used to compute “something”. float x, y, z; // Declare x, y, z as variables of type float 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!

Assignment statement 35CS 180. Fall Week 28/29/2011 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!

Back to classes and objects 36CS 180. Fall Week 28/29/2011

Creating an object: Mom’s car 37CS 180. Fall Week 28/29/2011 String color=“Yellow”; String owner=“Mom”; int miles=200; new Car (“Yellow”, “Mom”, 200); public class Car{ public int getMiles(){ } String color; String owner; int miles;

Creating an object: Dad’s car 38CS 180. Fall Week 28/29/2011 String color=“Black”; String owner=“Dad”; int miles=200000; new Car (“Black”, “Dad”, ); public class Car{ public int getMiles(){ } String color; String owner; int miles;

Creating an object: Instance variables 39CS 180. Fall Week 28/29/2011 public class Car{ public int getMiles(){ } String color; String owner; int miles; Instance variables are copied into the new object. Each object has its own values for these variables. Thus, two objects of the same type have their own copies of instance variables.

Creating an object: get and set instance variables 40CS 180. Fall Week 28/29/2011 public class Car{ public int getMiles(){ return miles; } String color; String owner; int miles; public void setMiles(int m ){ miles=m; } Get the value of miles from the object. int dadsCarMIles=dadsCar.getMiles(); Set the value of miles in an object. momsCar.setMiles(300);

Classes and Objects: Summary 41CS 180. Fall Week 28/29/2011 Class: Template to create objects. Object: Created from a class; may denote a real world or an abstract object; Inherits all instance variables and methods. Instance variables: Variables that become local to an object. get and set methods Used for getting data from and into an object. Constructor Used for creating objects.

Week 2: August 29-September 2, 2011 Hope you enjoyed this week! Questions? Contact your recitation instructor. Make full use of our office hours. 42CS 180. Fall Week 28/29/2011