Java Assignment
Assignments/Extra Assignment 2.pdf
©Vickram Sawh Page 1 Java – Level II Assignments
©Vickram Sawh Page 1 Java – Level II Assignments
Extra Assignment 2
Create a class named Car, which is supposed to represent cars within a Java program. The
following are the attributes of a typical car:
- year which indicates the year in which the car was made. - price indicating the price of the car
The year attribute is an integer, whereas the price is a double value.
The year can only lie between 1970 and 2011. The price can be any value between 0 and 100000. The class Car should be constructed in such a way that any attempt to place an
invalid value in the year and/or price, will result in the attribute being set to its lowest
possible value.
Create the class, which contains the necessary attributes and setter/get ter methods: setYear( ), getYear( ), setPrice( ) and getPrice( ). Test all of its public members, which
in this case only represents the setter/getter methods. The instructor will provide his/her
own testing code to verify that the class is functioning pro perly.
Due-date: As indicated by the instructor.
Assignments/Extra Assignment 3.pdf
©Vickram Sawh Page 1 Java – Level II Assignments
©Vickram Sawh Page 1 Java – Level II Assignments
Extra Assignment 3
Amend the class Car as follows:
Add a default constructor, which sets the year and price attributes to 1970 and 0
respectively.
Add an overloaded constructor, which accepts two parameters. The parameters
are used to set the year and price members of the newly created object.
Add a copy constructor, which accepts a reference to a Car object as parameter.
This constructors initializes the year and price members of the newly created
object, to values matching those of the Car parameter.
After adding these new members to the class, add new testing code to test them. Important:
you must NOT remove the old testing code, because sometimes adding new items to the class might “break” things in the class which used to work!
Due-date: As indicated by the instructor.
Assignments/Extra Assignment 4.pdf
©Vickram Sawh Page 1 Java – Level II Assignments
©Vickram Sawh Page 1 Java – Level II Assignments
Extra Assignment 4
Implement the following changes to the class Car:
The setter methods will throw an exception if the client attempts to set the encapsulated (private) variables to invalid values. For now, the exception object
being thrown will be instantiated from the Exception class.
The constructor(s) which call the setter methods, should be made to ignore the
possible exception(s) the setters can throw, and pass them to the client code which called them.
After making the above changes, test your class by calling the setter and constructor, purposely sending invalid parameter values, and checking to see whether they throw
exceptions.
Due-date: As indicated by the instructor.
Assignments/Extra Assignment 5.pdf
©Vickram Sawh Page 1 Java – Level II Assignments
©Vickram Sawh Page 1 Java – Level II Assignments
Extra Assignment 5
Make the following changes to the class Car:
Create your own exception class, named CarException, which inherits the
Exception class. This class should contain a private String member message,
which eventually will store an error message. Also, the CarException class should define a one-parameter constructor. The parameter is used to populate the private
member message. Finally, the class CarException defines a getMessage( ) method, which returns the error message stored in the member message.
The setter methods of the class Car should be updated to throw an exception object
of type CarException, instead of Exception. For example, the setYear( ) method will create and throw a CarException object if it determines that its int parameter
does not represent a valid year. In creating the CarException object, the string
"Invalid Year" is passed to the CarException class’ constructor, in order to be passed into the object’s message member. The setPrice( ) method should act in a
similar fashion.
Create a toString( ) method for the class Car, which displays the year and price of
the current Car object onto the screen. The following code:
Car x = new Car( 2001, 15000); System.out.println("Car x is created. Its stats: " + x + ".");
would produce the following screen output:
Car x is created. Its stats: [Year:2001,Price:15000].
Create a finalize( ) method, which does nothing, except to print the diagnostic
statement "The finalize method called." onto the screen.
After making these changes, add client testing code which tests all of the additions
indicated above. It is important that the previous testing code is included with the new tests, because the programmer should make sure that the new additions do not
negatively affect that which worked in the past!
Due-date: As indicated by the instructor.
Assignments/Extra Assignment 6.pdf
©Vickram Sawh Page 1 Java – Level II Assignments
©Vickram Sawh Page 1 Java – Level II Assignments
Extra Assignment 6
Prequisites:
- Static members
- Application of Static members: keep track of object count.
Modify the Car class as follows:
Add two attributes named make and vin to the class. Both attributes are String
variables. The make represents the manufacturer of the car, for example Ford or GM. The vin represents the vehicle's VIN number, which can be any combination of
letters and numbers. A make or vin is invalid if they are "empty" strings. Any
attempt to set the make an empty string, will result in a CarException exception
object being thrown, containing the error message "Invalid Make". Likewise, any attempt to set the vin to an empty string, will result in a CarException exception
object being thrown containing the string "Invalid Vin".
Add the necessary setter/getter methods to allow the client to indirectly access the make and vin variables. Remember that these setter methods should have a throws statement in their headers.
Update the existing constructors to reflect the addition of the make and vin
attributes. For example, the overoaded constructor will now accept four parameters (instead of two): an int parameter representing the year of the car, a double
prarameter representing the price, and two additional String parameters
representing the two newly added attributes: make and vin. The overloaded and copy constructor should be updated as well. The toString( ) method should also be
updated to reflect the two new fields.
Change the existing testing code to reflect the changed state of the constructors.
Append testing code to test the newly added setter/getter methods.
Create a private static member named Count which keeps track of the number of
Car objects in existence. Update the constructors / finalize method to increment/decrement Count. Provide for a method getCount( ) which returns the
value of Count to the client code.
To accurately test the ability of your class to track the number of Car objects,
getCount( ) should return an increasing value each time an object is created, but a
decreasing number when an object is deleted. Creating objects is easy to do: just call the new statement. However, objects are deleted invisibly, only if no references point to them, or if their block ends. Therefore, to test the getCount( ), use the
following methodoly: Make a couple of objects in the client code, calling getCount( )
after each instantiation, to see if it returns an accurate count. Then make your client code call a method. Inside this second method, create several additional Car objects.
Of course, within the method, you would call getCount( ) after each object creation.
When this second method finishes, all objects created in it are erased from memory.
©Vickram Sawh Page 2 Java – Level II Assignments
©Vickram Sawh Page 2 Java – Level II Assignments
Therefore, upon return to your original client code, the getCount( ) should report
the same number of objects as before it was exited.
Assignments/ExtraAssignment7.pdf
©Vickram Sawh Page 1 of 4 Java – Level II – Extra Assignment 7
©Vickram Sawh Page 1 of 4 Java – Level II – Extra Assignment 7
Extra Assignment 7
Pre-requisites:
- Composition - Overridding the methods of the class Object:
toString( ) finalize()
equals( )
clone( )
Write the classes, which manage the inventory of a shoe store. The shoe store sells different types of shoes. Each type of shoe has a unique string id, however, every "shoe type" comes
in different colors and sizes. It is important to keep track of the number of pairs in
inventory of each type of shoe.
This assignment is different than the previous assignments. The instructor will forward you the "testing code", and the output it should produce. You are supposed to write the 4
classes necessary for the project.
The Shoe class identifies shoe objects, whereby a shoe is defined by its id, size and color.
The ShoeType class is composed of the Shoe class, with the additional attribute quantity,
which identifies the number of shoes of a particular type in inventory. The store's inventory is managed by a third class ShoeInventory, which internally contains an array of
ShoeType objects.
So, if the store contains:
12 pairs of size 7, black shoes of id 'Y128A'
3 pairs of size 6, brown shoes of id 'MQ43T'
44 pairs of size 6 ½ black shoes of id 'MQ43T' then the object of class ShoeInventory would contain a ShoeType array containing the following information: (Note that color black is represented by integer 1, and brown is 2)
a ShoeType array
………………………
12
3
44
Y128A
7
1
MQ43T
6
2
MQ43T
6.5
1
©Vickram Sawh Page 2 of 4 Java – Level II – Extra Assignment 7
©Vickram Sawh Page 2 of 4 Java – Level II – Extra Assignment 7
The blocks shown in black are ShoeType objects, whereas the blocks in red represent
Shoe objects. It is not difficult to see that a ShoeType object is composed of Shoe objects.
In this project, you will create 4 classes: ShoeException, Shoe, ShoeType and
ShoeInventory.
The ShoeException class defines exception objects to be thrown by the other classes in the
project. It contains: a private String member (which represents an error message encapsulated within
the exception object) a constructor which accepts a String parameter, used to populate the internal
String member a toString( ) method which returns a copy of the internal String member.
The Shoe class defines shoe objects, which will be embedded within ShoeType objects. A shoe is defined by its id, size and color, respectively a String, a double and an int. It
contains:
the private id, size and color members. The id cannot be an empty string. The size
cannot be less than 1.0. The color cannot be less than 1. one constructor which accepts a String, double and int parameter, which it uses to
construct the Shoe object. It may throw a ShoeException exception.
The setter/getter methods named setId( ), setSize( ), setColor( ), getId( ),
getSize( ) and getColor( ). Each of the setter methods can throw a ShoeException exception, with an embedded error message of : Invalid xxx,
whereby xxx can be the word "id", "size" or "color".
a toString( ) method which returns a string in the format of:
"id:id,size:size,color:color"
The size is formatted with one decimal. an equals( ) method. Two Shoe objects are equal if their corresponding id, size and
color fields match. a clone( ) method which produces a duplicated Shoe object
a finalize( ) method which prints on the screen the diagnostic statement:
"The shoe with id id and size of size, and color of color will now be deleted."
(id, size and color are to be substituted with the id, size and color of the current
object)
the necessary facilities for keeping track of the number of Shoe objects in existence,
including a getCount( ) method, which returns the current number of Shoe objects.
The ShoeType class identifies a particular type of shoe, including the number of pairs of it
there are in inventory. It defines the following members: a private Shoe and int members. The int member represents the quantity of the
shoe (identified by the Shoe member) in inventory.
a setQuantity( ) and getQuantity( ) methods, for respectively changing or
reporting the quantity member. The setQuantity( ) may throw a ShoeException
exception.
©Vickram Sawh Page 3 of 4 Java – Level II – Extra Assignment 7
©Vickram Sawh Page 3 of 4 Java – Level II – Extra Assignment 7
a getShoe( ) method which returns the Shoe object embedded within the current
ShoeType object.
two non-default constructors. The first accepts a Shoe object and an integer representing the number of pairs of the ShoeType objects in inventory. The second
constructor accepts 4 parameters: a String, a double, and two ints. The first three
parameters will be used to construct a Shoe object, whereas the last int parameter indicates how many pairs of that Shoe object there are in inventory. Both
constructors can throw a ShoeException exception.
a toString( ) method, which combines the String returned by its Shoe member,
with the contents of the quantity member, to return the following String:
"id:id,size:size,color:color,quantity:quantity"
an equals( ) method which compares two ShoeType objects for equality
a clone( ) method which produces a duplicate of the current ShoeType object
The ShoeInventory class, contains an internal ShoeType array which represents the
inventory of the store. This class contains the following members: a ShoeType array, a counter variable (representing the number of cells used in the
array), and an int variable representing the number of cells the array should have. a singular constructor of the ShoeInventory, which accepts an integer variable,
representing the number of cells the internal array should have. The constructor copies its parameter value in the private int variable representing the internal
array's size. It also sets the counter to zero, and sets the size of the int ernal array. an isFull( ) method, which returns true if the internal array is full, and false if not.
an isEmpty( ) method, which returns true if the internal array is empty, and false
if not. a getCapacity( ) method, which returns the size of the internal array.
a getNumItems( ) method, which returns the number of items store in the internal
array. a find( ) method. This method accepts a reference to a Shoe object as parameter. It
scans the internal ShoeType array and returns an integer representing the index of
the ShoeType object, whose "Shoe" portion matches its Shoe parameter. If the Shoe object was not found, the find( ) method returns -1. If the internal array is
empty, then find( ) returns -3. If a null parameter is passed as argument into
find( ), it returns -2. These return values were deliberately chosen: a negative
return value means an error; a return value of 0 or more represents the index at which the Shoe item was found. The find( ) will scan the items in the internal array, from index 0 up to (but not including) counter, looking for the target Shoe
object. Within the loop the equals( ) method of the Shoe class should be used to
locate the target object (if present). an add( ) method which accepts a reference to a ShoeType object as parameter.
This ShoeType object is not added to the internal array if it is full, or if the shoe
type being added is already in the array. Note that two shoe types in the internal
array cannot have the same combination of id, size and color. (The quantity does not matter, when doing this determination) The add( ) method throws a
ShoeException exception, if the internal array is full or an attempt is made to
©Vickram Sawh Page 4 of 4 Java – Level II – Extra Assignment 7
©Vickram Sawh Page 4 of 4 Java – Level II – Extra Assignment 7
store a duplicate shoe type into it. Be aware that add( ) will call upon isFull( ) and
find( ) methods. Do not forget to increase the counter by one in the add( ) method. a delete( ) method, which removes a ShoeType object from the internal array. It
takes an integer as argument, representing the index of the ShoeType item to be
removed. This method throws a ShoeException if the array is empty, or if the
indicated index is not used. (Remember: the "used" portion of the array lies between
the cell with index 0 and counter – 1). The deletion occurs by shifting all objects to
the right of the target object, one place to the left. This prevent s the removal from creating "gaps" in the internal array. Of course, the counter should be decremented as well. The delete( ) method will rely on the isEmpty( ) method. The delete( )
method returns a reference to the ShoeType object being deleted. a toString( ) method which prints the items stored in its internal array. It will need
to repeatedly call the toString( ) method for each of the stored ShoeType objects.
The string returned by toString( ) should, when printed on the screen, has the
format:
Item 1: id:RR215,size:6.5,color:1,quantity:2 Item 2: id:MR197,size:4.0,color:2,quantity:2 Item 3: id:YX231,size:8.5,color:1,quantity:2 : :
an equals( ) method which checks to see if two ShoeInventory objects are
identical. a clone( ) method which allows for one ShoeInventory object to be cloned.
Create the 4 classes, combine them with the client code provided by the instructor, and run
the project. Your output should match the output the instructor will give you.
Due-date: As indicated by prof. Sawh