In: Computer Science
Worksheet 3 – Object Oriented Code Analysis
OBJECTIVE: Explore analyzing existing code.
INSTRUCTIONS: Complete the questions and submit this document with your answers.
Problem:
Use the code provided in class for the game that we are updating to answer the following questions.
Object Oriented Analysis
Student Name: Date:
Questions Details & Rationale for Answer
(1 pt) Which class contains the main/start for the
program?
(2 pts) What are the classes in the game?
(3 pts) Create a bulleted list of the Player class attributes &
methods. Mark (A) for attribute and (M) for Method and provide a
brief description of the purpose for the attribute and methods.
Hence, What do the attributes describe and methods do?
(1 pt) List one example of encapsulation
(1 pt) List one example of abstraction
(1 pt) List one example of usage of an access modifier
(1 pt) Describe when to use the modifier “private static final”?
code:
import java.awt.Color;
import java.awt.Container;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferStrategy;
import java.io.Serializable;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
public class SpaceInvaders extends JFrame implements
Serializable {
private static final long serialVersionUID =
6037236323540109415L;
public static final int WIDTH = 600;
public static final int HEIGHT = 600;
BufferStrategy strategy;
Container c = getContentPane();
static JTextArea names = new JTextArea(10, 50);
static JList<String> userList;
private static int direction = 1;
private static final String TITLE = "Space
Invaders";
public SpaceInvaders() {
super(TITLE);
final JPanel startPanel = new
JPanel();
startPanel.setBackground(Color.BLACK);
JButton start = new
JButton("Start");
JButton multiPlayer = new
JButton("Multiplayer");
JButton exit = new
JButton("Exit");
startPanel.add(start);
startPanel.add(multiPlayer);
startPanel.add(exit);
this.add(startPanel);
ImageIcon pic = new
ImageIcon("Resources\\Enemy.jpg");
Image img = pic.getImage();
setVisible(true);
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setIgnoreRepaint(true);
createBufferStrategy(2);
start.addActionListener(new
ActionListener() {
@Override
public void
actionPerformed(ActionEvent e) {
c.removeAll();
JFrame frame = SpaceInvaders.this;
c.add(GameCanvas.getGameCanvas(false));
GameCanvas.getGameCanvas(false).grabFocus();
frame.setContentPane(c);
frame.repaint();
}
});
multiPlayer.addActionListener(new
ActionListener() {
@Override
public void
actionPerformed(ActionEvent e) {
c.removeAll();
JFrame frame =
SpaceInvaders.this;
c.add(multiplayer());
GameCanvas.getGameCanvas(true);
multiplayer().grabFocus();
frame.setContentPane(c);
frame.repaint();
}
});
exit.addActionListener(new
ActionListener() {
@Override
public void
actionPerformed(ActionEvent e) {
System.exit(0);
}
});
strategy =
getBufferStrategy();
strategy.show();
setIconImage(img);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new
Runnable() {
@Override
public void
run() {
new SpaceInvaders();
}
});
}
public static int getDirection() {
return direction;
}
public static void setDirection(int direction) {
SpaceInvaders.direction =
direction;
}
public JPanel multiplayer() {
JPanel panel = new JPanel();
names.setEditable(false);
panel.setBackground(Color.BLACK);
userList = new JList<String>();
userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane theList = new
JScrollPane(userList);
userList.setListData(GameCanvas.listVector);
names.add(userList);
names.add(theList);
panel.add(names);
this.add(panel);
return panel;
}
}
(1 pt) Which class contains the main/start for the program? => class SpaceInvaders (2 pts) What are the classes in the game? => class SpaceInvaders (3 pts) Create a bulleted list of the Player class attributes & methods. Mark (A) for attribute and (M) for Method and provide a brief description of the purpose for the attribute and methods. Hence, What do the attributes describe and methods do? => there is no player class in the given code. (1 pt) List one example of encapsulation => private static int direction = 1; This variable can only be used and changed inside the class, Hence it is encapsulated. (1 pt) List one example of abstraction => frame.repaint(); This method is defined indeed in JFrame class, we do not know what exactly is written in JFrame class for this method, but with abstraction, we know that calling this method will re-draw everything on canvas. (1 pt) List one example of usage of an access modifier => public SpaceInvaders() { Here public means that the SpaceInvaders constructor can be publically accessed from anywhere. (1 pt) Describe when to use the modifier “private static final”? => When the value is supposed to be used only inside the current class, and should never be changed.
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.