Java Programming, Loose-leaf Version
Java Programming, Loose-leaf Version
8th Edition
ISBN: 9781337685917
Author: Joyce Farrell
Publisher: Cengage Learning
Expert Solution & Answer
Book Icon
Chapter 3, Problem 2GZ

Explanation of Solution

Program code:

MyCharacter.java

//define a class MyCharacter

public class MyCharacter

{

//define the class member variables

private String name;

private int health;

private int power;

//constructor

public MyCharacter()

{

}

//constructor

public MyCharacter(String n, int h, int p)

{

//initialize the variables

name = n;

health = h;

power = p;

}

//define the method getName()

public String getName()

{

//return the value of name

return name;

}

//define the method setName()

public void setName(String name)

{

//set the value of name

this.name = name;

}

//define the method getHealth()

public int getHealth()

{

//return the value of health

return health;

}

//define the method setHealth()

public void setHealth(int health)

{

//set the value of health

this.health = health;

}

//define the method getPower()

public int getPower()

{

//return the value of power

return power;

}

//define the method setPower()

public void setPower(int power)

{

//set the value of power

this.power = power;

}

}

Explanation:

The above snippet of code is used to create a class “MyCharacter”. In the code,

  • Import the required header files.
  • Define a class “MyCharacter”
    • Declare the class variables “name”, “health” and “power”.
    • Define the constructor “MyCharacter”.
      • Set the values of variables “name”, “health” and “power”.
    • Define the “getName()” method.
      • Return the value of “name”.
    • Define the “setName()” method...

Blurred answer
Students have asked these similar questions
In a Java program, create an automobile class that will be used by a dealership as a vehicle inventory program. The following attributes should be present in your automobile class: private string make, private string model, private string color, private int year, private int mileage. Your program should have appropriate methods such as: constructor, add a new vehicle, remove a vehicle, update vehicle attributes. At the end of your program, it should allow the user to output all vehicle inventory to a text file.
Create a simple java program that reads all the content in a .txt file, the user will enter the filename to be opened. In this case, the file will contain multiple lines of Car details with each line containing a single car detail. Your task is to then create a Car class that models the Make, Colour, type and year manufactured. The Car class should further be able to assign every car created with a unique serial number using a class variable[format is AAAYYYYCXX, AAA is the first 3 letters of the make, YYYY is the year it was manufactured, C is the first letter of the colour and XX is the value provided by the class variable(in form 01) all in upper caps see samples]. Within the driver class add all the cars read from the file onto a stack following LIFO approach and then print details of 2 cars from the stack that are older then 5 years. You are additionally expected to deal with most common exceptions(See samples below) [HINT: DO NOT PROVIDE A PATH BUT RATHER ONLY OPEN THE FILE WITH…
Create a simple java program that reads all the content in a .txt file, the user will enter the filename to be opened. In this case, the file will contain multiple lines of Car details with each line containing a single car detail. Your task is to then create a Car class that models the Make, Colour, type and year manufactured. The Car class should further be able to assign every car created with a unique serial number using a class variable[format is AAAYYYYCXX, AAA is the first 3 letters of the make, YYYY is the year it was manufactured, C is the first letter of the colour and XX is the value provided by the class variable(in form 01) all in upper caps see samples]. Within the driver class add all the cars read from the file onto a stack following LIFO approach and then print details of 2 cars from the stack that are older then 5 years. You are additionally expected to deal with most common exceptions(See samples below) [HINT: DO NOT PROVIDE A PATH BUT RATHER ONLY OPEN THE FILE WITH…
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
  • Text book image
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781337671385
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT