580 likes | 656 Views
COP 3503 FALL 2012 Shayan Javed Lecture 2. Programming Fundamentals using Java. Introduction to Java. High-level language. Paradigm: Object-Oriented. WORA (“write-once, run anywhere”). Introduction to Java. Some languages are compiled (C/C++). Introduction to Java.
E N D
COP 3503 FALL 2012ShayanJavedLecture 2 Programming Fundamentals using Java
Introduction to Java • High-level language. • Paradigm: Object-Oriented. • WORA (“write-once, run anywhere”)
Introduction to Java • Some languages are compiled (C/C++)
Introduction to Java • Some languages are compiled (C/C++) • Some are interpreted (Python, Perl, Ruby, etc.)
Introduction to Java • Some languages are compiled (C/C++) • Some are interpreted (Python, Perl, Ruby, etc.) • What about Java?
Introduction to Java Compilation and Interpretation .java = compiled to .class .class = interpeted by Java Virtual Machine (JVM)
Java Syntax • Similar to C/C++. • Variables: byte, short, int, long, float, double, char, boolean • Field Modifiers: final (“constant”) , static (applies to classes)
Java Operators • Numerical: +, -, *, /, % • Boolean: >, <, <=, >=, ==, !=, !,instanceof • Others: ++, --, • Bitwise operators: & (AND), ^ (XOR), | (OR), <<, >> (shift)
if statements • if (boolean-expression) { … } else if (…) { … } else { … }
Conditional expression • boolean-expression? expression1 : expression2 • Example: int x = 3; int y = (x > 0) ? 1 : 5;
switch statement • switch (byte/short/char/int/String/etc.) { case x: break; case y: ….. break; case …: break; default: ….. }
Loops • while (boolean-expression) { // do something } • do { // something } while (boolean-expression); Difference?
Loops • for (expression1; boolean-expression; expression2) { // do something } Example: inti; for (i = 0; i <= 10; i++) i++;// Value of i?
keyword break • break = used to “break out” of a loop. • Rest of the code is not executed. int sum = 0; int number = 0; while (number < 20) { number++; sum += number; if (sum > 100) break; }
keyword continue • continue = used in loops. • Break out of current statement, but continue with the rest of the loop. int sum = 0; int number = 0; while (number < 20) { number++; if (number == 10) continue; sum += number; }
Methods • Program modularity. • Avoid redundant code! Use whenever possible • Methods can be “called”
Methods • modifierreturnValueType name (list of parameters) { ... } public static int max (int num1, int num2) { if (num1 > num2) return num1; else return num2; }
Methods - modifiers • public= can be called by any class • private= can only be called by the class • Also protected(will look at it later) • static= don’t require an “instance” of the class to call the method. ClassName.method(...) The Math class – Math.sin(), Math.acos(), etc.
Methods • returnValueType= Can be primitive, class, etc. Even void (nothing to return) • list of parameters = a list of primitives, classes, etc. (or nothing)
Recursion • Methods calling themselves • Write base case first! Otherwise might be stuck forever. • Classic example: Fibonacci numbers Integer sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21... F(n) = F(n-1) + F(n-2) F(0)= 0, F(1) = 1
Recursion publicintfibonacci (int n) { if (n == 0 || n == 1) // base case(s) return n; else return fibonacci (n-1) + fibonacci(n-2); }
Recursion • Later on we will look at recursion for other algorithms (searching/sorting)
Method overloading • Can have multiple methods with the same name. • Showed “max” method with ints • Write one with double: public static double max (double num1, double num2) { if (num1 > num2) return num1; else return num2; }
Commenting • Single-line: // This is a single-line comment • Multi-line: /* This is going to be on multiple lines */ Comment your code properly! Very helpful – to you and others.
Object-Oriented Programming • Paradigm which uses “objects” and “classes”.
Object-Oriented Programming • Paradigm which uses “objects” and “classes”. • Used to represent real-life objects or concepts that can be distinctly identified.
Object-Oriented Programming • Paradigm which uses “objects” and “classes”. • Used to represent real-life objects or concepts that can be distinctly identified. • Objects have properties, methods.
Object-Oriented Programming • Paradigm which uses “objects” and “classes”. • Used to represent real-life objects or concepts that can be distinctly identified. • Objects have properties, methods. • Interaction between objects.
Object-Oriented Programming • Most modern languages support OOP
Object-Oriented Programming • Most modern languages support OOP • Alternatives: • Procedural/Imperative ( C ) • Functional (Lisp/PROLOG)
Classes in Java • A template for objects of the same type.
Classes in Java • A template for objects of the same type. • You create “objects” (or “instances”) of a class.
Objects in Java • Unique identity, state and behavior.
Objects in Java • Unique identity, state and behavior. • state (properties/attributes): Data fields and their current values.
Objects in Java • Unique identity, state and behavior. • state (properties/attributes): Data fields and their current values. • behavior: The methods for that class
Example public Circle { // Properties private double radius; // Constructors public Circle() { radius = 0.0; } public Circle(double radius) { this.radius = radius; } // Methods publicdoublegetArea() { return radius * radius * Math.PI; } }
Properties // Properties private double radius; • private= only accessible by that class directly.
Properties // Properties private double radius; • private= only accessible by that class directly. • Not a good idea to have public properties (for security reasons).
Properties // Properties private double radius; • private= only accessible by that class directly. • Not a good idea to have public properties (for security reasons). • What if another class needs to access/modify the property?
Properties • Add get/set methods:
Properties • Add get/set methods: • publicgetRadius() { return radius; }
Properties • Add get/set methods: • publicgetRadius() { return radius; } • publicvoidsetRadius(double radius) { this.radius = radius; }
Properties • Add get/set methods: • publicgetRadius() { return radius; } • publicvoidsetRadius(double radius) { this.radius = radius; } POINT OUT THE MISTAKE
this keyword • Refers to the property of this specific class • Used to distinguish between similar-named variables
Constructors // Constructors // default constructor public Circle() { } public Circle(double radius) { this.radius = radius; }
Constructors • Special kind of method • Same name as the class • No return type (even void) • Used to initialize objects (using the newkeyword)
Constructors • Initialization example: Circle circle1 = newCircle(); Circle circle2 = newCircle(4.5);
Constructors • Should always provide a default constructor. • Does not take in any properties • Good idea to have multiple constructors and default values
Reference Variables • Objects accessed via reference variables. • Example from before: Circle circle2 = newCircle(4.5); circle2 = Reference variable used to access the object.
Reference Variables • Can declare without initializing Circle circle2; // What’s the value? • Initialize later: circle2 = newCircle(4.5);