|
Hi, I'm practicing polymorphism,
inheritance and abtraction, my tostring method in the base class isn't printing to the console only the child class
public abstract class Animal
{
private String name;
private int age;
private String breed;
private String furColor;
public Animal(String name,int age,String breed,String furColor)
{
this.name = name;
this.age = age;
this.breed = breed;
this.furColor = furColor;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return this.age;
}
public void setBreed(String breed)
{
this.breed = breed;
}
public String getBreed()
{
return this.breed;
}
public void setFurColor(String furColor)
{
this.furColor = furColor;
}
public String getFurColor()
{
return this.furColor;
}
public abstract void sound();
public string ToStirng()
{
return ("Name: " + getName()
+"\n"
+"Age: " + getAge()
+"\n"
+"Breed: " + getBreed()
+"\n"
+"Fur Color: " + getFurColor());
}
}
<pre>public class Cat : Animal
{
private String favoriteToy;
public Cat(String name,int age,String breed,String furColor,String favoriteToy):base(name,age,breed,furColor){
this.favoriteToy = favoriteToy;
}
public void setFavoriteToy(String favoriteToy)
{
this.favoriteToy = favoriteToy;
}
public String getFavoriteToy()
{
return this.favoriteToy;
}
public override void sound()
{
Console.WriteLine( getName() + " is a beautiful cat, he is " + getAge() + " and he likes to Meow.");
}
public override string ToString() => (base.ToString() +
"\n"
+ "Favorite Toy: " + getFavoriteToy());
}
static void Main(string[] args)
{
Cat C = new Cat("Fluffy",4,"Tabby","Ginger","Mouse");
Console.WriteLine(value: C.ToString());
C.sound();
}
its only printing out "Favorite toy" and the sound method, would appreciate any help please.
|
|
|
|
|
Get to know the debugger. It would easily help you out with this.
public string ToStirng()
{
return ("Name: " + getName()
+"\n"
+"Age: " + getAge()
+"\n"
+"Breed: " + getBreed()
+"\n"
+"Fur Color: " + getFurColor());
}
Why do you have all these get and set methods? You could easily just replace them with properties:
public abstract class Animal
{
public string Name { get; set; }
public int Age { get; set; }
public string Breed { get; set; }
public string FurColor { get; set; }
public Animal(string name, int age, string breed, string furColor)
{
Name = name;
Age = age;
Breed = breed;
FurColor = furColor;
}
public abstract void Sound();
public override string ToString()
{
return $"Name: {Name}\nAge: {Age}\nBreed: {Breed}\nFur Color: {FurColor}";
}
}
|
|
|
|
|
|
If it's any consolation, I read what I meant to type as well
public string ToStirng()
{
return ("Name: " + getName()
+"\n"
+"Age: " + getAge()
+"\n"
+"Breed: " + getBreed()
+"\n"
+"Fur Color: " + getFurColor());
}
And we've all mistyped ToString before ...
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
How did I miss that, thanks for that.
|
|
|
|
|
As I said - I read what I meant to write ... you probably did as well.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi, I'm relatively new to the vast world of code. I've used google to find tutorials and watched plenty of videos on "how to code..." I'm learning C# at the moment and I'm extremely basic in my knowledge. I under the concept of object-oriented coding. However, I draw a blank when taking the next step in the learning process - ie: what do I build? how do I design it? etc.
If anyone can help point me in the right direction that would be awesome! Thank you.
|
|
|
|
|
Member 14511008 wrote: what do I build? Something simple; don't start building an Operating System yet.
If you really need a project to start, try a notepad. Once you're done, add a versioning system.
Member 14511008 wrote: how do I design it? Too soon; don't worry about that. Try to get your objects to be as "SOLID" as can be (small, simple). In time you'll learn patterns and with experience, you'll be able to design stuff up front.
Don't run, but walk. We'll be here to kick you along the road if need be
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Sorry wrong reply
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
np, I did that too
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
When you progress past the very basic and into the design an application stage, pick a subject you know or a close friend knows well. It will strain a friendship as you need to understand how the process works and that take time and skill.
I started by building a dive logging system as my son was a scuba diver and I could use his domain knowledge to gather the requirements.
As Eddy suggested try and duplicate a few of the simple tools around, notepad, calculator maybe solitaire (ignoring the graphics and concentrating on the functionality).
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
I found Andrew Troelsen's book on C# (available from Apress - if you want the latest edition) to be an excellent introduction.
Ger
|
|
|
|
|
|
The way I taught myself coding was to find an app, and write code to duplicate it as precisely as possible.
I often suggest trying to duplicatre Windows Notepad. That should get you reasonably comfortable in your chosen language and framework.
The only way you're going to learn it is by doing it.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I wrote a few "casino games" when starting out, like Blackjack / 21.
You learn about "collections" (of cards) and random number generation. You can get fancy with "playing card images" found on the web.
You're somewhat motivated to "beat your own game" while making the game as true to life as possible.
You'll find out that once you have the game "right", the house always wins in the long run.
If not, you go on tour.
(I've moved on to horse races since then).
The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.'
― Confucian Analects
|
|
|
|
|
Hi. I am a beginner with C# and tried using visual studios to make a simple program.
Console.WriteLine("What is your name: ");
string name = Console.ReadLine();
Console.WriteLine("What is your age: ");
string age = Console.ReadLine();
Console.WriteLine("What is your occupation: ");
string occupation = Console.ReadLine();
Console.WriteLine(? "{name}, is {age} years of age and he works as a {occupation}.");
The error message is CS1001 (Identifier expected) for the last line of code.
Anyone knows what is wrong?
|
|
|
|
|
|
Oh my god i completely forgot. Thank you so much. God knows i would have been stuck on it for about a week. My code works perfectly now.
|
|
|
|
|
I'm glad Richard has fixed that for you, but would you mind a suggestion?
Your code lacks what we call "validation" - which means that you don't check what the user types at all. As a result, he can enter anything he likes for "Age" and you will accept it - even if it isn't a number.
Instead of "trusting the user to get it right" it's a good idea to validate the input and check that what he entered is legit, or you will get problems later on. (It doesn't really matter for this code, but if you start to expand it then it will cause you grief).
So let's check that his Age is actually a number:
int age;
string ageInput;
do
{
Console.WriteLine("What is your age: ");
ageInput = Console.ReadLine();
} while (!int.TryParse(ageInput, out age));
What that does is sets up a loop:
do
{
...
} while (condition); which continues to go round until condition is no longer true.
Inside the loop, we prompt the user and read his response:
do
{
Console.WriteLine("What is your age: ");
ageInput = Console.ReadLine();
} while (...);
And finally we try to convert his input to an integer:
do
{
...
} while (!int.TryParse(ageInput, out age)); int.TryParse is a standard method which takes two parameters: the input string and a place to put the result (don't worry about the word out in there - you'll come to that later), then returns a boolean value (true or false ) which says "It converted OK" (true ) or "That wasn't an integer he entered" (false ). We put an exclamation mark in front of that to "reverse the decision" so that true becomes false and vice versa, so the loop goes round until we do get an integer.
Now, you can do maths with his input:
Console.WriteLine($"{name}, is {age} years of age and he works as a {occupation}.");
Console.WriteLine($"Next year, {name} will be {age + 1} years old."); because you have validated his input and are sure it's a "real number".
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
//User "$" instead of "?" sign.
Console.WriteLine($"{name}, is {age} years of age and he works as a {occupation}.");
Console.ReadKey();
|
|
|
|
|
Can you please explain
administratively scalable with examples?
Third, a system can be administratively scalable, meaning that it can still be easy to manage even if it spans many independent administrative organizations
|
|
|
|
|
|
I'm starting to detect a strong whiff of "homework" here with these two and your QA question: and the idea is that you think about the question and what you have been taught, and post your ideas; not ours. Part of the idea of homework is to get you thinking in a particular way rather than just parroting what you have been told.
Copy'n'paste from someone else doesn't teach you anything - you don't even remember what it was you copied in any detail. So if questions about this material come up in your exams you can't answer them; and future material which assumes you understand this stuff becomes a lot harder for you to work with.
So try it: read the document, review your lectures. And think about what it means then try to answer it yourself. Just getting our thoughts only helps you briefly in the short term - it damages your chances in the longer term, honest!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Can you please explain the bolded part?
When a system needs to scale, very different types of problems need to be solved. Let us first consider scaling with respect to size. If more users or resources need to be supported, we are often confronted with the limitations of centralized services, data, and algorithms (see Fig. 1-3). For example, many services are centralized in the sense that they are implemented by means of only a single server running on a specific machine in the distributed system. The problem with this scheme is obvious: the server can become a bottleneck as the number of users and applications grows. Even if we have virtually unlimited processing and storage capacity, communication with that server will eventually prohibit further growth.
modified 24-Jun-19 3:39am.
|
|
|
|
|
Don't post the same question in multiple forums.
|
|
|
|
|