Question

WRITE IN PSEUDOCODE USING THE FOLLOWING GUIDELINES Classes Program 1: WAKA, WAKA. Pac-Man was a big...

WRITE IN PSEUDOCODE USING THE FOLLOWING GUIDELINES

Classes

Program 1: WAKA, WAKA. Pac-Man was a big hit back in the 80s. One of the things he could do was “teleport” from one side of the screen to the other, and that’s what we’re going to implement here. For this program, you’re going to write a class (called “PacMan”) that only has two attributes: an X and Y location. Imagine the player is on a 10x10 grid and starts at location (5, 5). If the player exceeds the bounds of the screen, their location teleports to the other side of the screen (e.g. far-right players go to the far-left). The class should include methods for going up (goUp), down (goDown), left (goLeft) and right (goRight). Finally, you should make a “driver” that brings an instance of this class to life in the middle of the “screen” and enables the player to move up, down, left and right. Design (pseudocode) and implement (source code) this program.

Sample run

Current location – X: 5 Y:5

(U)p, (D)own, (L)eft, (R)ight or (Q)uit:

U

Current location – X : 5 Y :4

(U)p, (D)own, (L)eft, (R)ight or (Q)uit:

U

Current location – X : 5 Y :3

(U)p, (D)own, (L)eft, (R)ight or (Q)uit:

U

Current location – X : 5 Y :2

(U)p, (D)own, (L)eft, (R)ight or (Q)uit:

U

Current location – X : 5 Y :1

(U)p, (D)own, (L)eft, (R)ight or (Q)uit:

U

Current location – X : 5 Y :0

(U)p, (D)own, (L)eft, (R)ight or (Q)uit:

U

Current location – X : 5 Y :9

(U)p, (D)own, (L)eft, (R)ight or (Q)uit:

R

Current location – X : 6 Y :9

(U)p, (D)own, (L)eft, (R)ight or (Q)uit:

R

Current location – X : 7 Y :9

(U)p, (D)own, (L)eft, (R)ight or (Q)uit:

R

Current location – X : 8 Y :9

(U)p, (D)own, (L)eft, (R)ight or (Q)uit:

R

Current location – X : 9 Y :9

(U)p, (D)own, (L)eft, (R)ight or (Q)uit:

R

Current location – X : 0 Y :9

(U)p, (D)own, (L)eft, (R)ight or (Q)uit:

Q

Program 2: BACK AWAY FROM THE TV! If you sat close enough to an old TV, you could see individual (R)ed, (G)reen and (B)lue (or RGB) “phosphors” that could represent just about any color you could imagine (using “additive color model”). When these three-color components are at their maximum values, the resulting color is white from a distance (which is amazing – look at your screen with a magnifying glass!). When all are off, the color results in black. If red and green are fully on, you’d get a shade of yellow; red and blue on would result in purple, and so on. For computers, each color component is usually represented by one byte (8 bits), and there are 256 different values (0-255) for each. To find the “inverse” of a color (like double-clicking your iFone® button), you subtract the RGB values from 255. The “luminance” (or brightness) of the color = (0.2126*R + 0.7152*G + 0.0722*B).

For this program, you need to design (pseudocode) and implement (source code) a Color class that has R, G and B attributes (which can be ints). The constructor should take three parameters representing the initial color of (R:254, B:2, G:100). You should include 6 setter methods to increase and decrease each component (e.g. increaseRed), not to exceed 255 or be less than 0. You should include a toString() that returns a string representing the current values for each component as well as the luminance. Finally, you should include a method that calculates and prints the inverse color.

Next, create a “driver” (or main) that creates a default color, prints its values to the screen, and enables the user to increase/decrease values as well as print the inverse. It should behave like below.

Sample run:

R:254 G:2 B:100 L:62.6508

Do you want to:

1) Increase Red, 2) Decrease Red

3) Increase Green, 4) Decrease Green

5) Increase Blue, 6) Decrease Blue

7) Print the inverse

or 8) Quit

1

R:255 G:2 B:100 L:62.8634

Do you want to:

1) Increase Red, 2) Decrease Red

3) Increase Green, 4) Decrease Green

5) Increase Blue, 6) Decrease Blue

7) Print the inverse

or 8) Quit

1

R:255 G:2 B:100 L:62.8634

Do you want to:

1) Increase Red, 2) Decrease Red

3) Increase Green, 4) Decrease Green

5) Increase Blue, 6) Decrease Blue

7) Print the inverse

or 8) Quit

7

Inverse is R:0 G:253 B:155

R:255 G:2 B:100 L:62.8634

Do you want to:

1) Increase Red, 2) Decrease Red

3) Increase Green, 4) Decrease Green

5) Increase Blue, 6) Decrease Blue

7) Print the inverse

or 8) Quit

8

R:255 G:2 B:100 L:62.8634

GUIDLINES FOR WRITING THIS PSEUDOCODE:

Simple Classes and Objects

One of the core principles you are going to learn in this course is Object-Oriented Programming (or OOP), and it involves classes and objects. What's the difference? A class is a description (or a template) of something, whereas an object is an actual instance of it. For example, a class can be thought of as a cookie cutter. However, it's not a cookie (an instance of the cookie cutter). To demonstrate this concept, let's start off with a simple example.

CLASS Dog
BEGIN
END CLASS

This is an example of the smallest (and most worthless) class you can have. Note, we don't have a dog yet, just the description of one (or the beginning of one).

If we want to bring a dog to "life", we need to do 2 things. First, you need to declare a variable of type Dog:

CREATE d1 AS Dog

The variable d1 is dead, so you need to bring it "to life" using NEW:

d1 = NEW Dog ( )

So, d1 is called an object, and technically we can have as many dogs (objects) as we want!

Next, we need to think of variables that a dog might have, and put it in the class. For example, a dog may have a name, a weight, and whether or not it is rabid. So, we would modify the class to look like:

CLASS Dog
BEGIN
    Name = "Fluffy"
    Weight = 13
    IsRabid = FALSE
END CLASS

However, there's a problem! If I create multiple dogs (d1, d2, ...), they'll all be named "Fluffy", and this is a travesty.

Constructors

The way we overcome the problem above is to create a constructor, which is a special method within a class that is called whenever we use the word NEW. Yes, NEW calls the constructor because we're creating a new object. The constructor's job is to initialize all the variables inside the class, and it is most often done through parameter passing.

Questions:

  • Do all dogs have the same name? No.
  • Do all dogs weigh the same? No.
  • Are all dogs born non-rabid? Yes.

The reason these questions are important is because it will change the way we build our constructor. Here's the new class:

CLASS Dog
BEGIN
    Name = ""
    Weight = -1
    IsRabid = FALSE
    CONSTRUCTOR Dog (parameter: NewName, NewWeight)
        Name = NewName
        Weight = NewWeight
        IsRabid = FALSE
    END CONSTRUCTOR
END CLASS
Then, we can create a few dogs, calling the constructor with different parameters:

CREATE d1, d2 AS Dog
d1 = NEW Dog ("Spike", 50)
d2 = NEW Dog ("Fluffy", 4)

Note that d1 and d2 are Dogs, and they are completely independent from one another. d1's name is Spike who weighs 50 pounds and is NOT rabid. d2's name is Fluffy who weighs 4 pounds and is NOT rabid. Why are they not rabid? Because, regardless of what we passed when we created the dogs, we "hard coded" the value of IsRabid to be FALSE. What if we wanted to make Fluffy rabid? Here we go...

d2.IsRabid = TRUE

What have we done? We now have a 4-pound rabid dog named Fluffy. Please don't let the power of programming go to your head.

Methods inside of Classes

The last thing we'll talk about is how to include methods inside of a class. Though we'll have to suspend reality for a minute, imagine our dogs can talk, giving us their name. However, if the dog is rabid, they tell us their name backwards. We might have something like:

CLASS Dog
BEGIN
    Name = ""
    Weight = -1
    IsRabid = FALSE

    CONSTRUCTOR Dog (parameter: NewName, NewWeight)
        Name = NewName
        Weight = NewWeight
        IsRabid = FALSE
    END CONSTRUCTOR

    METHOD bark()
    BEGIN
       IF (IsRabid == FALSE) THEN
          PRINT ("My name is " + Name)
       ELSE
          PRINT ("My name is " ... code to reverse Name)
       END IF
    END METHOD
END CLASS


Then, we can call those methods for both d1 and d2:

d1.bark()  // prints out "My name is Spike"
d2.bark()  // prints out "My name is yffulF"
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/ Pseudocode Information

1). Define a class named Pacman having 2 attributes, X and Y as coordinates.

2). moveUp() method --> decrement Y coordinate by 1 and then check if player goes to far-up,if so then goto far-down.

3). moveDown() method --> increment Y coordinate by 1 and then check if player goes to far-down,if so then goto far-up.

4). moveLeft() method --> decrement X coordinate by 1 and then check if player goes to far-left,if so then goto far-right.

5). moveRight() method --> increment X coordinate by 1 and then check if player goes to far-right,if so then goto far-left.

6).driver() method --> initialise X and Y as middle coordinates ( 5, 5 ) assuming 10 by 10 grid.

7). main() method -> make a class object named pac and first plot the current location

run while loop ( base condition if choice == 'q' || choice == 'Q' )

{

let the user enter his/her choice for teleportation.

depending upon choice ((U)p, (D)own, (L)eft, (R)ight or (Q)uit)player moves up, down, left,right or quit

if user enters other than these choices show error message for input.

output

}

Add a comment
Know the answer?
Add Answer to:
WRITE IN PSEUDOCODE USING THE FOLLOWING GUIDELINES Classes Program 1: WAKA, WAKA. Pac-Man was a big...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Program

    Program 1: WAKA, WAKA. Pac-Man was a big hit back in the 80s. One of the things he could do was “teleport” from one side of the screen to the other, and that’s what we’re going to implement here. For this program, you’re going to write a class that only has two attributes: an X and Y location. Imagine the player is on a 10x10 grid. If the player exceeds the bounds of the screen, their location teleports to the...

  • IN C++!! Program 2: BACK AWAY FROM THE TV! If you sat close enough to an...

    IN C++!! Program 2: BACK AWAY FROM THE TV! If you sat close enough to an old TV, you could see individual (R)ed, (G)reen and (B)lue (or RGB) “phosphors” that could represent just about any color you could imagine (using “additive color model”). When these three color components are at their maximum values, the resulting color is white from a distance (which is amazing – look at your screen with a magnifying glass!). When all are off, the color results...

  • C ++ Exercises (TV)

    Program 2: BACK AWAY FROM THE TV!  If you sat close enough to an old TV, you could see individual (R)ed, (G)reen and (B)lue (or RGB) “phosphors” that could represent just about any color you could imagine (using “additive color model”).  When these three-color components are at their maximum values, the resulting color is white from a distance (which is amazing – look at your screen with a magnifying glass!).  When all are off, the color results in black.  If...

  • Using C# Language Develop a Visual C# .NET application that performs a colour control operation. The...

    Using C# Language Develop a Visual C# .NET application that performs a colour control operation. The main form contains a reset button and sets the form's background colour according to the colour values indicated by the colour control objects. Each colour control object is controlled by a corresponding colour control form which provides a progress bar to show the value (in the range 0 to 255) of the corresponding colour control object. The user can click the increase (+) or...

  • Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class...

    Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class header instance variable UML class diagram encapsulation client visibility (or access) modifier accessor method mutator method calling method method declaration method invocation return statement parameters constructor Goals By the end of this activity you should be able to do the following: > Create a class with methods that accept parameters and return a value Understand the constructor and the toString method of a class...

  • While using JAVA , solve this. There are three basic classes we'll need this week: Card:...

    While using JAVA , solve this. There are three basic classes we'll need this week: Card: A class like the one presented in the modules, but with a few changes. Hand: A class that represents the cards held by a single player. Deck: A class that represents the source of the cards for dealing and, as the game progresses, the place from which players can receive new cards (say, as they pick cards "from the deck" or when future hands...

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

  • Write a WebGL program that displays a rotating pendulum. The pendulum bob is free to rotate...

    Write a WebGL program that displays a rotating pendulum. The pendulum bob is free to rotate through 360 degrees about an anchor point at the center of the canvas. The pendulum has the following three components. 1) The anchor point is a green square centered at the origin (0,0) with point size = 5 pixels. 2) The bob is a blue hexagon of radius r = 0.1. Render this with a triangle fan centered at the origin (along with a...

  • Game Description: Most of you have played a very interesting game “Snake” on your old Nokia...

    Game Description: Most of you have played a very interesting game “Snake” on your old Nokia phones (Black & White). Now it is your time to create it with more interesting colors and features. When the game is started a snake is controlled by up, down, left and right keys to eat food which appears on random locations. By eating food snake’s length increases one unit and player’s score increases by 5 points. Food disappears after 15 seconds and appears...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT