Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.*;// Creating Class voterclass Voter{ static int nVoters=0; String name; String voterId; // Creating the constructor Voter(String name) { this.name=name; nVoters=nVoters+1; voterId=Hi+nVoters++name.length(); } public

import java.util.*;// Creating Class voterclass Voter{ static int nVoters=0; String name; String voterId; // Creating the constructor Voter(String name) { this.name=name; nVoters=nVoters+1; voterId="Hi"+nVoters+""+name.length(); } public static int getNVoters() { return nVoters; } @Override public String toString() { return voterId+" "+name; } }public class Main { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("Voter name: " ); String name1=input.nextLine(); Voter voter1=new Voter(name1); System.out.println("Voter "+Voter.getNVoters()+" created."); System.out.print("Voter name: " ); String name2=input.nextLine(); Voter voter2=new Voter(name2); System.out.println("Voter "+Voter.getNVoters()+" created."); System.out.print("Voter name: " ); String name3=input.nextLine(); Voter voter3=new Voter(name3); System.out.println("Voter "+Voter.getNVoters()+" created."); System.out.println(); System.out.println(voter1); System.out.println(voter2); System.out.println(voter3); } }
image text in transcribed
MA . Write a more complicated constructor . Practice incrementing and accessing a static variable . Practice using toString() Details: In JDoodle, create a class called Assignment05. As always, put your Assignment number, name, and class section in a block comment at the beginning of your code. Assignments missing this information will not be graded. 1. Create a Voter class consistent with the following class diagram: Voter - nvoters : int name : String - voterID : String + Voter(n : String) + getNVoters() : int + toString() : String The Voter class will be used to create instances of voters. Each voter will have a name and a voter ID which are referenced by the private instance variables name and voterID respectively. The constructor takes a string, passed to the parameter n, which is the name of the voter and which should be assigned to the name instance variable. Every time a new voter is created, the static variable nVoters should be incremented. Also, every time a new voter is created, a new voterID should be constructed by concatenationg the string "HI" with the value of nVoters and the length of the name. For example, if the second voter is named "Clark Kent", then the voterID should be "HI210" because 2 is the value of nVoters and 10 is the number of characters in "Clark Kent". If the third voter is named "Peter Parker", then the voterID should be "H1312" because 3 is the value of nVoters and 12 is the number of characters in "Peter Parker". Note that you will have to use a method to find the length of the names since you can not predict what will be typed. The static method getNVoters returns the value of nVoters. The instance method toString returns a concatenation of voterID and name, separated by a space. Remember to comment your methods! 2. Create a main method that uses the Voter class to create voters. It will also use the Scanner class to get each voter's name from the keyboard. Every time a name is entered from the keyboard, new voter will be instantiated using that name. After getting three voters, it will print out the three voter IDs and names. The printing at the end should be accomplished by sending each voter instance as an argument to printin For example, here is one run of the main method (the user input is in bold): Voter name: Clark Kent Voter 1 created. Voter name: Carol Danvers Voter 2 created. Voter name: Peter Parker Voter 3 created. HI110 Clark Kent HI213 Carol Danvers HI312 Peter Parker In the example above, the program prints "Voter name: " and then waits for input from the keyboard. Once the user types in a name and hits return, the program creates a voter instance and prints a confirmation message (using getNVoters to print the voter number). The program repeats this for two more voters, creating three voters total. Once all three voters have been created, the program prints the voterID and name data (using the variable referencing each voter instance as an argument to printin) Here is a different output from the same code but with different user inputs (note the different voter ID numbers and be sure you understand how they are constructed): Voter name: King Charles III Voter 1 created Voter name: Prince William Duke of Wales Voter 2 created. Voter name: Prince Harry Duke of Sussex Voter 3 created. HI116 King Charles III HI228 Prince William Duke of Wales HI327 Prince Harry Duke of Sussex Be sure your program works correctly with different keyboard inputs. Important: JDoodle has to be toggled to "Interactive" for Scanner to work. Hints: Here is how you should be creating a new voter instance and assigning it to a variable, assuming that you want to assign it to the variable voter1 and that you have the voter name stored in a variable called voterName (you do not have to use those exact variable names): Voter voterl = new Voter (voterName) ; Here is how you should be getting the data from a voter instance printed out, assuming the variable voter1 refers to a voter instance (you do not have to use that exact variable name): System . out . printin (voterl)

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

Unity From Zero To Proficiency Beginner A Step By Step Guide To Coding Your First Game

Authors: Patrick Felicia

1st Edition

1091872023, 978-1091872028

More Books

Students also viewed these Programming questions