Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

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.
expand button
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
Check Mark
Step 1

Answer the above program are as follows

Knowledge Booster
Background pattern image
Computer Science
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
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education