Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Prompt The goal is for the program to be able to guess the user's political party before they reach the end of the survey. This

Prompt
The goal is for the program to be able to guess the user's political party before they reach the end of the survey. This will require your program to gather a substantial amount of data before it can make accurate guesses.
1. You should begin by presenting the user with questions that contain answer options that differ based on their political beliefs.
2. The last question should ask which political party they affiliate with. This way you will be able to gather and store data corresponding to the results you acquire. Create at least 4 political party storages.
3. As you gain more data on each political party, you should have a way of storing this information to create a program that will use this data to accurately survey users and guess their political affiliations.
4. Some answers to questions correspond with more than one political party. Find a way to make your program advanced enough so that it can weigh answers with differing levels of intensity depending on which parties they correspond best with.
5. Overall, you will prepare data storage files, obtain and store data through the usage of questions, and then write code using machine learning to create a survey that will accurately guess a user's political party before they complete the survey.
Here is my code written in Java:
Main.Java
//Start
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
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?"
};
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."}
};
// define political parties and their respective random initial scores
Map partyScores = new HashMap<>();
partyScores.put("Republican", getRandomScore());
partyScores.put("Democratic", getRandomScore());
partyScores.put("Libertarian", getRandomScore());
partyScores.put("Green", getRandomScore());
// 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
String selectedParty = "Republican"; // Replace with the actual party selected by the user
partyScores.put(selectedParty, partyScores.get(selectedParty)+1);
}
// guess user's political party based on party scores
int maxScore =0;
String guessedParty ="";
for (Map.Entry entry : partyScores.entrySet()){
String party = entry.getKey();
int score = entry.getValue();
if (score > maxScore){
maxScore = score;
guessedParty = party;
}
}
System.out.println("Based on your answers, we guess that you are a "+ guessedParty +".");
scanner.close();
}
// Method to get a random score between 0 and 20
private static int getRandomScore(){
Random random = new Random();
return random.nextInt(21); // Generates a random integer between 0(inclusive) and 21(exclusiv

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions