Download presentation
Presentation is loading. Please wait.
1
Some Special Issues (c) IDMS/SQL News http://www.geocities.com/idmssql
2
Special Cases (c) IDMS/SQL News2 Selected Special Issues ñ Class Libraries / Packages ñ Dot notation ñ String Class ñ ‘this’ ñ Switch statement ñ Method Overloading ñ Polymorphism ñ Applets ñ IDEs
3
Special Cases (c) IDMS/SQL News3 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
Special Cases (c) IDMS/SQL News4 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.lang java.applet java.awt javax.swing java.net java.util Purpose General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities and components Network communication Utilities
5
Special Cases (c) IDMS/SQL News5 The import Declaration ñ When you want to use a class from a package, you could use its fully qualified namejava.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
Special Cases (c) IDMS/SQL News6 Dot notation By now it must be clear that Java uses the ‘.’ notation extensively String title = “Developing Java Software"; String title = “Developing Java Software"; title.length() to get length title.length() to get length the dot operator used to invoke methods, refer to variables, objects etc System.out.println(”Hello”"); System.out.println(”Hello”");
7
Special Cases (c) IDMS/SQL News7 System.out.println (”Here we see many OO features "); object method Information provided to the method ) (parameters) System.out.print System - Class out - a ‘variable’ defined within Class System public static final PrintStream out println, print - methods inherited by out from class PrintStream
8
Special Cases (c) IDMS/SQL News8 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
Special Cases (c) IDMS/SQL News9 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
Special Cases (c) IDMS/SQL News10 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
Special Cases (c) IDMS/SQL News11 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); { 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
12
Special Cases (c) IDMS/SQL News12 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; } If no match default will be executed All in blue are reserved words
13
Special Cases (c) IDMS/SQL News13 // 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
Special Cases (c) IDMS/SQL News14 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
Special Cases (c) IDMS/SQL News15 Overloaded Methods The println method is overloaded: 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 (total); System.out.println (total); invoke different versions of the println method
16
Special Cases (c) IDMS/SQL News16 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
17
Special Cases (c) IDMS/SQL News17 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); }}
18
Special Cases (c) IDMS/SQL News18 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
Special Cases (c) IDMS/SQL News19 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.
20
Special Cases (c) IDMS/SQL News20 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
21
Special Cases (c) IDMS/SQL News21 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
Special Cases (c) IDMS/SQL News22 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
Special Cases (c) IDMS/SQL News23 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
Special Cases (c) IDMS/SQL News24 ñTypically we have.java,.class and.html file ñWithin the html file we code ñ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! Applets
25
Special Cases (c) IDMS/SQL News25 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
Special Cases (c) IDMS/SQL News26 Sun’s View on Modern Applications
27
Special Cases (c) IDMS/SQL News27 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
Special Cases (c) IDMS/SQL News28 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... 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!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.