1 / 98

Advanced Java Programming CSE 7345/5345/ NTU 531

Welcome Back!!!. Advanced Java Programming CSE 7345/5345/ NTU 531. Session 4. Office Hours: by appt 3:30pm-4:30pm SIC 353. Chantale Laurent-Rice. Welcome Back!!!. trice75447@aol.com. claurent@engr.smu.edu. What value is printed out at line 6?.

stu
Download Presentation

Advanced Java Programming CSE 7345/5345/ NTU 531

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Welcome Back!!! Advanced Java ProgrammingCSE 7345/5345/ NTU 531 Session 4

  2. Office Hours: by appt 3:30pm-4:30pm SIC 353 Chantale Laurent-Rice Welcome Back!!! trice75447@aol.com claurent@engr.smu.edu

  3. What value is printed out at line 6? • // Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts • 1. class Checking{ • 2. public static void main(String args[]) { • 3. Holder h = new Holder(); • 4. h.held = 100; • 5. h.bump(h); • 6. System.out.println(h.held); • 7. } • 8. } • 9. • 10. class Holder{ • 11. public int held; • 12. public void bump(Holder theHolder) { • 13. theHolder.held++; • 14. } • 15. }

  4. Input/output selection • Introducing Java's Control Statements • The if Statement  • The if statement is one of Java's selection statements (sometimes called conditional statements). • Its operation is government by one of the outcome of a conditional test that evaluates to either true or false.

  5. Example • if (10 > 9) System.out.println("true");  // Input/output selection public class IfDemo { public static void main(String[] args) { if (args.length == 0) System.out.println("You must have command line argument"); } }

  6. Remember • Remember, a number is not a boolean. Therefore, it is not valid to have an if statement such as the following: if (count + 1) System.out.println("Not Zero"); Such a line generates a compiler error.

  7. If-else statement public class IfDemo { public static void main(String[] args) { if (args.length = = 0) System.out.println("You must have command line argument"); else System.out.prinltn(“not good”); } }

  8. The for statement • The for loop is one of Java's three loop statements. • It allows one or more statements to be repeated and is considered by many Java programmers to be its most flexible loop. • The for loop is used to repeat a statement or block of statements a specified number of times. Its general form for repeating a single statement is as followed: for(initialization; test; increment) statement;

  9. Example • public class ForLoop { public static void main(String[] args) { for (int num = 1; num < 11; num = num + 1) System.out.print(num + " "); System.out.println("terminating"); } }

  10. The Increment operators • public class ForLoopIncr { public static void main(String[] args) { for (int num = 1; num < 11; num ++) System.out.print(num + " "); System.out.println("terminating"); } }

  11. The decrement operators public class ForLoopDecr { public static void main(String[] args) { for (int num = 1; num >= 11; num --) System.out.print(num + " "); System.out.println("terminating"); } }

  12. What value is printed out at line 6? • // Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts • 1. class DecMe{ • 2. public static void main(String args[]) { • 3. double d = 12.3; • 4. Decrementer dec = new Decrementer(); • 5. dec.decrement(d); • 6. System.out.println(d); • 7. } • 8. } • 9. • 10. class Decrementer{ • 11. public void decrement(double decMe){ • 12. decMe = decMe -1.0; • 13. • 14. } • 15. }

  13. Introduction • Chapter 6 con’t • Objects and classes

  14. In this chapter • Object-Oriented Programming • Introducing classes and objects • Class methods • Input and output methods • Objects and garbage collection

  15. Object and classes • Overloading Method Names • Overloading is the reuse of a method name in the one class or subclass for a different method. • Overloading methods are effectively independent, and there are no constraints on the accessibility, return type, or exceptions that may be thrown. Changing parameter names is not sufficient to count as overloading. • public void oMethod(String s){ } • public void oMethod() { } • public void oMethod(int i, String s) { } • public void oMethod(String s, int i) { }

  16. 1. public class ConsideringIt{ 2. public float oMethod(float a, float b){ 3. } 4. } Which of the following methods would be legal if added (individually) at line 4? public int oMethod(int a, int b) { } public float oMethod(float a, float b) { } c. public float oMethod(float a, float b, int c ) throws Exception { } d. public float oMethod(float c, float d) { } e. private float oMethod(int a, int b, int c) { } Consider this code

  17. Class • The keyword class indicates that a class names clsName is being declared. • This name must follow the Java naming conventions for identifiers.

  18. Class (con’t) • Constructors always have the same name as the class. They do not have return values.

  19. Declaring classes • A class begins with the class keyword followed by braces that delimit the class’s content: • class AnyClass • { • ………. • }

  20. Declaring classes • Most classes have one or more methods, such as main(), which is found in all stand-alone Java applications. • Class AnyClass • { • public static void main(String[] args) • { • //statements inside main( ) • } • }

  21. Declaring classes • You can also create other methods and call them from main() and from other places. class AnyClass { public static void main(String args[]) { HiThere(); // Call HiThere( ) method } public static void HiThere() { System.out.println("Hi There!"); } }

  22. Class Name • Class names are usually capitalized. • Variables and methods begin with lowercase letters. • These conventions are not requirements, but help make program clearer to read and understand.

  23. Class Bag class Bag { boolean flag; int i, j =2, k = 3, l, m; double array[] = { -3.4, 8.8e100, 09.2e-100}; String s1, s2 = new String(“Hello”); }

  24. Class Bag class BagTest { public static void main(String[] args) { Bag bag = new Bag(); System.out.println(bag.flag); System.out.println(bag.i); System.out.println(bag.j); System.out.println(bag.k); System.out.println(bag.l); System.out.println(bag.m); for(int i =0; i < bag.array.length; i++) System.out.println(bag.array[i]); System.out.println(bag.s1); System.out.println(bag.s2); } }

  25. Object-oriented key concepts: • In Java, object-oriented programming revolves around a few concepts: • classes • objects • data members • methods • and inheritance

  26. Questions to consider…. • What are those terms mean: • classes, • objects, • data members, • methods, • and inheritance.

  27. Consider this code Which one statement is true about this code?a. Line 8 will not compile, it is static reference to a private variableb. line 13 will not compile, because it is a static reference to a private variablec. The program compiles, and the output is x = 102.d. The program compiles, and the output is x = 103.e. The program compiles, and the output is x = 104. • 1. class HasStatic • 2. { • 3. private static int x =100; • 4. • 5. public static void main(String args[]) • 6. { • 7. HasStatic has1 = new HasStatic(); • 8. has1.x++; • 9. HasStatic has2 = new HasStatic(); • 10. has2.x++; • 11. has1 = new HasStatic(); • 12. has1.x++; • 13. HasStatic.x++; • 14. System.out.println("x = " + x); • 15. } • 16. }

  28. Creating an object • To create a object, you call a class’s constructor, which is a method with the same name as the class itself. • This constructor creates a new object of the class. • You call an instance of a class an object. • An object is a variable.

  29. Creating an object • An object occupies space in memory, and it must be initialized.

  30. The new Operator • Up to this point, you have been creating objects indirectly, such as through the use of some Java’s static methods. • It is now time to learn how to create an object directly.

  31. Objects • Objects are created using the new operator. • Or, put differently, the new operator creates an instance of a class. • It is invoked as follows: • clsName ObjRef = new clsName(args);

  32. Creating objects • clsName is the name of the class to be instantiated. • Instantiated means to create an instance of that class. • A reference to the new object is assigned to a variable name objRef. • Notice the expression immediately to the right of the keyword new. • clsName ObjRef = new clsName(args); • This is known as a constructor.

  33. Constructor • A constructor creates an instance of a class. It has the same name as the class and may optionally have an argument list args. • The next slide illustrates the relationship between objects and object reference variables. • In the diagram, the variable named varA refers to one object. • Variables named varB and varC both refer to a second object. • The third object is referred to by the variable named varD.

  34. Objects and object references • Here, i is a simple int value and s is a String object. Notice that the second form of the constructor can throw an exception if the constructor argument is not correctly formatted. • The first form of the constructor cannot throw an exception because any int can be used to create an Integerobject. varA Object varB Object varC Object varD

  35. Keypoint • A key point to understand is that the variable is distinct from the object. • In effect, a variable that serves as an object reference has an implicit pointer to the object. • However, a Java programmer cannot directly access the pointer. • Also note that multiple variables may refer to the same object.

  36. The symbol null • The symbol null has a special meaning in Java. • It represents the value of an object reference variable when that variable does not reference any object.

  37. Again Classes and Objects • Classes and objects form the building blocks of any Java program. Therefore, a basic understanding of them is necessary.

  38. Constructor Modifiers These are three modifiers that may precede the declaration of a constructor. These are summarized in the following table: KeywordMeaning Private Can be invoked only by code in the same class Protected Can be invoked only by code in a subclass or the same package Public Can be invoked by any other class

  39. Constructor Modifiers • These modifiers are mutually exclusive. • If none is specified • The default is that only code in the same package may access that • constructor.

  40. Example: class Person { // declare variable; String name; int age; public Person(String name, int age) { this.name = name; this.age = age; } private Person() { } }

  41. Example class PrivateConstructor { public static void main(String[] agrs) { // Public constructor may be invoked Person p1 = new Person(“John”, 30); System.out.println(p1.name); System.out.println(p1.age); //Private constructor may not be invoked //Person p2 = new Person(); } }

  42. Method Modifiers • There are eight possible modifiers that may precede the declaration of a method. • These are summarized in the following table:

  43. Method Modifiers Keyword Meaning Abstract Is not implement by this class Final May not be overridden Native The method is implemented in the machine Code used by the host CPU, not using Java bytecodes.

  44. Method Modifiers Private Can be invoked only by code in the same class Protected Can be invoked only by code in a subclass. Or the same package Public Can be invoked by any other class Static Is not an instance variable Synchronized Acquires a lock when it begins execution.

  45. Meanings If a class contains an abstract method, that class itself must also be declared abstract. Otherwise, the Java compiler issues an error message. The public, protected, and private modifiers are mutually exclusive. The synchronized modifier is very important in multithreaded programming. The native modifier is beyond the scope of programming. If none of these modifiers are specified, the method is assumed to be a non-abstract, non-final, non-native, non-synchronized method. It may be accessed only by code in the same package.

  46. Example • The class JetPlane declares one abstract method named numEngines(). • Therefore, the class itself must also be declared abstract. • There are two concrete subclasses named DC8 and DC10. Each of these provided a different implementation of numEngines() method. • The main() method instantiates each of these classes and invoke its numEngines method. • This is an excellent example of run-time polymorphism. • Each subclass provides a different form of the method.

  47. abstract class JetPlane { abstract int numEngines(); } class DC8 extends JetPlane { int numEngines() { return 4; } } class DC10 extends JetPlane { int numEngines() { return 3; } } Example

  48. Example cont class JetPlanes { public static void main(String[] args) { System.out.println(new DC8().numEngines()); System.out.println(new DC10().numEngines()); } }

  49. Chapter 7The StringBuffer Class • There is no way to change the character sequence encapsulated by String object after it is created. • The StringBuffer class also encapsulates a sequence of characters.

  50. Its constructor has the following forms: • /* This form of the constructor initializes the buffer size to 16 character*/ StringBuffer( ) • /*This form explicitly sets the buffer capacity to size characters*/ StringBuffer(int size)  • /*This form initializes the buffer with the contents of s and also reserves another 16 characters for expansion*/ StringBuffer String s

More Related