CM0551 Lecture 2

eyelashesnectarineSoftware and s/w Development

Nov 3, 2013 (3 years and 9 months ago)

94 views

2
-
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


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?

2
-
2

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/
.

2
-
3

A very useful URL

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.

2
-
4

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(...


2
-
5

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.


2
-
6

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

2
-
7

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




2
-
8

2
-
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

2
-
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.




2
-
11

Some Scanner Methods

To read this


Use this

A number with no decimal
point

nextInt();

A number with a decimal point

nextDouble();

A word ending in a blank space

next();

A line ( or what remains of the
line)

nextLine();

A single character

findInLine(“.”).charAT(0);

2
-
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);


}

}

2
-
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);


}

}

2
-
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();




2
-
15

Scanner class and file input (2)



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);


}

File
name

2
-
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);



}



}


}

2
-
17

Data and results


File studentAccs contains

p01234

p01235

p01236

p01237

p01238

p01239

p01240

Program output

[p01234,p01235,p01236,p01237,p01238,p01239,p01240]

2
-
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"));



}




2
-
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);



}





2
-
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);




}



}


}

2
-
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