Intro to CS – Honors I Programming Basics GEORGIOS PORTOKALIDIS

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
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.
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.
JavaScript, Third Edition
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CPS120: Introduction to Computer Science
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Lesson 3 – Primitive Data Types Objective: Students will investigate the definition and usage of objects and primitive data types in Java in order to practice.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Primitive Variables.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Chapter 2 Variables.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
COMP Primitive and Class Types Yi Hong May 14, 2015.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CPS120: Introduction to Computer Science Variables and Constants.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
1 Primitive Types n Four integer types:  byte  short  int (most common)  long n Two floating-point types:  float  double (most common) n One character.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
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.
CS 201 Lecture 2: Elementary Programming Tarik Booker CS 201 California State University, Los Angeles.
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.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Building Java Programs
Lecture 2: Data Types, Variables, Operators, and Expressions
ITEC113 Algorithms and Programming Techniques
Data Types, Identifiers, and Expressions
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Type Conversion, Constants, and the String Object
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Basics of ‘C’.
Building Java Programs
Chapter 2 Variables.
Fundamentals 2.
Object-Oriented Programming
Building Java Programs Chapter 2
Expressions and Assignment
Building Java Programs
Building Java Programs
Building Java Programs
Primitive Types and Expressions
Building Java Programs Chapter 2
Chapter 2 Variables.
Building Java Programs
Presentation transcript:

Intro to CS – Honors I Programming Basics GEORGIOS PORTOKALIDIS

Overview Object-oriented programming basics Algorithms Java fundamentals ◦Variables ◦Data types ◦Arithmetic operators ◦Operator precedence

Object-Oriented Programming Objects ◦They are all around us ◦Cars, people, trees… Each object can perform certain actions ◦Independently or interacting with other objects ◦These actions are called methods Each objects has characteristics or attributes ◦Example: a car has static attributes like its color and dynamic attributes like its speed A class defines the type or kind of object ◦Example: all car objects could belong to the automobile class Objects belonging to a particular class have similar attributes, but are not the same!

Object-Oriented Languages What other OOP languages do you know?

OOP Design Principles Encapsulation ◦Putting things into a capsule ◦The capsule hides what’s inside ◦AKA information hiding ◦Part of the capsule is exposed ◦Applies to all programming ◦OOP languages help you enforce the separation Polymorphism ◦Means “many forms” ◦For Java it means that the same method can cause differently actions based on the object it is used with ◦Example: the accelerate method of all objects in the automobile class, will accelerate the vehicle, but may do so in a very different way Inheritance ◦A way of organizing classes Capsule Implementation Attributes Methods FerrariTesla accelerate The user knows he just wants to accelerate

Inheritance Generic class More specialized classes Even more Specialized classes

Algorithms An algorithm is a set of directions for solving a problem How can you express an algorithm? ◦In English ◦In a programming language ◦In pseudocode ◦A combination of the above Come up with an algorithm for ordering N numbers from lowest to highest and write it in pseudocode

Variables public class EggBasket { public static void main(String[] args) { int numberOfBaskets, eggsPerBasket, totalEggs; numberOfBaskets = 10; eggsPerBasket = 6; totalEggs = numberOfBaskets * eggsPerBasket; System.out.println("If you have"); System.out.println(eggsPerBasket + " eggs per basket and"); System.out.println(numberOfBaskets + " baskets, then"); System.out.println("the total number of eggs is " + totalEggs); } Variables are used to store data Values can be assigned to variables Variables are of a type SYNTAX Type Variable_Name_1, Variable_Name_2,...;

Data Types Determine how much memory will be required for the data and how the data are stored in memory Java has two main types ◦Primitive types ◦Classes

Identifiers Identifier is the technical term for a name In Java it can contain: ◦Letters ◦Digits ◦The underscore character (_) Cannot begin with a digit Case sensitive Cannot be keywords? public static void main(String[] args) { int 1st_number, secondNumber, secondnumber; Example An identifier cannot begin with a digit

Naming Conventions Avoid words frequently used with other meanings ◦Example: println and main Multiple conventions ◦Pick one and be consistent ◦Example: ◦Variables should begin with a lowercase letter, ◦Names composed by multiple words should have the first letter of every word besides the first capitalized ◦ numberOfBaskets, eggsPerBasket, totalEggs

Constants Constants or literals are data that, unlike variables, cannot change numberOfBaskets = 10; pi = ; firstInitial = 'B'; Constants also have a type. What is the type of these?

Floating-Point Numbers Scientific notation: number can be written as 8.65 × 10 8 Java’s e notation or floating-point notation: 8.65e8 Example:  4.83x10 -4  3.83e-4 Is 5.0 the same as 5? Floating point numbers are stored with limited precision … -> Is stored as exactly Includes decimal point Single digit before decimal point Exponent cannot include decimal point

Named Constants Variables with a constant value public static final Type Variable = Constant; Examples: ◦public static final double PI = ; ◦public static final int DAYS_PER_WEEK = 7;

Assignments SYNTAX Variable_name = Expression; EXAMPLE score = goals – errors; interest = rate * balance; number = number + 5; An expression can be many things. We will learn many different expressions as we go. Arithmetic expressions.

Assignment Compatibilities “You can’t put a square peg in a round hole” byte → short → int → long → float → double Where is char?

Type Casting What happens here? double distance = 9.56; int points = distance; How about now? double distance = 9.56; int points = (int)distance; System.out.println(points); Numbers are not rounded! Assignment is illegal The typecast transforms the value to the type: double->int

Arithmetic Operators Five binary operators (between two operands) ◦Addition + ◦Can also be used as an unary operator to negate a number ◦Subtraction – ◦Can also be used as an unary operator to negate a number ◦Multiplication * ◦Division / ◦Integer division does not produce a floating-point result later being cast to an integer ◦Remainder % ◦When performing an integer division The type of the result is the same as the “largest” of the two operands ◦byte → short → int → long → float → double balance + deposit balance * rate 11 / 3 11 % 3

Other Unary Operators Increment/decrement operators: ++, -- ◦Increase/decrease value of variable by one Can be used as a prefix or suffix ◦As prefix to a variable it first applies the operator to it, and then returns its value ◦As suffix to a variable it returns its value, and then operator on it Can also be used independently: ++n; What is the value of d and n? ◦ n = 10; d = n; What is the value of n, b, and d; ◦ n = 3; b = 5; d = ++b - n--;

Operator Precedence From highest to lowest ◦Unary operators ++, --, +, - ◦Binary operators *, /, % ◦Binary operators + and – Parentheses can be used to change the priority of operations Example: d = (n + 10) * 2 What is the result of this d = 5; d = ++d + (--d * 4); ?

Special Operators II You can precede the = assignment operator with arithmetic operators Example: d += 10; is the same as d = d + 10; Are these the same? d = 5; ◦d = d * 5 + 5; ◦d *= 5 + 5; Operator precedence from highest to lowest ◦Unary operators ++, --, +, - ◦Binary operators *, /, % ◦Binary operators + and – ◦Special operators +=, *=, etc.

Bitwise Operators Bitwise operators: ◦AND  & ◦OR  | ◦XOR  ^ Operator precedence from highest to lowest ◦Unary operators ++, --, +, - ◦Binary operators *, /, % ◦Binary operators + and – ◦Bitwise &, ^, |

Shift Operations Java has no unsigned numbers Numerical shift ◦Left << ◦Right >> Logical shift is still possible ◦Left <<< ◦Right >>> Operator precedence from highest to lowest ◦Unary operators ++, --, +, - ◦Binary operators *, /, % ◦Binary operators + and – ◦Shift operators ◦Bitwise &, ^, |