Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This is my code: import java.io . * ; import java.util. * ; public class Main { public static void main ( String [ ]
This is my code:
import java.io;
import java.util.;
public class Main
public static void mainString args
Define questions with answer options
String questions
"What should the government do to help the poor?",
"What is your opinion on samesex marriage?",
"What should be done to reduce income inequality?",
"What is your stance on gun control?",
"What is your opinion on abortion?",
"Which political party do you affiliate with?"
;
String answers
Make it easier to apply for assistance.", "Put more money into schools.", "Incentivize welfare.", "Nothing."
Support it fully.", "Support it with some reservations.", "Oppose it with some reservations.", "Oppose it fully."
Increase taxes on the wealthy.", "Increase funding for education and job training programs.", "Encourage job creation and economic growth.", Do nothing."
Implement stricter regulations and background checks.", "Support the Second Amendment, but with some restrictions.", "Support the Second Amendment without any restrictions.", "Repeal existing gun control laws."
Abortion should always be legal.", "Abortion should be legal in most cases.", "Abortion should be illegal in most cases.", "Abortion should always be illegal."
Republican "Democratic", "Libertarian", "Green"
;
Create or load data storage files for each political party
Map partyFiles new HashMap;
partyFiles.putRepublican new FileRepublicantxt;
partyFiles.putDemocratic new FileDemocratictxt;
partyFiles.putLibertarian new FileLibertariantxt;
partyFiles.putGreen new FileGreentxt;
Load party scores from files or initialize if files don't exist
Map partyScores loadPartyScorespartyFiles;
Loop through questions and gather user input
Scanner scanner new ScannerSystemin;
for int i ; i questions.length; i
System.out.printlnquestionsi;
for int j ; j answersilength; j
System.out.printlnj answersij;
int userAnswer;
do
System.out.printYour choice answersilength : ;
userAnswer scanner.nextInt;
while userAnswer userAnswer answersilength;
Update party scores based on user answer
if i questions.length
String selectedParty answersanswerslength userAnswer ;
partyScores.putselectedParty partyScores.getselectedParty;
Store user answers in respective party file
storeUserAnswerpartyFilesgetselectedParty questionsi answersiuserAnswer ;
Guess user's political party based on party scores
String guessedParty getGuessedPartypartyScores;
System.out.printlnBased on your answers, we guess that you are a guessedParty ;
scanner.close;
Method to load party scores from files or initialize if files don't exist
private static Map loadPartyScoresMap partyFiles
Map partyScores new HashMap;
for MapEntry entry : partyFiles.entrySet
String party entry.getKey;
File file entry.getValue;
int score file.exists loadScoreFromFilefile : getRandomScore;
partyScores.putparty score;
return partyScores;
Method to load party score from file
private static int loadScoreFromFileFile file
try Scanner fileScanner new Scannerfile
return fileScanner.nextInt;
catch FileNotFoundException e
eprintStackTrace;
return ;
Method to store user answer in respective party file
private static void storeUserAnswerFile file, String question, String answer
try PrintWriter writer new PrintWriternew FileWriterfile true
writer.printlnquestion : answer;
catch IOException e
eprintStackTrace;
Method to get a random score between and
private static int getRandomScore
Random random new Random;
return random.nextInt;
Method to guess user's political party based on party scores
private static String getGuessedPartyMap partyScores
in
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