Question

ISu a The paradox d I ata fields (or mputers. Program 3 Assume the existence of a class called Voter which defines two instan
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// Voter.java

public class Voter {
   //Declaring instance variables
   private String name;
   private char party;

   //Parameterized constructor
   public Voter(String name, char party) {
       this.name = name;
       this.party = party;
   }

   // getters
   public char getParty() {
       return party;
   }

   //toString method is used to display the contents of an object inside it
   @Override
   public String toString() {
       return "Voter [name=" + name + ", party=" + party + "]";
   }

}
__________________________

// Test.java

import java.util.ArrayList;
import java.util.Scanner;

public class Test {

   public static void main(String[] args) {

       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);

       ArrayList<Voter> voters = new ArrayList<Voter>();
       Voter v1 = new Voter("Lelbowitz", 'D');
       Voter v2 = new Voter("Billy", 'D');
       Voter v3 = new Voter("Bowles", 'R');
       voters.add(v1);
       voters.add(v2);
       voters.add(v3);
       for (int i = 0; i < voters.size(); i++) {
           if (voters.get(i).getParty() == 'D') {
               System.out.println(voters.get(i));
           }
       }

   }

}
_________________________

Output:

Voter [name=Lelbowitz, party=D]
Voter [name=Billy, party=D]


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
ISu a The paradox d I ata fields (or mputers. Program 3 Assume the existence of a class called Vo...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT