- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPointAndRectangle.java
More file actions
Latest commit
143 lines (128 loc) · 4.56 KB
/
Copy pathPointAndRectangle.java
File metadata and controls
143 lines (128 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Chapter 10.1
*/
importjava.awt.Point;
importjava.awt.Rectangle;
importjava.util.Scanner;
publicclassPointAndRectangle
{
publicstaticvoidmain(String[] args)
{
Pointcordinates = newPoint();
//cordinates = cordinates.Point(3,2);
cordinates = newPoint(3, 4);
Pointblank;
blank = newPoint(7, 7);
printPoint(cordinates);
System.out.println(cordinates);
doublepointsDist = distance(cordinates, blank);
System.out.println(pointsDist);
Rectanglebox = newRectangle(0, 0, 100, 100);
System.out.println(box);
System.out.println(findCenter(box));
Rectangleboxy = newRectangle(0, 0, 100, 200);
moveRect(boxy, 50, 100);
boxy.translate(50, 100);
System.out.println(boxy);
//box1 and box2 are "aliases", they refer to the same object so they're
//the exactly the same, the Rectangle object has 2 names basically
//I assume this reinforces the idea that variables values are references
Rectanglebox1 = newRectangle(1, 2, 3, 4);
Rectanglebox2 = box1;
box1.grow(6, 8);
System.out.println("Box1=" + box1 + " & Box2=" + box2);
//blanky will be destroyed completely in memory
Pointblanky = newPoint(3, 4);
blanky = null;
Strings1 = "Hi, Mom!";
Strings2 = "Hi, " + "Mom!";
if(s1 == s2)
System.out.println("s1 and s2 are the same");
//REMEMBER String returns a completely new String and it must be saved
//in order for it to be remembered
//s1.concat(" Coolio!");
s1 += " Coolio!";
System.out.println(s1);
System.out.println(s2);
Scannerin = newScanner(System.in);
Stringtext = "";
for(inti = 0; i < 10; i++)
{
Stringline = in.nextLine();
text = text + line + '\n';
//text = text + line;
//System.out.print(text);
}
//It's supposed to return the last created String and delete all the prior
//created objects but it returns nothing. BUT I think the newLine command
//is whats returned cause ?it creates the start of a new String????^
/*
* I BELIEVE this is right it 1st creates a String object for line then it
* creates a String object for text that saves all prior inputs BUT the
* newLine character also creates a new String by going to the next Line
* AND this ultimately causes text to equal the new empty line and not the
* prior Strings? So this would cause 3 objects to be made every iteration
*/
System.out.println(text);
//I assume this is just showing how the below only creates ?1 object with
//mutable objects vs the above that does 30
//ALSO, I think '\n' is just an annoying add on that makes new empty
//objects? and the below shows that it doesn't for mutable objects
StringBuildertexts = newStringBuilder();
for(inti = 0; i < 10; i++)
{
Stringline = in.nextLine();
texts.append(line);
texts.append('\n');
//System.out.println(texts);
}
System.out.println(texts);
}
publicstaticvoidprintPoint(Pointp)
{
System.out.println("x=" + p.x + " & y=" + p.y);
}
//originally had return type int and changed to ensure all #'s work and no
//rounding
//I BELIEVE Math. methods return double and doubles can't => ints so cast
publicstaticdoubledistance(Pointone, Pointtwo)
{
Pointsubtraction = newPoint((two.x - one.x), (two.y - one.y));
// sum below goes from double->int BUT int squareX won't WHY? (I changed it)
// int sum = (subtraction.x + subtraction.y);
doublesquareX = Math.pow(subtraction.x, 2);
doublesquareY = Math.pow(subtraction.y, 2);
// Wrong you want to call the class Math in which the method sqrt
// lies! BUT WHEN do you put the variable in front and in the parenthesis?
// You invoke a method on that variable
// result.sqrt();
// Point is not a primitive type
doublesum = (squareX + squareY);
// int result = Math.sqrt(sum);
returnMath.sqrt(sum);
// System.out.println(result);
// return result;
}
publicstaticPointfindCenter(Rectanglez)
{
/*
* //Why is Point an int here but in distance it's a double?
* Point center = new Point();
* center = new Point((int)Math.sqrt(z.width), (int)Math.sqrt(z.height));
* //int w = (int)Math.sqrt(z.width);
* //int h = (int)Math.sqrt(z.height);
* //center = new Point(w, h);
* center = new Point(center.x, center.y);
* return center;
*/
intx = z.x + z.width/2;
inty = z.x + z.height/2;
returnnewPoint(x, y);
}
publicstaticvoidmoveRect(Rectanglemoved, intdx, intdy)
{
moved.x = moved.x + dx;
moved.y = moved.y + dy;
System.out.println(moved);
}
}