Java PowerPoint PPT Presentation

presentation player overlay
1 / 27
About This Presentation
Transcript and Presenter's Notes

Title: Java


1
Java Writing it!
Programming in Java a recap in to the
implementation of Java OO Programming.
  • John Murray john.murray_at_sunderland.ac.uk

2
A good coding idea
  • Java was developed by Sun Systems
    http//java.sun.com
  • Language developed to help with the idea of code
    reusability
  • Multi platform
  • Been around for over 10 years

3
Contents of a Java Class
  • Constructor inputting parameters
  • Attributes Class Variables
  • Methods Code functions

4
Class content descriptions
  • Constructor
  • Used when creating an instance of a class so that
    parameters / variables can be initialised.
  • Constructor can be empty or can accept inputs.
  • Constructor can call other methods of the class
    or any class that it has access to (providing
    methods are public)

5
Class contents description cont.
  • Constructor
  • public class Tree
  • public Tree() //Empty Constructor
  • public Tree(String theName)
  • treeName theName

6
Class contents description cont.
  • Attributes
  • Used to store variables / parameters used in the
    class.
  • Can be either public, private or protected
  • Can use any of the Java primitive types.

7
Class contents description cont.
  • Attributes
  • public class Tree
  • public String treeName
  • public int ageOfTree
  • public double widthOfTrunk

8
Class contents description cont.
  • Methods
  • Are used to execute / call a specific section of
    code.
  • Think of like a C or C function.
  • Can receive parameters or return them

9
Class contents description cont.
  • Methods
  • private void setTreeName(String theName)
  • treeName theName
  • public int getAgeOfTree()
  • return ageOfTree

10
Complete Class
  • public class Tree
  • public String treeName
  • public int ageOfTree
  • public double widthOfTrunk
  • public Tree(String theName)
  • treeName theName
  • private void setTreeName(String theName)
  • treeName theName
  • public int getAgeOfTree()
  • return ageOfTree

11
Exceptions
  • Java has a very powerful exception handling
    system.
  • Program may compile however invalid runtime
    errors may occur due to user input.
  • These need to be handled!
  • Use of try, catch and finally.
  • Method must specify if it is to throw an
    exception

12
The Try Block
  • The try block contains any code that may be
    able to throw an exception
  • BufferedReader read new BufferedReader(new
  • InputStreamReader(System.in))
  • public void getAge()
  • ageOfTree read.readLine()

13
Using the Try block
  • public void getAge() throws IOException
  • try
  • int age read.readLine()

14
The Catch block
  • The catch block is used to process the exception
    thrown.
  • Can have multiple catch blocks each processing a
    different exception.
  • catch (InterruptedIOException ie)
  • System.out.println(Stream was interupted
    ie)
  • catch (IOException ioe)
  • System.out.println(High Level Exception
    ioe)

15
The Finally block
  • finally is used to execute any code that is
    needed if none of the catch blocks resolve the
    problem.
  • finally
  • int ageOfTree 20 // Specify a default age

16
Try Catch Finally
  • try
  • int ageOfTree read.readLine() // Get from
    user
  • catch (InterruptedIOException ie)
  • System.out.println(Stream was interrupted
    ie)
  • catch (IOException ioe)
  • System.out.println(High Level Exception
    ioe)
  • finally
  • int age 20 // Set default age

17
Creating class instances
  • We want to be able to create multiple instances
    of our Tree class, each having their own name and
    other properties.
  • To do this we have to think about efficient code
    and code reusability.

18
Consider the Tree class
  • public class Tree
  • int ageOfTree
  • String treeName
  • double widthOfTrunk
  • public Tree(String theName, int theAge, double
    theWidth)
  • treeName theName
  • ageOfTree theAge
  • widthOfTrunk theWidth

19
Creating an instance of Tree
  • An instance of a class creates a copy of that
    object in memory initialised with the set
    parameters.
  • public class Forrest
  • public static void main(String args)
  • // This line creates an instance of Tree
  • Tree oak new Tree(Oak,100,1.2)

20
Creating a whole Forrest
  • You can create arrays of the Tree class as
    follows
  • public class Forrest
  • public static void main (String args)
  • Tree myTrees new Tree500
  • myTrees0 new Tree(Oak,,)
  • myTrees1 new Tree(Fair,,)
  • . . . . .

21
Abstract classes
  • abstract classes are designed in the same was as
    we think about the word abstract, we abstract
    away from the inner workings of the world.
  • Take a car, if we abstract away all we concern
    our self with is a car is simply an automobile,
    such as a motorbike, tractor or truck, i.e. a
    form of transport.

22
Abstracting
  • What properties does an automobile have?
  • Wheels
  • Registration number
  • Colour
  • Plus others
  • Therefore, our abstract class should contain
    these as EVERY subclass of Automobile will also
    have them.

23
Our Abstract Class
  • If we use the Automobile example
  • public abstract class Automobile
  • public abstract void numOFWheels(int theWheels)
  • public abstract void regNum(String theReg)
  • public abstract void setColour(String
    theColour)
  • This class ensures that any sub-class MUST have
    these methods.

24
Sub-Classes
  • We now create any sub classes of Automobile using
    extends
  • public class Car extends Automobile //Class
    derives from Automobile
  • // From super class
  • public void numOfWheels(int theWheels)
  • wheels theWheels
  • // Methods only appropriate to this class
  • public void setEngineSize(double theEngine)
  • engine theEngine

25
Creating instances of sub-class
  • Now when we want to create instances of Car we
    have to ensure we override all abstract methods
    in Automobile.
  • Car.java1 Car is not abstract and does not
    override abstract method setColour(java.lang.Strin
    g) in Automobile
  • public class Car extends Automobile

26
Summary
  • Creating class
  • Passing parameters between classes
  • Understand Exceptions
  • Be able to handle exceptions
  • Abstract Classes
  • Create
  • Extend

27
Questions
  • ?
Write a Comment
User Comments (0)
About PowerShow.com