CIS 1068 Assignment 8
Warm Up with Objects
Assigned: Saturday, March 21
Due: Friday, April 3
70 points
Car (35 points)
Implement a class Car, which contains the fields (5 points):
- make, e.g. Ford, Subaru, Toyota ...
- model, e.g., Escape, Outback, Camry ...
- year
- MPG miles per gallon
- milesDriven, the total number of miles ever driven in this car.
- fuelCapacity in gallons, i.e., the size in gallons of the fuel tank.
- fuelRemaining, which represents the amount of fuel remaining in the gas tank.
Implement at least the following methods within the Car class (5 points each):
- a constructor, which initializes each of the fields
- fillTank(double g), which adds up to g gallons of gas to the fuel tank, but not more than the car's fuel capacity.
- drive(double m), which simulates driving m miles in the car, adding to the total number of miles driven, and reducing the amount of gas in the car according to this car's average MPG.
- toString( ), which returns a String representation of the car.
- getFuelRemaining( ), which returns the amount of fuel left in the tank.
For example, we should be able to do something like the following:
Car oldJunker = new Car("Ford", "Pinto", 1972, 17.5, 132480, 12, 8); // creates a new Car object
oldJunker.drive(5); // drives the Car 5 miles
oldJunker.fillTank(1); // put in a gallon of gas
System.out.println(oldJunker.getFuelRemaining()); // prints the amount of fuel left
System.out.println(oldJunker); // prints the attributes of the car to the screen
Write a short driver program to test your Car class with a short
array of Cars (5 points).
YouTube video (35 points)
Create a class that models a YouTube video. It should contain the following fields:
- title – the title of the video
- uploader – the name of the person or channel who uploaded the video
- views – the number of times the video has been viewed
- likes – the number of likes
- dislikes – the number of dislikes
- duration – the length of the video in minutes
- String comments – a single string holding all comments, separated by new lines
Implement at least the following methods:
- A constructor that accepts values for title, uploader, and
duration. All other fields are initialized to default values.
- play() – simulates playing. prints a message like “Now playing: [title]”
- like() – increases the like count by 1
- dislike() – increases the dislike count by 1
- addComment(String comment) – appends the comment to the comments string followed by a newline
- toString() – which returns a String representation of the video. What's in the string is up to you, but it could be something like:
Title: My Favorite Sandwiches
Uploader: fudluvr22
Duration: 58.5 minutes
Views: 2
Likes: 1
Dislikes: 0
Comments:
@Jimmy1776 Captivating. Can't wait for the sequel
@TheOneAndOnlyCamille Try the pepper jack. You can't go wrong
Create a short driver progam (YouTubeVideoTest) that
- Creates an array of YouTubeVideo objects
- Calls several methods to simulate interaction (e.g., views, likes, comments)
- Prints out the video information using toString()
Note about files
There are several possible ways to organize your files for this assignment. The simplest and recommended way would be that you have four files in two different Eclipse projects:
- Car.java - contains a Car class definition (i.e., what we've been calling a blueprint or a cookie cutter)
- CarMain.java - contains your test program
- Vid.java - contains a Vid class definition
- VidMain.java - contains a test program
what to submit
Please upload your .java files to Canvas.
It's a good idea to confirm through the Canvas submission page that
what you've intended to submit was uploaded. We will grade what you
submit. If you submit a corrupted, empty, or otherwise incorrect file,
this is what we'll grade. It is your responsibility to verify through
the Canvas submission page that you've submitted the correct files and
that they were uploaded properly.
If you're unsure about how to submit, please consult Canvas'
tutorial or see us for help.