Skip to content

Commit 8354d28

Browse files
authored
Add files via upload
1 parent d0f9846 commit 8354d28

15 files changed

+643
-0
lines changed

Tortoise And Hare/TortoiseAndHare.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

Tortoise And Hare/src/Certify.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Certify {
2+
public static void main(String[] args) {
3+
// Print the following line if you got part 1 working and ran some races. -- 11 points
4+
System.out.println("I certify on my honor that I ran the Race code in part one.");
5+
6+
// Print the following line if you added and raced a new racer type in part 2. -- 7 points
7+
System.out.println("My new racer class is Pig.java");
8+
9+
// Print the following line if you added the morph method in part 2. -- 7 points
10+
System.out.println("The action added is morph.");
11+
}
12+
}

Tortoise And Hare/src/Hare.java

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/** Hare class
2+
* inherits from abstract Racer class
3+
*/
4+
5+
import java.awt.Graphics;
6+
import java.awt.Color;
7+
import java.util.Random;
8+
9+
public class Hare extends Racer
10+
{
11+
private boolean isMorphed = false;
12+
/** Default Constructor: calls Racer default constructor
13+
*/
14+
public Hare( )
15+
{
16+
super( );
17+
}
18+
19+
/** Constructor
20+
* @param rID racer Id, passed to Racer constructor
21+
* @param rX x position, passed to Racer constructor
22+
* @param rY y position, passed to Racer constructor
23+
*/
24+
public Hare( String rID, int rX, int rY )
25+
{
26+
super( rID, rX, rY );
27+
}
28+
29+
/** move: calculates the new x position for the racer
30+
* Hare move characteristics: 30% of the time, Hare jumps 5 pixels
31+
* 70% of the time, Hare sleeps (no move)
32+
* generates random number between 1 & 10
33+
* for 1 - 7, no change to x position
34+
* for 8 - 10, x position is incremented by 5
35+
*/
36+
public void move( )
37+
{
38+
Random rand = new Random( );
39+
int move = rand.nextInt( 10 ) + 1 ;
40+
41+
if ( getX( ) < 100 )
42+
{
43+
if ( move > 6 )
44+
setX( getX( ) + 4 );
45+
}
46+
else
47+
{
48+
if ( move > 8 )
49+
setX( getX( ) + 4 );
50+
}
51+
}
52+
53+
/** draw: draws the Hare at current (x, y) coordinate
54+
* @param g Graphics context
55+
*/
56+
public void draw( Graphics g )
57+
{
58+
int startY = getY( );
59+
int startX = getX( );
60+
61+
// tail
62+
g.setColor( Color.LIGHT_GRAY );
63+
g.fillOval( startX - 37, startY + 8, 12, 12 ) ;
64+
65+
//body
66+
if (isMorphed) {
67+
g.setColor(Color.BLUE);
68+
}
69+
else {
70+
g.setColor(Color.GRAY);
71+
}
72+
g.fillOval( startX - 30, startY, 20, 20 );
73+
74+
//head
75+
g.fillOval( startX - 13, startY + 2, 13, 8 );
76+
g.fillOval( startX - 13, startY - 8, 8, 28 );
77+
78+
//flatten bottom
79+
g.clearRect( startX - 37, startY + 15, 32, 5 );
80+
}
81+
@Override
82+
83+
public void morph(Graphics g) {
84+
85+
isMorphed = true;
86+
87+
draw(g);
88+
89+
}
90+
}

Tortoise And Hare/src/Pig.java

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/** Tortoise class
2+
* inherits from abstract Racer class
3+
*/
4+
5+
import java.awt.Graphics;
6+
import java.awt.Color;
7+
import java.util.Random;
8+
9+
public class Pig extends Racer
10+
{
11+
private int speed;
12+
private Random rand;
13+
private boolean isMorphed = false;
14+
15+
/** Default Constructor: calls Racer default constructor
16+
*/
17+
public Pig( )
18+
{
19+
super( );
20+
setRandAndSpeed();
21+
}
22+
23+
/** Constructor
24+
* @param rID racer Id, passed to Racer constructor
25+
* @param rX x position, passed to Racer constructor
26+
* @param rY y position, passed to Racer constructor
27+
*/
28+
public Pig( String rID, int rX, int rY )
29+
{
30+
super( rID, rX, rY );
31+
setRandAndSpeed();
32+
}
33+
34+
/** move: calculates the new x position for the racer
35+
* Tortoise move characteristics: "slow & steady wins the race"
36+
* increment x by 1 most of the time
37+
*/
38+
public void move( )
39+
{
40+
int move = rand.nextInt( 100 ) + 1;
41+
if ( move < speed )
42+
setX( getX( ) + 1 );
43+
}
44+
45+
/** draw: draws the Tortoise at current (x, y) coordinate
46+
* @param g Graphics context
47+
*/
48+
public void draw( Graphics g )
49+
{
50+
int startX = getX( );
51+
int startY = getY( );
52+
53+
if (isMorphed) {
54+
55+
g.setColor(Color.MAGENTA);
56+
}
57+
else {
58+
g.setColor(new Color(50, 150, 50)); // dark green
59+
}
60+
61+
//body
62+
g.fillOval( startX - 20, startY, 15, 25 );
63+
64+
//head
65+
g.fillOval( startX - 57, startY + 6, 45, 30 );
66+
67+
//flatten bottom
68+
g.clearRect( startX - 90, startY + 31, 55, 6 );
69+
70+
//feet
71+
g.setColor( new Color( 54, 129, 33 ) ); // brown
72+
g.fillOval( startX - 37, startY + 12, 6, 9 );
73+
g.fillOval( startX - 13, startY + 12, 7, 6 );
74+
}
75+
76+
private void setRandAndSpeed( ) {
77+
// percentage of time (between 90 - 99%) that this tortoise moves each turn
78+
rand = new Random( );
79+
speed = rand.nextInt( 10 ) + 90;
80+
}
81+
@Override
82+
83+
public void morph(Graphics g) {
84+
85+
isMorphed = true;
86+
87+
draw(g);
88+
89+
}
90+
}

0 commit comments

Comments
 (0)