1 Review of Java Basic Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – conditional statements –Flow of Control.

Slides:



Advertisements
Similar presentations
Java Language Quick-Reference Guide B. Oracle10g: Java Programming B - 2 Console Output Java applications and applets can output simple messages to the.
Advertisements

COSC 2006 Data Structures I Instructor: S. Xu URL:
Programming Languages and Paradigms
Written by: Dr. JJ Shepherd
File I/O and Exceptions File I/O Exceptions Throwing Exceptions Try statement and catch / finally clauses Checked and unchecked exceptions Throws clause.
Inheritance Inheritance Reserved word protected Reserved word super
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 2 Object-Oriented Design. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 2-2 Chapter Objectives Review the core concepts underlying.
Copyright © 2003 ProsoftTraining. All rights reserved. Sun Certified Java Programmer Exam Preparation Guide.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Java Programming Review (Part I) Enterprise Systems Programming.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Appendix B: Object-Oriented Design Java Software Structures: Designing.
Programming Languages and Paradigms Object-Oriented Programming.
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The Java Programming Language
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
C# EMILEE KING. HISTORY OF C# In the late 1990’s Microsoft recognized the need to be able to develop applications that can run on multiple operating system.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Information and Computer Sciences University of Hawaii, Manoa
Key Words / Reserved Words
Topic: Classes and Objects
File I/O CLI: File Input CLI: File Output GUI: File Chooser
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3 Introduction to Classes, Objects Methods and Strings
Java Programming Language
Java Programming Course
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Chap 2. Identifiers, Keywords, and Types
Presentation transcript:

1 Review of Java Basic Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – conditional statements –Flow of Control – loops –Arrays Reading: Your CS110 Text/Notes

2 Review of Java If you are not completely familiar with this material, please see me. You should not be taking this course! Don’t expect that you’ll catch up as we go through the CS210 material. That is not going to work!

3 Names and Reserved Words Names are used for classes and methods: –Classes: Scanner, BingoBall –Methods: next( ), hasNext( ) Names are used for variables and constants –Variables: i, j, current, currentValue, isOn –Constants: SIZE, MAX_VALUE Reserved words are java language identifiers: –Examples: class, return, public, static, int, boolean

4 Expressions and Precedence There are three major groups of basic operations in Java: –Arithmetic (+, -, *, /, %, ++, --,... ) –Comparison ( !=, ==,, <,...) –Logical ( ||, &&, !) Arithmetic operators have the highest precedence followed by comparison operators. Logical operators have the least precedence.

5 Expressions and Precedence The precedence of operators determines the order of evaluation for expressions. Here is the order precedence of For arithmetic operations from highest to lowest: –Highest: [ ], ( ), ++, -- –Next: +(unary), -(unary), ~, ! –Next: new, (type) –Next: *, /, % –Next: +(binary), -(binary) –Etc.

6 Expressions and Precedence An Expression is a construct made up of operators and method invocations applied on variables and constants. Each expression has a type. Examples: –x+y+3 (x and y are integer variables) –z/3+y%2 (z is a double and y is an int) –x 2) –obj.doIt()

7 Flow of Control - Selection If statements with optional else clauses: if (boolean condition){ statement; } else { statement; } Switch statements switch (integer value) { case FIRST_VALUE: statements; case SECOND_VALUE: statements; default: statements; }

8 Flow of Control - Repetition While while (scan.hasNext()) { statements; // repeated until false above } For for (int i = 0; i < MAX; i++) { statements; // repeated until false above } Do … while do { statements; // repeated until false below } while (!done);

9 Arrays Arrays are a group of elements that can be referenced via a name and an index value Declaring an array with or w/o initialization int [] digits = new int [10]; OR int [] digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; Setting the value of an element of an array digits [0] = 0; Using the values of elements of an array int sum = digits[2] + digits[4];

10 Review of Java Object Oriented Programming Concepts –Objects and Classes –Encapsulation, Constructors, and Methods –References and Aliases –Interfaces and Inheritance –Class Hierarchies and Polymorphism –Generic Types (ArrayList Class) –Exceptions Reading: L&C Appendix B (3 rd Ed.) or Chap. 2 (2 nd ed.)

11 Data Types Java Primitive types: int, double, boolean, char, byte, … Arrays (Array of ints, Array of boolean, …)

12 Data Types Java Primitive types: int, double, boolean, char, byte, … Arrays (Array of ints, Array of boolean, …) To define more complex customized types, we write our own classes which define the type of the data objects (instances) of that class. We define a class by using and combining other types (primitive types, arrays and other defined classes)

13 Objects and Classes Object and class relationship Class Definition public class ClassName { // attributes <=== defines the state of the objects of this type (instances of the class) // methods <=== defines the behavior of the } objects Instantiating Objects using Classes ClassName myClassName = new ClassName();

14 Class Definition public class Car { private int speed; <== object attributes or private Engine engine; instance variables public int getSpeed() { return speed; } public void drive() {.... }

15 Encapsulation Encapsulation of Attributes public class ClassName { // constants public static final int MAX_SIZE = 20; private static final int DEFAULT_SIZE = 10; // class variables private static int largestSizeOfAll; // instance variables private int mySize; }

16 Constructors and Methods Constructor (ClassName with no return type) public class ClassName { public ClassName (parameter list if any) { statements; } Method (Method name with a return type) public type methodName(parameter list if any) { statements; }

17 Constructor A constructor is a special method that is designed to create an object (initialize an object). A constructor is different from a regular method in three ways.

18 Using References Using a class constant if (size <= ClassName.MAX_SIZE) statement; Using a class method (static method) type returnValue = ClassName.methodName(...); Using an instance method via a reference type returnValue = myClassName.methodName(...);

19 Aliases and Garbage Collection Creating an alias of a reference ClassName yourClassName = myClassName; Making object eligible for garbage collection myClassName = yourClassName = null; Object of class ClassName myClassName yourClassName null Object of class ClassName myClassName yourClassName “Garbage”

20 Primitive/Non-primitive Types int x; x x = 12; x String y; y String y = new String(“ABC”); y y = null; y 0 12 null “ABC”

21 Method Overloading public class Car {.... public void drive() { int temp = 12; < local attribute (scope?).... } public void drive(int speed) {.... } Car ford = new Car(...); ford.drive(110); this method call binds to ??? instance mthod

22 Static/non-static var public class MyClass { private int objAttr;.... } instance variable (object attribute): one copy of attribute for each Object of type MyClass. Each object can access and modify its own copy. An object changing its own attribute will not be seen by other instances. public class YourClass { priavate static classAttr;.... } static variable (class variable): there is only one copy of the attribute which is associated with the class itself. All Objects of type YourClass can access and modify this class variable and changing it in one object is seen by all other objects.

23 Static/non-static method public class MyClass { public String myMethod() {.... } instance method or object method is associated with an object of type MyCLass. To call the method: MyClass mine= new MyClass(...); String x = mine.myMethod(); public class YourClass { public static String yourMethod(int y) {.... } static method is associated with the class itself. To call the method: String y = YourClass.yourMethod(211);

24 Interface An interface is a collection of constants and abstract methods. An abstract method has no code attached to it. It is just composed of the method header. A class implements an interface by providing method implementation for each of the abstract methods of the interface.

25 Interfaces A class that implements an interface public class ClassName implements InterfaceName {... } > InterfaceName ClassNameClass must define code for all methods defined in InterfaceName Interface defines the method signature for all required methods

26 Inheritance A class that extends another class public class ClassName extends SuperClassName {... } SuperClassName ClassNameSubClass may define code to override some or all of the SuperClass methods SuperClass defines all the methods for all SubClasses.

27 Inheritance All the public and protected methods of the super-class are accessible by the subclasses, but not the private ones. A subclass may have additional methods and instance variables of its own. The super-class does not know anything about its subclasses. A sub-class may override some or all of the methods of the super-class. (Example)

28 class/interface/abstarct class Difference between: –class and abstract class? –abstract class and interface?

29 Inheritance A class that extends an abstract class public class ClassName extends SuperClassName {... } > SuperClassName ClassNameSubClass may define code to override some or all of the SuperClass methods, but must define code for the signatures SuperClass defines some of the methods for all SubClasses, but only defines method signatures for the rest of the methods

30 Class Hierarchies and Polymorphism An object reference variable may hold as a reference to any compatible type of object Compatibility may be via implementing an interface or inheritance from another class ClassName a = new ClassName(); InterfaceName b = new ClassName(); SuperClassName c = new ClassName(); Object behaves as class it was “born as” (i.e. class used with the new operator)

31 Polymorphism via interface interface Service public int provide(); Class Service A Class Service B implements Service public int provide() { … } Clas Class Service A implements Service public int provide() { … } Service[] services = new Service[2]; services[0] = new ServiceA(...); services[1] = new ServiceB(...); for(int i = 0; i < services.length; i++) { services[i].provide(); }

32 Generic Types Collection classes like the ArrayList class can be defined to hold a specific type of object via a generic type designation ArrayList myList = new ArrayList (); We will use generics often in CS210 with many other types of “collection” classes

33 Exceptions When code encounters a situation that is impossible for it to resolve at that point, it may throw an Exception object, e.g. NameOfException instead of executing its normal return If a method may throw an exception, it should indicate that in its method header public void methodName() throws NameOfException { if (boolean condition of impossible situation) throw new NameOfException(); }

34 Process the Exception Three options to handle an Exception: –Not handle it at all and let the program terminate –Handle the exception where it occurs –Handle the exception at another point in the program (for example the caller of the method or caller of the caller of the method and... )

35 Exception Handling Code that calls a method that may throw a checked exception must use try-catch or indicate that it throws that exception in its own method header try { statements with call to methodName(); } catch (NameOfException e) // may be multiple catch clauses { statements to recover from occurrence of exception } finally // optional finally clause { statements always performed, e.g. clean up actions }

36 File Input: Example import java.util.Scanner; import java.io.*; public class FileDisplay { public static void main (String [] args) throws IOException { Scanner scan = new Scanner(System.in); System.out.println("Enter name of file to display"); File file = new File(scan.nextLine()); Scanner fileScan = new Scanner (file); while (fileScan.hasNext()) System.out.println(fileScan.nextLine()); }

37 File Output: Example import java.util.Scanner; import java.io.*; public class FileWrite { public static void main (String [] args) throws IOException { // Get filename and instantiate File object as before PrintStream out = new PrintStream(file); while (scan.hasNext()) { String line = scan.nextLine(); if (line.equals("END")) // A sentinel String value break; else out.println(line); } out.close(); }