CSE 1321L: Programming and Problem Solving I Lab Assignment 8 – 100 points
Object Oriented Programming and Classes
What students will learn:
1) Designing classes including attributes, constructors and methods 2) Creating objects of those class types
Overview: The programs you've written up to this point have started at “main” and basically executed from "top to bottom". In the last assignment, functions/methods were introduced, where you learned that the execution of code could jump from main, to a function, then back to main. For this assignment, we're going to practice OOP, or Object-Oriented Programming. OOP is a completely different way to think about programming. The concept originated in the 1960's, but it wasn't widely adopted until the mid 1980's.
So, what is it? At its core is the concept of a "class" which is simply a grouping of variables and methods; the behavior of those methods usually change based on the state (also known as values) of the variables.
Assignment 8 A: Rare Collection. We can make arrays of custom objects just like we’ve done with ints and strings. While it’s possible to make both 1D and 2D arrays of objects (and more), for this assignment we’ll start you out with just one dimensional arrays.
You have been commissioned to develop a program to digitally record a rare collection of LaserDisc movies. You will start by creating a LaserDisc class. It should have the following private attributes:
String movieTitle
int releaseYear
String genre
float rating
Your class should also have the following methods:
Default Constructor: Initializes the four attributes to the following default values:
◦ movieTitle = “Star Wars Holiday Special”
◦ releaseYear = 1978
◦ genre = “Science Fiction”
◦ rating = 5.0
Overloaded Constructor: Initializes the four attributes based on values passed into the formal parameters
◦ If rating is less than 0.0 or greater than 5.0, set it equal to 0.0
◦ If releaseYear is less than 1978, set it equal to 1978
bool/Boolean isItGood(): Returns “true” if rating is 3.0 or greater, and “false” otherwise.
Getter method for all class attributes
You will then create a separate class, Assignment8A. In its main method, you should do the following:
Ask the user how many movies are in their collection
Create an array (of type LaserDisc) of that size
Use a loop to ask the user to enter information for all movie
◦ Create a LaserDisc object for each movie and store it in the next index in the array Then using another loop, you should give the user the following options:
Print movie information
◦ This should ask the user for a number, and then print the information from the LaserDisc object at that index. If the index is out of bounds, it should notify the user instead.
Recommend a good movie from the collection
◦ This should choose a random movie, check if it is a good movie based on the relevant function, and either print the information to the user or guess again if it was not a good movies If there are no good movies in the collection, tell the user that no good movies exist in the collection.
Hint: You can use a counter variable to keep track of the guesses. When you’ve guessed more times than numbers of elements in the array, you can stop. However, this means you might miss some movies if the same index is randomly selected multiple times. If you want to challenge yourself further, consider another way you could handle that situation.
Quit
◦ Ends the loop and the program
Sample Output:
[Laser Disc Collection]
How many movies do you have? 10 Movie 1:
*Enter Title: Back to the Future
*Enter Genre: Science Fiction
*Enter Release Year: 1985
*Enter Rating: 5.0 Movie 2:
*Enter Title: Twin Peaks Season 1
*Enter Genre: Mystery-Horror Serial Drama
*Enter Release Year: 1992
*Enter Rating: 4.5
//Keep going for all 10 movies (THIS IS NOT PART OF THE OUTPUT) [Main Menu]
1) Movie Info
2) Recommend a Good Movie 3) Log off
Choice: 1
Which Movie do you want? 100
Sorry, that’s not a valid Movie index [Main Menu]
1) Movie Info
2) Recommend a Good Movie 3) Log off
Choice: 1
Which movie do you want? 0 0. Back to the Future, 1985 Genre: Science-Fiction Rating: 5.0
[Main Menu]
1) Movie Info
2) Recommend a Good Movie 3) Log off
Choice: 2
You should try: Dune (1984)!
It has a rating of 4.1 [Main Menu]
1) Movie Info
2) Recommend a Good Movie 3) Log off
Choice: 3 Goodbye!
Assignment 8 B: Hit Boxes (Part 2). Back in Assignment 2 (so long ago!) we created a simple program to determine what a hit box would be. Now we’re going to use that information to determine if two characters would collide based on those hit boxes.
You will create a Player class that takes in the following private attributes (as integers) when creates
Width
Height
X position
Y position
In addition to Getter methods for all four attributes, the Player class should also have the following methods:
MoveHorizontal(int x_delta)
◦ Takes in either a negative integer (for moving left) or a positive integer (for moving right). It should update the X position of the Player object
MoveVertical(int y_delta)
◦ Takes in either a negative integer (for moving down) or a positive integer (for moving up). It should update the Y position of the Player object
DidTheyCollide(Player otherPlayer)
◦ Takes in another player object, and returns true if they collided with each other (and false otherwise).
To detect collisions in the DidTheyCollide() method, you will use the following equation for Axis- Aligned Bounding Box collision detection:
[PseudoCode]
if(Player1’s X Coordinate < (Player2’s Width + X Coordinate) AND (Player1’s Width + X Coordinate) > Player2’s X Coordinate AND Player1’s Y Coordinate < (Player2’s Height + Y Coordinate) AND (Player1’s Height + Y Coordinate) > Player2’s Y Coordinate) {
PRINT(“We collided!”) }
Source:
https://learnopengl.com/In-Practice/2D-Game/Collisions/Collision-detection
You will then create a second class, Assignment8B, that will operate as a driver class. It will create two Player objects based on user input, and then prompt the user to choose a Player to move. After the player moves, the program should check to see if the two Player objects have collided. The program should keep prompting the user to move a player until they collide.
Sample Output:
[Collision Tester]
Create Player 1 Enter X position: 0 Enter Y position: 0
Enter Player Hitbox Width: 10 Enter Player Hitbox Height: 10
Create Player 2
Enter X position: -10 Enter Y position: 0
Enter Player Hitbox Width: 5 Enter Player Hitbox Height: 14
Player 1 is at (0,0) and Player 2 is at (-10,0) Which one do you want to move?
1
Which direction should Player 1 move (up, down, left, or right)?
Up
How far should Player 1 move?
2
Player 1 is at (0,2) and Player 2 is at (-10,0) Which one do you want to move?
2
Which direction should Player 2 move (up, down, left, or right)?
Right
How far should Player 2 move?
12
Player 1 is at (0,2) and Player 2 is at (2,0) They collided!
Program Ends Submission:
1. You will submit two, three, or four separate files (depending on whether you put your classes in one file or two separate ones)
2. File names and class names must be correct.
3. Upload all files (simultaneously) to the assignment submission folder in Gradescope.