1 / 24

Intro to CS – Honors I Programming Basics

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

whitley
Download Presentation

Intro to CS – Honors I Programming Basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Intro to CS – Honors IProgramming Basics Georgios Portokalidis gportoka@stevens.edu

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

  3. 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!

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

  5. 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

  6. Inheritance Generic class More specialized classes Even more Specialized classes

  7. 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

  8. 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, ...;

  9. 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

  10. 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

  11. 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

  12. 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?

  13. 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

  14. 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;

  15. 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.

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

  17. 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

  18. 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

  19. 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--;

  20. 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); ?

  21. 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.

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

  23. 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 &, ^, |

More Related