Introduction Richard Soden ECET-370 Data Structures and Algorithms
Topics Course Introduction Abstract Data Types (ADTs) Terminology Types of ADTs Introduction to Java Background and Characteristics Java Program Development Anatomy of a Simple Output Program Getting User Input: The Scanner Class
Organizing Our Lives For each of the above examples, consider how the objects are organized
Organizing Computer Data Computer stores/organizes items in similar manners as the examples Ways of organizing data are represented by specifications called  Abstract Data Types  (ADTs)
ADT Terminology  An ADT specifies: Kind of data that is stored Operations that can be done on the data Data structure:  implementation of an ADT within a programming language, sometimes called a  Container Collection:  a Data Structure that contains a group of objects
Types of ADTs Bag Unordered collection, may contain duplicates List A collection that numbers its items Stack Orders items chronologically (Last In, First out) Queue Orders items chronologically (First in, First out) Dictionary Pairs of items – one is a key Tree Arranged in a hierarchy Graph Generalization of a tree Match each of these to the pictures ? Click here to return to pictures
Brief History of Java James Gosling and Sun Microsystems 1991 Originally called Oak Java, May 20, 1995, Sun World HotJava  The first Java-enabled Web browser
JDK Versions JDK 1.02 (1/1996) JDK 1.1 (2/1997) J2SE 1.2 (12/1998) J2SE 1.3 (5/2000) J2SE 1.4 (2/2002) J2SE 5.0 (9/2004) Java SE 6 (12/2006) Java SE 7 (Preview versions are  available - codename Dolphin)
JDK Editions Java Standard Edition (J2SE) J2SE can be used to develop client-side standalone applications or applets. Java Enterprise Edition (J2EE) J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages.  Java Micro Edition (J2ME)  J2ME can be used to develop applications for mobile devices such as cell phones.
Characteristics of Java Java is simple Java is object-oriented Java is distributed Java is interpreted Java is robust Java is secure Java is architecture-neutral Java is portable Java is multithreaded Java is dynamic Java performance?
Free Java Development Tools Editors: TextPad Integrated Development Environments (IDEs): Xinox JCreator NetBeans Eclipse Borland JBuilder Oracle JDeveloper
A Simple Application Example /** This application program prints  *  Welcome to Java! */ public class Welcome { public static void main(String[] args) {  System.out.println("Welcome to Java!"); } } Run
Creating and Compiling Programs On command line: javac  filename .java Compiling produces Java Virtual Machine code called Bytecode
Executing Applications On command line: java  classname
Example javac Welcome.java java Welcome output:...
Anatomy of a Java Program Comments Reserved words Modifiers Statements Blocks Classes Methods The main method
Comments Line comments are preceded by two slashes ( // ) in a line, and Block comments are enclosed between  /*  and  */  in one or multiple lines.  When the compiler sees  // , it ignores all text after  //  in the same line. When it sees  /* , it scans for the next  */  and ignores any text between  /*  and  */ .
Reserved Words Reserved words  or  keywords  are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word  class , it understands that the word after  class  is the name for the class. Other reserved words in Example 1.1 are  public ,  static , and  void .
Modifiers Java uses certain reserved words called  modifiers  that specify the properties of the data, methods, and classes and how they can be used.  Examples of modifiers are  public  and  static . Other modifiers are  private ,  final ,  abstract , and  protected .  A  public  datum, method, or class can be accessed by other programs.  A  private  datum or method cannot be accessed by other programs .
Statements A  statement  represents an action or a sequence of actions. The statement: System.out.println("Welcome to Java!") ;  in the Example program is a statement to display the greeting "Welcome to Java!"  Every statement in Java ends with a semicolon (;).
Blocks A pair of braces in a program forms a block that groups components of a program.
Classes The  class  is the essential Java construct. A class is a template or blueprint for  objects.  To program in Java, you must understand classes and be able to write and use them.
Methods System.out.println  is a  method : a collection of statements that performs a sequence of operations.  It is used by invoking a statement with a string argument enclosed in parentheses.  In this case, the argument is  "Welcome to Java!"   You can call the same  println  method with a different argument to print a different message.
main Method The  main  method provides the control of program flow. The Java interpreter executes the application by invoking the  main  method.    The  main  method looks like this:   public static void main(String[] args) { // Statements; }
Getting User Input The Java class Scanner is one of the many predefined classes in the Java system, which can be reused rather than “reinventing the wheel.”   The Scanner class is used to get user input in a command window as shown in the following program.
/** InputExample.java: This application program repeatedly  asks the user if he wants to learn Java until the user    types “yes”. */ import java.util.Scanner;  // Note: Need to import Scanner public class InputExample { public static void main(String[] args) {   Scanner myInput = new Scanner(System.in);   String answer;   do { System.out.println("Do you want to learn Java now?"); answer = myInput.next();   } while (!answer.equals("yes"));   System.out.println("Excellent! Let's begin..."); } } Getting User Input Run
Getting User Input Three things are needed to get user input with Scanner: 1.  Import the Java Scanner utility: import java.util.Scanner; 2.  Create a Scanner object that is attached to the  “standard input stream” System.in: Scanner  myInput  =    new Scanner(System.in); 3.  Capture and store the next input using your  Scanner object and appropriate input function: answer = myInput.next();
Scanner Functions next()   Returns next word as a String  nextLine()   Returns next line as a String  nextInt()   Returns next integer as an int nextDouble  Returns next double as a double hasNext()   Tests if there is another word hasNextInt()   Tests if next input is an int hasNextDouble  Tests if next input is a double
Thank you!

Introduction

  • 1.
    Introduction Richard SodenECET-370 Data Structures and Algorithms
  • 2.
    Topics Course IntroductionAbstract Data Types (ADTs) Terminology Types of ADTs Introduction to Java Background and Characteristics Java Program Development Anatomy of a Simple Output Program Getting User Input: The Scanner Class
  • 3.
    Organizing Our LivesFor each of the above examples, consider how the objects are organized
  • 4.
    Organizing Computer DataComputer stores/organizes items in similar manners as the examples Ways of organizing data are represented by specifications called Abstract Data Types (ADTs)
  • 5.
    ADT Terminology An ADT specifies: Kind of data that is stored Operations that can be done on the data Data structure: implementation of an ADT within a programming language, sometimes called a Container Collection: a Data Structure that contains a group of objects
  • 6.
    Types of ADTsBag Unordered collection, may contain duplicates List A collection that numbers its items Stack Orders items chronologically (Last In, First out) Queue Orders items chronologically (First in, First out) Dictionary Pairs of items – one is a key Tree Arranged in a hierarchy Graph Generalization of a tree Match each of these to the pictures ? Click here to return to pictures
  • 7.
    Brief History ofJava James Gosling and Sun Microsystems 1991 Originally called Oak Java, May 20, 1995, Sun World HotJava The first Java-enabled Web browser
  • 8.
    JDK Versions JDK1.02 (1/1996) JDK 1.1 (2/1997) J2SE 1.2 (12/1998) J2SE 1.3 (5/2000) J2SE 1.4 (2/2002) J2SE 5.0 (9/2004) Java SE 6 (12/2006) Java SE 7 (Preview versions are available - codename Dolphin)
  • 9.
    JDK Editions JavaStandard Edition (J2SE) J2SE can be used to develop client-side standalone applications or applets. Java Enterprise Edition (J2EE) J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages. Java Micro Edition (J2ME) J2ME can be used to develop applications for mobile devices such as cell phones.
  • 10.
    Characteristics of JavaJava is simple Java is object-oriented Java is distributed Java is interpreted Java is robust Java is secure Java is architecture-neutral Java is portable Java is multithreaded Java is dynamic Java performance?
  • 11.
    Free Java DevelopmentTools Editors: TextPad Integrated Development Environments (IDEs): Xinox JCreator NetBeans Eclipse Borland JBuilder Oracle JDeveloper
  • 12.
    A Simple ApplicationExample /** This application program prints * Welcome to Java! */ public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Run
  • 13.
    Creating and CompilingPrograms On command line: javac filename .java Compiling produces Java Virtual Machine code called Bytecode
  • 14.
    Executing Applications Oncommand line: java classname
  • 15.
    Example javac Welcome.javajava Welcome output:...
  • 16.
    Anatomy of aJava Program Comments Reserved words Modifiers Statements Blocks Classes Methods The main method
  • 17.
    Comments Line commentsare preceded by two slashes ( // ) in a line, and Block comments are enclosed between /* and */ in one or multiple lines. When the compiler sees // , it ignores all text after // in the same line. When it sees /* , it scans for the next */ and ignores any text between /* and */ .
  • 18.
    Reserved Words Reservedwords or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class , it understands that the word after class is the name for the class. Other reserved words in Example 1.1 are public , static , and void .
  • 19.
    Modifiers Java usescertain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers are public and static . Other modifiers are private , final , abstract , and protected . A public datum, method, or class can be accessed by other programs. A private datum or method cannot be accessed by other programs .
  • 20.
    Statements A statement represents an action or a sequence of actions. The statement: System.out.println("Welcome to Java!") ; in the Example program is a statement to display the greeting "Welcome to Java!" Every statement in Java ends with a semicolon (;).
  • 21.
    Blocks A pairof braces in a program forms a block that groups components of a program.
  • 22.
    Classes The class is the essential Java construct. A class is a template or blueprint for objects. To program in Java, you must understand classes and be able to write and use them.
  • 23.
    Methods System.out.println is a method : a collection of statements that performs a sequence of operations. It is used by invoking a statement with a string argument enclosed in parentheses. In this case, the argument is "Welcome to Java!" You can call the same println method with a different argument to print a different message.
  • 24.
    main Method The main method provides the control of program flow. The Java interpreter executes the application by invoking the main method.   The main method looks like this:   public static void main(String[] args) { // Statements; }
  • 25.
    Getting User InputThe Java class Scanner is one of the many predefined classes in the Java system, which can be reused rather than “reinventing the wheel.” The Scanner class is used to get user input in a command window as shown in the following program.
  • 26.
    /** InputExample.java: Thisapplication program repeatedly asks the user if he wants to learn Java until the user types “yes”. */ import java.util.Scanner; // Note: Need to import Scanner public class InputExample { public static void main(String[] args) { Scanner myInput = new Scanner(System.in); String answer; do { System.out.println("Do you want to learn Java now?"); answer = myInput.next(); } while (!answer.equals("yes")); System.out.println("Excellent! Let's begin..."); } } Getting User Input Run
  • 27.
    Getting User InputThree things are needed to get user input with Scanner: 1. Import the Java Scanner utility: import java.util.Scanner; 2. Create a Scanner object that is attached to the “standard input stream” System.in: Scanner myInput = new Scanner(System.in); 3. Capture and store the next input using your Scanner object and appropriate input function: answer = myInput.next();
  • 28.
    Scanner Functions next() Returns next word as a String nextLine() Returns next line as a String nextInt() Returns next integer as an int nextDouble Returns next double as a double hasNext() Tests if there is another word hasNextInt() Tests if next input is an int hasNextDouble Tests if next input is a double
  • 29.