Answered step by step
Verified Expert Solution
Link Copied!

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 main(String[] args){
// Define questions with answer options
String[] questions ={
"What should the government do to help the poor?",
"What is your opinion on same-sex 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.put("Republican", new File("Republican.txt"));
partyFiles.put("Democratic", new File("Democratic.txt"));
partyFiles.put("Libertarian", new File("Libertarian.txt"));
partyFiles.put("Green", new File("Green.txt"));
// Load party scores from files or initialize if files don't exist
Map partyScores = loadPartyScores(partyFiles);
// Loop through questions and gather user input
Scanner scanner = new Scanner(System.in);
for (int i =0; i < questions.length; i++){
System.out.println(questions[i]);
for (int j =0; j < answers[i].length; j++){
System.out.println((j +1)+"."+ answers[i][j]);
}
int userAnswer;
do {
System.out.print("Your choice (1-"+ answers[i].length +"): ");
userAnswer = scanner.nextInt();
} while (userAnswer <1|| userAnswer > answers[i].length);
// Update party scores based on user answer
if (i < questions.length -1){
String selectedParty = answers[answers.length -1][userAnswer -1];
partyScores.put(selectedParty, partyScores.get(selectedParty)+1);
// Store user answers in respective party file
storeUserAnswer(partyFiles.get(selectedParty), questions[i], answers[i][userAnswer -1]);
}
}
// Guess user's political party based on party scores
String guessedParty = getGuessedParty(partyScores);
System.out.println("Based 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 loadPartyScores(Map partyFiles){
Map partyScores = new HashMap<>();
for (Map.Entry entry : partyFiles.entrySet()){
String party = entry.getKey();
File file = entry.getValue();
int score = file.exists()? loadScoreFromFile(file) : getRandomScore();
partyScores.put(party, score);
}
return partyScores;
}
// Method to load party score from file
private static int loadScoreFromFile(File file){
try (Scanner fileScanner = new Scanner(file)){
return fileScanner.nextInt();
} catch (FileNotFoundException e){
e.printStackTrace();
return 0;
}
}
// Method to store user answer in respective party file
private static void storeUserAnswer(File file, String question, String answer){
try (PrintWriter writer = new PrintWriter(new FileWriter(file, true))){
writer.println(question +": "+ answer);
} catch (IOException e){
e.printStackTrace();
}
}
// Method to get a random score between 0 and 20
private static int getRandomScore(){
Random random = new Random();
return random.nextInt(21);
}
// Method to guess user's political party based on party scores
private static String getGuessedParty(Map partyScores){
in

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions

Question

Define Administration and Management

Answered: 1 week ago

Question

Define organisational structure

Answered: 1 week ago