lecture4

advertisement
Lecture Four, OOP Examples and Java core package
Here you will find information on:




Card program(s)
Object Orientation
Java core package
Suplemental reading
Card program(s)
Card.java:
class Card {
// Instance Methods
public String getRank() {
return ranks[rank];
}
public String getSuit() {
return suits[suit];
}
public boolean higherRank(Card otherCard) {
return rank > otherCard.rank;
}
// Class Methods
public static void shuffle() {
for (int card =0; card < cards.length; card++) {
int othercard = (int)(Math.random()*100) % DECKSIZE;
Card temp = cards[othercard];
cards[othercard] = cards[card];
cards[card] = temp;
}
nextCard = 0;
}
public static Card deal() {
if (nextCard == DECKSIZE) {
return null;
}
else {
return cards[nextCard++];
}
}
// Instance Variables
private int rank;
private int suit;
// Constructor
private Card(int newSuit, int newRank) {
suit = newSuit;
rank = newRank;
}
public Card() {
Card.deal();
}
// Class Variables
private
private
private
private
private
private
static
static
static
static
static
static
final
final
final
final
final
final
int DECKSIZE =
Card[] cards =
int NUMRANKS =
int NUMSUITS =
String[] suits
String[] ranks
52;
new Card[DECKSIZE];
13;
4;
= {"C", "D", "H", "S"};
= {"A", "2", "3", "4","5","6",
"7",
"8","9","10","J","Q","K"};
private static int nextCard;
// static initializer
static {
int cardNumber = 0;
for (int cardSuit =0; cardSuit <NUMSUITS ; cardSuit++) {
for (int cardRank =0; cardRank <NUMRANKS; cardRank++) {
cards[cardNumber++] = new Card(cardSuit, cardRank);
}
}
shuffle();
}
public static void main (String[] args) {
System.out.println("Class Started");
Card card1 = new Card(1,1);
System.out.println(card1.getSuit());
System.out.println(card1.getRank());
Card.shuffle();
Card card2 = Card.deal();
System.out.println(card2.getSuit() + " "+ card2.getRank());
}
}
myCard.java:
class myCard extends Card {
public static Card mydeal() {
Card card1 = Card.deal();
while ((card1.getRank()).compareTo("J") < 0)
{
// System.out.println(card1.getRank());
card1 = Card.deal();
}
return card1;
}
public static void main(String[] args) {
System.out.println("In myCard class");
Card mycard1 = myCard.mydeal();
System.out.println(mycard1.getSuit() + " "+
mycard1.getRank());
}
}
Object Orientation

getRank, getSuit, higherRank are instance methods of Card class. These methods
are associated with a particular instance of a class.

shuffle and deal are class methods of the Card class. These methods deal with the
class as a whole rather than with individual instances of the class.

rank and suit are instance variables for the Card class. Each card has its own
copies of rank and suit.
Card(..) is the constructor for the Card class. A call to it creates a new instance of
Card.
Class variables are denoted with the keyword static. There is only one copy of
each of these for the whole class.
Static initializer is used to initialize the class data when a class is first created.
Inheritance is done using the keyword extends. All the methods and attributes of
the class are applicable to the subclass. In this example myCard is the subclass.
When this file is compiled 2 class files are created. Each class can be interpreted
seperately.





Java core package
The /usr/local/jdk1.2/docs/api/overview-summary.html contains documentation on the
Java Platform Packages. Here are some highlights on some of the packages in the
platform.
General

java.lang - Provides components that are fundamental to the design of the Java
programming language. (Example: Object, System, Math, String, Integer, Short,
Long, etc.)

java.applet - Provides the components necessary to create an applet and the
components an applet uses to communicate with its applet context

java.util - Provides components for containers, date and time facilities,
internalization, etc.

java.util.zip - Provides components for reading and writing the standard ZIP and
GZIP file formats.
File

java.io - Provides components for system input and output through data streams,
serialization and the file system.
Imaging and Graphical User Interface

java.awt, javax.swing - Contains all of the components for creating user interfaces
and for painting graphics and images.
Network, Security and Database

java.net - Provides components for implementing TCP/IP networking
applications. (Example: Socket, URL, InetAddress, etc.)

java.security - Provides the classes and interfaces for the security framework.
(Certificates, Cryptography, etc. components.)

java.sql - Provides components for executing SQL (Stuctured Query Language)
statements. (Database components.)
The source for the packages are in /usr/local/jdk1.2/src.jar. The file with .jar extension is
compatible with the .zip type compression. Here is a snap-shot of src.jar with Applet.java
being selected under WinZip.
Figure 1, WinZip view of src.jar
Suplemental reading

Object-Oriented Programming Concepts
http://java.sun.com/docs/books/tutorial/java/concepts/index.html

Object and Classes in Java
http://java.sun.com/docs/books/tutorial/java/javaOO/index.html

Building Applications
http://developer.java.sun.com/developer/onlineTraining/Programming/BasicJava
1/prog.html
Download