Question

(JAVA NetBeans)

Write programs in java

3. Modify example 10.21-24 to have a number of tortoiseRacer objects moving with a different speed..

Example 10.21-24

//Animal.java

/**  Animal class
*    Anderson. Franceschi
*/

import java.awt.Graphics;

public abstract class Animal
{
  private int x;      // x position
  private int y;      // y position
  private String ID;  // animal ID

  /** default constructor
  *    Sets ID to empty String
  */
  public Animal( )
  {
    ID = "";
  }

  /** Constructor
  *    @param rID   Animal ID
  *    @param rX    x position
  *    @param rY    y position
  */
  public Animal( String rID, int rX, int rY )
  {
    ID = rID;
    x = rX;
    y = rY;
  }

  /** accessor for ID
  *   @return  ID
  */
  public String getID( )
  {
    return ID;
  }

  /** accessor for x
  *   @return  x coordinate
  */
  public int getX( )
  {
    return x;
  }

  /** accessor for y
  *   @return  y coordinate
  */
  public int getY( )
  {
    return y;
  }

  /** mutator for x
  *   @param  newX  new value for x position
  */
  public void setX( int newX )
  {
    x = newX;
  }

  /** mutator for y
  *   @param  newY  new value for y position
  */
  public void setY( int newY )
  {
    y = newY;
  }

  /** abstract method for drawing Animal
  *   @param   g    Graphics context
  */
  public abstract void draw( Graphics g );
}

======================================================================================

//Moveable.java

/** Moveable interface
*   Anderson, Franceschi
*/

public interface Moveable
{
  int FAST = 5; // static constant
  int SLOW = 1; // static constant

  void move( ); // abstract method
}

======================================================================================

//Pause.java

/* Pause class to pause applications
   Anderson, Franceschi
*/

public class Pause
{
  public static void wait( double time )
  {
     try {
      Thread.sleep(  ( int ) ( time * 1000 ) );
     }
     catch ( InterruptedException e )
     {
      e.printStackTrace();
     }
  }
}

======================================================================================

//TortoiseRacer.java

/**  TortoiseRacer class
*    inherits from abstract Animal class
*    implements Moveable interface
*    Anderson, Franceschi
*/

import java.awt.Graphics;
import java.awt.Color;

public class TortoiseRacer extends Animal implements Moveable
{
   /** Default Constructor: calls Animal default constructor
   */
   public TortoiseRacer( )
   {
     super( );
   }

   /** Constructor
   *    @param rID  racer Id, passed to Animal constructor
   *    @param rX   x position, passed to Animal constructor
   *    @param rY   y position, passed to Animal constructor
   */
   public TortoiseRacer( String rID, int rX, int rY )
   {
     super( rID, rX, rY );
   }

   /** draw: draws the Tortoise at current (x, y) coordinate
   *       implements abstract method in Animal class
   *       @param g   Graphics context
   */
   public void draw( Graphics g )
   {
     int startX = getX( );
     int startY = getY( );

     g.setColor( new Color( 34, 139, 34 ) ); // dark green

     //body
     g.fillOval( startX, startY, 25, 15 );

     //head
     g.fillOval( startX + 20, startY + 5,  15, 10 );

     //flatten bottom
      g.clearRect( startX, startY + 11, 35, 4 );

     //feet
     g.setColor( new Color( 34, 139, 34 ) );  // brown
     g.fillOval( startX + 3, startY + 10,  5, 5 );
     g.fillOval( startX + 17, startY + 10, 5, 5 );
   }

    /** implements move method in Moveable interface
    *   move:  calculates the new x value for the racer
    *   Tortoise move characteristics: "slow & steady wins the race"
    *      increment x by SLOW (inherited from Moveable interface)
    */
    public void move( )
    {
        setX( getX( ) + SLOW );
    }
}

======================================================================================

//TortoiseRacerClient.java

/* A Graphical Application 
   TortoiseRacer Client
   Anderson, Franceschi
   mchang 20190307
*/

import javax.swing.JFrame;
import java.awt.Graphics;

public class TortoiseRacerClient extends JFrame
{
  private TortoiseRacer t;

  public TortoiseRacerClient( )
  {
    t = new TortoiseRacer( "Tortoise", 50, 50 );
  }

  public void paint( Graphics g )
  {
    super.paint( g );
    for ( int i = 0; i < getWidth( ); i++ )
    {
      t.move( );
      t.draw( g );
      Pause.wait( .03 );
      g.clearRect( 0, 0, getWidth( ), getHeight( ) );
    } 
  }
  
  public static void main( String [] args )
  {
    TortoiseRacerClient app = new TortoiseRacerClient( );
    app.setSize( 400, 300 );
    app.setVisible( true );
  }
}

t

3. Modify example 10.21-24 to have a number of tortoiseRacer objects moving with a different speed..
t
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Following are the things I have changed in the code.

  1. I have changed the speed, added one more speed and changed the move function to get the variation of the speed.
  2. I have added two more tortoise objects which move at different speeds added.

Following is the updated code of all the files, Changed code would be marked in the bold.

//Animal.java

/** Animal class
* Anderson. Franceschi
*/

import java.awt.Graphics;

public abstract class Animal
{
private int x; // x position
private int y; // y position
private String ID; // animal ID

/** default constructor
* Sets ID to empty String
*/
public Animal( )
{
ID = "";
}

/** Constructor
* @param rID Animal ID
* @param rX x position
* @param rY y position
*/
public Animal( String rID, int rX, int rY )
{
ID = rID;
x = rX;
y = rY;
}

/** accessor for ID
* @return ID
*/
public String getID( )
{
return ID;
}

/** accessor for x
* @return x coordinate
*/
public int getX( )
{
return x;
}

/** accessor for y
* @return y coordinate
*/
public int getY( )
{
return y;
}

/** mutator for x
* @param newX new value for x position
*/
public void setX( int newX )
{
x = newX;
}

/** mutator for y
* @param newY new value for y position
*/
public void setY( int newY )
{
y = newY;
}

/** abstract method for drawing Animal
* @param g Graphics context
*/
public abstract void draw( Graphics g );
}

//Moveable.java

/** Moveable interface
* Anderson, Franceschi
*/

public interface Moveable
{
int FAST = 3; // static constant
int SLOW = 1; // static constant
int SUPERFAST = 7; // static constant
void move(int variation); // abstract method

}

//Pause.java

/* Pause class to pause applications
Anderson, Franceschi
*/

public class Pause
{
public static void wait( double time )
{
try {
Thread.sleep( ( int ) ( time * 1000 ) );
}
catch ( InterruptedException e )
{
e.printStackTrace();
}
}
}

//TortoiseRacer.java

/** TortoiseRacer class
* inherits from abstract Animal class
* implements Moveable interface
* Anderson, Franceschi
*/

import java.awt.Graphics;
import java.awt.Color;

public class TortoiseRacer extends Animal implements Moveable
{
/** Default Constructor: calls Animal default constructor
*/
public TortoiseRacer( )
{
super( );
}

/** Constructor
* @param rID racer Id, passed to Animal constructor
* @param rX x position, passed to Animal constructor
* @param rY y position, passed to Animal constructor
*/
public TortoiseRacer( String rID, int rX, int rY )
{
super( rID, rX, rY );
}

/** draw: draws the Tortoise at current (x, y) coordinate
* implements abstract method in Animal class
* @param g Graphics context
*/
public void draw( Graphics g )
{
int startX = getX( );
int startY = getY( );

g.setColor( new Color( 34, 139, 34 ) ); // dark green

//body
g.fillOval( startX, startY, 25, 15 );

//head
g.fillOval( startX + 20, startY + 5, 15, 10 );

//flatten bottom
g.clearRect( startX, startY + 11, 35, 4 );

//feet
g.setColor( new Color( 34, 139, 34 ) ); // brown
g.fillOval( startX + 3, startY + 10, 5, 5 );
g.fillOval( startX + 17, startY + 10, 5, 5 );
}

/** implements move method in Moveable interface
* move: calculates the new x value for the racer
* Tortoise move characteristics: "slow & steady wins the race"
* increment x by SLOW (inherited from Moveable interface)
*/
public void move(int variation)
{
   switch(variation){
   case 1: {
       setX( getX() + SLOW );break;
   }
   case 2: {
       setX( getX() + FAST );break;
   }
   case 3:{
       setX( getX() + SUPERFAST );break;
   }
   default:
       break;
   }

  
}

}

/* A Graphical Application
TortoiseRacer Client
Anderson, Franceschi
mchang 20190307
*/

import javax.swing.JFrame;
import java.awt.Graphics;

public class TortoiseRacerClient extends JFrame
{
private TortoiseRacer t;
private TortoiseRacer t1_new;
private TortoiseRacer t2_new;

public TortoiseRacerClient( )
{
t = new TortoiseRacer( "Tortoise", 50, 50 );
t1_new = new TortoiseRacer("Tortoise_new1", 50, 100);
t2_new = new TortoiseRacer("Tortoise_new2", 50, 200);

}

public void paint( Graphics g )
{
super.paint( g );
for ( int i = 0; i < getWidth(); i++ )
{
// 1 --> for slow
// 2 --> for fast
// 3 --> for super-fast
t.move(1);
t.draw( g );  

t1_new.move(2);
t1_new.draw( g );  

t2_new.move(3);
t2_new.draw( g );


Pause.wait( .03 );
g.clearRect( 0, 0, getWidth(), getHeight() );
}
}

public static void main( String [] args )
{
TortoiseRacerClient app = new TortoiseRacerClient( );
app.setSize( 400, 300 );
app.setVisible( true );
}
}

I have tested it, I cannot put the snippet of animation here. But I have tested it thoroughly.

I have tested the code properly. If you would have any doubt please comment.
Thanks

Add a comment
Know the answer?
Add Answer to:
(JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Making a rectangle class in java write a simple class that will represent a rectangle. Later...

    Making a rectangle class in java write a simple class that will represent a rectangle. Later on, we will see how to do this with graphics, but for now, we will just have to use our imaginations for the visual representations ofWthe rectangles. Instance Variables Recall that an object is constructed using a class as its blueprint. So, think about an rectangle on your monitor screen. To actually draw a rectangle on your monitor screen, we need a number of...

  • The assignment is to take the current program that draws a line and to add 4...

    The assignment is to take the current program that draws a line and to add 4 buttons at the bottom of the frame that will change the line drawn to a different color (ie. No button pressed draws a black line, Button labeled Red changes any new lines drawn to red and retains any previously colored lines, etc for all the buttons). This is the code I have so far but I'm having issues with the buttons. This is also...

  • (JAVA NetBeans) Write programs in java Modify the textbook example textbook example: //BankAccount.java import java.tex...

    (JAVA NetBeans) Write programs in java Modify the textbook example textbook example: //BankAccount.java import java.text.DecimalFormat; public class BankAccount { public final DecimalFormat MONEY = new DecimalFormat( "$#,##0.00" ); private double balance; /** Default constructor * sets balance to 0.0 */ public BankAccount( ) { balance = 0.0; System.out.println( "In BankAccount default constructor" ); } /** Overloaded constructor * @param startBalance beginning balance */ public BankAccount( double startBalance ) { if ( balance >= 0.0 ) balance = startBalance; else balance...

  • // ====== FILE: Point.java ========= // package hw3; /** * A class to that models a...

    // ====== FILE: Point.java ========= // package hw3; /** * A class to that models a 2D point. */ public class Point { private double x; private double y; /** * Construct the point (<code>x</code>, <code>y</code>). * @param x the <code>Point</code>'s x coordinate * @param y the <code>Point</code>'s y coordinate */ public Point(double x, double y) { this.x = x; this.y = y; } /** * Move the point to (<code>newX</code>, <code>newY</code>). * @param newX the new x coordinate for...

  • a derived class from Organism. You must complete the constructors, and the method definitions that were...

    a derived class from Organism. You must complete the constructors, and the method definitions that were abstract methods in Organism. /** Organism.java * Definition for the Organism base class. * Each organism has a reference back to the World object so it can move * itself about in the world. */ public abstract class Organism {    protected int x, y;       // Position in the world    protected boolean moved;   // boolean to indicate if moved this turn   ...

  • Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles....

    Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...

  • On the Turtle class I need to make a method so turtle 0 and turtle 2...

    On the Turtle class I need to make a method so turtle 0 and turtle 2 to "kiss" or touch their heads. Everything that can be use is in the SimpleTurtle link. https://www2.cs.uic.edu/~i101/doc/SimpleTurtle.html public void turtleTricks() { int worldWidth = 800; int worldHeight = 600; Random random = new Random(); World earth = new World(worldWidth, worldHeight );//Make a big world for turtles.    Turtle[] turtles = new Turtle[5]; int xLocation, yLocation, angle; for ( int i = 0; i <...

  • Must be in Java. Show proper reasoning with step by step process. Comment on the code...

    Must be in Java. Show proper reasoning with step by step process. Comment on the code please and show proper indentation. Use simplicity. Must match the exact Output. CODE GIVEN: LineSegment.java LineSegmentTest.java Point.java public class Point { private double x, y; // x and y coordinates of point /** * Creates an instance of Point with the provided coordinates * @param inX the x coordinate * @param inY the y coordinate */ public Point (double inX, double inY) { this.x...

  • Using the following Java program, modify the code so that every time you run your program,...

    Using the following Java program, modify the code so that every time you run your program, it generates random numbers for your array, and then prints it (insertion sort) import java.awt.Graphics; import java.applet.Applet; public class SortingProg extends Applet { int a[] = { 55, 25, 66, 45, 8, 10, 12, 89, 68, 37 }; public void paint(Graphics g)     {       print(g,"Data items in original order",a,25,25);       sort();       print(g,"Data items in ascending order",a,25,55);     } public void sort() {...

  • JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to...

    JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to maintain a list of shapes drawn as follows: Replace and upgrade all the AWT components used in the programs to the corresponding Swing components, including Frame, Button, Label, Choice, and Panel. Add a JList to the left of the canvas to record and display the list of shapes that have been drawn on the canvas. Each entry in the list should contain the name...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT