Question: Programming Assignment 1 Create a Java class hierarchy to represent oil volumes in barrels, gallons and pints using the UML diagrams below. Create abstract class



Programming Assignment 1 Create a Java class hierarchy to represent oil volumes in barrels, gallons and pints using the UML diagrams below. Create abstract class Volume and classes Volume 1, Volume2. Volume +toPints(): int +convert() > Comparable +compare Toother: T): int Volume 1 -barrels: int -gallons: int +Volume 10 +Volume 1(b:int, g: int) +Volume 1(v: Volume 1) +setGallons(g: int): void +setBarrels(b: int): void +getGallons(): int +getBarrels(): int +add(v: Volume 1): Volume 1 +subtract(v: Volume1): Volume 1 +toPints(): int +convert(): void +toString(): String +compare To(v: Volume 1): int Volume 2 -pints: int +Volume2() +Volume 2(b: int, g: int, p:int) +Volume2(v: Volume 2) +setPints(p: int): void +getPints(): int +add(v: Volume 2): Volume 2 +subtract(v: Volume 2): Volume 2 +toPints(): int +convert(): void +toString(): String +compare To(v: Volume 1): int . Method Notes add and subtract() are invoked as a.add(b) and a.subtract(b). Both add() and subtract() must return Volume1s or Volume2s that have been converted to Pints() - returns the this object as a measurement in pints convert() - converts this into a form where the number of gallons is greater than or equal to 0 and less than 42, number of pints is greater than or equal to 0 and less than 8 You can add extra methods if needed or exclude an inherited method that is not overridden. . . Testing Write a test file, Assign1 that does exactly the following: . . o o O o o o Declare Volume 1 references a, b, c, d, e, f, g, h Declare Volume 2 references w, x, y, z Instantiate objects (with new) as follows: a = 114 barrels, 37 gallons b = 56 barrels, 41 gallons w = 57 barrels, 38 gallons, 6 pints x = 56 barrels, 41 gallons, 7 pints c, d as Volume 1 objects (no parameters) Oy, z, e, f, g, h as Volume 2 objects (no parameters) Carry out the following calculations: (use Add and Subtract methods, no casting) C= a + b od=a-b o y = W + X O Z=W-X e = a + W of-a-w og = x + b oh = x-b Create a Volume array (a, b, c, d, e, f, g, h, w, x, y, z) Print the contents of the array Sort the array Print the contents of the array O . Print b.compareTo(x) Print x.compareTo(b) Print a in pints Print w in pints (should be negative integer) (should be positive integer) Notes 1. 2. 3. No GUI interface is necessary - display using System.out. When defining compareTo(), you should return -1 if the "this" object is less than the parameter object, return 0 if both objects are equal, and 1 if the "this" object is greater than the parameter object. Your program must be able to compare objects of the same type or different types. The method convert() is used to ensure correct format for the numbers. It should be invoked within the Volume1 and Volume2 classes, for example when adding. To sort the array, use the sort method in the Arrays class (see People example). When overriding methods in Volume2, you should use calls to the corresponding superclass methods where possible. 1 barrel = 42 gallons, 1 gallon = 8 pints. You can assume that we are not dealing with negative total volumes when instantiating objects or subtracting. 4. 5. 6