240 likes | 375 Views
Intro to CS – Honors I Programming Basics. Georgios Portokalidis gportoka@stevens.edu. Overview. Object-oriented programming basics Algorithms Java fundamentals Variables Data types Arithmetic operators Operator precedence. Object-Oriented Programming. Objects They are all around us
E N D
Intro to CS – Honors IProgramming Basics Georgios Portokalidis gportoka@stevens.edu
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 Implementation Attributes • 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 Methods Ferrari Tesla accelerate 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) { intnumberOfBaskets, 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 Variables are of a type Values can be assigned to variables 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? Example public static void main(String[] args) { int1st_number, secondNumber, secondnumber; An identifier cannot begin with a digit
Naming Conventions • Avoid words frequently used with other meanings • Example: printlnand 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 = 3.14159; • firstInitial= 'B'; Constants also have a type. What is the type of these?
Floating-Point Numbers • Scientific notation: number 865000000.0 can be written as 8.65 × 108 • Java’s e notation or floating-point notation: 8.65e8 • Example: • 0.000483 4.83x10-4 3.83e-4 • Is 5.0 the same as 5? • Floating point numbers are stored with limited precision • 0.3333 … -> Is stored as exactly 0.3333333333 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 = 3.14159; • public static final intDAYS_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 = 5 + ++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 &, ^, |