1 / 27

Welcome to the 3rd Meeting of Java Discovering It’s Powers

Welcome to the 3rd Meeting of Java Discovering It’s Powers. Meeting Time: September Tuesday 6:30pm to 9:30pm SF Downtown Center, UC Berkeley Extension. Tonight’s Agenda List. API, and Java Methods Strings Arrays Object Oriented Programming / Inheritance Abstract class and interface

Download Presentation

Welcome to the 3rd Meeting of Java Discovering It’s Powers

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. Welcome to the 3rd Meeting ofJava Discovering It’s Powers Meeting Time: September Tuesday 6:30pm to 9:30pm SF Downtown Center, UC Berkeley Extension

  2. Tonight’s Agenda List • API, and Java Methods • Strings • Arrays • Object Oriented Programming / Inheritance • Abstract class and interface • Discuss of Programming Assignment two

  3. What material was covered in Meeting 3 ? • Idenitifiers, Tokens • Valid Java syntax and variable naming, what was white space, blocks {}, and semicolons. • Different styles of commenting • Primitives and default memory allocation for each primitive type • Looked at coding convention of capitalizing classes, and making methods to be verbs.

  4. Java API • Not a lot of programmers write code from scratch. • The Java Development kid included prewritten classes and packages(collection of related classes). • Prominent Package with the Java class library include • java.lang //include System, Object, and Class • java.awt //abstract windowing toolkit • java.applet //applet, internet client application • java.net //networking application • java.io //file input,output • java.util //collections, hashtables, and Random • We shall explore different packages as we go through the class

  5. == verse equal() • For example, String s1 = new String(“JDK1.4”); String s2 = new String(“JDK1.4”); if (s1 == s2) // true or false ? if (s1.equals(s2) ) //true or false ?

  6. Object, equals() • In java.lang class • Has a method public boolean equals(Object obj) { } • And most of the classes like Date, String, File, and the class you define can have this method to override what happens in the a object to object comparision. This is a deep comparision verses a sallow comparasion.

  7. Object, toString() • Example. Date now = new Date(); System.out.println(now); is translated into Date now = new Date(); System.out.println(now.toString() ); Where toString() has the following signature String toString() { do something} • The object class has the String toString() and is a derived method for all classes. The method just return class name and reference address

  8. Abstract Classes • Number class in the java.lang package represents the abstract concept of numbers • class that you define represents an abstract concept and, as such, should not be instantiated • I.e. color, shape, food (as we eat carrots, chocolate, so we eat real representation of food)

  9. Defining an abstract class • Definition of an abstract class uses the key work abstract abstract class Number { . . . } abstract class GraphicObject { int x, y; . . . void moveTo(int newX, int newY) { . . . } abstract void draw(); } • class Circle extends GraphicObject { • void draw() { . . . } • } • class Rectangle extends GraphicObject { • void draw() { . . . } • }

  10. Never instantiate an abstract class • So we would never say Number a = new Number(); //Number is an abstract class • But we could say Number a; a = new BigDecimal(“99999999999999999”); or a= new Integer(“5”);

  11. Interfaces • An interface defines a protocol of behavior that can be implemented • An interface is a named collection of method definitions (without implementations) • An interface can also declare constants. • A variation on the idea of abstract classes.

  12. Difference between an Abstract Class and an interface • An interface cannot implement any methods, whereas an abstract class can • A class can implement many interfaces but can have only one superclass. • An interface is not part of the class hierarchy. Unrelated classes can implement the same interface

  13. How to define an interface • Use the key word interface • All variables inside interface are “static final”, by default. • All methods are abstract

  14. Interface that extend other interfaces • SuperInterfaces are parent interface which InterfaceName inherits from, and there could be more than one

  15. In Java, there are many predefine interfaces • Runnable is an interface; Public interface Runnable { void run(); • Thread class is an implementation of Runnable interface Public class Thread extend Object implements Runnable { void run() { do something in the thread when it is awake} }

  16. Can Implement many interfaces Example: public class MyApplet extends Applet implements Runnable, MouseListener { ….. }

  17. Simple interface example interface SayHello { void printMessage(); } class SayHelloImpl implements SayHello { void printMessage() { System.out.println(“Hello”); } }

  18. What are interfaced good for ? • Useful for • Delcaring methods that one or more classes are expected to implment • Determining an object’s programming interface without revealing the actual body of the class • Capturing similarities between unrelated classes without forcing a class relationship • Describing “function-like” object that can be passed as parameters to methods invoked on other objects.

  19. Packages • Java provides a way group relayed classes. • For assignment 2, a package called flashcard could include classes like Card, Deck, and Session. • Package names are hierarchical and follow the dot convention. • Example package ucbx.financeDept; public class Employee { }

  20. Defining a Package • Package statement must be being of the source file and only one package statement is allow per source file • All files with the file package name will end up in the same package • As to using the packages in other files • Example import ucbx.financeDept.*; public class Manager extends Employee { Employee [] subordinates; Manager() { do something…}; }

  21. Packages and Directory structure • Packages are store in a directory tree containing the package name. package ucbx.financedept; public class Employee { } now suppose you run javac -d . Employee.java • What is the directory path for Employee.class ? • The roots of package trees to search for class files are in the CLASSPATH • The “-d” option in compiler uses the next argument as the root

  22. ClassPATH and new packages created • Suppose you needed to use the Employee.class file in other class files such as the Manager.class • Manager.class has a import statement import ucbx.financeDept; • When you compile you Manager.class, there is a compile error that tells you that Employee.class “ClassNoFound”. • What is the problem ??

  23. What was the error ? • Need to add directory of where you created packages to the classpath. • javac -d /home/anton/packages Employee.java • Need to add your package directory to the classpath. • Set CLASSPATH = CLASSPATH;/home/anton/packages

  24. Assignment 2 :This assignment will give you practice with classes, packages, and interfaces. The goal of this assignment is to emulate a deck of flashcards. These flashcards contain a word and a definition. All the classes written will be in a package called flashcards. Here are the following classes you will write:Card: A class which represents an individual card. It will be useful to override the toString method here to provide ease of displaying a Card.Deck: A class which represents a collection of cards. The Deck class will have responsibility such as shuffling itself, flashing a card on the top of the deck(display a card and after a constant wait period, it displays the definition), adding a Card to the Deck, deleting a Card, go to the next card, go to the previous card, check if there are Cards in the Deck.

  25. Session: A driver class to instaniate Deck class and present the user with a menu of the following choices: 1. Add a Card2. Delete a Card3. Show the current Card4. Move to the next Card5. Move to the previous Card6. Shuffle Deck7. Exit ProgramEnter Option[1-7]:1The user should be presented with this menu until he/she hits option 7.

  26. Details about the option: • Add a Card takes in the two user input, one is the word, and another is the definition • Delete a Card takes in no input and deletes the current card • Show the current Card will display the current card and after a delay time, the definition of the card is outputted to the console. • Move to the next Card will display the next card in the deck and will set that card to be the current card. • Move to the previous Card will display the previous card in the deck and will set that card to be the current card. • Shuffle Deck will randomize the position of the Cards. • Exit Program will exit out of the program.

  27. Notes about this assignment • You will need to use java.util collection classes such as Set or Vector class to create a dynamic array which can grow as you add new cards. • It will be a long assignment. • Wise to create some class diagrams to help design the implementation of program.

More Related