Creating Objects Practice

posted by: Mr. Bergquist 25 February 2012 No Comment

You will now create two Object Classes of your own, for a Car and Student – Pick which one you want to try first, but please do BOTH.  You can reference our Object Class Concepts Sheet & Object Class Review Slides or for more details see our recent presentations on: Object Encapsulation, ToString method & this keyword , Arrays of Objects & Null , and Adding Exceptions.

1) Car Class:

Implement a class Car with the following properties (fields) and behavior (methods):

  • fuel efficiency (measured in miles/gallon or liters/km — pick one and make sure to specify it in your comments)
  • current mileage
  • fuel tank capacity (again, pick your units)
  • current fuel level
  • constructor that allows the user to set efficiency, mileage and tank capacity. Fuel level should be set to 0.  Throw IllegalArgumentException as appropriate.
  • drive method that simulates driving for a certain distance, reducing the gasoline in the fuel tank (you may assume that drive is never called with a distance that consumes more than the available gas)
  • getGasInTank method that returns the current amount of fuel
  • addGas method that adds gas to the tank and will throw an IllegalArgumentException if it exceeds capacity.

Sample usage:
Car myHybrid = new Car(50, 20000, 15); // 50 miles per gallon, 20,000 miles and 15 gallon tank (no gas in tank)
myHybrid.addGas(15); // pump 15 gallons of gas
myHybrid.drive(100); // drive 100 miles
double gasLeft = myHybrid.getGasInTank(); // get gas remaining in tank
Create a Client Class that tests all of your methods of your Object Class by creating a couple of different cars. Make sure you test all cases (ex: adding more gas than the tank has space for).  Be prepared to demonstrate how you tested the cases with your Client program and explain your Object Class details to me.

2) Student Class:

In a system like The Source, student information is stored in a database.  Every time student information is requested, an object is most likely built from the database information.  We’re going to design and build a Student class.

This is a little unusual (as in it’s probably not the kind of thing your boss would ask you to do), but should serve as good practice — I am giving you the client code from which you need to write the class from which to build appropriate objects.  Download StudentGrades.java and write Student.java from it.  You may want to comment out certain parts of StudentGrades.java in order to test one piece of your class at a time.  Note that grades are stored in an array initialized in the constructor, pay close attention how to size the Array, see slide 6 in the Arrays of Objects & Null presentation (hint, hint).

It should never be possible for a student to have a negative score.  In order to enforce this, you need to make sure no one can break encapsulation (Object Encapsulation) and change a field directly… you should also throw exceptions (Adding Exceptions) as needed (i.e. entering a negative score – I will try it).

When you are done simply run the StudentGrades.java and call Mr. Bergquist over to test it out.

1 Star2 Stars3 Stars4 Stars5 Stars (17 votes, average: 3.47 out of 5)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>