1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.

Slides:



Advertisements
Similar presentations
CSci 1130 Intro to Programming in Java
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.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Class 7.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
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.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
CMT Programming Software Applications
1 Data types, operations, and expressions Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 2 Data Types, Declarations, and Displays
Hello, world! Dissect HelloWorld.java Compile it Run it.
String Escape Sequences
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Objectives You should be able to describe: Data Types
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
CS591x A very brief introduction to Java. Java Developed by Sun Microsystems was intended a language for embedded applications became a general purpose.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
Chapter 2 Variables.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
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.
Primitive data types Lecture 03. Review of Last Lecture Write a program that prints the multiplication table of 5. class MultiplicationTable { public.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Fundamentals 2.
Java Variables and Types
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Lecture 2: Data Types, Variables, Operators, and Expressions
Multiple variables can be created in one declaration
Computer Programming Methodology Introduction to Java
Type Conversion, Constants, and the String Object
Escape Sequences What if we wanted to print the quote character?
Fundamentals 2.
Chapter 2: Java Fundamentals
Unit 3: Variables in Java
Names of variables, functions, classes
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi

2 Who am I, and Who are You? Who am I?  2 nd Year PhD Student in the CS Dept  Did my undergrad in CS and MATH  Research Interests Computer Networks and Distributed Systems  Other interests Sports (Volleyball, Racket Ball etc) Who are you, and what do you do?  Introductions…

3 Other Information Office: 6803 SENSQ Office Hours: Wed 2:30PM - 5:40PM (10 min break) or by appointment

4 Agenda for Today’s class Review Exercises

5 Recall: Goals of the Course To write computer programs in Java To recognize and create well-written programs in good programming style Hands-on experience with several stages of the life-cycle of a computer program, including planning, implementation, and debugging Set of (computer understandable) instructions to perform a task or a set of tasks A programming language

6 Example Java Program Key Words Operator Punctuation Syntax Programmer-Defined Names public class Number { public static void main(String [] args) { int age = 25; int height = 75; int sum = age + height; } }

7 Last Time.. // This is a simple Java program. public class Hello { public static void main(String [] args) { System.out.println(“Hello World”); } }

8 Compiling and Running HelloWorld.java javac HelloWorld.java java HelloWorld HelloWorld.class compile run bytecode source code

9 Variables In a computer program a variable is a named storage location in the computer’s memory. Each variable has a type and a name A variable should be declared and initialized Variable Declaration:  char a;  char is the variable type and a is the variable name  Variables must be declared before they can be used Variable Initialization:  a = ‘z’; // Assigning value to a variable  The equal sign is an operator that stores the value on its right into the variable named on its left.

10 Primitive Data Types Java Data TypeDescription Booleantrue or false charA character int (4 bytes) Integer short (2 bytes) Short integer long (8 bytes) Long integer float (4 bytes) Single precision floating point Double (8 bytes) byte (1 byte) Double precision floating point limited range (Integer)

11 What Is A Byte? The smallest data unit in a computer is a bit (1 or 0) 8 bits are called a byte Computers store numbers in binary  0 = 0 in binary  1 = 1 in binary  2 = 10 in binary  3 = 11 in binary  4 = 100 in binary Difference between counting using decimal umbers and binary numbers

12 Special Cases float number = 23.5; float number = 23.5f; float number = 23.5F; long bigNumber = ; long bigNumber = L; long bigNumber = l; ERROR CORRECT ERROR CORRECT

13 The boolean Data Type a boolean variable can have two values:  true  false Example: boolean bool; bool = true; System.out.println(bool); bool = false; System.out.println(bool); true false

14 The char Data Type used to store characters character literals are enclosed in single quotes Example: char letter; letter = 'A'; System.out.println(letter); letter = ‘b'; System.out.println(letter); AbAb

15 The char Data Type Characters are internally represented by numbers. Java uses Unicode which is a set of numbers that are used as codes for representing characters  Example: 65 is the code for A, 66 is the code for B

16 The char Data Type variables of char type can be defined using either literal characters or their corresponding numeric codes  Example: char letter; letter = 65; System.out.println(letter) letter = 66; System.out.println(letter); ABAB

17 Arithmetic operators Operator MeaningTypeExample + AdditionBinarytotal = cost+tax; - SubtractionBinarycost = total-tax; * MultiplicationBinaryTax = cost*rate; / DivisionBinarysaleprice=original/2; % ModulusBinaryremainder=value%3;

18 The % Operator Returns the remainder of a division operation involving two integers Common operation in computer science Examples;  4%5 is 4  30%6 is 0  22%7 is 1  3205%100 is 5  3205%10 is 5

19 Operator Precedence 1.Evaluate – (unary negation) from right to left 2.Evaluate * / % from left to right 3.Evaluate + - from left to right Note: If equal precedence, evaluate from left to right except for negations where we evaluate from right to left

20 Precedence Examples Polynomial = 1+2*3+ 6/2 – 2;  Polynomial has the value of =8 Polynomial = –1 + 5 – 2; // 2 Polynomial = –(–3) + –(–5); //8

21 Grouping With Parentheses You can use parentheses to force the evaluation of enclosed operations before others Examples:  x * ( y + z*z ) instead of x*y + z*z  x * ( y * ( z ) + 85 ) – 65  Average = (a +b +c ) /3;

22 The Math class value = Math.pow(x,y); // now value holds x to the power of y value = Math.sqrt(x); //now value holds the square root of x

23 Combined Assignment Operators +=x += 1;x = x + 1; –=x –= 1;x = x – 1; *=x *= 1;x = x * 1; /=x /= 1;x = x / 1; %=x %= 1;x = x % 1;

24 Exercise 1: Swap the values of two integers public class Swap{  public static void main (String args []) {{ int x, y, z; x = 5; y = 8; System.out.println("x="+x+" y="+y); z = x; x = y; Y = z; System.out.println("x="+x+" y="+y); }} }

25 Exercise 2: Compute the average and Standard Deviation of three numbers Note: Make use of Math.pow() and Math.sqrt() Assume  double x = 4.0  double y = 5.0  double z = 6.0

26 Solution public class Multiply{ public static void main (String args []) { double x=4.0, y=5.0, z=6.0; double av, sd; av = (x+y+z)/3.0; sd = Math.pow((x-av),2) + Math.pow((y-av),2) + Math.pow((z-av),2); sd = sd/3.0; sd = Math.sqrt(sd); System.out.println("Average = "+av); System.out.println("Standard Deviation = "+sd); }

27 Exercise 3: Determining Prime Numbers Assume that you are given numbers 5,6,…10 Determine which of them are prime A prime number is a positive integer p>1 that has no positive integer divisors other than 1 and p itself Hint:  Use % (modulus) operator  For each number p compute p % 2, p % 3,…, p % (p-1)  Examples 5 % 1 = 0 5 % 2 = 1 7 % 6 = 1

28 Thanks !!