Some Special Issues - PowerPoint PPT Presentation

About This Presentation
Title:

Some Special Issues

Description:

Title: Java as a Second Language Author: Don McGregor Last modified by: Gopi Nathan Created Date: 9/27/1999 2:44:35 AM Document presentation format – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 29
Provided by: DonMcG9
Learn more at: https://www.oocities.org
Category:
Tags: applets | issues | java | special

less

Transcript and Presenter's Notes

Title: Some Special Issues


1
Some Special Issues
  • (c) IDMS/SQL News
  • http//www.geocities.com/idmssql

2
Selected Special Issues
  • Class Libraries / Packages
  • Dot notation
  • String Class
  • this
  • Switch statement
  • Method Overloading
  • Polymorphism
  • Applets
  • IDEs

3
Class Libraries
  • A class library is a collection of classes that
    we can use when developing programs.
  • There is a Java standard class library
  • These classes are not part of the Java language
    per se, but we rely on them heavily.
  • The System class and the String class are part of
    the Java standard class library.
  • Other class libraries can be obtained through
    third party vendors, or you can create them
    yourself.

4
Packages
  • The classes of the Java standard class library
    are organized into packages.
  • Some of the packages in the standard class
    library are

5
The import Declaration
  • When you want to use a class from a package, you
    could use its fully qualified name
  • java.util.Random
  • Or you can import the class, then just use the
    class name
  • import java.util.Random
  • To import all classes in a particular package,
    you can use the wildcard character
  • import java.util.
  • All classes of the java.lang package are
    automatically imported into all programs.
  • That's why we didn't have to explicitly import
    the System or String classes in earlier programs.

6
Dot notation
  • By now it must be clear that Java uses the .
    notation extensively
  • String title Developing Java Software"
  • title.length() to get length
  • the dot operator used to invoke methods,
    refer to variables, objects etc
  • System.out.println(Hello")

7
System.out.print
System.out.println (Here we see many OO features
")
System - Class out - a variable defined
within Class System public static final
PrintStream out println, print - methods
inherited by out from class PrintStream
8
String - Recap
  • Not a basic data type!
  • String in Java is implemented as a class
  • String str1 new String(string value)
  • String str2 string value
  • To compare the contents of strings, one must
    use a method called equals. Using can give
    surprises!
  • String is used as argument to many methods in
    Java (print, input parameters etc )

9
equals method of class String
  • equals - compare if they have the same
    characters
  • operator we are asking if the two strings
    refer to the same object!
  • In Java Strings are immutable! Once instantiated
    it cannot change.
  • String s1 new String(Hello")
  • String s1 new String(Bye")
  • Here s1 is NOT assigned two values. Instead two
    different strings are created. First s1 is no
    longer referenced and is not available to the
    pgm. They do not share the storage!

10
equals example
  • class String1
  • public static void main (String args)
  • String str1 "This is a string"
  • String str2 new String("This is a string)
  • if (str1.equals(str2))
  • System.out.println("str1 equals str2 ")
  • else
  • System.out.println("str1 not equals str2
    ")
  • if (str1 str2)
  • System.out.println("str1 str2 ")
  • else
  • System.out.println("str1 not str2 ")

11
this
  • this within a method refers to the current
    object, and is used to
  • qualify variable names
  • String surname, company
  • public void setEtternavn (String etternavn)
  • surnameetternavn
  • companyedb telekom
  • but if we define the same like
  • String etternavn, company
  • public void setEtternavn (String etternavn)
  • this.etternavn etternavn
  • this.companyedb telekom
  • calling program would have used
  • obj1.setEtternavn(Ibsen)

Argument passed from the calling program
12
Switch
  • Jumps to one of the many cases depending upon the
    value of an integer (Evaluate in Cobol)

switch(integral-selector) case
integral-value1 statement
break case integral-value2
statement break
default statement
All in blue are reserved words
If no match default will be executed
13
// Grades.java public class Grades public
static void main (String args) int grade,
category KeyboardInput kb new
KeyboardInput() // user-defined
System.out.print ("Enter a numeric grade (0 to
100) ") grade kb.readInteger()
category grade / 10 System.out.print ("That
grade is ") switch (category) case
10 System.out.println ("a perfect score.
Well done.") break case 9
System.out.println ("well above average.
Excellent.") break case 8
System.out.println ("above average. Nice job.")
break case 7 . . .
default System.out.println ("not
passing.") //switch // method
//class
14
Method Overloading
  • A class can have many methods with the same name
    but different arguments.
  • The signature of each overloaded method must be
    unique.
  • The signature is based on the number, type, and
    order of the parameters.
  • Depending upon the call, the right method will be
    picked up
  • A very powerful feature of Java

15
Overloaded Methods
  • The println method is overloaded
  • println (String s)
  • println (int i)
  • println (double d)
  • etc.
  • The lines
  • System.out.println ("The total is")
  • System.out.println (total)
  • invoke different versions of the println method

16
Another example
  • //this class finds the area of a
  • public class Shape
  • public float FindArea(int radius)
  • return 3.14fradiusradius // pi(rr)
  • public int FindArea(int length, int breadth)
  • return length breadth // ab
  • public float FindArea(int a, int b, int height)
  • return (1.0f/2) (ab) height //
    1/2(ab)h
  • // end of class

17
Shape Tester
public class TestShape public static void
main(String args) double area_of_circle
0 float area_of_rect, area_of_trapezium
0 Shape circle new Shape() Shape rectangle
new Shape() area_of_circlecircle.FindArea(10)
System.out.println("area_of_circle"
area_of_circle) area_of_rect
rectangle.FindArea(10, 20) System.out.println("ar
ea_of_rect" area_of_rect)
18
Polymorphism
  • The purpose of polymorphism as it applies to OOP
    is to allow one name to be used to specify a
    general class of actions.
  • Method overloading is a form of polymorphism
    (used to be ...)
  • Note There are other cases run-time
    polymorphism

19
Polymorphism
  • A polymorphic reference is one which can refer to
    one of several possible methods.
  • Suppose a Holiday class has a method called
    celebrate, and a Christmas class overrode it
    (class inherits from Holiday )
  • Now consider the following invocation
  • day.celebrate()
  • If day refers to a Holiday object, it invokes
    Holidays version of celebrate if it refers to
    a Christmas object, it invokes that version.

20
Polymorphism
class Holiday public void celebrate (.)
class Christmas extends Holiday public
void celebrate (..) overrides Holiday day
new Holiday or Holiday day new Christmas
day.celebrate () We have two methods called
celebrate. One will be executed depending upon
the type of day object.
21
Applets
  • Applets are small java programs that are designed
    to run inside of a web browser window.
  • They provide the ability to do significant
    processing that is not possible with HTML
  • Applets subclass the Applet class and take over a
    region of the screen inside of a web browser
    window
  • The .class files are downloaded from the server
    and run on the client side

22
Applets
  • To run applets, the browser must support JVM -
    (Microsoft or the original one from Sun)
  • Applets run in a sandbox in the browser. They
    cannot read or write to the disk
  • applets inherits from java.applet.Applet
  • or java.swing.JApplet
  • applets do not have a main method
  • applets may code init(), destroy(), start(),
    stop() methods

23
Applets
// Snowman.java - Draws a snowman. import
java.applet.Applet import java.awt. public
class Snowman extends Applet public void paint
(Graphics page) final int MID 150
final int TOP 50 setBackground
(Color.cyan) page.setColor (Color.blue)
page.fillRect (0, 175, 300, 50) // ground
page.setColor (Color.yellow) page.fillOval
(-40, -40, 80, 80) // sun .
24
Applets
  • Typically we have .java, .class and .html file
  • Within the html file we code
  • ltAPPLET CODE"Snowman.class" width400"
    height325"gtlt/APPLETgt
  • Appletviewer (in JSDK) can be used to test the
    applets
  • Commerically applets pose a security risk!
  • So lately the industry has realized the pitfalls
    of big claims So introduced Servlets (which
    runs on the Server side and not on your PC)
  • There are a variety of fancy applets readily
    available from the Web which you can use!

25
IDE - Integrated Development Environments
Sun Provides the basic Java Compiler Kit (JSDK)
Windows Based IDEs Sun Java Studio (prev
known as Forte) Visual Age from IBM JBuilder from
Borland Eclipse Visual Java . an endless
list All these provide some graphical tools which
generate some base code for you business logic
has to be filled in later. Most tools are heavy
and too heavy for learning! BlueJ - from
University of Southern Denmark is a compact tool
(only 2M), recommended for learning.
26
Suns View on Modern Applications
27
Server Side Programming
Servlets, JSP and Beans have to run at Server -
typically known as Java Application
Server Products - Websphere from IBM - Weblogic
from BEA - JBoss
28
Just one more!
We have come a long way from the simple Hello
World Java in commercial programming is as
complex as anything else (contrary to the
buzzword claims) OOP issues revolve around
defining and using the classes and methods the
right way . . . Thats the challenge! With
multiplatform execution and Database Access, the
Systems and code can be complex! If we
remember this, we can make successful systems!
Write a Comment
User Comments (0)
About PowerShow.com