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.

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

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
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 2: August 29-September 2, 2011 Aditya Mathur Department of Computer Science.
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 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 3: September 5-9, 2011 Aditya Mathur Department of Computer Science Purdue.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
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.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 20, 2004 Last update:
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.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
CPS120: Introduction to Computer Science
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Chapter 2: Java Fundamentals
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.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
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.
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.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
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.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Fundamentals 2.
Topic 2 Elementary Programming
Chapter 2 Variables.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Crash course in the Java Programming Language
Elementary Programming
Data types and variables
IDENTIFIERS CSC 111.
Chapter 2 Edited by JJ Shepherd
Chapter 2 Variables.
Chapter 2: Java Fundamentals
elementary programming
Chapter 2: Java Fundamentals
Java Programming Review 1
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
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 2010 Notes for Week 3: September 6-10, 2010 Aditya Mathur Department of Computer Science Purdue University West Lafayette, IN, USA This week: 1.Feedback for Week 2 2.Review 3.Primitive types 4.Writing simple programs to solve simple problems

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

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

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

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 Week 39/8/2010

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 Week 39/8/2010

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 Week 39/8/2010

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 Week 39/8/2010

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 Week 39/8/2010

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 Week 39/8/2010

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

The edit, compile, execute cycle 12©Aditya Mathur. CS 180. Fall 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)

Classes and Objects 13©Aditya Mathur. CS 180. Fall 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

String 14©Aditya Mathur. CS 180. Fall 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

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

Strings: Other operations You may apply a variety of operations to strings. Examples follow. 16©Aditya Mathur. CS 180. Fall 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

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

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

Primitive types: short, int, long 19©Aditya Mathur. CS 180. Fall Week 39/8/ Set of integersSet of Operations * % Integer.MAX_VALUE: Integer.MIN_VALUE: Long.MAX_VALUE: Integer.MIN_VALUE: short: 2 bytes int: 4 bytes long: 8 bytes

Primitive types: short, int, long: Examples 20©Aditya Mathur. CS 180. Fall 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

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

Primitive types: float, double: Examples 22©Aditya Mathur. CS 180. Fall 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)

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

Primitive types: boolean: Examples 24©Aditya Mathur. CS 180. Fall 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

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

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

Names 27©Aditya Mathur. CS 180. Fall 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.

Constants 28©Aditya Mathur. CS 180. Fall Week 39/8/2010 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 29©Aditya Mathur. CS 180. Fall Week 39/8/2010 A constant can be named and the name used instead of the constant itself. Examples: final float pi= ; final boolean dogsExist=true;

Variables 30©Aditya Mathur. CS 180. Fall 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.

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

Simple expressions 32©Aditya Mathur. CS 180. Fall 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!

Assignment statement 33©Aditya Mathur. CS 180. Fall 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!

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

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

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

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

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

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

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 Week 39/8/2010