+ 1

Java Question

Create a java class named Voter that has four private data . The class should contain a parameterized constructor to initialize its data members. The data members passed as parameter are voter id, voter name and sub county. Voted is not passed as a parameter but it is initialised within the constructor to false.The class should have one method to display the information. Now write a Java program that will use an array of Voter objects to represent the information.The program to take input from the keyboard

21st Dec 2016, 2:57 PM
victor
14 Answers
+ 2
public class Voter{ private long id; private String name; private String subCounty; private boolean isVoted; public Voter(long id, String name, String subCounty){ this.id = id; this.name = name; this.subCounty = subCounty; this.isVoted = false; } public long getId(){ return this.id; } public String getName(){ return this.name; } public String getSubCounty(){ return this.subCounty; } public boolean getIsVoted(){ return this.isVoted; } public void setIsVoted(boolean getValue){ this.isVoted = getValue; } public String toString(){ return "name : " + this.name + " id : " + this.id + " voted : " + this.isVoted + " sub county : " + this.subCounty; } } public static void main(String [] args){ List<Voter> voters = new ArrayList<Voter>(); Voter voter1 = new Voter(3892833, "John", "Example"); voter1.setIsVoted(true); Voter voter2 = new Voter(23923833, "Elia", "England"); voter2.setIsVoted(true); //populate objects with values Voter voter3 = new Voter(2349324, "Michelle", "USA"); voter3.setIsVoted(true); voters.add(voter1); // add each object to arrayList voters.add(voter2); voters.add(voter3); for(Voter voter : voters){ // print all the persons details System.out.println(voter.toString()); } } Finally, I know you mentioned input should come from keyboard, I shall leave that part to you but however if you're struggling then notify me for further help on that, if you have any questions regarding any of the code then feel free to post is below. HAPPY CODING!
21st Dec 2016, 3:23 PM
Ousmane Diaw
+ 1
section 2: if(condition != null) { while (!condition.equals("exit")) { System.out.println("enter id number : "); try { String getId = scanner.next(); condition = getId; if(condition.equals("exit")) break; identification = Long.parseLong(getId); }catch (NumberFormatException e) { System.out.println("please enter a valid number..."); break; }catch (Exception e){ System.out.println("incorrect details..."); break; } System.out.println("enter name : "); name = scanner.next(); condition = name; if(condition.equals("exit")) break; System.out.println("enter sub county : "); subCounty = scanner.next(); condition = subCounty; if(condition.equals("exit")) break; System.out.println("enter vote status e.g 'true' or 'false' : ");
21st Dec 2016, 5:25 PM
Ousmane Diaw
+ 1
section 3: try { String getVoteStatus = scanner.next(); condition = getVoteStatus; if(condition.equals("exit")) break; if(getVoteStatus.equals("true") || getVoteStatus.equals("false")){ isVoted = Boolean.parseBoolean(getVoteStatus); }else{ System.out.println("incorrect vote status entered"); break; } }catch(NumberFormatException e){ System.out.println("incorrect vote status entered"); break; }catch(Exception e){ System.out.println("incorrect details "); } } if(identification != 0 && subCounty !=null) { Node voter = new Node(identification, name, subCounty); voter.setIsVoted(isVoted); voters.add(voter); } }else{ System.out.println("program has finished executing..."); } for(Node voter : voters){ // print all the persons details System.out.println(voter.toString()); }
21st Dec 2016, 5:25 PM
Ousmane Diaw
+ 1
replace the previous code that was in the main method with the three sections I will post below respectively: ALSO WHERE EVER YOU SEE THE WORD 'Node' replace it with 'Voter'. i used Node just for example purposes. section 1: /** * THE RULES FOR THE BELOW CODE: * after you have entered as many details as you wish make SURE TO WRITE 'exit' * anywhere during the program execution for the loop to terminate. * finally it will display all details after the loop is terminated. */ Scanner scanner = new Scanner(System.in); // make sure you import the scanner class from java API List<Node> voters = new ArrayList<Node>(); long identification = 0; // default for now String name = null, subCounty = null; // default for now boolean isVoted = false; // default for now System.out.println("RULES TO KEEP IN MIND!!!!.. once the program has taken all the details and you wish to exit ,just type 'exit' "); System.out.println("the program will then display all the details entered"); System.out.println("enter 'exit' to terminate, otherwise press any code to proceed..."); String condition = null; if(scanner.hasNext()){ condition = scanner.next(); }else{ System.out.println("incorrect details entered..."); }
21st Dec 2016, 5:32 PM
Ousmane Diaw
0
Thanks alot
21st Dec 2016, 3:24 PM
victor
0
Consider the following java class definition: public class time { private int second; // from 1 to 60 private int minute; // from 1 to 60 private int hour; // from 0 to 23 public void decrease (); // move to previous second } a) Implement a constructor that initialises new objects of time class to be set to the 22:45:00. b) Implement setters for second, minute and hour c) Implement the increase method, moves to the next second, ensuring that all data members are updated appropriately
21st Dec 2016, 3:30 PM
victor
0
You welcome and also I would recommend you to post the latter question as a separate post that way more people can see it and I shall consider the question once it's posted in a different post. HAPPY CODING!
21st Dec 2016, 3:45 PM
Ousmane Diaw
0
Thanks.. If u don't mind can u Code that part where the program takes input from the keyboard
21st Dec 2016, 3:55 PM
victor
0
Sure i will do that part for you but i am busy right now but will do it and post it below anytime soon. That okay with you?
21st Dec 2016, 4:15 PM
Ousmane Diaw
0
No problem.. It will be okay with me
21st Dec 2016, 4:19 PM
victor
0
Finally, i have just provided the bare minimum as you suggested, there are more things you can do to make the program more user-friendly and robust but i will leave that section to you. ALSO WHERE EVER YOU SEE THE WORD 'Node' replace it with 'Voter'. i used Node just for example purposes. HAPPY CODING!
21st Dec 2016, 5:33 PM
Ousmane Diaw
0
Thanks for ur help
21st Dec 2016, 5:53 PM
victor
0
Pliz can u Code the other question if u don't mind its already on a different post
21st Dec 2016, 5:53 PM
victor
0
I will consider that question later, make sure to write the title of the question below and if i can be bothered later i will conisder it. :)
21st Dec 2016, 6:28 PM
Ousmane Diaw