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]
# 1

> //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".

bheilersa at 2007-7-12 8:41:35 > top of Java-index,Java Essentials,New To Java...
# 2

Hi bheilers,

Thanx for your info, its helpful. but i still can't understand with

private Vote vote; // is it declare an private object vote of Vote class?

vote = new Vote(); //then assign it to new Vote object?>

and the example you wrote, still i confused.

public Vote getVote() {//still can't get the meaning

//i browse through the link you provide, but not sure where to go

Pls assits me..Thanx

tsnta at 2007-7-12 8:41:35 > top of Java-index,Java Essentials,New To Java...
# 3

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)

sridanua at 2007-7-12 8:41:35 > top of Java-index,Java Essentials,New To Java...
# 4

Hi sridanu,

I understand with this:

> car cr=new car();

> char s=cr.getName().chatAt(0)

do pls help me, on this following, i'm really out of idea....

private Vote vote; // need clarafication...

vote = new Vote(); // need clarafication...

public Vote getVote() { // need clarafication...

Regards

tsnta at 2007-7-12 8:41:35 > top of Java-index,Java Essentials,New To Java...
# 5

private Vote vote;

Surely you have covered variable declarations. It is one of the basic and first things that should have been covered. This line declares a variable called vote that can hold a reference to a Vote object. The variable has private access so only the class it is declared in has direct access to it.

vote = new Vote();

Here a new Vote object is being created and the reference to that object is being stored in the vote variable.

public Vote getVote() {

This is a method called getVote that takes no parameters and returns a Vote object.

floundera at 2007-7-12 8:41:35 > top of Java-index,Java Essentials,New To Java...
# 6

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(){.....

sridanua at 2007-7-12 8:41:35 > top of Java-index,Java Essentials,New To Java...
# 7

tsnt, I think you need to get started with the basics of Java first; the syntax and semantics, declarations and object creation et al.

Seems to me like you're coming from a C background and you've got this code from somewhere ( for a submission maybe? Hm? ) and are trying to understand what it does.

Trust me, you won't get very far with this, it'll only confuse you more. Get a quick start on Java first and then get back to the forum.

nogoodatcodinga at 2007-7-12 8:41:35 > top of Java-index,Java Essentials,New To Java...
# 8

Hi Sridanu, Flounder ,

Thanx for your clarification, now i did get a clear picture for my doubts.

I'm still at beginning of learning java, since i'm doing self studying, sometimes i did stuck in somewhere, so i need some help. But to be honest, i'm not doing any submission on this. I'm working in telemarketing nothing to do with any programming assignments.

Regards,

tsnt

tsnta at 2007-7-12 8:41:35 > top of Java-index,Java Essentials,New To Java...