Input and Java A Quick Intro - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Input and Java A Quick Intro

Description:

A Constructor taking an Input Stream -- it turns out that ... Sex. EyeColor. LivingPerson. CurrentAddress. Marital Status. Net worth. Dead Person. Date Died ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 31
Provided by: geraldh6
Category:
Tags: input | intro | java | quick | streaming

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
Write a Comment
User Comments (0)
About PowerShow.com