Using This Class I need to make a program I have to make a program using the cla
ID: 653933 • Letter: U
Question
Using This Class I need to make a program
I have to make a program using the class below :creating a menu one for 1.creating a player, 2.edit stats, 3.display, and 4.quit
When i type 1 it should make me create a player -type in player first and last name, at bats , hits. When i type 2 it should let do the same thing. When i type 3 it should display the at bats, hits and average. When i type 4 it should quit the program. CAN NOT USE DO WHILE
/**********************************************************
* Program Name : Practice Coding - Creating Classes
* Author :
* Date : April 01, 2010
* Course/Section : CSC111 - 01
* Program Description:
*
* Methods:
* -------
* Main - manages Class Player
*
**********************************************************/
public class Class Player
{
//class constants
//class variables
private String name;
private int hits;
private int atBats;
private float batAvg;
/**********************************************************
* Method Name : Constructor
* Author :
* Date : April 01, 2010
* Course/Section : CSC111 - 01
* Program Description:
*
* BEGIN Constructor
*
* END Constructor
**********************************************************/
public Player(String inName)
{
//local constants
//local variables
/******************** Start main method *****************/
//create a constructor that will receive the name of the player and init all other class data to 0
name = inName;
hits = 0;
atBats = 0;
badAvg = 0;
}//end constructor
/**********************************************************
* Method Name : updateGrades
* Author :
* Date : April 01, 2010
* Course/Section : CSC111 - 01
* Program Description:
*
* BEGIN updateGrades
*
* END updateGreades
**********************************************************/
public void addToTotal(int hits, int atBats)
{
//local constants
//local variables
int start_Hits;
int start_AtBats;
/******************** Start main method *****************/
//create a method that will receive hits and at bats for a player and add them to his total
start_Hits = start_Hits + hits;
start_AtBats = start_AtBats + atBats;
}//end update
/**********************************************************
* Method Name : updateAverage
* Author :
* Date : April 01, 2010
* Course/Section : CSC111 - 01
* Program Description:
*
* BEGIN toString
*
* END Creating Methods
**********************************************************/
public void updateAverage()
{
//local constants
//local variables
/******************************************************/
//create a method that will calculate a players batting average using the current instance data
start_BatAvg = (float)start_Hits / (float)start_AtBats;
}//end toString
/**********************************************************
* Method Name : toString
* Author :
* Date : April 01, 2010
* Course/Section : CSC111 - 01
* Program Description:
*
* BEGIN toString
*
* END Creating Methods
**********************************************************/
public String toString()
{
//local constants
//local variables
/******************************************************/
//create a method that will calculate a players batting average using the current instance data
System.out.println(name =" + name + ", hits =" + hits +", atBats =" + atBats + ", batAvg =" + batAvg + ");
}//end toString
} //end Creating Classes
Explanation / Answer
//Player.java
import java.text.DecimalFormat;
public class Player
{
//variables
private String firstName;
private String lastName;
private int hits;
private int atBats;
private float batAvg;
//default constructor that sets the default values
public Player()
{
//local constants
/* Create a constructor that will receive the name of the player
and init all other class data to 0 */
firstName = "";
lastName="";
hits = 0;
atBats = 0;
batAvg = 0;
}//end constructor
//parameterized constructor that sets the first name,last name,
//hits and bats to the class variables
public Player(String firstName,String lastName, int hits,int atBats)
{
this.firstName = firstName;
this.lastName=lastName;
this.hits = hits;
this.atBats = atBats;
batAvg = 0;
}//end constructor
//setter method that set hits
public void setHits(int hits)
{
this.hits=hits;
}
//Returns the hits
public int getHits()
{
return hits;
}
//setter method that set bats
public void setBats(int bats)
{
this.atBats=bats;
}
//Returns the bats
public int getBats()
{
return atBats;
}
public void addToTotal(int hits, int atBats)
{
//Add hits to the class hits variable
this.hits = this.hits + hits;
//Add hits to the class hits variable
this.atBats=this.atBats+atBats;
}//end update
public void updateAverage()
{
//create a method that will calculate a players batting average using the current instance data
batAvg = (float)hits / (float)atBats;
}//end toString
public String toString()
{
String playerStringDescription;
DecimalFormat formatter=new DecimalFormat("##.00");
updateAverage();
playerStringDescription="Name"+firstName+" "+lastName+
" Hits = "+hits +
" Bats = "+ atBats+
" BatAvg = "+formatter.format(batAvg);
return playerStringDescription;
}//end toString
} //end Creating Classes
--------------------------------------------------------------------
//The java program that allows user to create a player and allows
//player to enter first name, last name ,hits and bats .
//And also allows user to change the stats hits,and bats of the palyer
//and allows user to select to display the stats of a player
//and finally allows user to exit from proram
//MenuProgram.java
import java.util.Scanner;
public class MenuProgram
{
public static void main(String[] args)
{
//Scanner class to read input
Scanner reader=new Scanner(System.in);
//Create an instance of Player class
Player player=null;
while(true)
{
//calls the menu method that retusnt the menu choice of user
switch (menu())
{
case 1:
System.out.println("Enter first name");
String firstName=reader.nextLine();
System.out.println("Enter last name");
String lastName=reader.nextLine();
System.out.println("Enter hits");
int hits=reader.nextInt();
//read end of line or enter key
reader.nextLine();
System.out.println("Enter bats");
int atBats=reader.nextInt();
//read end of line or enter key
reader.nextLine();
//create a player instance with calling its parameter constructor
//that sets the first name,last name ,hits and bats
player=new Player(firstName, lastName, hits, atBats);
break;
case 2:
//change the hits and bats of a player
System.out.println("Edit hits");
hits=reader.nextInt();
//read end of line or enter key
reader.nextLine();
System.out.println("Edit bats");
atBats=reader.nextInt();
//read end of line or enter key
reader.nextLine();
//call addToTotal method that adds the hits and atBats to player object
player.addToTotal(hits, atBats);
break;
case 3:
//display the player statistics
System.out.println("Player Statistics");
System.out.println(player.toString());
break;
case 4:
//exit the program
System.out.println("Exiting from program");
System.exit(0);
break;
default:
//display invalid choice message if user enter a invalid choice
System.out.println("Invalid choice");
break;
}
}
}
//menu method that prompts the user to enter the choice
private static int menu()
{
Scanner reader=new Scanner(System.in);
System.out.println("1.Create a player");
System.out.println("2.Edit player");
System.out.println("3.Display stats");
System.out.println("4.Quit");
System.out.println("Your choice");
int choice=reader.nextInt();
//return user choice
return choice;
}
}
--------------------------------------------------------------------
Sample output:
1.Create a player
2.Edit player
3.Display stats
4.Quit
Your choice
1
Enter first name
kunal
Enter last name
sng
Enter hits
10
Enter bats
10
1.Create a player
2.Edit player
3.Display stats
4.Quit
Your choice
3
Player Statistics
Namekunal sng
Hits = 10
Bats = 10
BatAvg = 1.00
1.Create a player
2.Edit player
3.Display stats
4.Quit
Your choice
2
Edit hits
20
Edit bats
30
1.Create a player
2.Edit player
3.Display stats
4.Quit
Your choice
3
Player Statistics
Namekunal sng
Hits = 30
Bats = 40
BatAvg = .75
1.Create a player
2.Edit player
3.Display stats
4.Quit
Your choice
2
Edit hits
10
Edit bats
10
1.Create a player
2.Edit player
3.Display stats
4.Quit
Your choice
3
Player Statistics
Namekunal sng
Hits = 40
Bats = 50
BatAvg = .80
1.Create a player
2.Edit player
3.Display stats
4.Quit
Your choice
2
Edit hits
10
Edit bats
10
1.Create a player
2.Edit player
3.Display stats
4.Quit
Your choice
3
Player Statistics
Namekunal sng
Hits = 50
Bats = 60
BatAvg = .83
1.Create a player
2.Edit player
3.Display stats
4.Quit
Your choice
4
Exiting from program
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.