Enhanced Class Design - PowerPoint PPT Presentation

1 / 46
About This Presentation
Title:

Enhanced Class Design

Description:

System.out.println(' 3. Anchovy'); response = stdin.readLine(); 8/23/09. Komar Associates ... 3. Anchovy. 1. Do you want another topping (Y or N))? 8/23/09 ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 47
Provided by: joseph120
Category:

less

Transcript and Presenter's Notes

Title: Enhanced Class Design


1
Enhanced Class Design
  • Joe Komar

2
Enhanced Class Design Tools
  • Abstract classes
  • Interfaces
  • Packages

3
Abstract Classes
  • Typically represents a concept in hierarchy
  • Cannot be instantiated
  • Contain abstract methods
  • Some methods typically not abstract
  • Expect the children to finish the definition of
    the class
  • Format abstract methodname()
  • defines what parameters are accepted and what is
    returned

4
Abstract Classes
  • The modifiers final and static cannot be used
    with abstract methods
  • If there is one or more abstract methods in a
    class is must be declared abstract
  • e.g., abstract class Food
  • Children must override the abstract methods of
    the abstract parent
  • If dont override all abstract methods, the child
    must be declared abstract

5
Printer Example
File
Binary_File
Text_File
Image_File
6
Printer Example
public class Printer public static void
main (String args) byte logo_data
41, 42, 49, 44 Text_File report new
Text_File ("Sand Reconner", 66, "One
two three") Image_File logo new
Image_File ("Number 1", 45,
logo_data) Print_Logger
daily new Print_Logger()
daily.log(report) daily.log(logo)
// method main // class Printer
7
Printer Example (contd)
abstract class File protected String id
protected int size public File (String
file_id, int file_size) id file_id
size file_size // constructor File
public String name() return id
// method name abstract public String
print() // class File
8
Printer Example (contd)
class Text_File extends File protected
String text public Text_File (String id, int
size, String file_contents) super(id,
size) text file_contents
//constructor Text_File public String
print() return text // print
method // class Text_File
9
Printer Example (contd)
class Binary_File extends File protected
byte data public Binary_File (String
id, int size, byte file_data)
super(id, size) data file_data
//constructor Binary_File public String
print() return "" //method
print // class Binary_File
10
Printer Example (contd)
class Image_File extends Binary_File
public Image_File (String id, int size, byte
file_data) super(id, size, file_data)
// constructor Image_File public
String print() return new String
(data) //method print //class Image_File
11
Printer Example (contd)
class Print_Logger public void log (File
file) System.out.println (file.name()
" " file.print()) // method
log //class Print_Logger
12
Printer Example Output
13
Abstract Classes
Crust
Toppings
Traditional
Deep Dish
Mushroom
Sausage
Cheese
14
Interfaces
  • Collection of constants and abstract methods
  • Not classes
  • Can be used in the definition of a class
  • interface interface-name constants and methods
  • class class-name implements interface-name
    implementation of abstract methods

15
Interfaces
  • All methods in an interface are, by default,
    public and abstract
  • All constants are public and final
  • Constants can be used in the class as if they
    were declared locally
  • Interfaces can be linked in a hierarchy (extends)
    like classes

16
Interfaces
  • Interface names can be used as a class type in
    a formal parameter
  • Any class that implements that interface will be
    accepted as the actual parameter
  • A class can implement more than one interface
  • Interfaces allow for multiple inheritance

17
Printer2 Example
public class Printer2 public static void
main (String args) byte logo_data
41, 42, 49, 44 Text_File report new
Text_File ("Sand Reconner", 66, "One
two three") Image_File logo new
Image_File ("Number 1", 45,
logo_data) Print_Logger
daily new Print_Logger()
daily.log(report) daily.log(logo) //
method main // class Printer2
18
Printer2 Example
class File protected String id
protected int size public File (String
file_id, int file_size) id file_id
size file_size // constructor File
public String name() return id
// method name // class File
No Print() method Not abstract any longer
19
Printer2 Example
class Text_File extends File implements Printable
protected String text public Text_File
(String id, int size, String file_contents)
super(id, size) text
file_contents //constructor Text_File
public String print() return text
// print method // class Text_File
Implements the print() method name() method in
File
20
Printer2 Example
class Binary_File extends File protected
byte data public Binary_File (String
id, int size, byte file_data)
super(id, size) data file_data
//constructor Binary_File // class
Binary_File
Doesnt implement Printable Parent to Image_File
21
Printer2 Example
class Image_File extends Binary_File implements
Printable public Image_File (String
id, int size, byte file_data)
super(id, size, file_data) // constructor
Image_File public String print()
return new String (data) //method
print //class Image_File
Implements print() name() in File class
22
Printer2 Example
interface Printable String name()
String print() // interface
Printable class Print_Logger public void
log (Printable file) System.out.println
(file.name() " " file.print()) //
method log //class Print_Logger
Names name() and print() methods to be implemented
interface Name
23
Printer2 Example Output
24
Abstract Classes and Interfaces
  • Used together become a powerful tool
  • Multiple Inheritance
  • Encapsulate and hide information
  • Specify what must be done not how
  • Allow systems to be built by many programmers
  • Basically, represent the heart of object
    orientation

25
PizzaPlace
interface Costable double cost() //interfac
e Costable class Coster public double
cost(Costable c) return c.cost() //
method cost // class Coster
26
PizzaPlace
class Pizza private Toppings toppings
private Crust crust private Sauce
sauce Pizza (Crust c, Sauce s, Toppings
t) toppings t crust
c sauce s //constructor
public int time() return
(toppings.time() crust.time() sauce.time())
//time public double cost()
Coster coster new Coster()
return (coster.cost(toppings)
coster.cost(crust)
coster.cost(sauce)) //class Pizza
27
PizzaPlace
abstract class Crust implements Costable
abstract public int time() //class
Crust abstract class Sauce implements Costable
abstract public int time() //class Sauce
28
PizzaPlace
class DeepDish extends Crust public int time
() return 15 public double
cost() return 4.50
//DeepDish class Traditional extends Crust
public int time () return 13
public double cost() return 4.00
//Traditional
class Thin extends Crust public int time ()
return 10 public double cost()
return 4.00 //Thin
29
PizzaPlace
class Red extends Sauce public int time ()
return 6 public double cost()
return 2.00 //Red class White
extends Sauce public int time ()
return 5 public double cost()
return 2.30 //White
30
PizzaPlace
class Toppings implements Costable private
Vector tops new Vector() public void
addTopping(Toppings top)
tops.addElement(top) // public int
time() int total 0 for (int i
0 i lt tops.size() i) total
((Toppings)tops.elementAt(i)).time()
return total public double cost()
Coster coster new Coster()
double total 0 for (int i 0 i lt
tops.size() i) total
coster.cost((Toppings)tops.elementAt(i))
return total //class Toppings
31
PizzaPlace
class Cheese extends Toppings public int
time() return 5 public double
cost() return 1.25 //Cheese class
Sausage extends Toppings public int time()
return 7 public double cost()
return 1.50 //Sausage
class Anchovy extends Toppings public int
time() return 4 public double
cost() return 2.25 //Anchovy
32
PizzaPlace
import java.io. import java.util. class
PizzaPlace public static void main
(String args) throws IOException
BufferedReader stdin new BufferedReader
(new InputStreamReader(System.in), 1)
String response null, again "n"
Crust crust null Sauce sauce null
33
PizzaPlace
do Toppings top new
Toppings() do
System.out.println("Enter the number of the crust
you want\n") System.out.println(" 1.
Traditional") System.out.println("
2. Deap Dish") System.out.println("
3. Thin") response
stdin.readLine() switch
(response.charAt(0)) case '1'
crust new Traditional()
break case '2'
crust new DeepDish()
break case '3'
crust new Thin() break
default
System.out.println("\nPlease make a valid
choice") //switch while
(response.charAt(0) lt '1' response.charAt(0) gt
'3')
34
PizzaPlace
do System.out.println("Enter
the number of the sauce you want\n")
System.out.println(" 1. Red")
System.out.println(" 2. White")
response stdin.readLine() switch
(response.charAt(0)) case '1'
sauce new Red()
break case '2'
sauce new White() break
default
System.out.println("\nPlease make a valid
choice") //switch while
(response.charAt(0) lt '1' response.charAt(0) gt
'2')
35
PizzaPlace
String repeat "n" do
do System.out.println("Enter the
number of the topping you want\n")
System.out.println(" 1. Cheese")
System.out.println(" 2. Sausage")
System.out.println(" 3. Anchovy")
response stdin.readLine()
36
PizzaPlace
switch (response.charAt(0))
case '1' Cheese cheese new
Cheese() top.addTopping(cheese)
break case '2'
Sausage sausage new Sausage()
top.addTopping(sausage)
break case '3'
Anchovy anchovy new Anchovy()
top.addTopping(anchovy)
break default
System.out.println("\nPlease make a valid
choice") //switch while
(response.charAt(0) lt '1' response.charAt(0) gt
'3') System.out.print("\nDo you want
another topping (Y or N))? ") repeat
stdin.readLine() while
(repeat.equals("y") repeat.equals("Y"))
37
PizzaPlace
Pizza pizza new Pizza(crust, sauce,
top) System.out.println("\nBaking time for
your pizza is " pizza.time())
System.out.println("\nTotal cost for your pizza
is " pizza.cost()) System.out.print("\nD
o you want to order another pizza (Y or N)? ")
again stdin.readLine() while
(again.equals("y") again.equals("Y"))
//main method //class PizzaPlace
38
PizzaPlace
Enter the number of the crust you want 1.
Traditional 2. Deap Dish 3. Thin 1 Enter the
number of the sauce you want 1. Red 2.
White 1 Enter the number of the topping you
want 1. Cheese 2. Sausage 3. Anchovy 1 Do
you want another topping (Y or N))?
39
PizzaPlace
Do you want another topping (Y or N))? n Baking
time for your pizza is 24 Total cost for your
pizza is 7.25 Do you want to order another
pizza (Y or N)?
40
Packages
  • Used to group similar classes together under one
    name
  • Identify files and classes for the package
  • Put compiled versions into a subdirectory
    associated with that package name
  • Include in the classpath environment variable
  • Include in the standard path
  • Name at compile and run time

41
Simple_IO Example
package Simple_IO import java.io. public class
Reader public static int read() throws
IOException BufferedReader stdin new
BufferedReader (new InputStreamReader
(System.in)) String value
stdin.readLine() return Integer.parseInt
(value) // method read public static
String read_line() throws IOException
BufferedReader stdin new BufferedReader
(new InputStreamReader (System.in))
return stdin.readLine() // method
read_line //class Reader
42
Simple_IO Example
package Simple_IO public class Writer
public static void write (int value)
System.out.println (value) // method
write public static void write_line (String
line) System.out.println (line)
// method write_line // class Writer
43
Compiling and Running
  • Assume that Simple_IO directory on A
  • Place Reader.java and Writer.java in that
    directory and compile as normal creating the
    .class files
  • You can continue to add classes to this directory
  • To use them use import Simple_IO.

44
Example use of a package
import java.io.IOException import
Simple_IO. class Simple_IO_Test public
static void main (String args) throws
IOException int value Reader.read()
String line Reader.read_line()
Writer.write (value) Writer.write_line
(line) // method main //class
Simple_IO_Test
45
Finding the Directory
  • Got your own system -- add directories in the
    standard classpath
  • At UST, store directories on diskette, then to
    compile and run
  • javac -classpath a\classpath prog.java
  • java -classpath a\classpath prog

46
Package Considerations
  • If you have a conflict with method names, use the
    fully qualified name
  • Simple_IO.Reader.read()
  • Simple_IO.Writer.write()
  • Dont then need to use an import statement
  • Note that these packages you create need to exist
    on the system on which the program runs

47
Summary
  • Abstract classes -- if any method is abstract,
    the entire class is abstract
  • Abstract methods must all be implemented
    somewhere in the hierarchy
  • Interfaces are useful in implementing more than
    one hierarchy
  • Packages are ways of grouping similar and/or
    interdependent classes under one name
Write a Comment
User Comments (0)
About PowerShow.com