I took an introductory Java course in which we used BlueJ to write out code so I never learned about the main function and how it is used to execute programs. I understand it for the most part, I just have a couple of questions about it.
When I say main() function I am talking about "public static void main (String args[])".
Do you have to have a main function in each class? If not, which class does it go in?
How does it work? What do I actually put inside the method? Do I put the entire source code of the class inside of it?
I am switching over to Eclipse and getting away from BlueJ as it is much more professional, but I am a little confused here. Thanks for any help.
Help with the main() function
Page 1 of 18 Replies - 1012 Views - Last Post: 19 January 2012 - 10:54 AM
Replies To: Help with the main() function
#2
Re: Help with the main() function
Posted 18 January 2012 - 08:58 PM
The main() method is the entry point of any java program
When at the console prompt you execute
> javac MyClass
the control is transfer to the main() method of MyClass
you can't actually access any other method because you need a MyClass object to access it
MyClass mc = new MyClass();
mc.print(); // you need a MyClass object to access its print() method
Only the master class that launches the whole application needs a main() method but it is a good idea to have a main() method in each class for unit test.
You can have a class Dog which has a main() method that creates a Dog object and prints its name.
You can have a class Kennel which has a main() method that creates a Kennel object and populates it whit Dogs objects.
A main() method should have only a few lines, it is used to create an object and the class then all action should happen within the class methods.
When at the console prompt you execute
> javac MyClass
the control is transfer to the main() method of MyClass
you can't actually access any other method because you need a MyClass object to access it
MyClass mc = new MyClass();
mc.print(); // you need a MyClass object to access its print() method
Only the master class that launches the whole application needs a main() method but it is a good idea to have a main() method in each class for unit test.
You can have a class Dog which has a main() method that creates a Dog object and prints its name.
You can have a class Kennel which has a main() method that creates a Kennel object and populates it whit Dogs objects.
A main() method should have only a few lines, it is used to create an object and the class then all action should happen within the class methods.
#3
Re: Help with the main() function
Posted 18 January 2012 - 09:10 PM
Thank you so much for the response. I am still trying to get the hang of it. It seems like it is something that should be so basic yet it was never taught in my class since we used BlueJ.
To try and understand it a little more, I am going to try and put it to what I am currently working on.
I am building a Blackjack game which consists of a Card class, a Deck class and a TwoPlayerGame class. I have the classes written for the most part (still working on the TwoPlayerGame class), but I am not sure where to put the main() functions or what to put inside of it. I'll post my Card class and perhaps you could suggest what the best route would be when putting my main() method in.
To try and understand it a little more, I am going to try and put it to what I am currently working on.
I am building a Blackjack game which consists of a Card class, a Deck class and a TwoPlayerGame class. I have the classes written for the most part (still working on the TwoPlayerGame class), but I am not sure where to put the main() functions or what to put inside of it. I'll post my Card class and perhaps you could suggest what the best route would be when putting my main() method in.
public class Card {
// declare variables
private String rank; // card rank: a number between 2-10 or Jack, Queen, King, Ace
private char suit; // suit rank: S, C, H, or D (Spades, Clubs, Hearts or Diamonds)
// helpful supporting structures
private static String [] ranks = {"2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace"};
private static char [] suits = {'S','C','H','D'};
// default constructor sets card rank to 2 and card suit to H
public Card()
{
rank = "2";
suit = 'H';
}
// class constructor sets rank and suit to provided values
// the given values are validated. if invalid, default values are assigned
public Card(String rank, char suit)
{
this.rank = "2";
this.suit = 'H';
for(String checkRank : ranks)
if(checkRank.equals(rank))
this.rank = rank;
for(char checkSuit : suits)
if(checkSuit == suit)
this.suit = suit;
}
/*
* Return the rank of the card
*
* @return String rank of card
*/
public String getRank()
{
return rank;
}
/*
* Return the suit of the card
*
* @return char suit of card
*/
public char getSuit()
{
return suit;
}
/*
* Set the rank of a card based on a given input
* If the input is invalid, do not change the rank
*/
public void setRank(String rank)
{
for(String checkRank : ranks)
if(checkRank.equals(rank))
this.rank = rank;
}
/*
* Set the suit of a card based on a given input
* If the input is invalid, do not change the suit
*/
public void setSuit(char suit)
{
for(char checkSuit : suits)
if(checkSuit == suit)
this.suit = suit;
}
/*
* Return a string representation of the card
*
* @return String card rank and suit
*/
public String toString()
{
// variable that will hold the name of the card to be returned
String cardName;
switch(suit) {
case 'S': cardName = rank + " of Spades";
break;
case 'C': cardName = rank + " of Clubs";
break;
case 'H': cardName = rank + " of Hearts";
break;
case 'D': cardName = rank + " of Diamonds";
break;
default: cardName = "2 of Hearts";
break;
}
return cardName;
}
/*
* Display the possible card ranks
*
* @return Array of possible card ranks (2-10 or Jack, Queen, King, or Ace)
*/
public static String[] getPossibleRanks()
{
return ranks;
}
/*
* Display the possible card suits
*
* @return Array of possible card suits (S, C, H, or D)
*/
public static char[] getPossibleSuits()
{
return suits;
}
}
#4
Re: Help with the main() function
Posted 18 January 2012 - 09:13 PM
Just remember BlueJ and Eclipse a just fancy editor to edit Java source code.
#5
Re: Help with the main() function
Posted 18 January 2012 - 09:17 PM
I am having the same problem, and I am not certain why but I just cannot get my head wrapped around this concept. My instructor wants us to use a master main, contained in one java file, create a new object of a second java file. The second file is where all the actual mechanics happen. Part of the problem, every single example I can find has all code in one java file. So I am following this post, learning, and if my terms are wrong it would be a newbie mistake.
Here is a good, Java Cheat Sheet/
Also I just started watching a free online course from Stanford University, Programming Methodology on Java. The second lesson was very informative. They have the class tutorials etc.
Here is a good, Java Cheat Sheet/
Also I just started watching a free online course from Stanford University, Programming Methodology on Java. The second lesson was very informative. They have the class tutorials etc.
#6
Re: Help with the main() function
Posted 18 January 2012 - 09:23 PM
itsjimmy91, on 18 January 2012 - 11:10 PM, said:
I am building a Blackjack game which consists of a Card class, a Deck class and a TwoPlayerGame class. I have the classes written for the most part (still working on the TwoPlayerGame class), but I am not sure where to put the main()
Obviously in the Payer class.
The Card class does not know that the Deck class exists. It cannot reference it it would be a very bad design.
The Deck class is formed by Cards, it knows what is a Card but not what Blackjack, Poker, Bridge,... are so its code cannot reference your Players game.
The Blackjack class knows what a Deck of Cards is. It is there that the main() method goes.
#7
Re: Help with the main() function
Posted 18 January 2012 - 09:28 PM
pbl, on 18 January 2012 - 09:23 PM, said:
itsjimmy91, on 18 January 2012 - 11:10 PM, said:
I am building a Blackjack game which consists of a Card class, a Deck class and a TwoPlayerGame class. I have the classes written for the most part (still working on the TwoPlayerGame class), but I am not sure where to put the main()
Obviously in the Payer class.
The Card class does not know that the Deck class exists. It cannot reference it it would be a very bad design.
The Deck class is formed by Cards, it knows what is a Card but not what Blackjack, Poker, Bridge,... are so its code cannot reference your Players game.
The Blackjack class knows what a Deck of Cards is. It is there that the main() method goes.
Ok, cool. That explanation does help, thank you.
What would go inside the method though? I am just not sure what it is supposed to do.
#8
Re: Help with the main() function
Posted 18 January 2012 - 09:40 PM
public class Blackjack {
Deck deck;
Player player1, player2;
Blackjack() {
deck = new Deck();
player1 = new Player();
player2 = new Player();
}
void start() {
...
}
public static void main(String[] args) {
Blackjack bj = new Blackjack();
by.start();
}
}
#9
Re: Help with the main() function
Posted 19 January 2012 - 10:54 AM
Thank you. That was very helpful.
Page 1 of 1

New Topic/Question
Reply



MultiQuote



|