1 / 21

CM0551 Lecture 2

CM0551 Lecture 2. Object-Oriented Programming Concepts API’s The scanner class – file input A Spelling Checker Time and space requirements for various dictionary structures. Object-Oriented Programming Concepts.

dawson
Download Presentation

CM0551 Lecture 2

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. CM0551 Lecture 2 • Object-Oriented Programming Concepts • API’s • The scanner class – file input • A Spelling Checker • Time and space requirements for various dictionary structures

  2. Object-Oriented Programming Concepts The sun web site holds lots of VERY useful information and tutorials, Study this one THIS WEEK – go to the following url http://java.sun.com/docs/books/tutorial/java/concepts/index.html • What Is an Object? • What Is a Class? • What Is Inheritance? • What Is an Interface? • What Is a Package?

  3. More tutorials – enjoy! http://www.javaworld.com/javaworld/jw-04-2001/jw-0406-java101.html http://www.developer.com/java/article.php/935351 http://java.sun.com/javase/6/docs/api/java/util/package-summary.html Or just http://java.sun.com/javase/6/docs/api/. A very useful URL

  4. API's and Javadoc (http://www.apl.jhu.edu/~hall/java/beginner/api.html) • An API stands for a "Application Programming Interface“ for Java in this case. • It is like a super dictionary of the Java language, that one uses to look something up in. • It has an index of all Java packages, classes and interfaces, with all of their methods, fields and constructors, and how to use them. • When one programs, there are many classes that are commonly used, and therefore pre created, so that the programmer doesn't need to create them from scratch.

  5. So simplistically programming becomes; • Finding appropriate classes in an API. • Writing a programme to use a class to create a variable/object of that class. TheClass myObject = new ... • Then invoking (executing) the class’s methods to perform the required process to get the job done. myObject.methodA(... myObject.methodD(... myObject.methodB(...

  6. Sun's classes (not the red top UK paper) • As with all of Sun's classes, we do not care how it is implemented, all we need to know are what the methods are, the constructors, and it's fields. • Hence the name Java Application Programming Interface, the API is a collection of interfaces telling us what each class can do, not telling us how it is done. • The API contains many packages for many different purposes, among them, the awt and swing packages for graphics and GUI, the io package for input and output, the sql package for Java databasing, and many many more.

  7. Using API’s • For every package that you use a class from you must import it, with the exception of the java.lang package that deals with basic programming classes, that is automatically imported into your program. • Each API is organized in the following way. When you start the API you are given three frames, the first listing all of the Java packages in that API, and when you click on any particular package, the second frame lists all of the interfaces, classes, and exceptions in that package

  8. The Scanner class http://java.sun.com/javase/6/docs/api/java/util/Scanner.html • Can be used for keyboard input • Part of JDK 1.6 • import java.util.Scanner; • A simple text scanner which parses primitive types and strings • Uses a delimiter pattern of whitespace to identify tokens

  9. Example Program 1 import java.util.Scanner; public class Example1 { public static void main (String [] args) { Scanner myScanner = new Scanner( System.in ); String aLine = myScanner.nextLine(); System.out.println( aLine); } } keyboard

  10. Example Program 1 cont’ import java.util.Scanner; // Tells Java to look for the Scanner class Scanner myScanner = new Scanner( System.in ); // Declares a Scanner object called myScanner String aLine = myScanner.nextLine(); // nextLine() is a method of the Scanner class // it reads a line of text from the keyboard.

  11. Some Scanner Methods

  12. Example Program 2 import java.util.Scanner; public class Example2 { public static void main (String [] args) { Scanner myScanner = new Scanner( System.in ); int anInt = myScanner.nextInt(); System.out.println( anInt); } }

  13. Example Program 3 import java.util.Scanner; public class Example3 { public static void main (String [] args) { Scanner myScanner = new Scanner( System.in ); int anInt = myScanner.nextInt(); double aDouble = myScanner.nextDouble(); System.out.println( “The int was “+anInt); System.out.println( “The double was “+aDouble); } }

  14. Scanner class and file input import java.util.*; import java.io.*; public class ReadAccs { public static void main(String [] args) { TreeSet accs = new TreeSet();

  15. Scanner class and file input (2) File name try { readAccounts ("D:\\DSA&DP\\w3\\studentAccs.txt", accs); } catch(IOException ioe) { System.out.println("Cannot open file"); System.exit(1); } System.out.println(accs); }

  16. Scanner class and file input (3) public static void readAccounts(String filename, TreeSet accs) throws IOException { Scanner sc = new Scanner(new FileReader(filename)); String accName; while (sc.hasNext()) { accName = sc.next(); accs.add(accName); } } }

  17. Data and results • File studentAccs contains p01234 p01235 p01236 p01237 p01238 p01239 p01240 Program output [p01234,p01235,p01236,p01237,p01238,p01239,p01240]

  18. A simple spell checker method public static void spellChecker() { TreeSet dictionary = new TreeSet(); Scanner dictFile = null, docFile = null; String word; try { dictFile = new Scanner(new FileReader ("D:\\DSA&DP\\w3\\dict.txt")); docFile = new Scanner(new FileReader ("D:\\DSA&DP\\w3\\doc.txt")); }

  19. A simple spell checker method (2) catch(IOException ioe) { System.out.println("Cannot open a file"); System.exit(1); } while (dictFile.hasNext()) { word = dictFile.next(); dictionary.add(word); }

  20. A simple spell checker method (3) System.out.println ("The following words are not in the dictionary!"); while (docFile.hasNext()) { word = docFile.next(); if (!dictionary.contains(word)) { System.out.println(word); } } }

  21. Time and space requirements for various dictionary structures time (sec) space Array and sequential search 57 714K Linked list and sequential search 57 1.3M Array (sorted) and binary search .04 714K Binary search tree .04 1M Two level packed tree .08 360K Good data structure design is important

More Related