Note that colors from C can be used more than once and that not all colors in C must be used. An example of a solid cube over the set of colors C = {Red, Green, Blue} is:
Top - Green Front - Green Right - Blue
Back - Green Left - Blue Bottom - Green
Notice that the color Red is not used in this example. Also assume that, before painting, each face of the cube is indistinguishable from each of the other five faces (e.g. ignore the wood-grain on the cube faces in the above image). Since what which cube faces we call Top, Front, Right, Back, Left, and Bottom above is arbitrary, notice that another representation of the same cube is:
Top - Green Front - Blue Right - Green
Back - Blue Left - Green Bottom - Green
Furthermore, we will define two cubes to be equal if, and only if, one cube can be physically reoriented so that the two cubes look identical to a human observer. So, there is only one solid cube over the set of colors C = {Red, Blue} with exactly one Blue face, namely:
Top - Red Front - Red Right - Red
Back - Blue Left - Red Bottom - Red
All other cubes with one Blue face are equivalent to this one since, given a cube with exactly one Blue face, we can reorient this cube so that the Back face is Blue
You are to write code which, given a set of colors C, will compute the set of pairwise distinct (i.e., non-equal) solid cubes over the set C. In particular, write a method with the following signature:
public static Set<Cube> getDistinctSolidCubes(Set<Color> colors);
Please, use all the files provided to write and fix the code that works, thanks!
Here is the interface:
package model;
import java.util.Set;
public interface Cube {
public Set<CubeNet> getAllPossibleCubeNets();
}
Here is the CubeImpl file, please, fix and complete it:
package model;
import java.util.HashSet;
import java.util.Set;
public class CubeImpl implements Cube
{
private CubeNet representativeNet;
public CubeImpl (CubeNet cubeNetRepresentative)
{
assert cubeNetRepresentative != null : "cubeNetRepresentative is null!";
this.representativeNet = cubeNetRepresentative;
}
@Override
public Set<CubeNet> getAllPossibleCubeNets()
{
Set<CubeNet> allNets = new HashSet<CubeNet>();
allNets.add(this.representativeNet);
// Here you can generate all the possible nets for the cube by
// rotating the representative net along its different axes and
// adding each resulting net to the set of all nets.
// You can use the different methods available in the CubeNet class
// to perform the rotations and generate the new nets.
return allNets;
}
}
package model;
another interface:
import java.awt.Color;
public interface CubeNet {
public Color getTop();
public Color getFront();
public Color getRight();
public Color getBack();
public Color getLeft();
public Color getBottom();
}
package model;
import java.awt.Color;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class CubeNetImpl implements CubeNet
{
private Color top;
private Col.
Note that colors from C can be used more than once and that not all co.docx
1. Note that colors from C can be used more than once and that not all colors in C must be used. An
example of a solid cube over the set of colors C = {Red, Green, Blue} is:
Top - Green Front - Green Right - Blue
Back - Green Left - Blue Bottom - Green
Notice that the color Red is not used in this example. Also assume that, before painting, each
face of the cube is indistinguishable from each of the other five faces (e.g. ignore the wood-grain
on the cube faces in the above image). Since what which cube faces we call Top, Front, Right,
Back, Left, and Bottom above is arbitrary, notice that another representation of the same cube is:
Top - Green Front - Blue Right - Green
Back - Blue Left - Green Bottom - Green
Furthermore, we will define two cubes to be equal if, and only if, one cube can be physically
reoriented so that the two cubes look identical to a human observer. So, there is only one solid
cube over the set of colors C = {Red, Blue} with exactly one Blue face, namely:
Top - Red Front - Red Right - Red
Back - Blue Left - Red Bottom - Red
All other cubes with one Blue face are equivalent to this one since, given a cube with exactly one
Blue face, we can reorient this cube so that the Back face is Blue
You are to write code which, given a set of colors C, will compute the set of pairwise distinct
(i.e., non-equal) solid cubes over the set C. In particular, write a method with the following
signature:
public static Set<Cube> getDistinctSolidCubes(Set<Color> colors);
Please, use all the files provided to write and fix the code that works, thanks!
Here is the interface:
package model;
import java.util.Set;
public interface Cube {
public Set<CubeNet> getAllPossibleCubeNets();
}
Here is the CubeImpl file, please, fix and complete it:
package model;
import java.util.HashSet;
import java.util.Set;
public class CubeImpl implements Cube
2. {
private CubeNet representativeNet;
public CubeImpl (CubeNet cubeNetRepresentative)
{
assert cubeNetRepresentative != null : "cubeNetRepresentative is null!";
this.representativeNet = cubeNetRepresentative;
}
@Override
public Set<CubeNet> getAllPossibleCubeNets()
{
Set<CubeNet> allNets = new HashSet<CubeNet>();
allNets.add(this.representativeNet);
// Here you can generate all the possible nets for the cube by
// rotating the representative net along its different axes and
// adding each resulting net to the set of all nets.
// You can use the different methods available in the CubeNet class
// to perform the rotations and generate the new nets.
return allNets;
}
}
package model;
another interface:
import java.awt.Color;
3. public interface CubeNet {
public Color getTop();
public Color getFront();
public Color getRight();
public Color getBack();
public Color getLeft();
public Color getBottom();
}
package model;
import java.awt.Color;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class CubeNetImpl implements CubeNet
{
private Color top;
private Color front;
private Color right;
private Color left;
private Color bottom;
private Color back;
private Set<Map<Face, Color>> allPossibleMaps = null;
private Integer hashCode = null;
private static int numberCallsToEqual = 0;
private static int numberCallsToAdd = 0;
private static int numberCallsToHashCode = 0;
private static long timeSpentEqual = 0;
private static long timeSpentHashCode = 0;
private static long timeSpentInSetAddMillis = 0;
private static boolean CALCULATE_POSSIBLE_MAPS_ONE_TIME = false;
private static boolean CALCULATE_HASHCODE_ONE_TIME = false;
4. private static int HASHCODE_LEVEL = 1;
public CubeNetImpl (Map<Face, Color> faceToColorMap)
{
assert faceToColorMap != null : "faceToColorMap is null!";
top = faceToColorMap.get(Face.TOP);
front = faceToColorMap.get(Face.FRONT);
right = faceToColorMap.get(Face.RIGHT);
left = faceToColorMap.get(Face.LEFT);
bottom = faceToColorMap.get(Face.BOTTOM);
if (CALCULATE_POSSIBLE_MAPS_ONE_TIME) {
long startTime = System.currentTimeMillis();
allPossibleMaps = generateAllPossibleMaps();
long endTime = System.currentTimeMillis();
timeSpentInSetAddMillis += endTime - startTime;
CALCULATE_POSSIBLE_MAPS_ONE_TIME = false;
}
if (CALCULATE_HASHCODE_ONE_TIME) {
long startTime = System.currentTimeMillis();
hashCode(HASHCODE_LEVEL);
long endTime = System.currentTimeMillis();
timeSpentHashCode += endTime - startTime;
CALCULATE_HASHCODE_ONE_TIME = false;
}
}
@Override
public Color getTop()
{
return top;
}
@Override
public Color getFront()
{
return front;
}
@Override
public Color getRight()
{
return right;
}
5. @Override
public Color getBack()
{
return back;
}
@Override
public Color getLeft()
{
return left;
}
@Override
public Color getBottom()
{
return bottom;
}
public int getDistinctColorCount() {
Set<Color> distinctColors = new HashSet<Color>();
distinctColors.add(top);
distinctColors.add(front);
distinctColors.add(right);
distinctColors.add(left);
distinctColors.add(bottom);
return distinctColors.size();
}
private Set<Map<Face, Color>> generateAllPossibleMaps() {
Set<Map<Face, Color>> maps = new HashSet<>();
for (Color c1 : Face.colorList) {
for (Color c2 : Face.colorList) {
for (Color c3 : Face.colorList) {
for (Color c4 : Face.colorList) {
for (Color c5 : Face.colorList) {
for (Color c6 : Face.colorList) {
Map<Face, Color> map = new HashMap<>();
map.put(Face.TOP, c1);
map.put(Face.FRONT, c2);
map.put(Face.RIGHT, c3);
map.put(Face.LEFT, c4);
map.put(Face.BOTTOM, c5);
map.put(Face.BACK, c6);
maps.add(map);
}
}
6. }
}
}
}
return maps;
}
// @Override
// public int hashCode() {
// if (hashCode != null) {
// return hashCode;
// }
// }
//Improving on the inherited hashCode() is mandatory
//to get improved performance
//Also, whenever you change hashCode() you probably
//need to change equals() to ensure that the "equals()/hashCode()
//contract" is satisfied
public int hashCode(int level)
{
int hashCode = 0;
if(this.hashCode != null) {
hashCode = this.hashCode();
}
else if (level == 0) {
hashCode = 0;
}
else if (level == 1) {
hashCode = (top.getRGB() + bottom.getRGB() + left.getRGB() + front.getRGB() +
back.getRGB() + right.getRGB());
}
else if(level == 2) {
hashCode = getDistinctColorCount();
}
else
{
assert level == 0 : "level = " + level + "is uniterpretable";
}
return hashCode;
}
//Improving on the inherited equals() is mandatory
//to get improved performance
//Also, whenever you change equals() you probably
7. //need to change hashCode() to ensure that the "equals()/hashCode()
//contract" is satisfied
public boolean equals(Object obj)
{
throw new RuntimeException("NOT IMPLEMENTED YET!");
}
//Implement a decent toString() for yourself during development
@Override
public String toString() {
return "CubeNetImpl {" +
"top=" + top +
", front=" + front +
", right=" + right +
", back=" + back +
", left=" + left +
", bottom=" + bottom +
'}';
}
Another class:
package model;
import java.awt.Color;
import java.util.Set;
public class CubeSolver_Opinca
{
private CubeSolver_Opinca()
{
assert false : "DO NOT INSTANTIATE!";
}
public static Set<Cube> getDistinctSolidCubes(Set<Color> colors)
{
throw new RuntimeException("NOT IMPLEMENTED YET!");
}
}
the enum:
package model;
public enum Face {