Objective: Using Java coding language, complete each \"TO-DO\" section for the following classes. Shape code (for reference, nothing...

50.1K

Verified Solution

Question

Programming

Objective: Using Java coding language,complete each \"TO-DO\" section for the followingclasses.

Shape code (for reference, nothing to completehere):

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.io.RandomAccessFile;

public abstract class Shape extends Rectangle {
   public int id;
   public Color color;
   public int xSpeed, ySpeed;
  
   public Shape(int id, int x, int y, int width, intheight, Color color, int xSpeed, int ySpeed) {
       super(x,y,width,height);
       this.id = id;
       this.color = color;
       this.xSpeed = xSpeed;
       this.ySpeed = ySpeed;
   }
  
   public abstract void move(int screenWidth, intscreenHeight);
   public abstract void draw(Graphics g);
   public abstract void save(RandomAccessFile raf) throwsException;
  
}

Ball code:

import java.awt.Color;
import java.awt.Graphics;
import java.io.RandomAccessFile;

public class Ball extends Shape {

   public Ball(int id, int x, int y, int size, Colorcolor, int xSpeed, int ySpeed) {
       super(id, x, y, size, size, color,xSpeed, ySpeed);
   }

   @Override
   public void move(int screenWidth, int screenHeight){
       x += xSpeed;
       y += ySpeed;
      
       if(x > screenWidth) x =-width;
       else if(x + width < 0) x =screenWidth;
      
       if(y > screenHeight) y =-height;
       else if(y + height < 0) y =screenHeight;      
   }

   @Override
   public void draw(Graphics g) {
       g.setColor(color);
       g.fillOval(x, y, width,height);
       g.setColor(Color.BLACK);
       g.drawOval(x, y, width,height);
   }

   // TO-DO:    Write the code thatsaves out the type of object (Ball)
   //          and all data about the box (\"Ball\", id, x, y, width, height,color,
   //          xSpeed, and ySpeed)
   @
Override
   public void save(RandomAccessFile raf) throwsException {
       // Note, when saving color you willsave it as an int: color.getRGB()
   }
}

Box code:

import java.awt.Color;
import java.awt.Graphics;
import java.io.RandomAccessFile;
// is-a
public class Box extends Shape {

   public Box(int id, int x, int y, int size, Colorcolor, int xSpeed, int ySpeed) {
       super(id, x, y, size, size, color,xSpeed, ySpeed);
   }

   @Override
   public void move(int screenWidth, int screenHeight){
       x += xSpeed;
       y += ySpeed;
      
       if(x > screenWidth) x =-width;
       else if(x + width < 0) x =screenWidth;
      
       if(y > screenHeight) y =-height;
       else if(y + height < 0) y =screenHeight;
                     
   }

   @Override
   public void draw(Graphics g) {
       g.setColor(color);
       g.fillRect(x, y, width,height);
       g.setColor(Color.BLACK);
       g.drawRect(x, y, width,height);
   }

   // TO-DO:    Write the code thatsaves out the type of object (Box)
   //          and all data about the box (\"Box\", id, x, y, width, height,color,
   //          xSpeed, and ySpeed)

   @Override
   public void save(RandomAccessFile raf) throwsException {
       // Note, when saving color you willsave it as an int: color.getRGB()
   }

}

Answer & Explanation Solved by verified expert
4.1 Ratings (439 Votes)
Data is saved into the file using writeBytes method ofRandomAccessFile objectI have overridden the toString method of Box Ball classes which will return all the data as string andthen finally saving it as bytes in save methodNote int value of color is taken care of in this methoditself before saving Appending n to the result returned bytoString so that new record goes into new line just to make fileeasily readableScreenshot of code and datatxt file are also attachedBoxjavaimport javaawtColorimport javaawtGraphicsimport javaioRandomAccessFile isapublic class Box extends Shape public Boxint id int x int y int size Color color int xSpeed int ySpeed superid x y size size color xSpeed ySpeed Override public void moveint    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students