pls help with this code
hi there,
I can't understand the certain coding written for the below program.
I had marked it as bold, pls do help me.
Thanx.
__
// TestVoteCandidateClass.java: Count votes
import javax.swing.JOptionPane;
public class Exercise6_8 {
/** Main method */
public static void main(String[] args) {
// Create two candidates
Candidate candidate1 = new Candidate("George King");
Candidate candidate2 = new Candidate("Kim Jones");
// Count votes
while (true) {
// Prompt the user to enter a vote for a candidate
String voteString = JOptionPane.showInputDialog(null,
"Enter a vote:",
"Exercise6_6 Input", JOptionPane.QUESTION_MESSAGE);
// Convert string into integer
int vote = Integer.parseInt(voteString);
if (vote == 0) break; // End of the votes
//what does it mean with candidate1.getVote().increment();
//can two methods can be called in single statement?
else if (vote == 1) candidate1.getVote().increment();
else if (vote == 2) candidate2.getVote().increment();
else if (vote == -1) candidate1.getVote().decrement();
else if (vote == -2) candidate2.getVote().decrement();
}
// Prepare the result
String output = "The total number of candidates is " +
Candidate.getNumberOfCandidates();
output += "\nThe votes for " + candidate1.getName() + " is " +
candidate1.getVote().getCount();
output += "\nThe votes for " + candidate2.getName() + " is " +
candidate2.getVote().getCount();
//again confused with candidate2.getVote().getCount();
// Display the result
JOptionPane.showMessageDialog(null, output,
"Exercise6_6 Output", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
// Define the Vote class
class Vote {
/** The count for a Vote object */
private int count = 0;
/** Return the count */
public int getCount() {
return count;
}
/** Set a new count */
public void setCount(int count) {
this.count = count;
}
/** Clear this count */
public void clear() {
count = 0;
}
/** Increment this count */
public void increment() {
count++;
}
/** Decrement this count */
public void decrement() {
count--;
}
}
class Candidate {
private String name;
//help me with this declaration, i really can't understand
private Vote vote;
private static int numberOfCandidates = 0;
public Candidate(String name) {
this.name = name;
//what actually done with this statement? declare object?
vote = new Vote();
numberOfCandidates++;
}
/** Return the vote of the candidate */
public Vote getVote() {//method refering to Vote object?
return vote;
}
/** Return the name of the candidate */
public String getName() {
return name;
}
/** Return the number of candidates */
public static int getNumberOfCandidates() {
return numberOfCandidates;
}
}
[3258 byte] By [
tsnta] at [2007-11-27 3:38:17]

> //what does it mean with candidate1.getVote().increment();
> //can two methods can be called in single statement?
The code compiled and executed, right? Yes. If candidate1.getVote() returns an Object, then you are now able to call any of the methods declared on that object. The limits on this are:
- access level (private, protected, package, public)
- dynamic vs. static type of Object
This should all be covered somewhere in here: http://java.sun.com/docs/books/tutorial/java/TOC.html
> //help me with this declaration, i really can't understand
> private Vote vote;
> ...
> //what actually done with this statement? declare object?
> vote = new Vote();
Sometimes you know you'll have a variable, but not sure what will be assigned to it until a later time. This wasn't such a great example. A better one might be something like:Object obj;
if ( decideWhetherTrueOrFalse() ) {
obj = createObjectA();
} else {
obj = createObjectB();
}
In each case, the variable "obj" is being assigned a value. If I had the line obj = new Value();
then I would be creating a new object of type Value, and setting aside some of the computer's memory to hold onto that object.
> public Vote getVote() { //method refering to Vote object?
I think here, your problem is that you need to learn the signaficance of each element in a "method signature".
ok. heres the deal every class has its own methods (user made or pre defined) and most methods (except once that have a return type as void) return a object of a specific class.
say for example i have a car class and it has a method returnName() which returns the name in String so if you create an object of the car class you can use the above method as such
car cr=new car ();
String name=cr.returnName();
now remember that the return type of the returnName() method is String...
meaning it also has methods such as charAt().
so you can use the name object {Of String type} as such
name.charAt(0) ........ which returns the character at position 0 of the String object "name"
instead of having so many lines of code you can do this in one go
car cr=new car();
char s=cr.getName().chatAt(0)
private Vote vote; // need clarafication...
vote = new Vote(); // need clarafication...
public Vote getVote() { // need clarafication...
ok ill try to keep things simple.. The 1st 2 lines are pretty easy
they are similar to
String word;
word=new String(); //what i think is confusing for you is the name of the Class and the name of the variable of that class. remember that java is case sensitive
so Vote is the Class name (or type name) and vote is the object name of the type Vote..
in the 1st line you have created a variable named vote of the Vote class.
but you have not initialized a value to it yet. it has been done on the 2nd line
vote =new Vote(); // Here the Vote Class constructor is used to initialize the variable named vote.
The problem for you i think is the use of names. Think of it as such
Vote x=new Vote();
or
Vote x;
x=new Vote();
As for the 3rd line --public Vote getVote()
this is actually a method declaration what happens here is that you define a method that when invoked(used) will return a object of Vote type
similar to
public string getName(){.....