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. Copy-paste 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 1: Finishing the Cat class
Open Cat.cs. In that file, you've got three things to do. First, constructors:
1. Create a public constructor that accepts the cat's name as a parameter and leaves the other two properties as zero (their default values).
2. Create a second public constructor with parameters for all three of the cat's properties: name, age, and weight.
These should be pretty self-explanatory. Refer back to your code from COIS-1020H 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 Person(string name){ this.Name = name; }
public override string ToString(){ return Name; }
}
Person matt = new Person("Matt");
Console.WriteLine("Hi, I'm {0}!", matt); // Prints, "Hi, I'm 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.
3. 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 (9 years old, 8.7 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 =5;
string someText = $"x ={x}==> x^2={x * x}.";
Console.WriteLine(someText); // x =5==> x^2=25.
That is, if you put a dollar-sign 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 +"==> x^2="+(x * 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 copy-paste this snippet into the top of your Main() method, if you'd like:
Cat cat1= new Cat("Tom");
Console.WriteLine(cat1); // Should print "Tom (0 years old, 0 kg)"
Cat jawa = new Cat("Jawa",9,8.7);
Console.WriteLine(jawa); // "Jawa (9 years old, 8.7 kg)"
(part 2 starts on next page)
Part 2: The main program
A lot of these should be self-explanatory, but of course don't forget to ask for help if you get stuck. There'll be some hints after the checklist(s).
First up, let's get our actual Main method working.
1. In the "print" case in the main menu, call the provided PrintCats method.
2. 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!".
3. After the do-while 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 (I'll 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!