Question: Write a test file, Assign1 that does exactly the following in Java: Declare Volume1 references a, b, c, d, e, f, g, h Declare Volume2

Write a test file, Assign1 that does exactly the following in Java: Declare Volume1 references a, b, c, d, e, f, g, h Declare Volume2 references w, x, y, z Instantiate objects (with new) as follows: o a = 114 barrels, 37 gallons o b = 56 barrels, 41 gallons o w = 57 barrels, 38 gallons, 6 pints o x = 56 barrels, 41 gallons, 7 pints o c, d as Volume1 objects (no parameters) o y, z, e, f, g, h as Volume2 objects (no parameters) Carry out the following calculations: (use Add and Subtract methods, no casting) o c = a + b o d = a b o y = w + x o z = w x o e = a + w o f = a w o g = x + b o h = 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

Print b.compareTo(x) (should be negative integer) Print x.compareTo(b) (should be positive integer) Print a in pints Print w in pints

public abstract class Volume { public abstract int toPints(); public abstract void convert(); }

public class Volume1 extends Volume implements Comparable{ private int barrels; private int gallons; /** * Default constructor */ public Volume1() { this.barrels = 0; this.gallons = 0; }

/** * Constructor * @param b * @param g */ public Volume1(int b, int g) { this.barrels = b; this.gallons = g; } /** * Copy constructor * @param v */ public Volume1(Volume1 v) { this.barrels = v.barrels; this.gallons = v.gallons; }

/** * @param b the barrels to set */ public void setBarrels(int b) { this.barrels = b; }

/** * @param g the gallons to set */ public void setGallons(int g) { this.gallons = g; }

/** * @return the barrels */ public int getBarrels() { return barrels; }

/** * @return the gallons */ public int getGallons() { return gallons; } /** * * @param v * @return a.add(b) */ public Volume1 add(Volume1 v) { Volume1 newVolume = new Volume1(); newVolume.setGallons(this.gallons + v.gallons); newVolume.setBarrels(this.barrels + v.barrels); newVolume.convert(); return newVolume; } /** * * @param v * @return a.subtract(b) */ public Volume1 subtract(Volume1 v) { Volume1 newVolume = new Volume1(); if (this.gallons >= v.gallons) { newVolume.setGallons(this.gallons - v.gallons); newVolume.setBarrels(this.barrels - v.barrels); } else { // borrow gallons from barrels newVolume.setGallons((this.gallons + 42) - v.gallons); newVolume.setBarrels((this.barrels-1) - v.barrels); } newVolume.convert(); return newVolume; }

/** * converts gallons into pints */ @Override public int toPints() { // 1 barrel = 42 gallons and 1 gallons = 8 pints int totalGallons = (this.barrels * 42) + this.gallons; return totalGallons * 8; }

@Override public void convert() { // converts this into a form where the number of //gallons is greater than or equal to 0 and less than 42 if(gallons >= 42) { // increase the barrels barrels += gallons / 42; gallons = gallons % 42; } }

@Override public int compareTo(Volume1 o) { if(this.toPints() > o.toPints()) { return 1; } else if(this.toPints() < o.toPints()) { return -1; } else { return 0; } }

@Override public String toString() { return "Barrels: " + barrels + ", Gallons: " + gallons; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!