comp 110 representation
DESCRIPTION
Comp 110 Representation. Instructor: Jason Carter. More on Objects. Testing objects and writing stubs Calling getters/setters from programs Two-way dependencies Representations with errors Default values of variables Null value Class variables/methods Polymorphism vs. overloading - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/1.jpg)
COMP 110REPRESENTATION
Instructor: Jason Carter
![Page 2: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/2.jpg)
2
MORE ON OBJECTS Testing objects and writing stubs Calling getters/setters from programs Two-way dependencies Representations with errors Default values of variables Null value Class variables/methods Polymorphism vs. overloading Structure vs. atomic types Physical vs. logical structure Graphics types Top-down, bottom-up, middle-out programming
![Page 3: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/3.jpg)
3
IMPORTANCE OF TESTING
Experienced programmers make errorsImportant to test programs
![Page 4: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/4.jpg)
4
TESTING BMI SPREADSHEET
![Page 5: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/5.jpg)
5
TESTING BMI SPREADSHEET
![Page 6: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/6.jpg)
6
TESTING BMI SPREADSHEET
![Page 7: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/7.jpg)
7
CORVETTE VS. BMI SPREADSHEET USAGE
Test new Corvette for mileage
Get car from Corvette factory
Drive car Compare actual and
advertised mileage
Test BMISpreadsheet for correctness
Instantiate class ABMISpreadsheet
Give values to height and weight
Compare actual and computed BMI
How to do this in a programmed method
such as test ?
![Page 8: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/8.jpg)
8
INTERACTIVE VS. PROGRAMMED SETTER
bmiSpreadsheet.setHeight(1.77);
![Page 9: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/9.jpg)
9
INTERACTIVE VS. PROGRAMMED GETTER
double computedBMI = bmiSpreadsheet.getBMI();
![Page 10: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/10.jpg)
10
BMI TESTER
public class ABMISpreadsheetTester {public void test (
double theHeight, double theWeight, double theCorrectBMI)
{
}}
![Page 11: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/11.jpg)
11
BMI TESTER (EDIT)
public class ABMISpreadsheetTester {public void test (
double theHeight, double theWeight, double theCorrectBMI)
{ ABMISPreadsheet bmiS = new ABMISpreadsheet(theHeight,theWeight); double computedBMI = bmiS.betBMI(); … System.out.println(theCorrectBMI – computedBMI);
}}
![Page 12: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/12.jpg)
12
BMI TESTER (EDIT)
public class ABMISpreadsheetTester {public void test (
double theHeight, double theWeight, double theCorrectBMI)
{ BMISpreadsheet bmiS = new ABMISpreadsheet(); bmiS.setHeight(theHeight); bmiS.setWeight(theWieght); double computedBMI = bmiS.betBMI(); … System.out.println(theCorrectBMI – computedBMI);
}}
![Page 13: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/13.jpg)
13
TESTING MULTIPLE VALUES
![Page 14: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/14.jpg)
14
TESTING BMI SPREADSHEET
![Page 15: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/15.jpg)
15
TESTING BMI SPREADSHEET
Steps taken by ABMISpreadsheetTester?
![Page 16: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/16.jpg)
16
BMI TESTERpublic class ABMISpreadsheetTester {
public void test (double theHeight, double theWeight, double
theCorrectBMI) {
BMISpreadsheet bmiSpreadsheet = new ABMISpreadsheet(theHeight, theWeight);
double computedBMI = bmiSpreadsheet.getBMI();System.out.println("------------");System.out.println("Height:" + theHeight);System.out.println("Weight:" + theWeight);System.out.println("Expected BMI:" + theCorrectBMI);System.out.println("Computed BMI:" + computedBMI);System.out.println(
"Error:" + (theCorrectBMI - computedBMI));System.out.println("------------");
}public void test () {
test(1.65, 60, 20); test(1.55, 60, 25); test(1.8, 65, 20);
}}
![Page 17: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/17.jpg)
17
BMI TESTER (EDIT)public class ABMISpreadsheetTester {
public void test (double theHeight, double theWeight, double
theCorrectBMI) {
BMISpreadsheet bmiSpreadsheet = new ABMISpreadsheet(theHeight, theWeight);
double computedBMI = bmiSpreadsheet.getBMI();System.out.println("------------");System.out.println("Height:" + theHeight);System.out.println("Weight:" + theWeight);System.out.println("Expected BMI:" + theCorrectBMI);System.out.println("Computed BMI:" + computedBMI);System.out.println(
"Error:" + (theCorrectBMI - computedBMI));System.out.println("------------");
}public void test () {}
}
![Page 18: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/18.jpg)
18
BMI TESTERpublic class ABMISpreadsheetTester {
public void test (double theHeight, double theWeight, double
theCorrectBMI) {
BMISpreadsheet bmiSpreadsheet = new ABMISpreadsheet(theHeight, theWeight);
double computedBMI = bmiSpreadsheet.getBMI();System.out.println("------------");System.out.println("Height:" + theHeight);System.out.println("Weight:" + theWeight);System.out.println("Expected BMI:" + theCorrectBMI);System.out.println("Computed BMI:" + computedBMI);System.out.println(
"Error:" + (theCorrectBMI - computedBMI));System.out.println("------------");
}public void test () {
test(1.65, 55, 2.0);test(1.55, 60, 25);test(1.80, 65, 20);
}} Actual
ParametersFormal
Parameters
![Page 19: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/19.jpg)
19
TRADITIONAL APPROACH
Tested Code
Tester
ABMISpreadsheet
ABMISpreadsheetTester
Code the complete object to be tested
Write and use test object
Recode and retest if necessary
Testing is an afterthought
All methods tested together – hard to pinpoint bugs
![Page 20: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/20.jpg)
20
INCREMENTAL TESTING
Tested Code
Tester
ABMISpreadsheet
ABMISpreadsheetTester
Tested code and tester could evolve concurrently
getBMI() cannot be tested until it has been written
![Page 21: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/21.jpg)
21
TEST-FIRST
Tester
Implement or
change the stub
ABMISpreadsheetTester
ABMISpreadsheet with a stub (re)
implemented
Write the complete tester, defining the expected behavior of the tested object
Code the object with only stubs
Test, implement a stub and retest
Stubs for Tested Code
ABMISpreadsheet with Stubs
![Page 22: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/22.jpg)
22
STEPS IN CREATING AND TESTING A NEW CLASS
Write the interface Create a class with stubs for each interface
method and constructor If method is procedure method does nothing If method is function, it returns 0 or null valueNo variables need be declared as this point!
Write a tester for it Write/rewrite in one or more stub methods Use tester and ObjectEditor If tester results and ObjectEditor results not
correct, go back to 4
Steps may be combined for simple classes!
![Page 23: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/23.jpg)
23
WRITING THE INTERFACE
public interface BMISpreadhsheet {public double getHeight() ;public void setHeight (double
newHeight) ;public double getWeight() ;public void setWeight(double newWeight) ;public double getBMI() ;
}
![Page 24: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/24.jpg)
24
STUBS
public class ABMISpreadsheet implements BMISpreadhsheet {
public ABMISpreadsheet(double theInitialHeight, double theInitialWeight)
{}public ABMISpreadsheet() {} public double getHeight() {}public void setHeight (double newHeight) {}public double getWeight() {}public void setWeight(double newWeight) {}public double getBMI() {}
}
No variables!
![Page 25: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/25.jpg)
25
STUBS (EDIT)
public class ABMISpreadsheet implements BMISpreadhsheet {
public ABMISpreadsheet(double theInitialHeight, double theInitialWeight)
{}public ABMISpreadsheet() {} public double getHeight() {return 0;}public void setHeight (double newHeight) {}public double getWeight() {return 0;}public void setWeight(double newWeight) {}public double getBMI() {return 0;}
}
No variables!
![Page 26: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/26.jpg)
26
STUBS
public class ABMISpreadsheet implements BMISpreadhsheet {
public ABMISpreadsheet(double theInitialHeight, double theInitialWeight)
{}public ABMISpreadsheet() {} public double getHeight() { return 0; }public void setHeight (double newHeight) {}public double getWeight() { return 0; }public void setWeight(double newWeight) {}public double getBMI() { return 0; }
}
No variables!
![Page 27: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/27.jpg)
27
USING OBJECTEDITOR
![Page 28: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/28.jpg)
28
USING TESTER
![Page 29: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/29.jpg)
29
USING TESTER
We know what the correctness criteria are!
![Page 30: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/30.jpg)
30
AFTER ALL STUBS ARE FILLED
![Page 31: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/31.jpg)
31
BMI TESTERpublic class ABMISpreadsheetTester {
public void test (double theHeight, double theWeight, double
theCorrectBMI) {
BMISpreadsheet bmiSpreadsheet = new ABMISpreadsheet(theHeight, theWeight);
double computedBMI = bmiSpreadsheet.getBMI();System.out.println("------------");System.out.println("Height:" + theHeight);System.out.println("Weight:" + theWeight);System.out.println("Expected BMI:" + theCorrectBMI);System.out.println("Computed BMI:" + computedBMI);System.out.println(
"Error:" + (theCorrectBMI - computedBMI));System.out.println("------------");
}public void test () {}
}Interface as
typeCalling
constructor Calling getter
![Page 32: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/32.jpg)
32
CALLING PARAMETERLESS CONSTRUCTOR AND SETTERS
public class ABMISpreadsheetTester {public void test (
double theHeight, double theWeight, double theCorrectBMI)
{BMISpreadsheet bmiSpreadsheet = new
ABMISpreadsheet();bmiSpreadsheet.setHeight(theHeight);bmiSpreadsheet.setWeight(theWeight);double computedBMI = bmiSpreadsheet.getBMI();System.out.println("------------");System.out.println("Height:" + theHeight);System.out.println("Weight:" + theWeight);System.out.println("Expected BMI:" + theCorrectBMI);System.out.println("Computed BMI:" + computedBMI);System.out.println(
"Error:" + (theCorrectBMI - computedBMI));System.out.println("------------");
}} Parameterles
s constructor Calling setter
Constructors with parameters make code more compact
![Page 33: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/33.jpg)
33
OBJECT VS. PRIMITIVE VARIABLE
public class ABMISpreadsheetTester {public void test (
double theHeight, double theWeight, double theCorrectBMI)
{BMISpreadsheet bmiSpreadsheet =
new ABMISpreadsheet(theHeight, theWeight);double computedBMI = bmiSpreadsheet.getBMI();System.out.println("------------");System.out.println("Height:" + theHeight);System.out.println("Weight:" + theWeight);System.out.println("Expected BMI:" + theCorrectBMI);System.out.println("Computed BMI:" + computedBMI);System.out.println(
"Error:" + (theCorrectBMI - computedBMI));System.out.println("------------");
}}
Primitive Variable
Object Variable
Primitive Variable
Object Variable
![Page 34: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/34.jpg)
34
UNINITIALIZED VARIABLES
public class ABMISpreadsheetTester {public void test (
double theHeight, double theWeight, double theCorrectBMI)
{BMISpreadsheet bmiSpreadsheet;double computedBMI ;System.out.println("------------");System.out.println("Height:" + theHeight);System.out.println("Weight:" + theWeight);System.out.println("Expected BMI:" + theCorrectBMI);System.out.println("Computed BMI:" + computedBMI);System.out.println(
"Error:" + (theCorrectBMI - computedBMI));System.out.println("------------");
}}
Uninitialized Primitive Variable
Uninitialized Object
Variable
![Page 35: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/35.jpg)
35
DEFAULT VALUES FOR VARIABLESPrimitive Variables
double computedBMI;double weight;
Object Variables
BMISpreadsheet bmiSpreadsheet;
variables memory
0.00.0
null
computedBMI;weight;
bmiSpreadsheet;
Legal double values
Illegal BMISpreadsheet
value
![Page 36: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/36.jpg)
36
INVOKING METHODS ON NULL bmiSpreadsheet.getBMI()
null pointer exception Exception is an unexpected event (error) Guilty method will be terminated and exception
reported Will see other exceptions later
![Page 37: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/37.jpg)
37
MATHEMATICAL POINT
X.
Y R
q.
![Page 38: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/38.jpg)
38
JAVA COORDINATE SYSTEM
Y
X(0,0)
(3,2)
pixels
X and Y coordinates must be int values
Radius and Angle can be double
Angle is a decimal value between 0 and 2
![Page 39: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/39.jpg)
39
POINT INTERFACE
public interface Point {public int getX(); public int getY(); public double getAngle(); public double
getRadius(); }
Read-only properties defining immutable
object!
![Page 40: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/40.jpg)
40
POINT REPRESENTATIONS X, Y (Cartesian Representation) Radius, Angle (Polar Representation) X, Radius X, Y, Radius, Angle …
![Page 41: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/41.jpg)
41
STUBS FOR ACARTESIANPOINTpublic class ACartesianPoint implements Point {
public ACartesianPoint(int theX, int theY) {}public int getX() {
return 0;}public int getY() {
return 0;} public double getAngle() {
return 0;}public double getRadius() {
return 0;}
}
![Page 42: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/42.jpg)
42
ACARTESIANPOINT TESTERpublic class ACartesianPointTester {
public void test (int theX, int theY, double theCorrectRadius, double theCorrectAngle)
{Point point= new ACartesianPoint (theX, theY);double computedRadius = point.getRadius();double computedAngle = point.getAngle();System.out.println("------------");System.out.println(“X:" + theX);System.out.println(“Y:" + theY);System.out.println("Expected Radius:" + theCorrectRadius);System.out.println("Computed Radius:" + computedRadius);System.out.println(“Radius Error:"
+ (theCorrectRadius - computedRadius));System.out.println("Expected Angle:" + theCorrectAngle);System.out.println("Computed Angle:" + computedAngle);System.out.println(“Angle Error:" +
(theCorrectAngle - computedAngle));System.out.println("------------");
}public void test () {
test (10, 0, 10.0, 0); // 0 degree angletest (0, 10, 10.0, Math.PI / 2); // 90 degree angle
}}
![Page 43: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/43.jpg)
43
ALGORITHMS
X.
Y R
q.
Cartesian Representation
R = sqrt (X2 * Y2)
q = arctan (Y/X)
Polar Representation
X = R*cos(q)
Y = R*sin(q)
![Page 44: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/44.jpg)
44
CLASS: ACARTESIANPOINTpublic class ACartesianPoint implements Point {
int x, y;public ACartesianPoint(int theX, int theY) {
x = theX;y = theY;
}public int getX() {
return x;}public int getY() {
return y;} public double getAngle() {
return Math.atan((double) y/x);}public double getRadius() {
return Math.sqrt(x*x + y*y);}
}
![Page 45: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/45.jpg)
45
CLASS: APOLARPOINTpublic class APolarPoint implements Point {
double radius, angle;public APolarPoint(double theRadius, double theAngle) {
radius = theRadius ;angle = theAngle;
}public int getX() {
return (int) (radius*Math.cos(angle));}public int getY() {
return (int) (radius*Math.sin(angle));}public double getAngle() {
return angle;} public double getRadius() {
return radius;}
}
![Page 46: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/46.jpg)
46
USING THE INTERFACE AND ITS IMPLEMENTATIONS
Point point1 = new ACartesianPoint (50, 50);
Point point2 = new APolarPoint (70.5, Math.pi()/4);
point1 = point2;
Constructor chooses implementation
Constructor cannot be in interface
![Page 47: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/47.jpg)
47
REPRESENTING GEOMETRIC OBJECTS Geometric example to show multiple useful
implementations of an interface Most geometric objects have multiple
representations
![Page 48: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/48.jpg)
48
USING OBJECTEDITOR
![Page 49: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/49.jpg)
49
USING OBJECTEDITOR
![Page 50: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/50.jpg)
50
JAVA GRAPHICS
Y
X
![Page 51: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/51.jpg)
51
ADDING TREE VIEW
![Page 52: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/52.jpg)
52
TREE VIEW
![Page 53: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/53.jpg)
53
REMOVING GRAPHICS VIEW
![Page 54: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/54.jpg)
54
TREE-ONLY VIEW
![Page 55: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/55.jpg)
55
OBJECTEDITOR GRAPHICS Can automatically display objects representing
points, rectangles, ovals, and lines as corresponding graphics Java provides libraries to manually display graphics
Has rules for recognizing these objects Rules based on Java graphics standards
Inverted coordinate system Cartesian coordinates for points Bounding rectangles for lines, rectangles, ovals
Plus naming conventions
![Page 56: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/56.jpg)
56
OBJECTEDITOR POINT RULES An object is recognized as a point
representation if: Its interface or class name has the string “Point” in
its name It has readable int properties, x and y, representing
Cartesian coordinates Can have additional properties
public interface Point {public int getX(); public int getY(); public double getAngle(); public double
getRadius(); }
![Page 57: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/57.jpg)
57
EQUIVALENT REPRESENTATIONS
StoredCompute
d
Computed
Stored
![Page 58: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/58.jpg)
58
REASON FOR ERROR
Calculated X and Y values are truncated when converted to int
Conversion back to Radius and Angle yields different values
public int getX() {return (int)
(radius*Math.cos(angle));}
![Page 59: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/59.jpg)
59
WHAT IS IN A REPRESENTATION? Representation determines
Space efficiency Time efficiency Precision of various properties
![Page 60: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/60.jpg)
60
COUNTING INSTANCES OF ACARTESIANPOINT
![Page 61: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/61.jpg)
61
INCREMENTED NUMBER OF INSTANCES
![Page 62: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/62.jpg)
62
CREATING MIDPOINT
![Page 63: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/63.jpg)
63
SPECIFYING TWO END POINTS
![Page 64: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/64.jpg)
64
TYPE OF METHODS Difference between method returning number of
instances and other methods seen so far? Difference between mid and other methods
seen so far?
![Page 65: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/65.jpg)
65
REAL-WORLD ANALOGY
new ACorvette(silver)
new ACorvette(red)
getMileage() 64000
blend(silverCar, redCar);
numCarsProduced();
101234
![Page 66: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/66.jpg)
66
O-O WORLD
ACartesianPoint
Point1
Point2new
ACartesianPoint( 25, 50)
new ACartesianPoint( 125,
150)getRadius()
59
mid(25,50, 125,150);mid(point1x,point1y,
point2x,point2y);
numInstances()
3
![Page 67: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/67.jpg)
67
CLASS METHODS Methods can be invoked on class itself Called class or static methods Declared in class on which they are invoked Keyword static in header Accesses no instance variable Header cannot appear in interface
![Page 68: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/68.jpg)
68
PROGRAMMER-DEFINED MID
public getRadius (){ return Math.sqrt(x*x + y*y);}
public static Point mid (int x1, int y1, int x2, int y2 ) { return new ACartesianPoint(x1 + (x2 - x1)/2, y1 + (y2 - y1)/2);}
Instance Method
Class Method
Access instance variables
Access no instance variable
![Page 69: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/69.jpg)
69
EXTERNAL CALL OF CLASS METHOD
ACartesianPoint.mid(x1, y1, x2, y2)
Math.sqrt(x*x + y*y);
Class as target
![Page 70: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/70.jpg)
70
NUMINSTANCES ALGORITHM
Declare variable, numInstances initialized to zero
Increment numInstances each time a new instance is created
getNumInstances() returns numInstances
![Page 71: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/71.jpg)
71
RETURNING NUMINSTANCES
public static int getNumInstances() {
return numInstances;}
numInstances() returns numInstances
Class property
![Page 72: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/72.jpg)
72
INCREMENTING NUMINSTANCES
Increment numInstances each time a new instance is created
![Page 73: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/73.jpg)
73
INCREMENTING NUMINSTANCES (EDIT)
Increment numInstances each time a new instance is created
![Page 74: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/74.jpg)
74
INCREMENTING NUMINSTANCES (SOLUTION)
public ACartesianPoint(int theX, int theY) {x = theX;y = theY;numInstances++;
}
Increment numInstances each time a new instance is created
![Page 75: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/75.jpg)
75
DECLARING NUMINSTANCES
static int numInstances = 0;
Declare variable, numInstances initialized to zero
// class variable
![Page 76: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/76.jpg)
76
TRACING EXECUTION
variables memory
00
numInstances;x;
public ACartesianPoint(int theX, int theY) {
x = theX;y = theY;numInstances++;
}
0y;2550
1
0x;0y;
125150
2
![Page 77: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/77.jpg)
77
INSTANCE VS. CLASS MEMBERS Class Members
Class method Class variables
Instance Members Instance methods Instance variables
![Page 78: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/78.jpg)
78
SCOPE OF CLASS AND INSTANCE MEMBERS
Class Members visible to other class
members visible to all instance
members class & instance
methods can access class variables
class and instance methods can call class methods
Instance Members visible to other
instance members not visible to class
members which of (zero to
many) copies of an instance variable should a class member refer to?
![Page 79: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/79.jpg)
79
LEGAL & ILLEGAL ACCESS
public static int getNumInstances() { System.out.println(x); return numInstances;}
public ACartesianPoint(int theX, int theY) { x = theX; y = theY; numInstances = numInstances + 1; System.out.println(getNumInstances());}
static int numInstances = 0;
int x, y;
public double getRadius() {return Math.sqrt(x*x + y*y);
}
![Page 80: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/80.jpg)
80
INSTANCE VS. CLASS NAMED CONSTANT
variables memory
2.2LBS_IN_KGS;
public class AnotherBMISpreadsheet implements BMISpreadsheet{
double height, weight, bmi;...public static final
double LBS_IN_KG = 2.2;public static final
double CMS_IN_INCH = 2.54;double calculateBMI() {
return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100);
}}
new AnotherBMISpreadsheet()new AnotherBMISpreadsheet()
AnotherBMISpreadsheet.LBS_IN_KGS;
![Page 81: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/81.jpg)
81
INSTANCE VS. CLASS NAMED CONSTANT
variables memory
2.2LBS_IN_KGS;
public class AnotherBMISpreadsheet implements BMISpreadsheet{
double height, weight, bmi;...final
double LBS_IN_KG = 2.2;public static final
double CMS_IN_INCH = 2.54;double calculateBMI() {
return (weight/LBS_IN_KG) / (height*CMS_IN_INCH/100*height*CMS_IN_INCH/100);
}}
new AnotherBMISpreadsheet()new AnotherBMISpreadsheet()
(new AnotherBMISpreadsheet()).LBS_IN_KGS;
public final
2.2LBS_IN_KGS;
![Page 82: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/82.jpg)
82
CLASS VS. INSTANCE CONSTANT Should be class constant
one copy easy to refer (require no instance creation)
Unless some good reason for hiding named constants from static methods
See inheritance-based exception later
![Page 83: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/83.jpg)
83
CLASS VS. INSTANCE METHODS
public static Point mid (int x1, int y1, int x2, int y2 ) { return new ACartesianPoint(x1 + (x2 - x1)/2, y1 + (y2 - y1)/2);}
public Point mid (int x1, int y1, int x2, int y2 ) { return new ACartesianPoint(x1 + (x2 - x1)/2, y1 + (y2 - y1)/2);}
ACartesianPoint.mid(25, 50, 125, 150)
Math.round(5.7)
(new ACartesianPoint(25, 50)).mid(25, 50, 125, 150)
(new Math()).round(5.7)
Class Method
Instance Method
Accesses no instance variable
![Page 84: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/84.jpg)
84
CLASS VS. INSTANCE METHOD Instance method has all the privileges of a class
method Any class method can be made an instance
method Bad style to have instance method that does not
access any instance variable They belong to the class Violate least privilege principle Require needless instantiation
![Page 85: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/85.jpg)
85
CLASS VS. INSTANCE CONSTANT/METHOD
Named constants should be static Unless some good reason for hiding named
constants from static methods See inheritance-based exception later
Methods not accessing instance variables should be static Unless need to be listed in interface See inheritance-based exception later
![Page 86: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/86.jpg)
86
MID WITH POINT ARGUMENTS
public static Point mid (int x1, int y1, int x2, int y2 ) { return new ACartesianPoint(x1 + (x2 - x1)/2, y1 + (y2 - y1)/2);}
public static Point mid (Point p1, Point p2 ) {
}
![Page 87: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/87.jpg)
87
MID WITH POINT ARGUMENTS (EDIT)
public static Point mid (int x1, int y1, int x2, int y2 ) { return new ACartesianPoint(x1 + (x2 - x1)/2, y1 + (y2 - y1)/2);}
public static Point mid (Point p1, Point p2 ) {
}
![Page 88: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/88.jpg)
88
MID WITH POINT ARGUMENTS
public static Point mid (int x1, int y1, int x2, int y2 ) { return new ACartesianPoint(x1 + (x2 - x1)/2, y1 + (y2 - y1)/2);}
public static Point mid (Point p1, Point p2 ) { return mid (p1.getX(), p1.getY(), p2.getX(), p2.getY());}
Polymorphic operation – one or more formal parameters can be assigned actual parameters of different types
Polymorphic vs. overloaded operations?
![Page 89: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/89.jpg)
89
OBJECT PARAMETERS
![Page 90: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/90.jpg)
90
OBJECT PARAMETERS
![Page 91: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/91.jpg)
91
OBJECT PARAMETERS
Cannot instantiate interface!
Java does not know all implementations of an interface!
![Page 92: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/92.jpg)
92
SPECIFYING PARAMETER CLASS
Edit interface name and hit return
Constructor of specified class
![Page 93: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/93.jpg)
93
SPECIFYING PARAMETER CLASS
Constructed object displayed and can
be edited
Constructed object becomes value of
first mid parameter
Object address
![Page 94: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/94.jpg)
94
SPECIFYING SECOND PARAMETER
Constructor of specified class with its parameters
![Page 95: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/95.jpg)
95
SECOND PARAMETER
Object address
![Page 96: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/96.jpg)
96
RESULT OF EXECUTING MID
Object returned by mid
![Page 97: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/97.jpg)
97
EQUIVALENT CODE
Point point1 = new ACartesianPoint (25, 50);
Point point2 = new APolarPoint (195.25, 087);
Point mid = mid (point1, point2);
![Page 98: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/98.jpg)
98
TYPING OBJECTS
ACartesianPoint APolarPoint
Point
![Page 99: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/99.jpg)
99
KINDS OF TYPEStypes
Primitive types
Object types
double int
ABMICalculator ABMISpreadsheet
AnotherBMISpreadsheet
BMISpreadsheet
Classes
Interfaces
type = set of operations
Point
![Page 100: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/100.jpg)
100
INTERFACES AS TYPES
public static Point mid (Point p1, Point p2 ) { return mid (p1.getX(), p1.getY(), p2.getX(), p2.getY());}
![Page 101: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/101.jpg)
101
CLASSES AS TYPES
public static Point mid (ACartesianPoint p1, Point p2 ) { return mid (p1.getX(), p1.getY(), p2.getX(), p2.getY());}
![Page 102: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/102.jpg)
102
TYPING AN OBJECT Use interface rather than class to type object
variables => Define interfaces for all classes that include
headers of all public method
![Page 103: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/103.jpg)
103
PROGRAMMER-DEFINED VS. PREDEFINED TYPES
Programmer-defined interface/class (Point/ACartesianPoint) is programmer-defined type
Programmer-defined types in Java must be object types
Some object types are predefined (String) All primitive types are predefined
![Page 104: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/104.jpg)
104
LINE?
![Page 105: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/105.jpg)
105
DESCRIBING LINES
Point
w2
h2
Boun
ding
Re
ctan
gle
x2, y2
w2
h2
![Page 106: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/106.jpg)
106
LINE INTERFACE
public interface Line {public int getX();public void setX(int newX);public int getY();public void setY(int newY);
public int getWidth();public void setWidth(int newVal);public int getHeight();public void setHeight(int newHeight);
}
![Page 107: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/107.jpg)
107
LINE IMPLEMENTATION
public class ALine implements Line{int x, y, width, height;public ALine (int initX, int initY, int initWidth, int initHeight) {
x = initX;y = initY;width = initWidth;height = initHeight;
}public int getX() {return x;}public void setX(int newX) {x = newX;}public int getY() {return y;}public void setY(int newY) {y = newY;}public int getWidth() {return width;}public void setWidth(int newVal) {width = newVal;}public int getHeight() {return height;}public void setHeight(int newHeight) {height = newHeight;}
}
![Page 108: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/108.jpg)
108
ANOTHERLINE INTERFACE
public interface AnotherLine {public Point getLocation();public void setLocation(Point newLocation);public int getWidth();public void setWidth(int newVal);public int getHeight();public void setHeight(int newHeight);
}
![Page 109: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/109.jpg)
109
ALTERNATIVE IMPLEMENTATION WITH TWO DIFFERENT KINDS OF PROPERTIES
public class AnAnotherLine implements AnotherLine {int width, height;Point location;public AnAnotherLine (Point initLocation, int initWidth, int initHeight)
{location = initLocation;width = initWidth;height = initHeight;
}public Point getLocation() {return location;}public void setLocation(Point newVal) {location = newVal;}public int getWidth() {return width;}public void setWidth(int newVal) {width = newVal;}public int getHeight() {return height;}public void setHeight(int newHeight) {height = newHeight;}
}
![Page 110: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/110.jpg)
110
PREDEFINED, PRIMITIVE VS. PROGRAMMER-DEFINED, OBJECT
Point
int
Programmer-defined Object
Predefined Primitive
![Page 111: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/111.jpg)
111
PROGRAMMER-DEFINED VS. PREDEFINED TYPES
Programmer-defined interface/class (Point/ACartesianPoint) is programmer-defined type
Programmer-defined types in Java must be object types
Some object types are predefined (String) All primitive types are predefined
![Page 112: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/112.jpg)
112
STRUCTURED VS. ATOMIC PROPERTIES
Angle
Radius
…
![Page 113: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/113.jpg)
113
STRUCTURE VS. ATOMIC TYPEStypes
Primitive types
Structured Types
double int
ABMICalculator ABMISpreadsheet
AnotherBMISpreadsheet
BMISpreadsheet
Classes
Interfaces
Instances of structure type decomposed into one or more smaller values
Point
![Page 114: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/114.jpg)
114
DECOMPOSING ANANOTHERLINE INSTANCE
public class AnAnotherLine implements AnotherLine {int width, height;Point location;public AnAnotherLine (Point intLocation, int initWidth, int
initHeight) {location = initLocation;width = initWidth;height = initHeight;
}public Point getLocation() {return location;}public void setLocation(Point newVal) {location = newVal;}public int getWidth() {return width;}public void setWidth(int newVal) {width = newVal;}public int getHeight() {return height;}public void setHeight(int newHeight) {height = newHeight;}
}
new AnAnotherLine(new ACartesianPoint (10, 10), 20, 20)
![Page 115: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/115.jpg)
115
PHYSICAL STRUCTURE
new AnAnotherLine(new ACartesianPoint (10, 10), 20, 20)
AnAnotherLine
ACartesianPoint
int
int
location
height
width
int
xy
intVariable name
Class or primitive type
of value stored in variable
![Page 116: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/116.jpg)
116
LOGICAL STRUCTURE
new AnAnotherLine(new ACartesianPoint (10, 10), 20, 20)
AnAnotherLine
Point
int
int
Location
Height
Width
intX
Y
int Property name
Class or primitive type
of property value
doubleAngle
double
Radius
![Page 117: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/117.jpg)
117
STRUCTURE SHOWN BY OBJECTEDITOR
Angle
Radius
…
ObjectEditor cannot see instance variables
![Page 118: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/118.jpg)
118
STRUCTURE SHOWN BY DEBUGGER
![Page 119: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/119.jpg)
119
TWO WAYS TO DECOMPOSE OBJECTS Physical Decomposition
components of objects are its instance variables x, y, location, width, size
the component can be further decomposed location into x, y
difference instances of same class may have different physical structure
Logical Decomposition components of object are its properties
Location, X, Y, Radius, Angle, Width, Height each of these can be further decomposed
Location can be decomposed into X, Y, Angle, Radius all instances of class have same logical structure class properties not considered part of instance
![Page 120: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/120.jpg)
120
DRAWING THE PHYSICAL STRUCTURE OF A VALUE OF SOME TYPE
Start with the name of the type of value type can be class or primitive type cannot be interface because no associated physical
representation For each instance variable of the value
draw an edge end point of edge is primitive type/class of value
assigned to variable if value is non-null and null otherwise
label of edge is variable name Follow the same algorithm to physically decompose
value of each instance variable Stop at atomic type or a type already drawn in the
figure
![Page 121: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/121.jpg)
121
DRAWING THE LOGICAL STRUCTURE OF A VALUE OF SOME TYPE
Start with the name of the class/primitive type of value
For each property defined by the type draw an edge end point of edge is primitive type/class of
property value if not null and null otherwise label of edge is property name
Follow the same algorithm to logically decompose value of each property
Stop at atomic type or a type already drawn in the figure
![Page 122: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/122.jpg)
122
INSTANCE-SPECIFIC STRUCTURE
new AnAnotherLine(new APolarPoint (14.01, 0.78), 20, 20)
AnAnotherLine
APolarPoint
int
int
location
height
width
doubleangle
radius
double
Structures of instances of same class can be different!
![Page 123: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/123.jpg)
123
INSTANCE-SPECIFIC STRUCTURE
AnAnotherLine
ACartesianPoint
int
int
location
height
width
int
xy
int
new AnAnotherLine(new ACartesianPoint (10, 10), 20, 20)
![Page 124: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/124.jpg)
124
ATOMIC VS. STRUCTURE TYPES All primitive types are atomic Object types with >= 1 properties are logically
structured Object types with >= 1 instance variables are
physically structured Some object types may be physically or logically
atomic
![Page 125: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/125.jpg)
125
OBJECTEDITOR SHAPE RULES
![Page 126: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/126.jpg)
126
OBJECTEDITOR SHAPE RULES
X, Y
X, Y
X, Y
WidthHeight
Width
Height
Width
Height
helloX, Y
WidthHeight
Rectangle
Line
Oval TextBox
EiffelX, Y
Width
Height
Label
![Page 127: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/127.jpg)
127
OBJECTEDITOR SHAPE RULES
Location
Location
Location
WidthHeight
Width
Height
Width
Height
helloLocation
WidthHeight
Rectangle
Line
Oval TextBox
Eiffel
Location
Width
Height
Label
The type of Location is any type following point rules given earlier
![Page 128: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/128.jpg)
128
OBJECTEDITOR BOUNDING BOX RULES An object describes the bounding box of a shape
if it:represents the size of the bounding box using
int (read-only or editable) properties, Height, and Width
describes the location of the upper left corner of the bounding box using either: X, Y properties of type int A Location (read-only or editable) property whose
type follows the ObjectEditor rules for a Point
![Page 129: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/129.jpg)
129
OBJECTEDITOR SHAPE RULES An object is recognized as a rectangle/line/oval
if: Its interface or class has the string
“Rectangle”/”Oval”/”Line” in its name It has properties describing the bounding box of the
shape Can have additional properties
public interface Line { public int getX(); public void setX(int newVal); public int getY(); public void setY(int newVal); public int getWidth(); public void setWidth(int newVal); public int getHeight() ; public void setHeight( int newVal);}
public interface AnotherLine {
public Point getLocation(); public void setLocation( Point newVal); public int getWidth(); public void setWidth(int newVal); public int getHeight() ; public void setHeight(int newVal);}
![Page 130: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/130.jpg)
130
OBJECTEDITOR TEXTBOX RULES An object is recognized as a textbox if:
Its interface or class has the string “Text” in its name It has properties described the bounding box of the text-box It has String property Text representing contents of the text-
box Can have additional properties
public interface TextBox { public int getX(); public void setX(int newVal); public int getY(); public void setY(int newVal); public int getWidth(); public void setWidth(int newVal); public int getHeight() ; public void setHeight(int newVal); public String getText(); public void setText(String newVal);}
![Page 131: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/131.jpg)
131
OBJECTEDITOR LABEL RULES An object is recognized as a label if:
Its interface or class has the string “Label” or “Icon” in its name
It has properties described the bounding box of the label. It either has String property
Text giving the label text. ImageFileName describing the name of a graphics file
describing the label icon. Can have additional propertiespublic interface Label {
public Point getLocation(); public void setLocation(Point newVal); public int getWidth(); public void setWidth(int newVal); public int getHeight() ; public void setHeight(int newHeight); public String getText(); public void setText(String newString); public String getImageFileName(); public void setImageFileName( String newVal);}
public interface AnotherLabel { public Point getLocation(); public void setLocation(Point newVal); public int getWidth(); public void setWidth(int newVal); public int getHeight() ; public void setHeight(int newVal); public String getImageFileName(); public void setImageFileName( String newVal);}
![Page 132: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/132.jpg)
132
LABEL IMPLEMENTATIONpublic class ALabel implements Label { int width, height; String text, imageFile; Point location; public ALabel ( int initX, int initY, int initWidth, int initHeight, String initText, String theImageFile) { location = new ACartesianPoint(initX, initY); width = initWidth; height = initHeight; text = initText; imageFile = theImageFile; } public Point getLocation() {return location;} public void setLocation(Point newVal) {location = newVal;} public int getWidth() { return width;} public void setWidth(int newVal) {width = newVal;} public int getHeight() {return height;} public void setHeight(int newHeight) {height = newHeight;} public String getText() {return text;} public void setText(String newVal) {text = newVal;} public String getImageFileName() {return imageFile;} public void setImageFileName(String newVal) {imageFile = newVal;}
}
![Page 133: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/133.jpg)
133
USING ALABEL
The label icon and image are offset 0, height/2 from upper left corner
Text follows the label in a left-to-right order.
![Page 134: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/134.jpg)
134
CREATING OTHER GEOMETRIC OBJECTS Compose the existing geometric objects
Point, Line, Rectangle, Oval, TextBox, Icon Any graphical image can be created from
triangles!
![Page 135: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/135.jpg)
135
ACARTESIANPLANE
![Page 136: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/136.jpg)
136
ACARTESIANPLANE
![Page 137: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/137.jpg)
137
ACARTESIANPLANE PROPERTIES
Axes Length (int)
![Page 138: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/138.jpg)
138
ACARTESIANPLANE PROPERTIES (EDIT)
Axes Length (int)
![Page 139: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/139.jpg)
139
ACARTESIANPLANE PROPERTIES
Axes Length (int)
XAxis (Line)
YAxis (Line)
XLabel (Label)
YLabel (Label)
![Page 140: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/140.jpg)
140
ACARTESIANPLANE IMPLEMENTATIONpublic class ACartesianPlane implements CartesianPlane { int axesLength ; Line xAxis; Line yAxis; Label xLabel; Label yLabel; final int LABEL_BOUNDS_SIDE_LENGTH = 10; public ACartesianPlane (int theAxesLength, int theOriginX, int theOriginY ) { } public Line getXAxis() {return xAxis;} public Line getYAxis() {return yAxis;} public int getAxesLength() {return axesLength;} public void setAxesLength(int newVal) { } public Label getXLabel() {return xLabel;} public Label getYLabel() {return yLabel;}
}
![Page 141: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/141.jpg)
141
ACARTESIANPLANE IMPLEMENTATION (EDIT)
public class ACartesianPlane implements CartesianPlane { int axesLength ; Line xAxis; Line yAxis; Label xLabel; Label yLabel; final int LABEL_BOUNDS_SIDE_LENGTH = 10; public ACartesianPlane (int theAxesLength, int theOriginX, int theOriginY ) { } public Line getXAxis() {return xAxis;} public Line getYAxis() {return yAxis;} public int getAxesLength() {return axesLength;} public void setAxesLength(int newVal) { } public Label getXLabel() {return xLabel;} public Label getYLabel() {return yLabel;}
}
![Page 142: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/142.jpg)
142
ACARTESIANPLANE IMPLEMENTATIONpublic class ACartesianPlane implements CartesianPlane { int axesLength ; Line xAxis, yAxis; Label xLabel, yLabel; final int LABEL_BOUNDS_SIDE_LENGTH = 10; public ACartesianPlane (int theAxesLength, int theOriginX, int theOriginY ) { axesLength = theAxesLength; xAxis = new ALine( theOriginX - theAxesLength/2, theOriginY, theAxesLength, 0); yAxis = new ALine( theOriginX, theOriginY - theAxesLength/2, 0, theAxesLength); xLabel = new ALabel( theOriginX + theAxesLength/2, theOriginY - LABEL_BOUNDS_SIDE_LENGTH/2, LABEL_BOUNDS_SIDE_LENGTH, LABEL_BOUNDS_SIDE_LENGTH, "X", null); yLabel = new ALabel( theOriginX, theOriginY - theAxesLength/2 - LABEL_BOUNDS_SIDE_LENGTH, LABEL_BOUNDS_SIDE_LENGTH, LABEL_BOUNDS_SIDE_LENGTH, "Y", null); }
![Page 143: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/143.jpg)
143
ACARTESIANPLANE IMPLEMENTATION public Line getXAxis() {return xAxis;} public Line getYAxis() {return yAxis;} public int getAxesLength() {return axesLength;}
public void setAxesLength(int newVal) { int lengthIncrease = newVal - axesLength; xAxis.setX(xAxis.getX() - lengthIncrease/2); yAxis.setY(yAxis.getY() - lengthIncrease/2); xLabel.setLocation(new ACartesianPoint( xLabel.getLocation().getX() + lengthIncrease/2, xLabel.getLocation().getY())); yLabel.setLocation(new ACartesianPoint( yLabel.getLocation().getX(), yLabel.getLocation().getY() - lengthIncrease/2)); axesLength = newVal; xAxis.setWidth(axesLength); yAxis.setHeight(axesLength); }
public Label getXLabel() {return xLabel;} public Label getYLabel() {return yLabel;}}
Cannot change instance of Point since Point is immutable!
We must create new instances of Point
instead.
![Page 144: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/144.jpg)
144
ACARTESIANPLANE IMPLEMENTATION public Line getXAxis() {return xAxis;} public Line getYAxis() {return yAxis;} public int getAxesLength() {return axesLength;}
public void setAxesLength(int newVal) { int lengthIncrease = newVal - axesLength; xAxis.setX(xAxis.getX() - lengthIncrease/2); yAxis.setY(yAxis.getY() - lengthIncrease/2); xLabel.setLocation(new ACartesianPoint( xLabel.getLocation().getX() + lengthIncrease/2, xLabel.getLocation().getY())); yLabel.setLocation(new ACartesianPoint( yLabel.getLocation().getX(), yLabel.getLocation().getY() - lengthIncrease/2)); axesLength = newVal; xAxis.setWidth(axesLength); yAxis.setHeight(axesLength); }
public Label getXLabel() {return xLabel;} public Label getYLabel() {return yLabel;}}
Can we refactor the
code?
![Page 145: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/145.jpg)
145
REFACTORING LABEL CODE public Line getXAxis() {return xAxis;} public Line getYAxis() {return yAxis;} public int getAxesLength() {return axesLength;}
public void setAxesLength(int newVal) { int lengthIncrease = newVal - axesLength; xAxis.setX(xAxis.getX() - lengthIncrease/2); yAxis.setY(yAxis.getY() - lengthIncrease/2); updateXOfLabel(xLabel, xLabel.getLocation().getX() + lengthIncrease/2); updateYOfLabel(yLabel, yLabel.getLocation().getY() - lengthIncrease/2); axesLength = newVal; xAxis.setWidth(axesLength); yAxis.setHeight(axesLength); }
public Label getXLabel() {return xLabel;} public Label getYLabel() {return yLabel;}}
![Page 146: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/146.jpg)
146
REFACTORING LABEL CODE (EDIT) public Line getXAxis() {return xAxis;} public Line getYAxis() {return yAxis;} public int getAxesLength() {return axesLength;}
public void setAxesLength(int newVal) { … updateXOfLabel(xLabel, xLabel.getLocation().getX() + lengthIncrease/2); updateYOfLabel(yLabel, yLabel.getLocation().getY() - lengthIncrease/2); … }
// v1 /*private Label updateXofLabel(Label label, int newX) { Label newLabel = new ALabel( newX, label.getLocation().getY(), label.getWidth(), label.getHeight(), label.getText(), label.getImageFileName()); return newLabel; }*/
//v2 private static void updateXofLabel(Label label, int newX) { label.setLocation(new ACartesianPoint(newX, label.getLocation().getY())); }
public Label getXLabel() {return xLabel;} public Label getYLabel() {return yLabel;}}
![Page 147: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/147.jpg)
147
REFACTORING LABEL CODE …
public void setAxesLength(int newVal) { … updateXOfLabel(xLabel, xLabel.getLocation().getX() + lengthIncrease/2); updateYOfLabel(yLabel, yLabel.getLocation().getY() - lengthIncrease/2); … }
public static void updateXOfLabel(Label label, int newX) { label.setLocation(new ACartesianPoint( newX, label.getLocation().getY()); }
public static void updateYOfLabel(Label label, int newY) { label.setLocation(new ACartesianPoint( label.getLocation().getX(), newY)); }
…}
New methods do not interact with instance variables
So make them class methods!
![Page 148: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/148.jpg)
148
![Page 149: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/149.jpg)
149
ACARTESIANPLANE
![Page 150: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/150.jpg)
150
SHUTTLE LOCATION
![Page 151: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/151.jpg)
151
SHUTTLE LOCATION
![Page 152: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/152.jpg)
152
SHUTTLE LOCATION
![Page 153: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/153.jpg)
153
SHUTTLELOCATION PROPERTIES
ShuttleX (int)
![Page 154: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/154.jpg)
154
SHUTTLELOCATION PROPERTIES (EDIT)
ShuttleX (int)
![Page 155: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/155.jpg)
155
SHUTTLELOCATION PROPERTIES (EDIT)
ShuttleX (int)
ShuttleY (int)
ShuttleLabel (Label)
CartesianPlane (CartesianPlane)
![Page 156: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/156.jpg)
156
ASHUTTLELOCATION IMPLEMENTATIONpublic class AShuttleLocation implements ShuttleLocation {
public final int SHUTTLE_WIDTH = 80; public final int SHUTTLE_HEIGHT = 30; public final int CP_ORIGIN_X = 200; public final int CP_ORIGIN_Y = 200; public final int CP_INIT_AXES_LENGTH = 300; public final String SHUTTLE_IMAGE = “shuttle2.jpg”; public final String SHUTTLE_TEXT = “”; int shuttleX, shuttleY; CartesianPlane plane = new ACartesianPlane(CP_ORIGIN_X, CP_ORIGIN_Y, CP_INIT_AXES_LENGTH); Label shuttleLabel = new ALabel(getShuttleX(), getShuttleY(), SHUTTLE_WIDHT, SHUTLTE_HEIGHT, SHUTTLE_TEXT, SHUTTLE_IMAGE);
private int getShuttleX(){ return CP_ORIGIN_X + shuttleX; } private int getShuttleY() { return CP _ORIGIN_Y– shuttleY – SHUTTLE_HEIGHT; }
![Page 157: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/157.jpg)
157
ASHUTTLELOCATION IMPLEMENTATION (EDIT)
public CartesianPlane cartesianPlane() { return plane; } public Label shuttleLabel() { return shuttleLabel; } public void setShuttleX(int newShuttleX) { shuttleX = newShuttleX; shuttleX.setLocation(new ACartesianPoint(getShuttleX(), shuttleLabel.getLocation().getY()); } public int getShuttleX() { return shuttleX; } public void setShuttleY(int newShuttleY) { shuttleY = newShuttleY; shuttleLabel.setLocation(new ACartesinaPoint(shuttleLabel.getLocation().getX(), getSHuttleY()); } public int getShuttleY() { return shuttleY; }}
![Page 158: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/158.jpg)
158
ASHUTTLELOCATION IMPLEMENTATIONpublic class AShuttleLocation implements ShuttleLocation {
static final int ORIGIN_X = 200, ORIGIN_Y = 200; static final int AXES_LENGTH = 300; static final String SHUTTLE_IMAGE = "shuttle2.jpg"; static final String SHUTTLE_TEXT = ""; static final int SHUTTLE_WIDTH = 80; static final int SHUTTLE_HEIGHT = 25; int shuttleX = 0, shuttleY = 0; ACartesianPlane cartesianPlane = new ACartesianPlane ( AXES_LENGTH, ORIGIN_X, ORIGIN_Y); ALabel shuttleLabel = new ALabel( labelX(), labelY(), SHUTTLE_WIDTH, SHUTTLE_HEIGHT, SHUTTLE_TEXT, SHUTTLE_IMAGE);
![Page 159: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/159.jpg)
159
ASHUTTLELOCATION IMPLEMENTATION public ACartesianPlane getCartesianPlane() {return cartesianPlane;} public ALabel getShuttleLabel() {return shuttleLabel;} public int getShuttleX() {return shuttleX;} public void setShuttleX(int newVal) { shuttleX = newVal; shuttleLabel.setLocation(new ACartesianPoint(labelX(), labelY())); } public int getShuttleY() {return shuttleY;} public void setShuttleY(int newVal) { shuttleY = newVal; shuttleLabel.setLocation(new ACartesianPoint(labelX(), labelY())); } int labelX() {return ORIGIN_X + shuttleX;} int labelY() {return ORIGIN_Y - shuttleY - SHUTTLE_HEIGHT;}}
![Page 160: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/160.jpg)
160
ALTERNATE VIEWS OF LOGICAL STRUCTURE
Complete Logical
Structure
Graphical Properties
Logical Structure Without Graphical
Properties
![Page 161: Comp 110 Representation](https://reader036.vdocument.in/reader036/viewer/2022062323/56816419550346895dd5ceb2/html5/thumbnails/161.jpg)
161
LINES OF COMPOSITION
Lines can be composed into
triangles
Triangles can be composed to create arbitrary computer
graphics