My program has a ZodiacType enum with constants and values for below.
enum ZodiacType {
ARI(ZodiacInfo.Aries, "March 21", "April 19"),
TAU(ZodiacInfo.Taurus, "April 20", "May 20"),
GEM(ZodiacInfo.Gemini, "May 21", "June 20"),
CAN(ZodiacInfo.Cancer, "June 21", "July 22"),
LEO(ZodiacInfo.Leo, "July 23", "August 22"),
VIR(ZodiacInfo.Virgo, "August 23", "September 22"),
LIB(ZodiacInfo.Libra, "September 23", "October 22"),
SCO(ZodiacInfo.Scorpio, "October 23", "November 21"),
SAG(ZodiacInfo.Sagittarius, "November 22", "December 21"),
CAP(ZodiacInfo.Capricorn, "December 22", "January 19"),
AQU(ZodiacInfo.Aquarius, "January 20", "February 18"),
PIS(ZodiacInfo.Pisces, "February 19", "March 20");
private final ZodiacInfo description;
private final String startDate;
private final String toDate;
ZodiacType(ZodiacInfo description, String startDate, String toDate) {
this.description = description;
this.startDate = startDate;
this.toDate = toDate;
}
}
And another Set class which contains one ArrayList as below
class Set {
private ArrayList<ZodiacType> s;
public Set() {
s = new ArrayList<>();
}
public void addElement(ZodiacType element) {
s.add(element);
}
public boolean subSet (Set otherSet) {
if (s.containsAll(otherSet.s)) {
return true;
} else
return false;
}
}
If I was to develop two methods
private static Set getASet() {
private ZodiacType getAnElement() {
for randomly generate and add to that particular ArrayList (5 elements) and display, is there any solution for this two new methods and I really curious any provided solutions since I am really weak to imagine the logic flow. The output that is required:
Here is an example of set A = {AQU, TAU, CAN, GEM, CAP}. All elements in set are distinct and random number.
CodePudding user response:
You can use unique random number generator to generate unique random numbers between 0-11, and use that index to pick random enum from your set of enums:
public class Set {
public static List<Integer> randomIndexes() {
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i=1; i < 11; i ) list.add(i);
Collections.shuffle(list);
return list.slice(0, 4);
}
public void addRandomSigns() {
List<Integer> indexes = randomIndexes();
for (Integer i : indexes) {
this.addElement(ZodiacType.values()[i);
}
}
}
CodePudding user response:
You can use the HashSet
and a simple while
loop to insert let's say 5 unique and random zodiacs using java.util.Random
.
final Random random = new Random();
final java.util.Set<ZodiacType> zodiacTypeSet = new HashSet<>();
while (zodiacTypeSet.size() < 5) {
int index = random.nextInt(ZodiacType.values().length);
zodiacTypeSet.add(ZodiacType.values()[index]);
}
Sample content of zodiacTypeSet
:
[LEO, SCO, CAP, SAG, PIS]
Note I used java.util.Set
to avoid a clash with your Set
that does not based on the Object's hash code. Also, it does not support adding multiple items at once, so you need another loop to insert them.
for (ZodiacType zodiacType: zodiacTypeSet) {
set.addElement(zodiacType);
}
... unless you define own method in Set
to do so:
public void addElements(Collection<ZodiacType> elements) {
s.addAll(elements);
}
set.addElements(zodiacTypeSet);