Question: 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.putPartyA new int;
partyStorages.putPartyB new int;
partyStorages.putPartyC new int;
partyStorages.putPartyD new int;
public void askQuestionString question, String options
System.out.printlnquestion;
for int i ; i options.length; i
System.out.printlni optionsi;
int userAnswer;
do
System.out.printYour answer options.length : ;
Scanner scanner new ScannerSystemin;
while scanner.hasNextInt
System.out.printlnInvalid input. Please enter a number.";
scanner.next;
userAnswer scanner.nextInt;
while userAnswer userAnswer options.length;
storeAnsweruserAnswer;
public void storeAnswerint answer
for MapEntry entry : partyStorages.entrySet
int storage entry.getValue;
int newArray new intstoragelength ;
System.arraycopystorage newArray, storage.length;
newArraystoragelength answer;
partyStorages.putentrygetKey newArray;
public void surveyUser
Ask survey questions and store user answers
for int i ; i ; i Adjust the number of questions as needed
String question "Sample question";
String options Option A "Option B "Option C "Option D;
askQuestionquestion options;
Ask for the political party affiliation
System.out.printWhich political party do you affiliate with? ;
Scanner scanner new ScannerSystemin;
String partyAffiliation scanner.nextLine;
Calculate weighted average for each party
Map partyScores new HashMap;
for MapEntry entry : partyStorages.entrySet
int storage entry.getValue;
double total ;
for int answer : storage
total answer;
double average total storage.length;
partyScores.putentrygetKey average;
Determine the guessed political affiliation
String guessedParty null;
double maxScore Double.NEGATIVEINFINITY;
for MapEntry entry : partyScores.entrySet
if entrygetValue maxScore
maxScore entry.getValue;
guessedParty entry.getKey;
Display the result
System.out.printlnBased on your answers, the program guesses you affiliate with: guessedParty;
System.out.printlnYour chosen party affiliation: partyAffiliation;
public static void mainString 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
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
