Java assignment 09: Selecting basketball players

  Quizzes     Assignments     Puzzles     Easy exercises     Required knowledge  
Java Assignments   Green = Easy,  Blue = Normal,  Red = Hard
Select    01   02   03   04   05   06   07   08   09   10   By title 

Write a simple program that allows users to insert the age and the height of
new basketball players. If the age of the player is between 16 and 30 years
and his height is 180 or higher, your program should write a message that the player is selected otherwise the player is rejected as shown below.

Insert the age: 28
Insert the height: 181
The player is selected

Java assignment 09: Selecting basketball players

Level: Normal

 

import java.util.Scanner;

public class Basketball
{
  public void acceptance(int age, int height)
  {
    if(age > 16 && age < 30 && height >= 180)
    {
      System.out.println("The player is selected");
    }
    else
    {
      System.out.println("The player is rejected");
    }
  }
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    System.out.print("Insert the age: ");
    // the age inserted by the user
    int age = input.nextInt();
    System.out.print("Insert the height: ");
    // the height inserted by the user
    int height = input.nextInt();
    Basketball bb = new Basketball();
    // Invoke the method acceptance
    bb.acceptance(age, height);
    input.close();
  }
}

 

Author: Sar Maroof

Answer Explanation


If you run the above program the following steps will happen.

  1. The program asks the user to insert the age of the player and press the enter button.
  2. The program asks the user to insert the height of the player and press the enter button.
  3. The program writes a message whether the player is selected or rejected based on the age and the height of the player.

Extra assignment

Add your own code to the program to inform the user to insert a number if the user enters symbols or letters.

 




Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.

 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

Leave a Reply

Your email address will not be published. Required fields are marked *