Create a program called TennisWinPercentArray.java. In the main method • Ask the
ID: 3719264 • Letter: C
Question
Create a program called TennisWinPercentArray.java.
In the main method
• Ask the user for the name of a tennis player
• Ask the user for the number of years the tennis player has played competitive tennis. Use this value as the size of a double array.
• Ask the user for the year the tennis player began playing competitive tennis
• In a for loop, ask the user to enter the number of wins and losses starting from the first year of play and store the win percentage (wins/(wins+losses)) value in the array. The element at index 0 will store the win percentage for the player’s first year.
• Call the method printWinPercentage with the player’s name, the first year of play and the array
In the printWinPercentage method
• There should be three parameters: a String for the player’s name, int for the first year of play, and a double array
• Display the player’s name
• Display the year and the win percentage for each year with 1 number after the decimal.
Explanation / Answer
Source Code:-
------------------------
package com.samples;
import java.text.DecimalFormat;
import java.util.Scanner;
public class TennisWinPercentArray
{
private static DecimalFormat format = new DecimalFormat(".#");
public static void printWinPercentage(String PlayerName,int year,double percentage)
{
System.out.println("Players Name: "+PlayerName);
System.out.println("Year of Playing: "+year);
System.out.println("Win Percentage :"+format.format(percentage));
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Please Enter the name of a tennis player");
String PlayerName=sc.next();
System.out.println("Please Enter number of years played Tennis");
int noof_years=sc.nextInt();
double array[]=new double[noof_years];
double wins,losses;
for(int i=0;i<noof_years;i++)
{
System.out.println("Enter "+(i+1)+" Year Records");
System.out.println("Please enter the No of. Losses");
losses=sc.nextInt();
System.out.println("Please Enter the No.of Wins");
wins=sc.nextInt();
double percentage=(wins/(wins+losses));
array[i]=percentage;
printWinPercentage(PlayerName,i+1,array[i]);
}
sc.close();
}
}
Sample Output:-
----------------------
Please Enter the name of a tennis player
venkanna
Please Enter number of years played Tennis
5
Enter 1 Year Records
Please enter the No of. Losses
3
Please Enter the No.of Wins
2
Players Name: venkanna
Year of Playing: 1
Win Percentage :.4
Enter 2 Year Records
Please enter the No of. Losses
10
Please Enter the No.of Wins
9
Players Name: venkanna
Year of Playing: 2
Win Percentage :.5
Enter 3 Year Records
Please enter the No of. Losses
2
Please Enter the No.of Wins
8
Players Name: venkanna
Year of Playing: 3
Win Percentage :.8
Enter 4 Year Records
Please enter the No of. Losses
9
Please Enter the No.of Wins
2
Players Name: venkanna
Year of Playing: 4
Win Percentage :.2
Enter 5 Year Records
Please enter the No of. Losses
4
Please Enter the No.of Wins
10
Players Name: venkanna
Year of Playing: 5
Win Percentage :.7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.