Question: To start, go and grab the starter files either from Blackboard or from the end of this document. There's two of them: Program.cs and Cat.cs
To start, go and grab the starter files either from Blackboard or from the end of this document. There's two of them: Program.cs and Cat.cs Copypaste them into your editor to get started. You can put the Cat class in the same file as the Program class, if you'd prefer it that way.
Once you have them loaded in your editor, take a look around the code. You'll notice that there are some comments in both files; some of them delimited with and some of them with Everywhere you see one of those style comments is somewhere you need to write some code of your own!
Part : Finishing the Cat class
Open Cat.cs In that file, you've got three things to do First, constructors:
Create a public constructor that accepts the cat's name as a parameter and leaves the other two properties as zero their default values
Create a second public constructor with parameters for all three of the cat's properties: name, age, and weight.
These should be pretty selfexplanatory. Refer back to your code from COISH if you don't remember how to write a constructor.
Next, we want some way to be able to print out and inspect our Cats when we're working in the console. There are a few ways to do this. Something you may have seen before is a void Print method that takes no parameters, returns nothing, and prints an object's description to the console. This approach is pretty limiting though, since it forces you to print to the console if you ever want to inspect your objects.
The "proper" way to do this is to implement the ToString method on our object. This method gets called automatically whenever you print something to the console. For example:
class Person
public string Name get; set;
public Personstring name this.Name name;
public override string ToString return Name;
Person matt new PersonMatt;
Console.WriteLineHi Im matt; Prints, Hi Im Matt!"
Notice that the ToString method gets a special "override" keyword in its method signature. That's because this method exists on every single object in C# and is inherited by all other classes. You don't have to worry about that for now, though. Just know that you should add this override keyword whenever you're adding ToString to an object.
Anyways, let's implement this method for our feline friends. Obviously, we want to return a string containing the cat's name; but it would also be nice if we got the age and weight, too.
In the skeleton provided, use string interpolation see below to finish implementing the ToString method. We want the cat's name, followed by its age and weight in brackets, all returned as a single string. So for Sri's cat Jawa, we would expect to get the string "Jawa years old, kg
"String interpolation" probably sounds scary, but I promise it isn't. Microsoft has a quick little guide but I think a single example will probably suffice:
int x ;
string someText $x x xx x;
Console.WriteLinesomeText; x x
That is if you put a dollarsign before the first quote, you get to do string formatting inside your string! I think you'll agree that that's much nicer than plus'ing a bunch of strings together all in a row like this: x x xx x
Basically, string interpolation lets you insert variables directly into strings, which makes it much easier to read and write strings with dynamic contents. We want you to continue to use this method of creating strings throughout the course, since it's so much nicer to read or at least, Matt does, who knows if Sri cares or not
Once you've got that up and running, you can now print your cats! I would advise creating and printing a few test cats to make sure things are appearing the way they should be You can copypaste this snippet into the top of your Main method, if you'd like:
Cat cat new CatTom;
Console.WriteLinecat; Should print "Tom years old, kg
Cat jawa new CatJawa;
Console.WriteLinejawa; "Jawa years old, kg
part starts on next page
Part : The main program
A lot of these should be selfexplanatory, but of course don't forget to ask for help if you get stuck. There'll be some hints after the checklists
First up let's get our actual Main method working.
In the "print" case in the main menu, call the provided PrintCats method.
In the "new" case in the main menu, call the provided AppendCat method. However:
a Only call the method if the user does not already have too many cats!
b Otherwise, print a message, "You have enough cats already!".
After the dowhile loop, when the user has quit the program, print out a final tally of the user's cats.
a First, print something along the lines of In the end, you had these cats:".
b Second, call another method Ill let you guess which one to print the user's cats one last time.
Next, we're goin
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
