Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can someone help me write comments on this code, it\'s a code for a Paddle.java

ID: 3677296 • Letter: C

Question

can someone help me write comments on this code, it's a code for a Paddle.java class for a ping pong game:

/**
* @authors: Hunter Lai, Isaac Fehr, Raghad Marta
* @about: Class that creates a paddle and accounts for it's moving activity
*/

import java.awt.event.KeyListener;
import objectdraw.DrawingCanvas;
import objectdraw.FilledRect;

public class Paddle extends Thread implements KeyListener {
private final double START_HEIGHT = .15;
private final double START_WIDTH = .01;

private final double INCREASE_FACTOR = .2;
private final double DECREASE_FACTOR = .2;
private final double MAX_HEIGHT_FACT = .5;
private final double MIN_HEIGHT_FACT = .1;

private int PADDLE_HEIGHT, PADDLE_WIDTH;
private final double PADDLE_WALL_PADDING = 0.05;
private int side;
  
private int x, y;
public FilledRect paddle;
private DrawingCanvas canvas;

private char upKey;
private char downKey;
private int dir;

private final double SPEED = 1;
  
public Paddle(char side, DrawingCanvas canvas, char up, char down){
PADDLE_WIDTH = (int) ((double)canvas.getWidth() * START_WIDTH);
PADDLE_HEIGHT = (int)((double)canvas.getHeight()* START_HEIGHT);
y = canvas.getHeight() / 2;
this.side = side;
  
if(side == 'l'){
x = (int)(PADDLE_WALL_PADDING * canvas.getWidth());
}
else{
x = canvas.getWidth() - PADDLE_WIDTH - (int)(PADDLE_WALL_PADDING * canvas.getWidth());
}
  
paddle = new FilledRect(x, y, PADDLE_WIDTH, PADDLE_HEIGHT, canvas);

this.canvas = canvas;
this.canvas.addKeyListener(this);
this.canvas.requestFocus();
  
upKey = up;
downKey = down;
dir = 0;
}
  
public void increase() {
int newHeight = (int) (PADDLE_HEIGHT * (1 + INCREASE_FACTOR));
if(newHeight < (int)(canvas.getHeight() * MAX_HEIGHT_FACT)) {
PADDLE_HEIGHT = newHeight;
paddle.setHeight(PADDLE_HEIGHT);
}
}
  
public void decrease() {
int newHeight = (int) (PADDLE_HEIGHT * (1 - DECREASE_FACTOR));
  
if(newHeight > (int)(canvas.getHeight() * MIN_HEIGHT_FACT)) {
PADDLE_HEIGHT = newHeight;
paddle.setHeight(PADDLE_HEIGHT);
}
}
  
public void vibrate() {
for(int i = 0; i< 5; i++) {
paddle.move(5,5);
  
try {
this.sleep(100);
} catch(Exception e) {
e.printStackTrace();
}
  
paddle.move(-5, -5);
}
}
  
public void run() {
while(true){
paddle.move(0, dir * (SPEED * (double)canvas.getHeight()) * ((double)10 / 1000.0));
  
if(paddle.getY() < 0){
paddle.move(0, paddle.getY() * -1);
}

else if(paddle.getY() + PADDLE_HEIGHT > canvas.getHeight()){
paddle.move(0, canvas.getHeight() - (paddle.getY() + paddle.getHeight()));
}
  
if(side == 'l'){
x = (int)(PADDLE_WALL_PADDING * canvas.getWidth());
}
else{
x = canvas.getWidth() - PADDLE_WIDTH - (int)(PADDLE_WALL_PADDING * canvas.getWidth());
}
paddle.moveTo(x, paddle.getY());
  
TrippyPong.catchSleep(10);
}
}

@Override
public void keyPressed(java.awt.event.KeyEvent e) {
char key = e.getKeyChar();

if(key == upKey){
dir = -1;
}
  
else if(key == downKey){
dir = 1;
}
}

@Override
public void keyReleased(java.awt.event.KeyEvent e) {
char key = e.getKeyChar();
  
if(key == upKey && dir == -1){
dir = -0;
}
  
else if(key == downKey && dir == 1){
dir = 0;
}
}

@Override
public void keyTyped(java.awt.event.KeyEvent e) {
}

public int getX() {
   return x;
}
}

Explanation / Answer

import java.awt.event.KeyListener;
import objectdraw.DrawingCanvas;
import objectdraw.FilledRect;

public class Paddle extends Thread implements KeyListener {
private final double START_HEIGHT = .15; //intitalizing the variables for the paddle
private final double START_WIDTH = .01;

private final double INCREASE_FACTOR = .2;
private final double DECREASE_FACTOR = .2;
private final double MAX_HEIGHT_FACT = .5;
private final double MIN_HEIGHT_FACT = .1;

private int PADDLE_HEIGHT, PADDLE_WIDTH; // set paddle width and height
private final double PADDLE_WALL_PADDING = 0.05;
private int side;
  
private int x, y;
public FilledRect paddle;
private DrawingCanvas canvas;

private char upKey;
private char downKey;
private int dir;

private final double SPEED = 1;
  
public Paddle(char side, DrawingCanvas canvas, char up, char down){
PADDLE_WIDTH = (int) ((double)canvas.getWidth() * START_WIDTH); //sets paddle width
PADDLE_HEIGHT = (int)((double)canvas.getHeight()* START_HEIGHT);// sets paddle height
y = canvas.getHeight() / 2; //calculate paddle along y axis
this.side = side;
// if the side is I then we calculate x axis element by multiplying paddle wall padding with getwidth but if it is not I then we will subtract the multiplication of paddle wall padding and width from original paddle width and canvas.
if(side == 'l'){
x = (int)(PADDLE_WALL_PADDING * canvas.getWidth());
}
else{
x = canvas.getWidth() - PADDLE_WIDTH - (int)(PADDLE_WALL_PADDING * canvas.getWidth());
}
  
paddle = new FilledRect(x, y, PADDLE_WIDTH, PADDLE_HEIGHT, canvas);// fills in the paddle rectangle with following values

this.canvas = canvas;
this.canvas.addKeyListener(this);
this.canvas.requestFocus();
  
upKey = up;
downKey = down;
dir = 0;
}
  
public void increase() { // this is to increase the paddle height with the increase_factor leading to a new height
int newHeight = (int) (PADDLE_HEIGHT * (1 + INCREASE_FACTOR));
if(newHeight < (int)(canvas.getHeight() * MAX_HEIGHT_FACT)) {
PADDLE_HEIGHT = newHeight;
paddle.setHeight(PADDLE_HEIGHT);
}
}
  
public void decrease() {// This is used to decrease the height of the paddle with the decrease_factor leading to a newheight here
int newHeight = (int) (PADDLE_HEIGHT * (1 - DECREASE_FACTOR));
  
if(newHeight > (int)(canvas.getHeight() * MIN_HEIGHT_FACT)) {
PADDLE_HEIGHT = newHeight;
paddle.setHeight(PADDLE_HEIGHT);
}
}
  
public void vibrate() {//This functions helps the paddle to vibrate and move with a delay of 100 seconds.
for(int i = 0; i< 5; i++) {
paddle.move(5,5);
  
try {
this.sleep(100);
} catch(Exception e) {
e.printStackTrace();
}
  
paddle.move(-5, -5);
}
}
  
public void run() {
while(true){
paddle.move(0, dir * (SPEED * (double)canvas.getHeight()) * ((double)10 / 1000.0));
  
if(paddle.getY() < 0){ //getY() gets the value of the y coordinate .if it is O then move.
paddle.move(0, paddle.getY() * -1);
}

else if(paddle.getY() + PADDLE_HEIGHT > canvas.getHeight()){
paddle.move(0, canvas.getHeight() - (paddle.getY() + paddle.getHeight()));
}
  
if(side == 'l'){
x = (int)(PADDLE_WALL_PADDING * canvas.getWidth());
}
else{
x = canvas.getWidth() - PADDLE_WIDTH - (int)(PADDLE_WALL_PADDING * canvas.getWidth());
}
paddle.moveTo(x, paddle.getY());
  
TrippyPong.catchSleep(10);
}
}

@Override
public void keyPressed(java.awt.event.KeyEvent e) {
char key = e.getKeyChar();

if(key == upKey){
dir = -1;
}
  
else if(key == downKey){
dir = 1;
}
}

@Override
public void keyReleased(java.awt.event.KeyEvent e) {
char key = e.getKeyChar();
  
if(key == upKey && dir == -1){
dir = -0;
}
  
else if(key == downKey && dir == 1){
dir = 0;
}
}

@Override
public void keyTyped(java.awt.event.KeyEvent e) {// This used to override this function.It means function in parent and child class have same name and this function should be executed.
}

public int getX() {
   return x; //Get the vaalue of the x coordinate
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote