Download presentation
Presentation is loading. Please wait.
Published byKatrina Snow Modified over 9 years ago
1
Some odds and ends about Classes and Objects
2
6-2 Object-Oriented Programming Object Data (Fields) Methods That Operate on the Data
3
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.
4
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.
5
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.
6
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.
7
Arrays.sort() 1 2 67 22 19 23 14 15 3 1 2 67 22 19 23 14 15 3 anArray public static void sort (int [] a) { } public static void sort (int [] a) { } 1 2 3 14 15 19 22 23 67 1 2 3 14 15 19 22 23 67
8
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
9
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
10
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
11
6-11 UML Diagram for Rectangle class Rectangle -length: double -width: double +setLength(): double +setWidth(): double getLength() getWidth() getArea()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.