Results 1 to 19 of 19
Like Tree8Likes
  • 1 Post By ponder6168
  • 1 Post By Artemia
  • 1 Post By DarrylBurke
  • 1 Post By JosAH
  • 1 Post By DarrylBurke
  • 2 Post By jim829
  • 1 Post By jim829

Thread: How to decide when to use methods or classes

  1. #1
    ponder6168 is offline Member
    Join Date
    Jun 2015
    Location
    Missouri
    Posts
    10
    Rep Power
    0

    Default How to decide when to use methods or classes

    I want to let users modify a baseball team in 4 ways. 1) Modify the description of the team, 2) Modify the batting order of the team, 3) Modify one player on the team and 4) Modify one statistic for all of the players on the team. My question is do I 1) make a method for the Team class which modifies the team using four other methods, 2) make a TeamModifier class which modifies the team using four methods 3) make a Modifier interface and have a DescriptionOrderModifier class, a BattingOrderModifier class, a StatisticModifier class and a PlayerModifier class which implement the Modifier interface and have a Modifier class use an Abstract Factory and polymorphism to implement it or 4) something else. As I write the question #3 seems to fit what I have been learning but any ideas would be appreciated. Thanks!
    umar likes this.

  2. #2
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: How to decide when to use methods or classes

    if it was me I'd make the players a class with it's own methods and the team a class with it's own method.

    you use the methods in the player class to make changes to an individual player, these changes won't affect the team.

    the 'team' is basically an array of players anyway, so you can make changes to everyone in one go from there.
    (you could do this by making a method in the team class that invokes a certain method for all player objects in the array through a loop)
    and changes to batting order can be done by changing the order of the array.
    ponder6168 likes this.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: How to decide when to use methods or classes

    General rule: if it's a noun, it's a class. If it's a verb, it's a method.

    db
    ponder6168 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: How to decide when to use methods or classes

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: How to decide when to use methods or classes

    General rule: if it's a noun, it's a class. If it's a verb, it's a method.
    Nicely put, never looked at it like that but it makes a lot of sense.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: How to decide when to use methods or classes

    It's part of this exercise: https://en.wikipedia.org/wiki/Class-...aboration_card

    kind regards,

    Jos
    ponder6168 likes this.
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    ponder6168 is offline Member
    Join Date
    Jun 2015
    Location
    Missouri
    Posts
    10
    Rep Power
    0

    Default Re: How to decide when to use methods or classes

    Quote Originally Posted by JosAH View Post
    It's part of this exercise: https://en.wikipedia.org/wiki/Class-...aboration_card

    kind regards,

    Jos
    Thanks for the link.

  8. #8
    ponder6168 is offline Member
    Join Date
    Jun 2015
    Location
    Missouri
    Posts
    10
    Rep Power
    0

    Default Re: How to decide when to use methods or classes

    Quote Originally Posted by Artemia View Post
    if it was me I'd make the players a class with it's own methods and the team a class with it's own method.

    you use the methods in the player class to make changes to an individual player, these changes won't affect the team.

    the 'team' is basically an array of players anyway, so you can make changes to everyone in one go from there.
    (you could do this by making a method in the team class that invokes a certain method for all player objects in the array through a loop)
    and changes to batting order can be done by changing the order of the array.
    That is basically what I have. Thanks for the response!

  9. #9
    ponder6168 is offline Member
    Join Date
    Jun 2015
    Location
    Missouri
    Posts
    10
    Rep Power
    0

    Default Re: How to decide when to use methods or classes

    I didn't realize cross-posting was an issue so I apologize if I have offended anyone. I just wanted to get the widest possible range of opinions.

  10. #10
    ponder6168 is offline Member
    Join Date
    Jun 2015
    Location
    Missouri
    Posts
    10
    Rep Power
    0

    Default Re: How to decide when to use methods or classes

    Quote Originally Posted by DarrylBurke View Post
    General rule: if it's a noun, it's a class. If it's a verb, it's a method.

    db
    In this case I have four verbs applied to one noun. So I should have one class with four methods. But what happens if you have 10 verbs and one noun and your class gets too big? When do you decide to break it into two nouns. Such as having a BattingOrderModifier class (I think that name qualifies as a noun). Thanks!

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: How to decide when to use methods or classes

    Yes, that can happen.

    Recently, I had occasion to write a DataModel class. As it was reading and writing a database, all the PreparedStatements and their SQL became such a large part of the code that it was difficult to find the methods of interest, which returned selections of the data. I refactored this into four classes: DataModel, WriteableDataModel extends DataModel, with two helper utility classes (a utility class is a class with all static methods) DataLoader and DataSaver. That got all the JDBC code into the last two classes, which I never have to look at any more. Not unless I change the data structure, and that's not going to happen any time soon.

    db
    ponder6168 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    ponder6168 is offline Member
    Join Date
    Jun 2015
    Location
    Missouri
    Posts
    10
    Rep Power
    0

    Default Re: How to decide when to use methods or classes

    Thanks for the example.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to decide when to use methods or classes

    You also forgot an important aspect of Java: interfaces. You should try to incorporate interfaces into your class structure. It will make your class hierarchy more versatile and may allow you to more easily evolve your code.

    Regards,
    Jim
    DarrylBurke and ponder6168 like this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    ponder6168 is offline Member
    Join Date
    Jun 2015
    Location
    Missouri
    Posts
    10
    Rep Power
    0

    Default Re: How to decide when to use methods or classes

    Quote Originally Posted by jim829 View Post
    You also forgot an important aspect of Java: interfaces. You should try to incorporate interfaces into your class structure. It will make your class hierarchy more versatile and may allow you to more easily evolve your code.

    Regards,
    Jim
    Would option 3 of my original post be how you would recommend implementing an Interface in this code?

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to decide when to use methods or classes

    I see (forgot) you did mention it there (but not in your title). That "sounds" reasonable, but it is difficult for me to comment on a partial design. I just wanted to ensure you were aware of them.

    Regards,
    Jim
    ponder6168 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    ponder6168 is offline Member
    Join Date
    Jun 2015
    Location
    Missouri
    Posts
    10
    Rep Power
    0

    Default Re: How to decide when to use methods or classes

    Thanks! Would it be appropriate to post the code here to see if I am doing it correctly?

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to decide when to use methods or classes

    Yes!

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    ponder6168 is offline Member
    Join Date
    Jun 2015
    Location
    Missouri
    Posts
    10
    Rep Power
    0

    Default Re: How to decide when to use methods or classes

    I should have some code to post by tomorrow.

  19. #19
    ponder6168 is offline Member
    Join Date
    Jun 2015
    Location
    Missouri
    Posts
    10
    Rep Power
    0

    Default Re: How to decide when to use methods or classes

    Here is the code. Since I have menus everywhere in the program I decided to use a Menu interface instead of a modify interface. I asked a similar question at How to code menus and sub-menus (Testing forum at JavaRanch) and posted the code which calls this code if anyone wants to see the whole thing. Thanks in advance for any suggestions.
    Java Code:
    public interface   Menu {
    
    	void printMenuChoices();
    	int getNumberOfMenuChoices();
    	void presentMenuToUser(Menu menu);
    
    }
    public class ModifyTeamMenu implements Menu {
    	
    	private String menuChoices;
    	private static final int numberOfMenuChoices=5;
    	
    	enum ModifyTeamMenuChoices  {MODIFY_DESCRIPTON, MODIFY_BATTING_ORDER, MODIFY_PLAYER, MODIFY_STAT, QUIT}
    	
    	public ModifyTeamMenu() {
    		menuChoices= String.format("%n%s%n%n%s%n%s%n%s%n%s%n%s%n",
    				"Enter the number of your choice.",
    				"  1. Modify the team description.",
    				"  2. Modify the team batting order.",
    				"  3. Modify a player on the team.",
    				"  4. Modify the team statistic.",
    				"  5. Return to Main Menu.");
    	}
    
    	@Override
    	public void presentMenuToUser(Menu modifyTeamMenu) {
    		ModifyTeamMenuChoices modifyTeamMenuChoice;
    		do{
    			modifyTeamMenuChoice = getModifyTeamMenuUserChoice(modifyTeamMenu);
    			Menu usersChoice = getNewMenu(modifyTeamMenuChoice);
    			usersChoice.presentMenuToUser(usersChoice);
    		}while(userHasNotEnteredQuit(modifyTeamMenuChoice));
    	}
    
    	private ModifyTeamMenuChoices getModifyTeamMenuUserChoice(Menu modifyTeamMenu) {
    		Input userChoice = new Input();
    		int modifyTeamMenuUserChoice = userChoice.menuChoice(modifyTeamMenu);
    		return ModifyTeamMenuChoices.values()[modifyTeamMenuUserChoice];
    	}
    
    	private Menu getNewMenu(ModifyTeamMenuChoices modifyTeamMenuChoice) {
    		Menu newMenu;
    		switch(modifyTeamMenuChoice){
    		case MODIFY_DESCRIPTON: 
    			newMenu = new ModifyTeamDescriptionMenu();
    			break;
    		case MODIFY_BATTING_ORDER: 
    			newMenu = new ModifyTeamBattingOrderMenu();
    			break;
    		case MODIFY_PLAYER: 
    			newMenu = new ModifyPlayerOnTeamMenu();
    			break;
    		case MODIFY_STAT: 
    			newMenu = new ModifyStatOnTeamMenu();
    			break;
    		default:
    			newMenu = new QuitMenu();
    		}	
    		return newMenu;
    	}
    
    	private boolean userHasNotEnteredQuit(ModifyTeamMenuChoices modifyTeamMenuChoice) {
    		return modifyTeamMenuChoice!=ModifyTeamMenuChoices.QUIT;
    	}
    
    	@Override
    	public void printMenuChoices() {
    		System.out.print(menuChoices);
    	}
    
    	@Override
    	public int getNumberOfMenuChoices() {
    		return numberOfMenuChoices;
    	}
    }
    
    public class Input {
    
    	private int INVALID_MENU_CHOICE = -1;
    	private int menuChoice;
    
    	public int menuChoice(Menu menu){
    		do{
    			menu.printMenuChoices();
    			menuChoice = getInteger();
    		}while(menuChoiceIsInvalid(menu));
    		return menuChoice-1; //Adjust for zero index of array
    	}
    
    	private boolean menuChoiceIsInvalid(Menu menu) {
    		boolean inValid =!(0<menuChoice && menuChoice<=menu.getNumberOfMenuChoices());
    		if (inValid){
    			System.out.println("You did not enter an integer from 1 to "+menu.getNumberOfMenuChoices()+".");
    		}
    		return inValid;
    	}
    
    	private int getInteger() {
    		int userChoice;
    		try{
    			Scanner scan = new Scanner(System.in);
    			userChoice = scan.nextInt();
    		}catch(InputMismatchException e){
    			userChoice= INVALID_MENU_CHOICE;
    		}
    		return userChoice;
    	}
    }

Similar Threads

  1. Replies: 1
    Last Post: 07-12-2012, 11:04 PM
  2. Replies: 2
    Last Post: 07-12-2012, 11:04 PM
  3. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  4. Replies: 23
    Last Post: 04-18-2011, 06:54 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •