Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following code has been provided to me by someone from Chegg in response to another request. This code is supposed to ask political questions

The following code has been provided to me by someone from Chegg in response to another request. This code is supposed to ask political questions in a survey. However, when I run it on Replit all it outputs is Hello World. Is it missing a class or do I need to run it on another platform? Thank you for any help you can provide - Below is a simple example of a political survey program in Java that fulfills the given requirements:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class PoliticalSurvey {
private Map partyStorages;
public PoliticalSurvey(){
partyStorages = new HashMap<>();
partyStorages.put("PartyA", new int[0]);
partyStorages.put("PartyB", new int[0]);
partyStorages.put("PartyC", new int[0]);
partyStorages.put("PartyD", new int[0]);
}
public void askQuestion(String question, String[] options){
System.out.println(question);
for (int i =0; i < options.length; i++){
System.out.println((i +1)+"."+ options[i]);
}
int userAnswer;
do {
System.out.print("Your answer (1-"+ options.length +"): ");
Scanner scanner = new Scanner(System.in);
while (!scanner.hasNextInt()){
System.out.println("Invalid input. Please enter a number.");
scanner.next();
}
userAnswer = scanner.nextInt();
} while (userAnswer <1|| userAnswer > options.length);
storeAnswer(userAnswer);
}
public void storeAnswer(int answer){
for (Map.Entry entry : partyStorages.entrySet()){
int[] storage = entry.getValue();
int[] newArray = new int[storage.length +1];
System.arraycopy(storage,0, newArray, 0, storage.length);
newArray[storage.length]= answer;
partyStorages.put(entry.getKey(), newArray);
}
}
public void surveyUser(){
// Ask survey questions and store user answers
for (int i =0; i <4; i++){// Adjust the number of questions as needed
String question = "Sample question";
String[] options ={"Option A", "Option B", "Option C", "Option D"};
askQuestion(question, options);
}
// Ask for the political party affiliation
System.out.print("Which political party do you affiliate with? ");
Scanner scanner = new Scanner(System.in);
String partyAffiliation = scanner.nextLine();
// Calculate weighted average for each party
Map partyScores = new HashMap<>();
for (Map.Entry entry : partyStorages.entrySet()){
int[] storage = entry.getValue();
double total =0.0;
for (int answer : storage){
total += answer;
}
double average = total / storage.length;
partyScores.put(entry.getKey(), average);
}
// Determine the guessed political affiliation
String guessedParty = null;
double maxScore = Double.NEGATIVE_INFINITY;
for (Map.Entry entry : partyScores.entrySet()){
if (entry.getValue()> maxScore){
maxScore = entry.getValue();
guessedParty = entry.getKey();
}
}
// Display the result
System.out.println("Based on your answers, the program guesses you affiliate with: "+ guessedParty);
System.out.println("Your chosen party affiliation: "+ partyAffiliation);
}
public static void main(String[] args){
PoliticalSurvey survey = new PoliticalSurvey();
survey.surveyUser();
}
}
Explanation:
This Java code implements a simple political survey program that stores user answers for each political party, calculates weighted averages, and guesses the political affiliation based on the answers.

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions

Question

6. What are the basic criteria for effective messages? (LO 1-5)

Answered: 1 week ago

Question

What is conservative approach ?

Answered: 1 week ago

Question

What are the basic financial decisions ?

Answered: 1 week ago