Some odds and ends about Classes and Objects
6-2 Object-Oriented Programming Object Data (Fields) Methods That Operate on the Data
private static int count = 0; public String name; public Name(String name) { this.name = name; count++; } public static int getCount() { return count; } public String getName() { return name; } private static int count = 0; public String name; public Name(String name) { this.name = name; count++; } public static int getCount() { return count; } public String getName() { return name; } 1Class Name sally.name:“Sally” sally.getName() sally.name:“Sally” sally.getName() molly.name: “Molly” molly.getName() molly.name: “Molly” molly.getName() polly.name: “Polly” polly.getName() polly.name: “Polly” polly.getName() 3 3 Name.count sally molly polly new Initial state. Declared three Name objects and instantiated them.
private static int count = 0; public String name; public Name(String name) { this.name = name; count++; } public static int getCount() { return count; } public String getName() { return name; } private static int count = 0; public String name; public Name(String name) { this.name = name; count++; } public static int getCount() { return count; } public String getName() { return name; } 1Class Name sally.name:“Sally” sally.getName() sally.name:“Sally” sally.getName() molly.name: “Molly” molly.getName() molly.name: “Molly” molly.getName() name: “Polly” getName() name: “Polly” getName() 3 3 Name.count sally molly polly new Name gerry; gerry = polly; gerry Declared a Name object, but then assigned the value in one object to the new object.
private static int count = 0; public String name; public Name(String name) { this.name = name; count++; } public static int getCount() { return count; } public String getName() { return name; } private static int count = 0; public String name; public Name(String name) { this.name = name; count++; } public static int getCount() { return count; } public String getName() { return name; } 1Class Name sally.name:“Sally” sally.getName() sally.name:“Sally” sally.getName() molly.name: “Molly” molly.getName() molly.name: “Molly” molly.getName() name: “Gerry” getName() name: “Gerry” getName() 3 3 Name.count sally molly polly new ? gerry.name = “Gerry”; gerry Changed the name of the recently added name.
Objects, pass by reference or value? In Java, ALL arguments are passed by value. The value of the argument is copied into the parameter. Return statements in Java always return a value. What does this really mean though for objects? Let’s look at Arrays.sort and why we deep copy for answers.
Arrays.sort() anArray public static void sort (int [] a) { } public static void sort (int [] a) { }
Why Deep Copy? DMV Exam object solution DMVExam test; test = new DMVExam(); char [] answers; answers = test.getSolution(); answers[0] = ‘X’; DMVExam test; test = new DMVExam(); char [] answers; answers = test.getSolution(); answers[0] = ‘X’; public char[] getSolution() { return solution; } public char[] getSolution() { return solution; } answers
Why Deep Copy? DMV Exam object solution DMVExam test; test = new DMVExam(); char [] answers; answers = test.getSolution(); answers[0] = ‘X’; DMVExam test; test = new DMVExam(); char [] answers; answers = test.getSolution(); answers[0] = ‘X’; public char[] getSolution() { char[] result; result = new char[solution.length]; for (int ii = 0; ii < solution.length; ii++) result[ii] = solution.length; return result; } public char[] getSolution() { char[] result; result = new char[solution.length]; for (int ii = 0; ii < solution.length; ii++) result[ii] = solution.length; return result; } answers result
6-10 UML Diagram – Gaddis 304 Unified Modeling Language (UML) provides a set of standard diagrams for graphically depicting object-oriented systems. Class name goes here Fields are listed here Methods are listed here
6-11 UML Diagram for Rectangle class Rectangle -length: double -width: double +setLength(): double +setWidth(): double getLength() getWidth() getArea()