Write a C# Class that will hold animal data in a file called animal.cs. The clas
ID: 3740772 • Letter: W
Question
Write a C# Class that will hold animal data in a file called animal.cs.
The class should have appropriate data type fields for animal type, breed, color, gender, and cost.
There should be properties for all fields.
Create a default constructor that will set the fields to “Cat”, “Siamese”, “Seal Point”, “Male”, 5000.00. Values match the order of fields above.
Also build a constructor that will accept the all field values to instantiate when needed.
Add a field to store sales tax. Do not add this field to the constructors built in the instructions above. You will need to build a method to calculate sales tax of 7.5 percent based on the cost and store that amount in the field.
Your gender public property should look for the values “F” or “M” and change the value to “Female” or “Male”.
You do NOT have to create a program that uses this class; only create the class in your Visual Studio project.
Explanation / Answer
class Animal
{
public String type;
public String breed;
public String gender;
public String color;
public double cost;
public double tax;
// constructor
public Animal()
{
this.type = "Cat";
this.breed = "Siamese";
this.color = "Seal Point";
this.gender = "Male";
this.cost = 5000.00;
}
// constructor
public Animal(String type, String breed, String gender, String color, double cost)
{
this.type = type;
this.breed = breed;
this.color = color;
this.gender = gender;
if( this.gender.Equals("M") || this.gender.Equals("m") )
this.gender = "Male";
else if( this.gender.Equals("F") || this.gender.Equals("f") )
this.gender = "Female";
this.cost = cost;
}
public void calculateSalesTax()
{
// tax = 7.5% of cost
this.tax = 7.5 * this.cost / 100.0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.