In Java
For the following questions, “define a class” means the following:
• Create an appropriately-named file containing the Java class definition, including the following:
– A block comment describing the file (and hence the class), as always
– Any instance variables, as required by the question
– Accessor (getter) and mutator (setter) methods for any instance variables
– Constructors as appropriate or as required by the question
– A toString method that prints instances of the class informatively
– Any additional methods as required by the question. All methods other than getters and setters MUST have a descriptive comment before their definition.
• Create a separate appropriately-named file containing a different Java class with a main method that tests your new class by doing the following:
– Create at least two instances of the class.
– Change at least one of them using the class’ methods.
– Print all the objects informatively
Document how to run your code and interpret the output in your README.txt file.
2. Define a class to represent a baseball player. For this question, baseball players will have played in some number of games, during which they will have had some number of at bats, made some number of hits, and scored some number of runs. Your class should include methods that compute and return a player’s batting average (hits per at bat) and runs per game. Your test program should show these methods in use (with informative output).
/*BaseballPlayer.java*/
public class BaseballPlayer {
private int numOfGames;//Number of games played
private int numAtBats;//Number of at bats
private int numOfHits;//Number of hits
private int numOfRuns;//Number of runs
//Getter
public int getNumOfGames() {
return numOfGames;
}
//Setter
public void setNumOfGames(int numOfGames) {
this.numOfGames = numOfGames;
}
//Getter
public int getNumAtBats() {
return numAtBats;
}
//Setter
public void setNumAtBats(int numAtBats) {
this.numAtBats = numAtBats;
}
//Getter
public int getNumOfHits() {
return numOfHits;
}
//Setter
public void setNumOfHits(int numOfHits) {
this.numOfHits = numOfHits;
}
//Getter
public int getNumOfRuns() {
return numOfRuns;
}
//Setter
public void setNumOfRuns(int numOfRuns) {
this.numOfRuns = numOfRuns;
}
//Default constructor
public BaseballPlayer() {
this.numOfGames = 0;
this.numAtBats = 0;
this.numOfHits = 0;
this.numOfRuns = 0;
}
//Parametrized constructor
public BaseballPlayer(int numOfGames, int numAtBats, int numOfHits, int numOfRuns) {
this.numOfGames = numOfGames;
this.numAtBats = numAtBats;
this.numOfHits = numOfHits;
this.numOfRuns = numOfRuns;
}
//toString method
public String toString() {
return "Number of games played = " + numOfGames +
"\nNumber of at bats = " + numAtBats +
"\nNumber of hits = " + numOfHits +
"\nNumber of runs = " + numOfRuns;
}
//Method to return batting average of the player per game
double getBattingAveragePerGame(){
if(numAtBats == 0 || numOfGames == 0)//To avoid division by 0
return 0.0;
return (double)numOfHits/(numAtBats * numOfGames);//Return batting average per game
}
//Method to return runs of the player per game
double getRunsPerGame(){
if(numOfGames == 0)//To avoid division by 0
return 0.0;
return (double)numOfRuns/numOfGames;//Return runs per game
}
}
/*Main.java*/
public class Main{
public static void main(String[] args) {
//Instance using default constructor
BaseballPlayer player1 = new BaseballPlayer();
//Instance using parametrized constructor
BaseballPlayer player2 = new BaseballPlayer(5, 20, 15, 18);
//Set player1 using setters
player1.setNumOfGames(20);
player1.setNumAtBats(50);
player1.setNumOfHits(37);
player1.setNumOfRuns(58);
System.out.println(player1);
System.out.println("Batting average per game = " + player1.getBattingAveragePerGame());
System.out.println("Runs per game = " + player1.getRunsPerGame());
System.out.println();
System.out.println(player2);
System.out.println("Batting average per game = " + player2.getBattingAveragePerGame());
System.out.println("Runs per game = " + player2.getRunsPerGame());
}
}
SAMPLE OUTPUT:

FOR ANY HELP JUST DROP A COMMENT
In Java For the following questions, “define a class” means the following: • Create an appropriately-named...
IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named file containing the Java class definition, including the following: – A block comment describing the file (and hence the class), as always – Any instance variables, as required by the question – Accessor (getter) and mutator (setter) methods for any instance variables – Constructors as appropriate or as required by the question – A toString method that prints instances of the class informatively –...
Create another Java class named EmployeeMain within the same project, which includes the main method. a. Include another class named Employee in the same Java file. This class has the following instance variables and instance methods. Define all the instance/static variables with private access modifier and constructors, instance/static methods with public access modifier. Instance Variables empID: int employeeName: String basicSalary: double Constructor Set the value for empID. Instance Methods Get and set methods for all instance variables. displayEmployee: Display the...
Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails(). Define a class named CashPayment that is...
Go to the Java API and review the Rectangle class briefly. As directed below, create a subclass of the Rectangle class called BetterRectangle as described below. Create another class called RectangleTester, which fully tests the BetterRectangle, by displaying a menu that allows a user to process data for multiple rectangles until they choose to quit. Be sure to include sample runs as block comments in your tester source code file. P9.10 The Rectangle class of the standard Java library does...
This is in Java, thanks! d Define two subclasses as below: 1) “Advanced Java” with an additional field called “grades_in_class” 2) “Web Technology” with an additional field called “grades_Quizzes”. Define the required constructors and getter methods in both classes. e Override the computeGrade() in both classes as below: 1. In Advanced Java, Fgrade= 40%* avg_exams+ 40%* avg_Assignments + 20* grades_in_class 2. In Web Technology, Fgrade= 30%* avg_exams+ 50%* avg_Assignments + 20*grades_Quizzes Note: Fgrade is a local variable in the computeGrade()...
Java - Object Oriented Programming
Declare a class named Customer that has two private fields? Write a set method to make sure that if age > 125 years or less than 0 then it is set to 0 (default value) What does it indicate when declaring a class's instance variables with private access modifier? What is the role of a constructor? The data part of by an object's instance variables is known as? Do all methods have to always return...
java
Practice Program 2: Create a project named Student that will cont ain two classes: Student and StudentClient Student class Instance variables . Name Social security number Gpa Methods: Constructor that accepts all three instance variables Getters for all three instance variables .Setters for name and social security number .Setter for gpa should only set the value if the value is between 0 and 4.0. toString-Outputs the values of the instance variables in this format: name: Smith; SSN: 333-99-4444 GPA:...
In JAVA
1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...
This is a java program that runs on the Eclipse.
Java Programming 2-1: Java Class Design Interfaces Practice Activities Lesson objectives: Model business problems using Java classes Make classes immutable User Interfaces Vocabulary: Identify the vocabulary word for each definition below. A specialized method that creates an instance of a class. A keyword that qualifies a variable as a constant and prevents a method from being overridden in a subclass. A class that it can't be overridden by a subclass,...
Create a class named Pizza that scores information about a single pizza. It should contain the following: Private instance variable to store the size of the pizza (either small, medium, or larger), the number of cheese toppings, the number of pepperoni toppings, and the number of ham toppings. Constructor(s) that set all of the instance variables. Public methods to get and set the instance variables. A public method named calsCost( ) that returns a double that...