an you help me attain this output? class 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()

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Can you help me attain this output?

 

class 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 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);

   }
   
}

**Assignment: Creating a Voter Class in Java**

This educational content focuses on implementing a Voter class using Java, emphasizing constructor creation, static variable manipulation, and the use of `toString()` methods. This assignment requires the following steps:

1. **Create a Voter class**:
   - **Instance Variables**:
     - `nVoters`: `int` (static)
     - `name`: `String`
     - `voterID`: `String`
  
   - **Methods**:
     - `Voter(n: String)`: Constructor that accepts a voter's name and constructs a `voterID`.
     - `getNVoters()`: Returns current number of voters.
     - `toString()`: Returns a concatenation of `voterID` and `name`.

   - **Class Diagram**:
     ```
     Voter
     - nVoters: int
     - name: String
     - voterID: String
     + Voter(n: String)
     + getNVoters(): int
     + toString(): String
     ```

   - **Constructor Details**:
     - Increments the static `nVoters` for each new voter.
     - Constructs a `voterID` using the format "H" + `nVoters` + length of `name`.

2. **Create a main method**:
   - Uses the `Scanner` class to input voter names.
   - Instantiates three Voter objects based on user input.
   - Prints out the `voterID` and `name` for each voter.

3. **Example Execution**: 
   - Input: “Clark Kent”, “Carol Danvers”, “Peter Parker”.
   - Output:
     ```
     H110 Clark Kent
     H212 Carol Danvers
     H312 Peter Parker
     ```

4. **Important Note**:
   - Ensure `JDoodle` is set to "interactive" for the `Scanner` to function properly.

5. **Hints**:
   - Example of creating a Voter instance: 
     ```java
     Voter voter1 = new Voter(voterName);
     ```
     
   - Printing a Voter instance:
     ```java
     System.out.println(voter1);
     ```

This assignment helps understand static and instance variables, string concatenation, and user interaction in Java.
Transcribed Image Text:**Assignment: Creating a Voter Class in Java** This educational content focuses on implementing a Voter class using Java, emphasizing constructor creation, static variable manipulation, and the use of `toString()` methods. This assignment requires the following steps: 1. **Create a Voter class**: - **Instance Variables**: - `nVoters`: `int` (static) - `name`: `String` - `voterID`: `String` - **Methods**: - `Voter(n: String)`: Constructor that accepts a voter's name and constructs a `voterID`. - `getNVoters()`: Returns current number of voters. - `toString()`: Returns a concatenation of `voterID` and `name`. - **Class Diagram**: ``` Voter - nVoters: int - name: String - voterID: String + Voter(n: String) + getNVoters(): int + toString(): String ``` - **Constructor Details**: - Increments the static `nVoters` for each new voter. - Constructs a `voterID` using the format "H" + `nVoters` + length of `name`. 2. **Create a main method**: - Uses the `Scanner` class to input voter names. - Instantiates three Voter objects based on user input. - Prints out the `voterID` and `name` for each voter. 3. **Example Execution**: - Input: “Clark Kent”, “Carol Danvers”, “Peter Parker”. - Output: ``` H110 Clark Kent H212 Carol Danvers H312 Peter Parker ``` 4. **Important Note**: - Ensure `JDoodle` is set to "interactive" for the `Scanner` to function properly. 5. **Hints**: - Example of creating a Voter instance: ```java Voter voter1 = new Voter(voterName); ``` - Printing a Voter instance: ```java System.out.println(voter1); ``` This assignment helps understand static and instance variables, string concatenation, and user interaction in Java.
Expert Solution
Step 1

Answer the above program are as follows

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Methods of StringBuilder class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education