some background activities around classes the last day!!!! we discussed some classes and programs...

58
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

Post on 19-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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

Page 2: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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

Page 3: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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

Page 4: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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

Page 5: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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

Page 6: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and
Page 7: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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

Page 8: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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.

Page 9: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

Consider the following two Classes

• Book

• And Bookstore1 due to June Barrett

• Their Code is

Page 10: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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

Page 11: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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

Page 12: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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

Page 13: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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;

Page 14: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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();• }

Page 15: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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 ();• }• }

Page 16: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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.

Page 17: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

Next How do we compile these

• Firstly Book.java and BookStore1.java must be in the same directory

Page 18: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and
Page 19: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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 .

Page 20: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and
Page 21: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

From this screen we see

• That the class correctly executes the specific objects.

Page 22: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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

Page 23: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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

Page 24: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

Packages

• An alternative way of accessing classes

• We have already seen

• import java.util.Scanner;

• For example in our Leapyear program

Page 25: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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");• }}

Page 26: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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

Page 27: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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:

Page 28: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

HelloWorld.java

• package world;

• public class HelloWorld {

• public static void main(String[] args) { System.out.println("Hello World");

• }

• }

Page 29: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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.

Page 30: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and
Page 31: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

• Then to use it we can use the import statement at the start of another class

• Consider

• The following variations of our Book class

Page 32: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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

Page 33: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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

Page 34: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

Using a mkdir command

• We create a directory jgpack;

• We put these files into a directory jgpack;

Page 35: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and
Page 36: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

Going back to c:\

• We have a modified version of BookStore1

Page 37: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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;

Page 38: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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();• }

Page 39: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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 ();• }• }

Page 40: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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;

Page 41: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and
Page 42: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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();• }

Page 43: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

Here

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

Page 44: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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

Page 45: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and
Page 46: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

Our variation bookstore is now

Page 47: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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 () {

Page 48: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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 ();• }• }

Page 49: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

• However another way to do this is

Page 50: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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;

Page 51: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

Exercise 1

• Design A Person Class which models the listing in a mobile phone address book.

• Name, Mobile Number ,E-mail• The methods in this class should• 1: List the phone number of the person given

their name• 2: List the Name of the person given their Phone

number• 3: Display a persons email address;• Write a second class which uses the Person

Class

Page 52: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

Exercise 2

• Construct a Class to represent a Grocery Item

• The following methods should be included• Method to display Stocklevel• Method to display Price• Method to sell quantity of item• Method to buy in quantity of item• Design another class which uses this class

Page 53: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

Exercise 3

• Write a class which represents a date

• Provide methods which get the day, the month and the year

• Provide a method which displays the date

• Provide a method which calculates the successor of a given date

• Design a class which uses the date class

Page 54: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

Exercise 4

• Write a class which represents a video in a video store

• Provide methods which display the Rental cost, the title and the status of the video i.e. whether it is in or out of the store

• A method to rent the video• A method to return the video• A method to change the price of renting the

video• A class which uses this video class

Page 55: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and
Page 56: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and
Page 57: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

Part 2• // The constructor for the program is• // where the initial work gets done• BookStorejg3 () {

• // Create three objects with different initial values• TextBook = new Bookjg1("Java Programming", 6);• Novel = new Bookjg2("The Unsung Hero", 30);• ShortStory = new jgpack.subjgpack.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();• }

Page 58: Some Background Activities Around Classes The last day!!!! We discussed some classes and programs that called them. For example we had the GradeBook and

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 BookStorejg3 ();• }• }