Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I built a football class and I want to take features like age from the user.
How do I do this with objects?


What I have tried:

package player;

public class SoccerPlayer {

	private final String name = "Cristiano";
	private final String family = "Ronaldo";
	private int age;
	private int height;
	private int weight;
	public int sumGoals = 0;
	public static final int numberOfMatches = 5;
	private int[] goal = new int[numberOfMatches];

	public void setAge(int age) {
		this.age = age;
	}

	public int getAge() {
		return age;
	}

	public SoccerPlayer(int h, int w) {
		if ((h >= 180 || h <= 190) && (w >= 70 || w <= 80)) {
			height = h;
			weight = w;
		}
			else
				System.out.println("Error");
				
		}

	

	public void print() {
		
		System.out.println("_______________Player information_____________");
		System.out.printf("Name: %s \nFamily: %s \n", name, family);
		System.out.printf("Height: %d \nWeight: %d \n", height, weight);
	
	}

}
/////////////////////////////////////////
package player;

public class MainClass {

	public static void main(String[] args) {
		SoccerPlayer player = new SoccerPlayer(187,79);
		player.setAge(35);
		player.print();
		System.out.println("Age:"+player.getAge());

	}

}
Posted
Updated 14-Sep-21 2:31am

1 solution

Your SoccerPlayer class should not be dealing with input, or output for that matter, at all. It's job is to maintain and manipulate the data on a player, and nothing more.

It should not have a print method. Replace that with something that returns a string representing the data how you want, like ToString().

In your case, the input and output should be handled in your main method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900