// Jeff Marinko (jeff@vhub.com) // Example Solution for Wockets, Wackets, and Widgets Lab (CSC 101) public class WidgetApp { // comment public static int randomInt(int startRange, int endRange) { int range = (endRange - startRange) + 1; // generate number in the range // need to cast to get an int again range = (int)(Math.random() * range); // add the start of the range to it range += startRange; return range; } // comment public static double randomDouble(double startRange, double endRange) { double range = (endRange - startRange) + 1; range = Math.random() * range; range += startRange; return range; } // comment public static Wocket randomWocket() { int ranCount = randomInt(-5, 25); return new Wocket(ranCount, ranCount * randomDouble(-1.0, 10)); } // comment public static Wacket randomWacket() { int ranCount = randomInt(-5, 25); return new Wacket(ranCount, ranCount * randomDouble(-1.0, 10)); } // comment public static Widget randomWidget() { int ranCount = randomInt(-5, 25); int ranCount2 = randomInt(-5, 25); return new Widget(ranCount, ranCount * randomDouble(-1.0, 10), ranCount2, ranCount2 * randomDouble(-1.0, 10)); } public static void main(String args[]) { Wocket wocket1, wocket2; Wacket wacket1, wacket2; Widget widget1, widget2; // create random objects wocket1 = randomWocket(); wocket2 = randomWocket(); wacket1 = randomWacket(); wacket2 = randomWacket(); widget1 = randomWidget(); widget2 = randomWidget(); // print all objects System.out.println("Wacket1: " + wacket1); System.out.println("Wacket2: " + wacket2); System.out.println("Wocket1: " + wocket1); System.out.println("Wocket2: " + wocket2); System.out.println("Widget1: " + widget1); System.out.println("Widget2: " + widget2); // add wacket2 to wacket1 wacket1.add(wacket2); System.out.println("\nSum of Wacket1 + Wacket2: " + wacket1); // add wocket2 to wocket1 wocket1.add(wocket2); System.out.println("\nSum of Wocket1 + Wocket2: " + wocket1); // widget + wacket widget1.add(wacket1); System.out.println("\nSum of Widget1 + Wacket1: " + widget1); // widget + wocket widget2.add(wocket1); System.out.println("\nSum of Widget2 + Wocket1: " + widget2); // widget + widget widget1.add(widget2); System.out.println("\nSum of previous 2 values: " + widget1); } } /*Example Run: Wacket1: Wacket: 9 male gremlins, 78.52 weight Wacket2: Wacket: 6 male gremlins, 17.95 weight Wocket1: Wocket: 13 female gremlins, 94.35 weight Wocket2: Wocket: 6 female gremlins, 53.16 weight Widget1: Widget: 9 male gremlins, 46.96 weight, 8 female gremlins, 32.73 weight Widget2: Widget: 19 male gremlins, 165.79 weight, 4 female gremlins, 9.10 weight Sum of Wacket1 + Wacket2: Wacket: 15 male gremlins, 96.47 weight Sum of Wocket1 + Wocket2: Wocket: 19 female gremlins, 147.51 weight Sum of Widget1 + Wacket1: Widget: 24 male gremlins, 143.43 weight, 8 female gremlins, 32.73 weight Sum of Widget2 + Wocket1: Widget: 19 male gremlins, 165.79 weight, 23 female gremlins, 156.61 weight Sum of previous 2 values: Widget: 43 male gremlins, 309.23 weight, 31 female gremlins, 189.33 weight */