Health Informatics CS7014 Introduction to programming - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Health Informatics CS7014 Introduction to programming

Description:

... programming functionality e.g. String, Math, File, FileReader. ... String class. Classes are logically grouped into Java packages. e.g. java.lang, java.io ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 15
Provided by: csT1
Category:

less

Transcript and Presenter's Notes

Title: Health Informatics CS7014 Introduction to programming


1
Health Informatics CS7014Introduction to
programming
  • Lecture Week 4
  • 31st October 2008
  • Ivana Dusparic
  • Ivana.Dusparic-at-cs.tcd.ie

2
Review
  • Class
  • Instance of a Class
  • Instance variables
  • Constants
  • Methods
  • Local variables
  • Passing parameters
  • Returning values

3
Lecture Overview
  • Constructors
  • Java String class

4
Constructor methods
  • Constructors are special methods in the class
    having the same name as the class that are used
    to create an instance of that class.
  • We can use a Constructor to initialise the
    instances instance variables.
  • Constructors have no return type, not even void
  • We can also write other Constructor methods which
    have parameters.
  • The Constructor method is invoked when the
    instance is created. e.g. new Rectangle()

5
Example Rectangle Constructors
  • class Rectangle
  • //instance variables
  • int width
  • int height
  • // Default Constructor
  • Rectangle()
  • width 5
  • height 5
  • //Another Constructor method with parameters
  • Rectangle(int w, int h)
  • width w
  • height h

6
Using the constructor
  • public static void main(String args)
  • Rectangle rect1 new Rectangle()
  • Rectangle rect2 new Rectangle(10,15)
  • Console.println(Area rect1.getArea())
  • Console.println(Area rect2.getArea())

7
Default Constructor
  • Constructor which takes no arguments
  • If you do not implement any constructors in your
    class, java will provide default one for you
  • e.g. We used Rectangle r new Rectangle()
    without implementing Rectangle() constructor
  • If you implement any constructors yourself, Java
    will not provide default one

8
The Java String class
  • The Java language is accompanied with a standard
    set of classes that can be used by any java
    program.
  • These classes provide basic programming
    functionality e.g. String, Math, File,
    FileReader.
  • String is a very useful class used to store and
    manipulate text.
  • The String class has an instance variable which
    is a list of type char to store the text.
  • It has many methods for manipulating the text.

9
Example Attribute of type String
  • class Rectangle
  • //instance variables
  • int width
  • int height
  • String colour
  • // Default Constructor
  • Rectangle()
  • width 5
  • height 5
  • colour blue
  • //Another Constructor method with parameters
  • Rectangle(int w, int h, String c)
  • width w
  • height h
  • colour c

10
String class
  • Classes are logically grouped into Java packages
  • e.g. java.lang, java.io
  • The java.lang package provides fundamental
    classes for programming in Java.
  • The String class is part of this package
  • full class name is java.lang.String
  • but we can just use String
  • You can view documentation on all standard java
    packages here -http//java.sun.com/j2se/1.5.0/docs
    /api/

11
String class documentation
  • In order to know how to use a classs methods we
    must look at its documentation.
  • To use a method, we just need to know the name of
    the method, its input parameters and its return
    value if any.
  • Go to the javadoc for the java packages and in
    the left top frame browse to the java.lang
    package and then in the left lower frame to the
    String class.
  • The documentation lists each of the classs
  • data attributes (Fields)
  • constructor methods
  • normal methods

12
Some example methods
  • some of the methods of the String class
  • int length()
  • String toUpperCase()
  • String toLowerCase()
  • String substring(int beginIndex) //starts at 0
  • String substring(int beginIndex, int endIndex)
  • char charAt(int index)
  • boolean equals(Object anotherObject)
  • boolean endsWith(String suffix)
  • boolean startsWith(String prefix)
  • String trim()
  • int indexOf(int ch)
  • int indexOf(String str)
  • String replace(char oldChar, char newChar)

13
Example things we can do using String methods
  • Count number of words in a sentence
  • Count number of times a given character occurs in
    a sentence
  • Reverse words in a sentence
  • Reverse words
  • Ideas how to do these using methods provided?

14
Todays Lab
  • Methods with parameters
  • Constructors
  • String methods
Write a Comment
User Comments (0)
About PowerShow.com