In: Computer Science
Objective: Using Java coding language, complete each "TO-DO" section for the following classes.
Shape code (for reference, nothing to complete here):
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, int
height, 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, int
screenHeight);
public abstract void draw(Graphics g);
public abstract void save(RandomAccessFile raf) throws
Exception;
}
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, Color
color, 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 that
saves 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) throws
Exception {
// Note, when saving color you will
save 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, Color
color, 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 that
saves 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) throws
Exception {
// Note, when saving color you will
save it as an int: color.getRGB()
}
}
Data is saved into the file using writeBytes() method of RandomAccessFile object.
I have overridden the toString() method of Box & Ball classes which will return all the data as string and then finally saving it as bytes in save() method.
Note: int value of color is taken care of in this method itself before saving. Appending '\n' to the result returned by toString() so that new record goes into new line (just to make file easily readable).
Screenshot of code and data.txt file are also attached.
Box.java
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, Color color, 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 that saves 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) throws Exception {
// Note, when saving color you will save it as an int: color.getRGB()
raf.writeBytes(this.toString());
}
@Override
public String toString() {
return "(Box, id:" + this.id + ", x:" + this.x + ", y:" + this.y + ", width:" + this.width + ", height:" + this.height + ", color:" + this.color.getRGB()
+ ", xSpeed:" + this.xSpeed + ", ySpeed:" + this.ySpeed + ")\n";
}
}
Ball.java
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, Color color, 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 that saves 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) throws Exception {
// Note, when saving color you will save it as an int: color.getRGB()
raf.writeBytes(this.toString());
}
@Override
public String toString() {
return "(Ball, id:" + this.id + ", x:" + this.x + ", y:" + this.y + ", width:" + this.width + ", height:" + this.height + ", color:" + this.color.getRGB()
+ ", xSpeed:" + this.xSpeed + ", ySpeed:" + this.ySpeed + ")\n";
}
}
Shape.java
No changes in this file.
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, int height, 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, int screenHeight);
public abstract void draw(Graphics g);
public abstract void save(RandomAccessFile raf) throws Exception;
}
Main.java
I have written main() in this file to run and test the code.
Created two objects of Ball & Box classes and called save() on these objects. Data is being saved in the file named data.txt.
import java.awt.*;
import java.io.RandomAccessFile;
public class Main {
public static void main(String[] args) throws Exception {
Shape ball = new Ball(1, 2, 3, 4, Color.CYAN, 10, 15);
Shape box = new Box(2, 4, 3, 9, Color.GREEN, 12, 11);
RandomAccessFile raf = new RandomAccessFile("src/data.txt", "rw");
ball.save(raf);
box.save(raf);
}
}
Data.txt
Below are the contents in the file after running the main().
(Ball, id:1, x:2, y:3, width:4, height:4, color:-16711681, xSpeed:10, ySpeed:15) (Box, id:2, x:4, y:3, width:9, height:9, color:-16711936, xSpeed:12, ySpeed:11)