1 / 58

Some Background Activities Around Classes

Some Background Activities Around Classes. The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and Grade Book Test Classes. Another Example. // Class declaration with one method. public class GradeBook {

iola
Download Presentation

Some Background Activities Around Classes

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. Some Background ActivitiesAround Classes • The last day!!!! We discussed some classes and programs that called them. • For example we had the GradeBook and Grade Book Test Classes

  2. Another Example • // Class declaration with one method. • public class GradeBook • { • // display a welcome message to the GradeBook user • public void displayMessage() • { • System.out.println( "Welcome to the Grade Book!" ); • } // end method displayMessage • } // end class GradeBook

  3. This is called from this class • // Create a GradeBook object and call its displayMessage method. • public class GradeBookTest • { • GradeBookTest() • { • // create a GradeBook object and assign it to myGradeBook • // call the constructor for the class GradeBook • GradeBook myGradeBook = new GradeBook(); • // call myGradeBook's displayMessage method • myGradeBook.displayMessage(); • } // end main • public static void main (String[] args) { • // Start the program running from its constructor • new GradeBookTest();} • } // end class GradeBookTest

  4. Consider what is happening • We are creating an instance of GradeBook from this class GradeBookTest • See the line • GradeBook myGradeBook = new GradeBook(); • We then call on the line • myGradeBook.displayMessage(); • This calls the displayMessage method from the GradeBook class

  5. Next Lecture • How do we implement all this • We need to do this by linking all the classes together. • One way of doing this is with the –classpath qualifier at compilation time using the javac command • We have already seen this at run time with the java command. But we can use the same idea at compilation time • This will tell the compiler where to find classes. • See the following dialogue

  6. Directory mnemonics • In Dos the directory hierarchy uses some short cuts • \ refers to the root directory • So if we type cd \ • We will be returned to the root directory • The parent directory of any directory is indicated by .. • So if we were in C:\johns\java\progs and we typed cd .. • We will be returned to C:\johns\java

  7. Directory mnemonics . notation • The current directory is indicated by . • So when we use . We are referring to the current directory • So javac –classpath . GradeBook will look for classes in the current directory. So the java classes used must be there.

  8. Consider the following two Classes • Book • And Bookstore1 due to June Barrett • Their Code is

  9. Book.java • public class Book { • // Declare instance fields • String name; • int price; • // The constructor initialises the instance fields • Book (String n, int p) { • name = n; • price = p; • } • // a method to output the object details • void write() { • System.out.println(name + " for £" +price); • } • }

  10. Book.java • public class Book1{ • // Declare instance fields • String name; • int price String booktype; • // The constructor initialises the instance fields • Book1 (String n, int p,String bt) { • name = n; • price = p; • Booktype = bt; • } • // a method to output the object details • void write() { • System.out.println(name + " for £" +price); • } • }

  11. Actual price method • void writeactualprice() { float tot; If (booktype == “hardback”) tot = price * 2; else if (booktype ==“paperback”) tot = price * 1.5; else if (booktype ==“booklet”) tot = price * 0.5; else tot = price * 3; • System.out.println(name + " for £" +tot); • }

  12. BookStore1.java • public class BookStore1 { • /* Illustrating the basic structure of an object oriented program */ • // Declare three object variables representing type of • // goods sold in the store • Book TextBook, Novel,ShortStory;

  13. BookStore1.java continued • // The constructor for the program is • // where the initial work gets done • BookStore1 () { • // Create three objects with different initial values • TextBook = new Book("Java Programming", 6); • Novel = new Book("The Unsung Hero", 30); • ShortStory = new Book("Stories for 5 year olds", 80); • // Print out a header • System.out.println("The Book Store sells\n"); • // Print the values contained in each of the objects • TextBook.write(); • Novel.write(); • ShortStory.write(); • }

  14. And Finally • // The program control class must have a main method • public static void main (String[] args) { • // Start the program running from its constructor • new BookStore1 (); • } • }

  15. The Constructor BookStore1 • We see from the code that the constructor BookStore1() make three instance of the class Book, namely TextBook,Novel and ShortStory • It then calls on the Book write method to display details about the book.

  16. Next How do we compile these • Firstly Book.java and BookStore1.java must be in the same directory

  17. From a Directory Listing • We see that they are both in the c:\ directory • Next we compile and run the classes using the • Javac and Java commands with • –classpath .

  18. From this screen we see • That the class correctly executes the specific objects.

  19. Some Dos issues • The javac command is in the jdk bin subdirectory and I am fed up typing out • C:\program files\java\jdk1.5.0_16\bin\javac • Or whatever in order to use the command

  20. Solution set a dos path • set path=c:\program files\java\jdk1.5.0_16\bin • Then the O/S will try this path when you invoke javac

  21. Packages • An alternative way of accessing classes • We have already seen • import java.util.Scanner; • For example in our Leapyear program

  22. Leapyear Class • // Second Program HelloWorld • import java.util.Scanner; • public class Leapyear { • public static void main(String args []){ • Scanner input = new Scanner( System.in ); • String theName = input.nextLine(); • int num1 = Integer.parseInt(theName); • if(num1 % 4 ==0 && (num1 % 100 != 0 ||num1 % 400 == 0)) • System.out.println("leap"); • else • System.out.println("not leap"); • }}

  23. Java Packages • java.util.Scanner is a set of classes held in java listing device known as a package • This allows us keep java classes in directories and subdirectories and import them into our user defined classes

  24. How do we create Packages • Consider our HelloWorld.java class, • Say we want to create a package world which will contain the HelloWorld class. • All classes in the package must contain the package Keyword at the start • First thing we have to do is to specify the keyword package with the name of the package we want to use (world in our case) on top of our source file, before the code that defines the real classes in the package, as shown in our HelloWorld class below:

  25. HelloWorld.java • package world; • public class HelloWorld { • public static void main(String[] args) { System.out.println("Hello World"); • } • }

  26. Create a world directory • One thing you must do after creating a package for the class is to create nested subdirectories to represent package hierachy of the class. In our case, we have the world package, which requires only one directory. So, we create a directory world and put our HelloWorld.java into it.

  27. Then to use it we can use the import statement at the start of another class • Consider • The following variations of our Book class

  28. Bookjg1.java • package jgpack; • public class Bookjg1 { • // Declare instance fields • public String name; • public int price; • // The constructor initialises the instance fields • public Bookjg1 (String n, int p) { • name = n; • price = p; • } • // a method to output the object details • public void write() { • System.out.println(name + " for £nookjg1" +price); • } • }

  29. Bookjg2.java • package jgpack; • public class Bookjg2 { • // Declare instance fields • String name; • int price; • // The constructor initialises the instance fields • public Bookjg2 (String n, int p) { • name = n; • price = p; • } • // a method to output the object details • public void write() { • System.out.println(name + " for £nookjg2" +price); • } • }

  30. Using a mkdir command • We create a directory jgpack; • We put these files into a directory jgpack;

  31. Going back to c:\ • We have a modified version of BookStore1

  32. BookStorejg1.java • import jgpack.*; • public class BookStorejg1 { • /* Illustrating the basic structure of an object oriented program */ • // Declare three object variables representing type of • // goods sold in the store • Bookjg1 TextBook; • Bookjg2 Novel,ShortStory;

  33. Part 2 • // The constructor for the program is • // where the initial work gets done • BookStorejg1 () { • // Create three objects with different initial values • TextBook = new Bookjg1("Java Programming", 6); • Novel = new Bookjg2("The Unsung Hero", 30); • ShortStory = new Bookjg2("Stories for 5 year olds", 80); • // Print out a header • System.out.println("The Book Store sells\n"); • // Print the values contained in each of the objects • TextBook.write(); • Novel.write(); • ShortStory.write(); • }

  34. Part 3 • // The program control class must have a main method • public static void main (String[] args) { • // Start the program running from its constructor • new BookStorejg1 (); • } • }

  35. BookStorejg1.java • Note at the start we use the statement • import jgpack.*; • The wildcard * gets all files in the package jgpack • Then we use these classes for declaring our book objects • // goods sold in the store • Bookjg1 TextBook; • Bookjg2 Novel,ShortStory;

  36. Part 2 • // The constructor for the program is • // where the initial work gets done • BookStorejg1 () { • // Create three objects with different initial values • TextBook = new Bookjg1("Java Programming", 6); • Novel = new Bookjg2("The Unsung Hero", 30); • ShortStory = new Bookjg2("Stories for 5 year olds", 80); • // Print out a header • System.out.println("The Book Store sells\n"); • // Print the values contained in each of the objects • TextBook.write(); • Novel.write(); • ShortStory.write(); • }

  37. Here • We use the constructors to create new instances of these objects and procede as before

  38. We can also use subdirectories • Say we create a subdirectory for jgpack called subjgpack then we can create a subpackage for jgpack • We include another variation of book in here

  39. Our variation bookstore is now

  40. Part 1 • import jgpack.*; • import jgpack.subjgpack.*; • public class BookStorejg2 { • /* Illustrating the basic structure of an object oriented program */ • // Declare three object variables representing type of • // goods sold in the store • Bookjg1 TextBook; • Bookjg2 Novel; • SubBookjg3 ShortStory; • // The constructor for the program is • // where the initial work gets done • BookStorejg2 () {

  41. Part 2 • // Create three objects with different initial values • TextBook = new Bookjg1("Java Programming", 6); • Novel = new Bookjg2("The Unsung Hero", 30); • ShortStory = new SubBookjg3("Stories for 5 year olds", 80); • // Print out a header • System.out.println("The Book Store sells\n"); • // Print the values contained in each of the objects • TextBook.write(); • Novel.write(); • ShortStory.write(); • } • // The program control class must have a main method • public static void main (String[] args) { • // Start the program running from its constructor • new BookStorejg2 (); • } • }

  42. However another way to do this is

  43. Part 1 • import jgpack.*; • //import jgpack.subjgpack.*; • public class BookStorejg3 { • /* Illustrating the basic structure of an object oriented program */ • // Declare three object variables representing type of • // goods sold in the store • Bookjg1 TextBook; • Bookjg2 Novel; • jgpack.subjgpack.SubBookjg3 ShortStory;

More Related