Question: To get started, open IntelliJ and create a new java project named asg 6 ( be sure to put it in your ist 2 4

To get started, openIntelliJand create a new java project namedasg6(be sure to put it in your ist242/assignmentsdirectory).
Part 1: Project Setup
Your project should adhere to the usual model, view, and controller package structure.
I.e.: you should have a root 'edu.psu.ist' package, and within that, an App class housing a main() method along with three (initially empty) sub-packages: model, view, and controller.
Part 2: An 'Orderable' PuzzleBox
Add a class to the model package called PuzzleBox.
The PuzzleBox class should be parameterized by some type T that extends Comparable.
The class should store the following fields:
List items (make it an array-list or linked list -- choose whichever you conclude will be faster for this asg)
Use appropriate access modifiers for the fields. Next, supply a constructor that accepts no parameters and initializes the items list.
PuzzleBox Methods:
The puzzlebox class should offer the following public methods (method names marked with a * must be implemented recursively). None of the methods should be marked static.
(Tip: do these one at a time -- use a Tester class in the model pkg to test them out as you go)
addItemsTo(..)-- takes a List as a parameter, returns nothing, and adds the passed items to this.items.
toString()-- override the toString method such that puzzlebox instances are rendered like so
// in a Tester.java main()
PuzzleBox box = new PuzzleBox<>();
box.addItemsTo(List.of(4,3,0,1));
System.out.println(box); //4301
//^ beware of your toString leaving trailing whitespace after the last element
*clear()-- takes nothing, returns nothing, recursively clears out the internal this.items list.
*numOfItems()-- takes nothing, returns an int, and recursively computes the number of items in this.items.
sort()-- sorts the contents of the puzzlebox; this method should mutate this.items list; you can implement any of the following sorting algorithms:
(easier)- implement the following algorithm and (in a comment near the method: give the big-O runtime and sentence or two of justification)
while this.contents is not sorted:
for each adjacent pair of elements in this.contents:
if the pair is not sortedthen swap its elements.
(harder)- translate Horstmann's array-based version of mergeSort(..) presented in the sorting chapter of the course text into a java.util.List based version.
Note: you can use Collections.swap(..) method included with java to swap two items in a list (read about this online for examples, etc.)
*inDoubleOrder()-- recursively determines if this.items is sorted in ascending double order; returns a boolean containing true if it is, false otherwise.
You should use a helper method for this one that takes a List as a parameter. Some pseudocode-ish examples for your tester (using the helper method):
box.inDoubleOrder([0,011])// true
box.inDoubleOrder([0,1,2,2]); // false
box.inDoubleOrder([a, a]);// true
box.inDoubleOrder([a, b]);// false
box.inDoubleOrder([a]);// false
box.inDoubleOrder([0,0,0]);// false
box.inDoubleOrder([1,1,1,1]); // true
box.inDoubleOrder([3,3,0,0]); // false
box.inDoubleOrder([0,0,3,3]); // true
box.inDoubleOrder([1,2,1,2]); // false
randomlyPopulate(..)-- Clears and populates the box with 8 randomly generated items. For this method you'll need the interface described below (add it to the model package):
write an interface, IProducer, that is parameterized by some type U and exports a single abstract method: produce() with a return type of U.
this interface will function as 'generator' of elements of type U; the interface itself should only be implemented as a lambda expression.
Here's the method signature + some pseudocode you'll need for the randomlyPopulate method:
public void randomlyPopulate(IProducer itemProducer){
// clear this.items
// iterate from 0 upto 8
// T generatedItem = itemProducer.produce();
// items.add(generatedItem);
}
As an example, here's how randomlyPopulate can be called from the main to (randomly) generate either a smiley or shocked face:
// in Tester
PuzzleBox faceBox = new PuzzleBox<>();
faceBox.addItemsTo(List.of("cat", "dog", "fish"));
IProducer faceStrGenerator =()->{
Random rand = new Random(); // declare a random num generator
if (rand.nextInt(0,2)==0){// generates a random number between 0..1(inclusive)
return ":-0";
} else {
return ":-)";
}
};
// clears, then fills the puzzlebox with 8 diff faces
faceBox.randomlyPopulate(faceStrGenerator);
System.out.println(faceBox); // :-0 :-0 :-) :-0 :-0 :-) :-0 :-0

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 Programming Questions!