Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
Advertisements

Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 10 Introduction to Arrays
Lab#1 (14/3/1431h) Introduction To java programming cs425
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Programming in Java; Instructor:Alok Mehta Objects, Classes, Program Constructs1 Programming in Java Objects, Classes, Program Constructs.
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.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 Course Lectures Available on line:
Java and C++, The Difference An introduction Unit - 00.
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.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
COS 240 Object-Oriented Languages The Anatomy of a Java Program AUBG, Spring 2014 Svetla Boytcheva.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
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.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Final Review Lecture 14.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
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.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
Copyright Curt Hill Variables What are they? Why do we need them?
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
By Mr. Muhammad Pervez Akhtar
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Object Oriented Programming Lecture 2: BallWorld.
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Most of these slides are.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Information and Computer Sciences University of Hawaii, Manoa
JAVA MULTIPLE CHOICE QUESTION.
Objectives You should be able to describe: Interactive Keyboard Input
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Objects, Classes, Program Constructs
C Basics.
Java Review: Reference Types
Java Programming: From Problem Analysis to Program Design, 4e
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Java Programming Language
IFS410 Advanced Analysis and Design
Presentation transcript:

Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2

Department of Computer Science2 Course Goals First three weeks: - To learn about Java programming - To learn about data structures – in particular, learning how to use those provided with Java (in libraries) Second three weeks: - To learn about Polymorphism - To learn about Java GUI programming (Swing) - To learn more about Java libraries such as FileIO and streams

Department of Computer Science3 Java programming language Developed by Sun in the early 1990s Object-oriented: programs consist of objects that interact with each other Platform-independent: programs can run on many operating systems  Java code is translated to byte code, which is executed by a JVM Similar to C#

Department of Computer Science4 Course Outline Lectures (Mon, Wed, Fri) Labs every day – supervised Assessment = 6 weekly assignments + 2 tests

Department of Computer Science5 Data Structures A way of organizing data so that certain operations can be performed efficiently Data structures are generic (ideally) Examples (linked lists, stacks, queues, trees, hash tables, heaps) Many of these are implemented in Java’s Collection Framework

Department of Computer Science6 IDEs Most programming languages have integrated development environments For Java we will use Eclipse For instructions on how to set up Eclipse please see the 203 homepage at Note that you can use Version 5 or Version 6 of Java – the book is based on Version 5 and Version 6 is the latest.

Department of Computer Science7 Java – summary of syntax Comments  One-line use:  // This is a comment  Multi-line use:  /* This is a bit longer */  Javadoc  /** Automatic documentation! */

Department of Computer Science8 Types Primitive types and constants: byte aByte = 12; // -128 to 127 short aShort = 354; // -32,768 to 32,767 int anInt = 55677; // +- 2 billion long aLong = ; // +-2 power 63 float aFloat = 32.1f; // 32-bit floating point 6 sdig double aDouble = 26.5; // 64-bit 15 sig digits char aChar = ‘b’; // Unicode 30,000 chars! boolean aBool = true;

Department of Computer Science9 Compiling Java code The command javac file.java compiles source code and creates bytecode  Source code is stored in.java files  Bytecode is stored in.class files The command java starts the Java Virtual Machine (JVM) and executes the bytecode  The bytecode is translated into machine-code  Most JVMs are just-in-time compilers

Department of Computer Science10 Useful methods When the bytecode for a.java file is executed, the main method is called: public static void main (String[ ] args) { }  This is the starting point of a Java program To get output from your program you can write to the standard output stream using the println method System.out.println(“Hello World”);

Department of Computer Science11 Basic Operators Assignment operators:  +, +=, -=, *=, /= Binary arithmetic operators:  +, -, *, /, % (remainder) Unary operators:  -, ++, -- Type conversion operators:  int x = 6; int y = 10;  double q = (double) x / y;

Department of Computer Science12 Conditional statements Equality operators:  ==, != Relational operators: , >= Logical operators:  &&, ||, ! The if statement if (expression) statement next statement

Department of Computer Science13 Conditionals continued The switch statement can be used when several options are needed Break statement exits from the innermost loop or switch statement Continue statement goes to the next iteration of the innermost loop immediately (ie avoiding the statements of the loop) Conditional operator  testExpr ? yesExpr : noExpr

Department of Computer Science14 Methods Methods can be overloaded: a class can have multiple methods with the same name (but different parameter lists) Public methods are visible in other classes Static methods are not bound to a particular object

Department of Computer Science15 Loops The for statement:  for (initialisation; test; update) statement next statement The while statement:  while (expression) statement next statement The do statement:  do statement while (expression); next statement

Department of Computer Science16 Methods Basic method header has a name, return type and parameter list The method declaration includes the body Arguments are copied into the parameters: so Java is pass by value The return statement is used to return a value to the caller  return must be present unless method is void

Department of Computer Science17 Reference types All types that are not primitive types are reference types  A reference variable holds the address of an object References can be compared using == and !=  This checks whether two variables refer to exactly the same object (ie they hold the same address) References have the value null if they do not refer to an object.

Department of Computer Science18 Objects An instance of any of the non-primitive types is an object The keyword new is used to invoke the constructor of an object The dot operator is used to access an object via a reference variable Objects are garbage-collected when they are no longer referenced by any variable

Department of Computer Science19 Arrays Indexed from 0 The [ ] operator is used to index an array An array behaves like an object  new is used to create a new array  An array variable is a reference to an array Multi-dimensional Dynamic arrays achieved via the ArrayList

Department of Computer Science20 Exception handling Handling exceptional circumstances incl errors A try block encloses code that might generate an exception A catch block processes the exception A finally block maybe used which is always executed Used for runtime (array bounds, null pointer etc), checked (IO, file not found) and errors Exceptions are thrown

Department of Computer Science21 Strings Objects with special properties:  Immutable – cannot change once constructed  Can be concatenated using + To check equality use the equals method Many other useful methods (check the Javadoc)

Department of Computer Science22 I/O in Java The java.io package contains methods for input and output Packages are made available in your class using an import statement  java.lang does not need an import statement Java I/O is based on streams Predefined streams include System.out, System.in and Sytem.err

Department of Computer Science23 Examples To read lines of input from the terminal  Create an InputStreamReader from System.in  Create a BufferedReader from the InputStreamReader  Call the readLine method on the BufferedReader To split up a line (string) into sub strings use a StringTokenizer For file I/O you can use a FileReader and a FileWriter