Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Out of the warriors generated, I need to create a code so that two of them are randomly chosen to fight against each other. The
Out of the warriors generated, I need to create a code so that two of them are randomly chosen to fight against each other.
The code:
import java.util.ArrayList; import java.util.Collections; enum SocialCliqueEnum { nerd, jock, prep, thug, freak } public class Warrior implements Comparable { private String name; private int IQ; private int strength; private SocialCliqueEnum socialClique; public Warrior(String name, SocialCliqueEnum socialClique) { this.name = name; this.socialClique = socialClique; this.generateStats(); System.out.println(this); System.out.println("----------"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getIQ() { return IQ; } public void setIQ(int iQ) { IQ = iQ; } public int getStrength() { return strength; } public void setStrength(int strength){ this.strength = strength; } public SocialCliqueEnum getSocialClique() { return socialClique; } public void setSocialClique(SocialCliqueEnum socialClique) { this.socialClique = socialClique; } public String toString() { return "Warrior [name=" + name + ", IQ=" + IQ + ", strength=" + strength + ", socialClique=" + socialClique + "]"; } private int randomgenerator(int i, int j) { java.util.Random random = new java.util.Random(); int r = random.nextInt(j - i + 1) + i; return r; } public void victory() { String message = name; switch (socialClique) { case nerd: message += " dispatched their opponent with a combination Jedi mind trick and Vulcan neck pinch!"; break; case jock: message += " won the fight by running over their opponent with daddys Bently!"; break; case prep: message += " "; break; case thug: message += " "; break; case freak: message += " "; break; default: message += " "; } System.out.println(message); } void generateStats() { switch (socialClique) { case nerd: this.IQ = randomgenerator(120, 180); this.strength = randomgenerator(20, 80); System.out.println("IQ : " + IQ); System.out.println("Strength: " + strength); System.out.println("Nerds are smarter than they are strong"); break; case jock: this.IQ = randomgenerator(80, 140); this.strength = randomgenerator(50, 100); System.out.println("IQ : " + IQ); System.out.println("Strength: " + strength); System.out.println("Jocks are stronger than they are smart"); break; case prep: this.IQ = randomgenerator(90, 150); this.strength = randomgenerator(40, 90); System.out.println("IQ : " + IQ); System.out.println("Strength: " + strength); System.out.println("Middle of the road, but from the right side of the tracks"); break; case thug: this.IQ = randomgenerator(60, 90); this.strength = randomgenerator(80, 100); System.out.println("IQ : " + IQ); System.out.println("Strength: " + strength); System.out.println("A weapon makes them strong, but they dont have much upstairs"); break; case freak: this.IQ = randomgenerator(60, 80); this.strength = randomgenerator(1, 100); System.out.println("IQ : " + IQ); System.out.println("Strength: " + strength); System.out.println("The wild-card"); break; default: System.out.println("socialClique is different " + socialClique); } } // Sort by IQ public int compareTo(Warrior w) { if (IQ == w.IQ) return 0; else if (IQ > w.IQ) return 1; else return -1; } public static void main(String[] args) { Warrior x1 = new Warrior("Bruce", SocialCliqueEnum.prep); Warrior x2 = new Warrior("Steve", SocialCliqueEnum.freak); Warrior x3 = new Warrior("Hulk", SocialCliqueEnum.jock); Warrior x4 = new Warrior("Stark", SocialCliqueEnum.nerd); Warrior x5 = new Warrior("Thor", SocialCliqueEnum.thug); System.out.println("--------------------------"); System.out.println(x1); System.out.println(x2); System.out.println(x3); System.out.println(x4); System.out.println(x5); System.out.println("-----------Sorting by IQ---------------"); ArrayList warriorList = new ArrayList(); warriorList.add(x1); warriorList.add(x2); warriorList.add(x3); warriorList.add(x4); warriorList.add(x5); Collections.sort(warriorList); for (Warrior w : warriorList) { System.out.println(w.getIQ() + " " + w.getName() + " " + w.getStrength() + " " + w.getSocialClique()); } warriorList.get(2).victory(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started