H:\ jg3e javagently ProgTwo Lab3 Laboratory Programming II Lecture 7 A case study (class containing an array) and model diagrams. 02/02/2003 Dr Andy Brooks 1
Scope The scope of an identifier is the region of a program source within which it represents a certain thing. This usually extends from the place where it is declared to the end of the smallest enclosing block. from FOLDOC 02/02/2003 Dr Andy Brooks 2
Accessibility control Modifier Class Member no modifier public private protected abstract final static Accessible only within its package. Accessible anywhere its package is. n/a n/a May not be instantiated. May not be sub-classed. A top-level class not an inner one. Accessible only within its package. Accessible anywhere its class is. Accessible only within its own class. Accessible within its package and its subclasses A method is not implemented here but in a subclass. A value may not be changed, a method not overridden. Class member accessed through its class name. 02/02/2003 Dr Andy Brooks JavaGently page 325 3
A clear understanding of accessibility control is required if programs in one project folder (package) are to make use of classes in another folder (package). import java.io.*; import javagently.*; class Experiments { int x; int y; package javagently; import java.io.*; import java.util.*; import java.text.*; public class Stream { 02/02/2003 Dr Andy Brooks 4
A reminder of modelling notation. Classid Classid Classid objectid variables methods objectid Classid - variableprivate -methodprivate +methodpublic packageid Classes variables methods objectid packageid java.io.* Hashtable h :CurioStore1 Key Value Classid mugs mugs CurioStore1() price = 6 write() arrayid[ ] 0 1 null null explanations 2 null 02/02/2003 Dr Andy Brooks 5
class IntArray { private int A []; IntArray (int n) { A = new int [n]; int get (int i) { return A [i]; void set (int i, int x) { A [i] = x; case study: class containing an array JavaGently page 210 Note the scope of A. // Object class has a tostring() method - example here of method overriding public String tostring () { String s = ""; for (int i = 0 ; i < A.length ; i++) { s += " " + A [i]; return s; 02/02/2003 Dr Andy Brooks 6 arrays are always indexed starting at 0. length is a property not a method.
import java.lang.*; //done automatically and includes Math class import java.io.*; class ArrayTest { A test program using ArrayTest () throws IOException { IntArray marks = new IntArray (5); class IntArray int m; for (int i = 0 ; i < 5 ; i++) { m = (int) (Math.random () * 100); marks.set (i, m); System.out.println (marks); public static void main (String args []) throws IOException { new ArrayTest (); Do we need throws IOException here? Do we need import java.io.* here? 02/02/2003 Dr Andy Brooks 7
Modelling notation Users of the notation may provide as much or as little detail and timewise detail as they wish Details may include: variables (attributes) and methods parameter lists accessibility control explanation notes for methods and statements classes and objects Under test, assignment, or examination conditions, the level of detail to be provided will be made clear. 02/02/2003 Dr Andy Brooks 8
IntArray Create the private array A - int A [] IntArray(int) int get (int) set(int,int) + String tostring() Return the value at the subscript given Set the value at the the subscript given Return a string representation of the private Array A - private + public 02/02/2003 Dr Andy Brooks 9
ArrayTest :ArrayTest ArrayTest() main(string args[]) ArrayTest() IntArray marks = new IntArray[5] - int A [] IntArray IntArray(int) int get (int) set(int,int) + String tostring() - int A [] marks IntArray(int) int get (int) set(int,int) + String tostring() 0 1 2 3 Model diagram at a particular moment in time. Nothing is set into the array. 4 02/02/2003 Dr Andy Brooks 10
Curiostore3() // Declare object variables relevant to all methods Display display = new Display ("Polelo Curio Store"); Curio mugs, tshirts, carvings; boolean open; CurioStore3() void report() void stockthestore() void openthestore() void sellcurios() void available() 02/02/2003 Dr Andy Brooks 11
Curiostore3 Display display Curio mugs,tshirts,carvings boolean open main(string [ ] args) CurioStore3() void report() void stockthestore() void openthestore() void sellcurios() void available() Note the absence of accessibility modifiers. 02/02/2003 Dr Andy Brooks 12