Skip to content

Commit 41382f8

Browse files
authored
Added and Updated
1 parent dd5e71e commit 41382f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3857
-0
lines changed

Diff for: Actor.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.awt.Graphics;
2+
3+
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!?????????????????????????????????????????????
4+
//IDEA:I believe the purpose of an interface is a way for multiple
5+
//objects(DP & {?Sprite or Game}?) in a code to invoke an interface(Actor)
6+
//for the purpose of identifying them as a new object that will allow them
7+
//to use a common code that they both require! {for the means of having less
8+
//code in your program} I think this package has only 2 objects that need
9+
//to be manipulated which is ?DP and Sprite?; the other objects can be/are
10+
//these objects already(?because they are subclasses?). Both objects have
11+
//different draw() and step() which is why Actor requires both common
12+
//objects to have and implement them. Identifying both of them as a common
13+
//object(Actor) allows them to use Drawing, which uses that common object as
14+
//a "type" in the class so the only object type to be used in parameters or
15+
//ArrayLists is that very common object type(Actor)!!!!!! ??Drawing has it's
16+
//own step method which then calls step() again BUT, ?I think it invokes the
17+
//step method that each object provided in Actor... so because dp is an Actor
18+
//type/object it doesn't use recursion it actually ?knows that the objects(dp)
19+
//is an interface? so it invokes the step method in Actor which then invokes
20+
//the proper objects step()? If this is true then ya BUT why is recursion not
21+
//the immediate ?thing to do?????? {Go through the section again and see how
22+
//dp.step() is used throughout because I think it changes!!!!} DRAWING is the
23+
//common code and ?it is what makes a blank Canvas for every class and allows
24+
//them to manipulate multiple objects(becasue of ArrayList) from each of the
25+
//2 main objects!!????
26+
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!?????????????????????????????????????????????
27+
//??With this idea in mind; WHY did he make us put the DP main method in
28+
//Drawing? rather than us invoking Drawing from DP??
29+
//IDEA:I think it was just a weird situation translate() & other methods
30+
//required a extends Canvas(which I'm pretty sure he explains as how to deal
31+
//with an object that needs 2 superclasses) so... ????????????????????
32+
//public class Actor
33+
public interface Actor
34+
{
35+
//(51)BOOK:Define a new superclass, which we call Actor, that
36+
//provides the two methods needed by Drawing:
37+
//???Drawing doesn't need any methods so how the heck would I
38+
//know what to provide?????????????????
39+
/*public void draw(Graphics g)
40+
{
41+
//do nothing
42+
}*/
43+
//??Why can't I put a breakpoint here to see when this method
44+
//is implemented or invoked??????????????????????????????????
45+
void draw(Graphics g);
46+
47+
/*public void step()
48+
{
49+
//do nothing
50+
}*/
51+
void step();
52+
53+
//Seems as though the 2nd one works here but not the 1st!!??
54+
//Polygon p1 = new DrawablePolygon();
55+
Actor a2 = new DrawablePolygon();
56+
}

Diff for: ArrayPractice.java

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Chapter 7.3
3+
*/
4+
import java.util.Arrays;
5+
import java.util.Random;
6+
7+
public class ArrayPractice
8+
{
9+
public static void main(String[] args)
10+
{
11+
int[] array = {1,2,3,4};
12+
printArray(array);
13+
14+
int[] g = printArrayReverse(array);
15+
System.out.println(Arrays.toString(g));
16+
17+
//Random nums = new Random(30);
18+
int[] scores = randomNumbers(30);
19+
System.out.println(Arrays.toString(scores));
20+
//int a = scores(scores, 90, 100);
21+
//System.out.println(a);
22+
23+
int[] range = new int[10];
24+
for(int i = 0; i < range.length; i++)
25+
{
26+
if(i == 0)
27+
range[i] = inRange(scores, i, i+10);
28+
else if (i == 9)
29+
range[i] = inRange(scores, i*10, i*10+11);
30+
else
31+
range[i] = inRange(scores, i*10, i*10+10);
32+
}
33+
System.out.println(Arrays.toString(range));
34+
35+
/*How do the above more efficiently
36+
int[] counts = new int[100];
37+
for(int i = 0; i < scores.length; i++)
38+
{
39+
int index = scores[i];
40+
counts[index]++;
41+
}
42+
System.out.println(Arrays.toString(counts));*/
43+
44+
//try to efficiently go through the scores array
45+
//and organize it by grades
46+
int[] counts = new int[10];
47+
for(int i = 0; i < scores.length; i++)
48+
{
49+
int index = 0;
50+
if (scores[i] == 100)
51+
counts[counts.length-1]++;
52+
else
53+
{
54+
//Int Division(/) doesn't include decimals
55+
//and always rounds the decimals down
56+
index = ((scores[i] % 100)/10);
57+
counts[index]++;
58+
}
59+
}
60+
System.out.println(Arrays.toString(counts));
61+
62+
//Enhanced for Loop
63+
int[] countsMore = new int[101];
64+
for (int score : scores)
65+
countsMore[score]++;
66+
System.out.println(Arrays.toString(countsMore));
67+
68+
int[] countAgain = new int[10];
69+
for(int assign : scores)
70+
{
71+
//assign replaces writing scores[i]
72+
if (assign == 100)
73+
countAgain[countAgain.length-1]++;
74+
else
75+
countAgain[((assign%100)/10)]++;
76+
}
77+
System.out.println(Arrays.toString(countAgain));
78+
}
79+
80+
public static String printArray(int[] a)
81+
{
82+
System.out.print("{");
83+
for(int i = 0; i < a.length; i++)
84+
{
85+
if(i == 0)
86+
System.out.print(a[0]);
87+
else
88+
System.out.print(", " + a[i]);
89+
//For a[4] why does an OutOfBounds err not occur?
90+
//Also why is there not another comma after 4?
91+
//YOUR DUMB! Do it!
92+
}
93+
System.out.println("}");
94+
return "}";
95+
//Why does it not return }?
96+
//RETURN prints nothing
97+
}
98+
99+
//To copy an array from one to another you do it this way
100+
//int[] c = Arrays.copyOf(b, b.length); does it too
101+
public static int[] printArrayReverse(int[] b)
102+
{
103+
int[] c = new int[b.length];
104+
//int j = 0;
105+
for(int i = b.length-1; i >= 0; i--)
106+
{
107+
for (int j = 0; j <= b.length-c.length; j++)
108+
{
109+
c[j] = b[i];
110+
System.out.print(c[j]);
111+
//break;
112+
//j++;
113+
}
114+
}
115+
//System.out.println();
116+
System.out.println(Arrays.toString(c));
117+
return c;
118+
}
119+
120+
public static int[] randomNumbers(int size)
121+
{
122+
Random numbers = new Random();
123+
int[] rando = new int[size];
124+
for(int i = 0;i < rando.length; i++)
125+
rando[i] = numbers.nextInt(101);
126+
return rando;
127+
}
128+
129+
public static int inRange(int[] scores, int low, int high)
130+
{
131+
int count = 0;
132+
for(int i = 0; i < scores.length; i++)
133+
{
134+
if(scores[i] < high && scores[i] >= low)
135+
count++;
136+
}
137+
return count;
138+
}
139+
}

Diff for: Automaton.java

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Chapter 16.2
3+
*/
4+
import javax.swing.JFrame;
5+
6+
//??????(10)WHY A SUPERCLASS if it only does so little???????????????
7+
//IDEA:I believe it's to show how refactoring and abstract works!!! We
8+
//wanted to remove repeated code by refactoring it. Then in order for
9+
//our repeated code to work it required the mainloop() to be present
10+
//and by adding this, it needed a method(update) from each class so we
11+
//used abstract to access the update() from each class. ?IN ORDER to
12+
//abstract something from another class(subclass) we ?need to make the
13+
//superclass abstract? so I think it was easier to show and implement
14+
//it here in this way!!?????
15+
//??If yes then why not abstract the mainloop???? I feel like adding it
16+
//here is only for organization because I think it would be faster to
17+
//leave them where they are as is and here it's just more computations
18+
//and also at putting more at risk??????????????
19+
//???Why not just inherit because this way for this problem seems dumb!??
20+
public abstract class Automaton
21+
{
22+
/*
23+
* BOOK:There are a few problems with the current design
24+
*
25+
* 1. The grid attribute is private, making it inaccessible in Conway
26+
* and Langton. We could make it public, but then other (unrelated)
27+
* classes would have access to it as well.
28+
*
29+
* 2. The Automaton class has no constructors, and even if it did, there
30+
* would be no reason to create an instance of this class.
31+
*
32+
* 3. The Automaton class does not provide an implementation of update.
33+
* In order to work properly, subclasses need to provide one.
34+
*
35+
* Java provides language features to solve these problems:
36+
*
37+
* 1. We can make the grid attribute protected, which means it's accessible
38+
* to subclasses but not other classes.
39+
*
40+
* 2. We can make the class abstract, which means it cannot be instantiated.
41+
* If you attempt to create an object for an abstract class, you will get
42+
* a compiler error.
43+
*
44+
* 3. We can declare update as an abstract method, meaning that it must be
45+
* overridden in subclasses. If the subclass does not override an abstract
46+
* method, you will get a compiler error.
47+
*/
48+
//??#2.1 To have/be an instance do you need a constructor??????????
49+
//??#1.1&1.2 By making it public other classes would have access to it too
50+
//but our solution is to just allow only subclasses access SOOOOO..... what's
51+
//stopping someone from just extending this super as well and boom now it's a
52+
//subclass of this class? OR just make a subclass to the subclass????????????
53+
//??#2.1 (Question still stands) Why is there no reason to create an instance
54+
//for this class!? If it's not an instance then what is it????? What is the
55+
//purpose or benefit of creating an instance and not?????????
56+
//??#2.2 Abstract classes can't be instantiated(make a new instance); again
57+
//what is the purpose or benefit in doing this????????
58+
//??#3.2 Why do the methods in the subclasses need to be overridden?? To me
59+
//overridden means that a method is written multiple times so override is
60+
//used to make a particular method the priority, but here we define the
61+
//object, we wish to access with, this; meaning that we know which object
62+
//is currently in use so we will find each update() in that objects class??
63+
//???After adding update() I was wondering that if only 1 class had update,
64+
//because it was a necessity for it, that if this would still be
65+
//necessary/needed? ALSO what if each class has update but different return
66+
//values for it then what?????????
67+
68+
69+
//BOOK:Automaton declares grid as an instance variable, so
70+
//every Automaton "has a" GridCanvas
71+
//???define the HAS-A here??? The variable grid has a value
72+
//that is = to a reference(?or instance?) that is from GridCanvas
73+
//private GridCanvas grid;
74+
protected GridCanvas grid;
75+
76+
//BOOK:The declaration specifies the name, arguments, and return type. But
77+
//it does not provide an implementation, because it is an abstract method.
78+
//Any class that extends Automaton must provide an implementation of update;
79+
//the declaration here allows the compiler to check.
80+
//??How does the compiler check??? Aside from that, does the compiler check
81+
//if a method like this exists; without the abstract, and if so run the code.
82+
//(Read the Aside part like, What does the compiler check for?)
83+
public abstract void update();
84+
85+
//????(11)Why a method and not a constructor???? I know it's
86+
//supposed to be a super but doesn't that only further my point??
87+
//BOOK:rate="frame rate", the # of time steps to show per second
88+
public void run(String title, int rate)
89+
{
90+
JFrame frame = new JFrame(title);
91+
//???Why JFrame.EXIT and not frame????
92+
//frame is a variable here and not there???
93+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
94+
frame.setResizable(false);
95+
frame.add(this.grid);
96+
frame.pack();
97+
frame.setVisible(true);
98+
this.mainloop(rate);
99+
}
100+
101+
private void mainloop(int rate)
102+
{
103+
while(true)
104+
{
105+
this.update();
106+
grid.repaint();
107+
108+
try {
109+
Thread.sleep(1000 / rate);
110+
} catch(InterruptedException e) {
111+
//do nada
112+
}
113+
}
114+
}
115+
}

Diff for: BlinkingPolygon.java

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.awt.*;
2+
3+
//??????????????????????????????????????????????????????????????????????????
4+
//??How and when is this class called or invoked since we never explicitly
5+
//invoke or refer to it anywhere?? We only go as far down as RP so how, why,
6+
//and when is this reached?????????????????
7+
//??I also never saw this occur in the debug!!!!!!????????????
8+
public class BlinkingPolygon extends RegularPolygon
9+
{
10+
public boolean visible;
11+
public int count;
12+
13+
//??My program won't work without a constructor but in DP the
14+
//book said that if I don't make one when I extend a class that
15+
//the compiler will make one for me??? Obviously this is false?
16+
public BlinkingPolygon(int nsides, int radius, Color c)
17+
{
18+
super(nsides, radius, c);
19+
//"initializes" visible and count
20+
visible = true;
21+
count = 0;
22+
}
23+
24+
//??Still don't know what this does but why not use a different/
25+
//already made draw?????????????????????????????????????????????
26+
public void draw(Graphics g)
27+
{
28+
if(visible)
29+
super.draw(g);
30+
//^Since super invokes draw in RP yet RP does not provide a
31+
//draw method, it goes to DP
32+
//??Does super always just work up the "extends" chain?????
33+
}
34+
35+
public void step()
36+
{
37+
count++;
38+
if(count == 10)
39+
{
40+
//??Don't understand the purpose of this?? Why??
41+
visible = !visible;
42+
//??Don't understand why is would reset count????
43+
count = 0;
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)