+ All Categories
Home > Documents > Some Special Issues (c) IDMS/SQL News .

Some Special Issues (c) IDMS/SQL News .

Date post: 28-Dec-2015
Category:
Upload: russell-thornton
View: 237 times
Download: 2 times
Share this document with a friend
28
Some Special Issues (c) IDMS/SQL News http://www.geocities.com/ idmssql
Transcript

Some Special Issues

(c) IDMS/SQL News http://www.geocities.com/idmssql

Special Cases (c) IDMS/SQL News 2

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

Special Cases (c) IDMS/SQL News 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.

Special Cases (c) IDMS/SQL News 4

Packages The classes of the Java standard class library are organized

into packages. Some of the packages in the standard class library are:

Package

java.langjava.appletjava.awtjavax.swingjava.netjava.util

Purpose

General supportCreating applets for the webGraphics and graphical user interfacesAdditional graphics capabilities and componentsNetwork communicationUtilities

Special Cases (c) IDMS/SQL News 5

The import Declaration When you want to use a class from a package, you could use its

fully qualified namejava.util.Randomjava.util.Random

Or you can import the class, then just use the class nameimport java.util.Random;import java.util.Random;

To import all classes in a particular package, you can use the * wildcard character

import java.util.*; 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.

Special Cases (c) IDMS/SQL News 6

Dot notation

By now it must be clear that Java uses the ‘.’ notation extensively

String title = “Developing Java String title = “Developing Java Software"; Software";

title.length() title.length() to get lengthto get length

the dot operator used to invoke methods, refer to variables, objects etc

System.out.println(”Hello”");System.out.println(”Hello”");

Special Cases (c) IDMS/SQL News 7

System.out.println (”Here we see many OO features ");

object methodInformation provided to the method

(parameters))

System.out.print

System - Classout - a ‘variable’ defined within Class System public static final PrintStream outprintln, print - methods inherited by out from class PrintStream

Special Cases (c) IDMS/SQL News 8

String - RecapNot a basic data type!String in Java is implemented as a classString 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 )

Special Cases (c) IDMS/SQL News 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!

Special Cases (c) IDMS/SQL News 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 ");} } }

Special Cases (c) IDMS/SQL News 11

this … ‘this’ within a method refers to the current object, and is used to qualify variable namesString surname, company;public void setEtternavn (String etternavn); { surname=etternavn; company=“edb telekom”;}but if we define the same like:String etternavn, company;public void setEtternavn (String etternavn); { this.etternavn= etternavn; this.company=“edb telekom”;}

calling program would have used obj1.setEtternavn(“Ibsen”);

Argument passed from the calling program

Special Cases (c) IDMS/SQL News 12

SwitchJumps 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; }

If no match default will be executed

All in blue arereserved words

Special Cases (c) IDMS/SQL News 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

Special Cases (c) IDMS/SQL News 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

Special Cases (c) IDMS/SQL News 15

Overloaded Methods

The println method is overloaded: println (String s)println (String s) println (int i)println (int i) println (double d)println (double d)

etc. The lines System.out.println ("The total is:");System.out.println ("The total is:"); System.out.println (total);System.out.println (total);

invoke different versions of the println method

Special Cases (c) IDMS/SQL News 16

Another example

//this class finds the area of a public class Shape { public float FindArea(int radius) { return 3.14f*radius*radius;} // pi(r*r) public int FindArea(int length, int breadth) { return length* breadth;} // ab public float FindArea(int a, int b, int

height) { return (1.0f/2) * (a+b)* height;} //

1/2(a+b)h } // end of class

Special Cases (c) IDMS/SQL News 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_circle=circle.FindArea(10);System.out.println("area_of_circle=" + area_of_circle);area_of_rect = rectangle.FindArea(10, 20);System.out.println("area_of_rect=" + area_of_rect);}}

Special Cases (c) IDMS/SQL News 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

Special Cases (c) IDMS/SQL News 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 Holiday’s

version of celebrate; if it refers to a Christmas object, it invokes that version.

Special Cases (c) IDMS/SQL News 20

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.

Polymorphism

Special Cases (c) IDMS/SQL News 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 HTMLApplets subclass the Applet class and take over a region of the screen inside of a web browser windowThe .class files are downloaded from the server and run on the client side

Special Cases (c) IDMS/SQL News 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.Appletor java.swing.JAppletapplets do not have a main methodapplets may code init(), destroy(), start(), stop() methods

Special Cases (c) IDMS/SQL News 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….} }

Special Cases (c) IDMS/SQL News 24

Typically we have .java, .class and .html file Within the html file we code <APPLET CODE="Snowman.class" width=”400" height=”325"></APPLET>Appletviewer (in JSDK) can be used to test the appletsCommerically 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!

Applets

Special Cases (c) IDMS/SQL News 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 IBMJBuilder from BorlandEclipse Visual Java …. an endless listAll these provide some graphical tools which generate some base code for you… business logic has to be filledin later. Most tools are heavy and too heavy for learning!

BlueJ - from University of Southern Denmark is a compacttool (only 2M), recommended for learning.

Special Cases (c) IDMS/SQL News 26

Sun’s View on Modern Applications

Special Cases (c) IDMS/SQL News 27

Server Side Programming

Servlets, JSP and Beans have to run at Server - typicallyknown as Java Application ServerProducts:- Websphere from IBM- Weblogic from BEA- JBoss

Special Cases (c) IDMS/SQL News 28

Just one more!

We have come a long way from the simple “Hello World”

Java in commercial programming is as complex as anythingelse (contrary to the buzzword claims)

OOP issues revolve around defining and using the classes and methods the right way . . . That’s the challenge!

With multiplatform execution and Database Access, the Systems and code can be complex!

If we remember this, we can make successful systems!


Recommended