INSTRUCTIONS FOR LAB #2 ON ENCAPSULATION =======================================
ID: 3623265 • Letter: I
Question
INSTRUCTIONS FOR LAB #2 ON ENCAPSULATION
===========================================
You are going to build a simple simulation of a video game. Don't worry, we're
not asking you to write the code for a complete game. What we we need is just
the skeleton code for part of the game. Therefore, we
won't be using too many classes or writing lots of functional code. We're
only interested in class construction with basic properties and some methods,
without only some bare bones implementation code (the body of the method)
to demonstrate mastery of the encapsulation principle. As a guideline, examine
the example4(example 4 is at bottom after insructions). That's about as much detail as you need.
Create the following classes:
1. A "GameCharacter" class
2. A "Game" class
3. A Startup class with a main method
Provide at least 3 properties for class #1 and #2.
Provide at least 3 methods for class #1 and #2. Make sure at least two
of the methods are to be completed in a specific order. Make sure at least
one method has one or more method parameters.
Create all of the "GameCharacter" objects you will need for your game. You
must think carefully where this object creation should take place.
Create the "Game" object. You must think carefully where this action should
take place.
Example 4
/**
* In this good example, notice that the Car talks to the engine so that
* the Startup class does not need to do this. We're hiding the details
* of how the engine works by encapsulating those details here.
*
*/
public class Car {
// NO magic numbers -- use constants
private static int MIN_CYL = 4;
private static int MAX_CYL = 12;
private String engineType;
private Engine engine;
// Arguments validated and sensible default applied if illegal
// Use of constants make change easier and less error prone.
// Furthermore, using a custom constructor guarantees that the
// number of cylinders for the engine is set. We can reduce the
// number of setter method needed if we use custom constructor arguments,
// which also promotes reliability. But don't use more than three or four.
public Car(int numOfCylinders) {
if(numOfCylinders < MIN_CYL || numOfCylinders > MAX_CYL) {
// if illegal use default
engine = new Engine(MIN_CYL);
engineType = "V" + MIN_CYL;
} else {
engine = new Engine(numOfCylinders);
engineType = "V" + numOfCylinders;
}
}
// Car delegates to engine
public void start() {
engine.start();
}
// Car delegates to engine
public void turnOff() {
engine.stop();
}
public String getEngineType() {
return engineType;
}
public boolean isRunning() {
return engine.isRunning();
}
}
package example4;
/**
*
* @
*/
public class Engine {
private int cylinderCount;
private boolean running;
// No validation necessary because it's done in Car
public Engine(int numOfCylinders) {
cylinderCount = numOfCylinders;
}
public void start() {
if(!running) {
running = true;
}
}
public void stop() {
if(running) {
running = false;
}
}
public int getCylinderCount() {
return cylinderCount;
}
public boolean isRunning() {
return running;
}
}
package example4;
/**
* Here is an improved version that uses good encapsulation. Notice that the
* "Startup" class ONLY communicates with, and is only dependent on, the
* "Car" class. As far as the Startup class is concerned the details of an
* engine are hidden by the Car. That's another example of encapsulation. Hide
* unnecessary or inappropriate details from the host class.
*
* This also reduces dependencies from 2 to 1. Fewer dependencies make for greater
* flexibility.
*
*/
public class Startup {
// No magic numbers!! Always use constants
private static int SIX_CYLINDERS = 6;
public static void main(String[] args) {
// Simple!!! Compare this to example3.
Car car = new Car(SIX_CYLINDERS);
car.start();
System.out.println("Car running status: " + car.isRunning());
System.out.println("Engine Type: " + car.getEngineType());
}
}
Explanation / Answer
class GameCharacter
{
private String Difficulty;
//Setter method
public GameCharacter(String str)
{
Difficulty=str;
}
public void setDifficulty(String diff)
{
Difficulty=diff;
}
//Getter method
public String getDifficulty()
{
return Difficulty;
}
//Displays data
public void Display()
{
System.out.println("Game Difficulty is:"+getDifficulty());
}
}
class Game
{
private String gameName;
//Constructor
public Game(String str)
{
setgameName(str);
}
//Setter method
public void setgameName(String name)
{
gameName=name;
}
//Getter method
public String getgameName()
{
return gameName;
}
//Displays data
public void Display()
{
System.out.println("Game Name:"+gameName);
}
}
public class Startup
{
public static void main(String[] args)
object.Display();
GameCharacter object1=new GameCharacter("Medium");
object1.Display();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.