Input and Java A Quick Intro - PowerPoint PPT Presentation

1 / 55
About This Presentation
Title:

Input and Java A Quick Intro

Description:

param vintner The vintner (maker) of the wine. ... double proof, String vintner, String varietal, int vintage) ... System.out.println('Vintner : ' bottle1.getVintner ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 56
Provided by: geraldh6
Category:
Tags: input | intro | java | quick | vintner

less

Transcript and Presenter's Notes

Title: Input and Java A Quick Intro


1
Input and Java A Quick Intro
  • In Java, we can do Input as well. Rather than go
    into details right now Ill introduce a class
    that does some input.

2
DemoInput.java
  • /
  • Created on Jan 20, 2004
  • _at_author Jerry Heuring
  • This class is used to demonstrate some
    different input
  • methods for different types. It isn't the
    best in all cases
  • but should get you started in this class. It
    will depend
  • heavily on some of the class equivalents of
    basic types.
  • /
  • import java.io.

3
Constructors
  • public class DemoInput
  • private BufferedReader inputSource
  • /
  • The constructor -- it would prefer to get a
    BufferedReader
  • as it will support what we want to do in the
    easiest way.
  • we can convert from other types to a
    BufferedReader as well.
  • _at_param source The source for input for this
    object.
  • /
  • public DemoInput(BufferedReader source)
  • inputSource source

4
  • /
  • Another constructor -- it gets an
    InputStreamReader instead of
  • the BufferedReader.
  • _at_param inStrRdr An InputStreamReader to use
    to do input
  • with this object.
  • /
  • public DemoInput(InputStreamReader inStrRdr)
  • inputSource new BufferedReader(inStrRdr)

5
  • /
  • A Constructor taking an Input Stream -- it
    turns out that
  • System.in is an InputStream so that this will
    be useful.
  • _at_param inStr The input stream to be used for
    input by
  • this object.
  • /
  • public DemoInput(InputStream inStr)
  • inputSource new BufferedReader(new
    InputStreamReader(inStr))

6
readLine Read a line
  • /
  • Reads a line from the input stream. It will
    return an
  • empty string if an error (exception) occurs
    during the
  • read.
  • _at_return The next line of the file or an empty
    string
  • (on error).
  • /
  • public String readLine()
  • try
  • return inputSource.readLine()
  • catch (Exception e)
  • return ""

7
readInt reads an Integer
  • /
  • This routine will get a line and use the
    parseInt routine
  • to convert it to an integer. It does handle
    the empty string
  • by returning a zero.
  • _at_return The integer on the next line of the
    file or zero
  • if an error occurs.
  • /
  • public int readInt()
  • String chunk
  • chunk readLine()
  • if (chunk "")
  • return 0
  • else
  • return Integer.parseInt(chunk)

8
readFloat read a float
  • /
  • Trying to do the same thing with a float. In
    this case
  • I've decided to lose the String local and do
    the conversion
  • directly. What happens if the read fails or
    the conversion
  • fails?
  • _at_return The float on the next line of the
    file or ??
  • /
  • public float readFloat()
  • return Float.parseFloat(readLine())

9
Main used for testing
  • /
  • The main program is going to try to test the
    input routines
  • in the object. I'm not a big fan of this
    class but it does
  • get us past some details and removes some
    initial guesswork.
  • _at_param args The command line arguments. Not
    used in this version.
  • /
  • public static void main(String args)
  • DemoInput testObject new DemoInput(System.in)
  • String line
  • int value
  • float floatValue

10
Main (continued)
  • System.out.println("Input a line to read ")
  • line testObject.readLine()
  • System.out.println("The line input was
    "line)
  • System.out.println("Input an integer ")
  • value testObject.readInt()
  • System.out.println("The value returned was
    "value)
  • System.out.println("Input a floating point value
    ")
  • floatValue testObject.readFloat()
  • System.out.println("The value returned was
    "floatValue)
  • System.exit(0)

11
Program Design
  • UML version

12
Identify the Objects
  • DotCom
  • Company
  • Size
  • Hits
  • GameGrid
  • List of companies
  • Where the companies are placed
  • Oriented horizontal/vertical
  • Player
  • Turns/Guesses
  • Rating
  • Name

13
Decide on the Methods/Procedures
  • DotCom
  • Set/Get Name, Size, Hits
  • GameGrid
  • Initialize
  • Add Dot Com
  • Check For Hit
  • Dot Coms Left
  • Player
  • Take Turn
  • Play Game
  • Print Results

14
UML For the Problem -- Player
15
UML -- GameGrid
16
UML -- DotCom
17
Polymorphism Inheritance
  • Much of the power in object oriented programming
    comes from polymorphism, inheritance, and
    interfaces
  • Polymorphism is at the heart of all object
    oriented programming languages. It is the
    ability to treat different items in the same way.

18
An Example of Polymorphism
  • Assume you have a sound object, a movie object,
    and a game object.
  • Because they are all objects they can be stored
    in a vector or any other collection of objects.
  • Assuming they all have a play method (obviously
    doing different things) we could put them in a
    list, step through the list, and play each one.
  • (I just told a lie it isnt quite this simple
    in Java but it is close)

19
Templates are not Polymorphism
  • Being able to handle different types of objects
    in a consistent manner is part of polymorphism.
  • Polymorphism is NOT the same as a C template.
    We are not creating a separate class for each
    type of object.

20
Inheritance
  • In Java, C, and most (if not all) object
    oriented languages you can inherit from another
    class.
  • Consider it as extending or enhancing an
    existing object. The existing object is still
    there but more features have been added to it.

21
Inheritance Examples
  • You buy a computer system and add on some
    specialized gaming equipment and I/O devices to
    extend its capabilities.
  • The base computer system is still there it
    could probably be used without all the new bells
    and whistles youve added but the new object is
    of more use.

22
More Inheritance
  • Another common use of inheritance is to allow
    multiple objects share a common subset of methods
    and attributes.

23
If You Look at Java There is All Kinds of
Inheritance Used
  • The Stack class extends the Vector Class
  • The Vector class extends the AbstractList class
  • The AbstractList class exteds the
    AbstractCollection class
  • The AbstractCollection class extends the Object
    class (All objects are descendents of the Java
    Object class)

24
Opportunities for Inheritance
  • Two or more subclasses that might need common
    behavior or that share some common underlying
    data.

25
Is-a or Has-a?
  • The book sites the Is-a test as a method to
    determine if something should be extended.
  • A vector is a list
  • A stack is a list (and in Javas case is a Vector
    as well)
  • Has-a would mean that the object has an instance
    variable of a given type.

26
Keep in Mind
  • A subclass extends its superclass
  • All public instance variables and methods are
    inherited from the superclass. Private variables
    and methods are not.
  • Inherited methods can be overridden. Instance
    variables can be redefined but not overridden.

27
Interfaces
  • An interface is a group of calls/methods.
  • Anybody that implements the interface must
    provide all the methods in the interface.
  • Objects implementing an interface dont need to
    be a subclass of the same superclass but will
    have a common set of methods.

28
Abstract Classes
  • Abstract classes can be used in a hierarchy and
    as a type but cant be instantiated (created via
    new).
  • Methods may also be abstract they have no body
    and look like a function prototype in C.
  • Any class with an abstract method must also be
    declared as abstract.

29
Where are Abstract Classes Used
  • Used heavily in the collections inside of Java to
    enforce a common set of methods and of behavior.

30
An Example of Inheritance
  • /
  • _at_author Jerry Heuring
  • Created Jan 28, 2004
  • _at_version 0.0
  • Bugs
  • Revisions
  • This is a base class that will be used to help
    keep
  • inventory at a "beverage" store. The base
    class will
  • implement some of the functionality and the
    subclasses
  • will add to it.
  • /
  • public class Beverage
  • private String name
  • private int size
  • private int numberInStock

31
  • /
  • _at_return returns the name of this beverage.
  • /
  • public String getName()
  • return name
  • /
  • _at_return the size in ounces of this beverage.
  • /
  • public int getSize()
  • return size
  • /
  • _at_return the quantity on hand of this beverage.
  • /
  • public int getNumberInStock()
  • return numberInStock

32
  • /
  • This routine allows you to change the amount
    in stock.
  • A positive valued argument adds to stock on
    hand and a
  • negative value removes stock (sale?). The
    class clamps
  • stock to 0 -- can't have a negative amount of
    stock.
  • _at_param delta The number of this item to
    add/remove from stock.
  • _at_return the new stock level.
  • /
  • public int changeStock(int delta)
  • numberInStock numberInStock delta
  • if (numberInStock lt 0)
  • numberInStock 0
  • return numberInStock

33
Constructors
  • /
  • A complete constructor for the beverage class.
    It
  • takes all the parameters and assigns them to
    the
  • appropriate fields. Again, it clamps stock to
  • non-negative values.
  • _at_param name the name of this beverage
  • _at_param size the size of this beverage in
    ounces
  • _at_param numberInStock the number of this
    beverage in stock.
  • /
  • public Beverage(String name, int size, int
    numberInStock)
  • this.name name
  • this.size size
  • this.numberInStock numberInStock
  • if (this.numberInStock lt 0)
  • this.numberInStock 0

34
  • /
  • The default constructor for the Beverage
    class. The
  • default sets the name to an empty string, the
    size
  • to zero ounces, and the quantity on hand to 0.
  • /
  • public Beverage()
  • this("", 0, 0)

35
toString() and main()
  • /
  • _at_return a printable representation of the
    object as a string.
  • /
  • public String toString()
  • return name " " size " oz "
    numberInStock " in stock"
  • /
  • The main program will run some testing code
    exercising
  • the various functions of the class.
  • _at_param args The command line arguments. Not
    used in
  • this version.
  • /

36
  • public static void main(String args)
  • Beverage bottle1 new Beverage()
  • Beverage bottle2 new Beverage("Coke", 12, 12)
  • Beverage bottle3 new Beverage("Coke", 12, 12)
  • / check what the constructors did and examine
    the
  • individual bottles using the toString
    routine.
  • /
  • System.out.println(bottle1)
  • System.out.println(bottle2)
  • System.out.println(bottle3)
  • / See if the two bottles are the same -- why?
  • /
  • if (bottle2 bottle3)
  • System.out.println("Bottles two and three are
    the same")
  • else
  • System.out.println("Bottles two and three are
    different?")

37
  • / Check if changing stock works.
  • /
  • bottle2.changeStock(12)
  • bottle3.changeStock(-25)
  • /
  • Output the second bottle using the access
    functions.
  • /
  • System.out.println("Name "bottle2.getName())
  • System.out.println("Size "bottle2.getSize())
  • System.out.println("Stock bottle2.getNumberIn
    Stock())
  • / Make sure the third bottle clamped stock at
    0.
  • /
  • System.out.println(bottle3)
  • System.exit(0)

38
Next Level an AlcoholicBeverage
  • Adds in the alcohol content and the proof of the
    beverage.

39
AlcoholicBeverage
  • /
  • _at_author Jerry Heuring
  • Created Jan 28, 2004
  • _at_version 0.0
  • Bugs
  • Revisions
  • A class for alcoholic beverages. It adds the
    alcohol
  • content but might also override a sales
    function (if
  • there is one) to check age/ID before selling
    the alcohol.
  • /
  • public class AlcoholicBeverage extends Beverage
  • private double percentAlcohol
  • private double proof

40
Access Functions
  • /
  • _at_return the percentage of alcohol in this
    beverage
  • /
  • public double getAlcohol()
  • return percentAlcohol
  • /
  • _at_return the "proof" level of the beverage
  • (double the alcohol)
  • /
  • public double getProof()
  • return proof

41
Constructors
  • /
  • This is a complete constructor -- it includes
    the portions that
  • are contained in the super class as well as
    those specific to
  • this class.
  • _at_param name the name of this beverage
  • _at_param size the size, in ounces, of this
    beverage
  • _at_param numberOnHand the quantity on hand of
    this beverage
  • _at_param percentAlcohol The percentage of
    alcohol by volume in this beverage
  • _at_param proof the "proof" of this beverage
  • /
  • AlcoholicBeverage (String name, int size, int
    numberOnHand, double percentAlcohol, double
    proof)
  • super(name, size, numberOnHand)
  • this.percentAlcohol percentAlcohol
  • this.proof proof

42
  • /
  • A constructor with just the fields for this
    class --
  • the superclass will be initialized with the
    default
  • constructor in Beverage.
  • _at_param percentAlcohol the percentage of
    alcohol by volume in this beverage
  • _at_param proof the "proof" level of this
    beverage
  • /
  • AlcoholicBeverage (double percentAlcohol, double
    proof)
  • this.percentAlcohol percentAlcohol
  • this.proof proof
  • /
  • The default constructor -- an alcohol free
    beverage.
  • /
  • AlcoholicBeverage ()
  • this (0.0, 0.0)

43
Main()
  • /
  • The main program will run simple tests of the
    class.
  • _at_param args the command line arguments -- not
    used
  • /
  • public static void main(String args)
  • AlcoholicBeverage test1 new AlcoholicBeverage()
  • AlcoholicBeverage test2 new AlcoholicBeverage(1
    2.0, 24.0)
  • AlcoholicBeverage test3 new AlcoholicBeverage("
    Blatz", 12, 10, 4.5, 9.0)
  • System.out.println("Test 1 -- Fields")
  • System.out.println("Name "test1.getName())
  • System.out.println("Size "test1.getSize())
  • System.out.println("Stock " test1.getNumberI
    nStock())
  • System.out.println("Alcohol "test1.getAlcohol()
    )
  • System.out.println("Proof "test1.getProof())

44
  • System.out.println("Test 2 -- Fields")
  • System.out.println("Name "
    test2.getName())
  • System.out.println("Size "
    test2.getSize())
  • System.out.println("Stock "
    test2.getNumberInStock())
  • System.out.println("Alcohol "
    test2.getAlcohol())
  • System.out.println("Proof "
    test2.getProof())
  • System.out.println("Test 3 -- Fields")
  • System.out.println("Name "
    test3.getName())
  • System.out.println("Size "
    test3.getSize())
  • System.out.println("Stock "
    test3.getNumberInStock())
  • System.out.println("Alcohol "
    test3.getAlcohol())
  • System.out.println("Proof "
    test3.getProof())

45
  • System.out.println("Output of entire objects")
  • System.out.println("Test3 " test3)
  • System.out.println("Test2 " test2)
  • System.out.println("Test1 " test1)
  • System.exit(0)

46
Wine Class
  • /
  • _at_author Jerry Heuring
  • Created Jan 28, 2004
  • _at_version 0.0
  • Bugs
  • Revisions
  • This class inherits everything from the parent
    class
  • (AlcoholicBeverage) and adds its own specific
    fields
  • and methods. It isn't very complete but does
    give a
  • start to the hierarchy.
  • /
  • public class Wine extends AlcoholicBeverage
  • private String varietal
  • private String vintner
  • private int vintage

47
Constructors
  • /
  • A Constructor with all parameters (including
    those
  • needed for the superclasses AlcoholicBeverage
    and
  • Beverage). This will call the super class
    (must be
  • done first in a constructor) and the local
    class to
  • do the initialization.
  • _at_param name the name of the beverage
  • _at_param size the size, in ounces, of the
    beverage
  • _at_param numberOnHand The quantity on hand (in
    units)
  • _at_param alcohol The percentage of alcohol in
    the beverage
  • _at_param proof The proof level of the alcohol
  • _at_param vintner The vintner (maker) of the
    wine.
  • _at_param varietal The types of grapes that are
    in the wine
  • _at_param vintage The year the wine was made
  • /

48
  • public Wine (String name, int size, int
    numberOnHand, double alcohol, double proof,
    String vintner, String varietal, int vintage)
  • super(name, size, numberOnHand, alcohol, proof)
  • this.vintner vintner
  • this.varietal varietal
  • this.vintage vintage

49
  • /
  • A constructor with only the fields for this
  • particular class. The super classes will end
    up
  • using their default constructors when the
  • instantiation takes place.
  • _at_param vintner The producer of the wine
  • _at_param varietal The variety or varieties of
    grapes
  • used in the wine
  • _at_param vintage The year the wine was produced
  • /
  • public Wine(String vintner, String varietal,
    int vintage)
  • this.vintner vintner
  • this.varietal varietal
  • this.vintage vintage

50
  • /
  • The default constructor. Sets some initial
    values.
  • /
  • public Wine()
  • this("", "", 0)

51
The Access Functions
  • /
  • _at_return The maker of this wine.
  • /
  • public String getVintner()
  • return vintner
  • /
  • _at_return This wines variety
  • /
  • public String getVarietal()
  • return varietal
  • /
  • _at_return The year this wine was made.
  • /
  • public int getVintage()
  • return vintage

52
  • /
  • Convert the object to some output friendly
    version.
  • _at_return a printable version of the object.
  • /
  • public String toString()
  • return vintner ", " varietal ", "
    vintage " ( " getNumberInStock() " ) "

53
Main()
  • /
  • The main program is being used to test the
    methods
  • in the class. It will exercise all 3
    constructors
  • as well as the access methods.
  • _at_param argsThe list of command line arguments
    (not used)
  • /
  • public static void main(String args)
  • Wine bottle1 new Wine()
  • Wine bottle2 new Wine("Old Fart",
    "Merlot Cabernet", 1991)
  • Wine bottle3 new Wine("Gallo", 32, 144, 12.0,
    24.0, "Gallo", "Pinot Grigio", 1992)
  • System.out.println("Bottle 1 " bottle1)
  • System.out.println("Bottle 2 " bottle2)
  • System.out.println("Bottle 3 " bottle3)

54
  • /
  • Try to check the access functions...
  • /
  • System.out.println("Bottle 1")
  • System.out.println("Vintner "bottle1.getVintne
    r())
  • System.out.println("Varietal
    "bottle1.getVarietal())
  • System.out.println("Vintage
    "bottle1.getVintage())
  • System.out.println("Bottle 2")
  • System.out.println("Vintner "bottle2.getVintne
    r())
  • System.out.println("Varietal "bottle2.getVariet
    al())
  • System.out.println("Vintage "bottle2.getVintag
    e())

55
  • System.out.println("Bottle 3")
  • System.out.println("Vintner "bottle3.getVintne
    r())
  • System.out.println("Varietal "bottle3.getVariet
    al())
  • System.out.println("Vintage "bottle3.getVintag
    e())
  • System.exit(0)
Write a Comment
User Comments (0)
About PowerShow.com