Learning Setter and Getter
I am making a console log app where I create a Box class and make an object and set values: width, height, and length using setter and getter. I was referencing an solution on github, but I haven't make it work yet. I don't know where I made mistake.
My Code
using System;
namespace ClassDemo2
{
class Box
{
private double _width;
private double _height;
private double _length;
public double Width
{
get { return _width; }
set { this._width = Width; }
}
public double Height
{
get { return _height; }
set { this._height = Height; }
}
public double Length
{
get { return _length; }
set {this._length = Length; }
}
public double volume()
{
return Width * Height * Length;
}
}
public class Program
{
static void Main(string[] args)
{
Box box = new Box();
//Set value
box.Width = 12;
box.Height = 12;
box.Length = 12;
//Get value
double width = box.Width;
double height = box.Height;
double length = box.Length;
Console.WriteLine("Box properties");
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Height: {0}", height);
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Volume: {0}", box.volume());
}
}
}
Console Window
The setters in the Box class aren't doing anything. The setters aren't assigning a value to the private fields in the Box class.
You may as well remove the fields altogether and use auto-implemented properties.
For example:
class Box
{
public double Width { get; set; }
public double Height { get; set; }
public double Length { get; set; }
public double Volume()
{
return Width * Height * Length;
}
}
class Box
{
public double Width { get; set; }
public double Height { get; set; }
public double Length { get; set; }
public double volume()
{
return Width * Height * Length;
}
}
Use Auto-Implemented Properties (https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties)
Related
How do I fix the error "Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?). I don't see where I am changing an int to a double. Here's the following code I am working with:
namespace Chapter_9
{
class Program
{
static void Main(string[] args)
{
Circle create = new Circle();
create.SetRadius(2);
WriteLine("Radius = {0}", create.GetRadius());
WriteLine("Diameter = {0}", create.GetDiameter());
WriteLine("Area = {0}", create.GetArea());
}
}
class Circle
{
private int Radius;
private readonly int Diameter;
private readonly double Area;
public Circle()
{
CircleRadius = 1;
}
public int CircleRadius { get; set; }
public int GetRadius()
{
return Radius;
}
public void SetRadius(int radius)
{
Radius = radius;
}
public int GetDiameter()
{
int Diameter = Radius * 2;
return Diameter;
}
public int GetArea()
{
double Area = Radius * Radius * Math.PI;
return Area; <--------- !!!!ERROR IS HERE!!!
}
}
}
As you said, your main problem is that you want precise decimal calculation over integers. Yet your return type of public int GetArea() is int. To fix your immediate problem - just change its type to double and that will be it!
However there are a couple of other improvements you can make.
First of all - you have public int CircleRadius { get; set; } that is not used. Then, as #Enigmativity said you are writing this in Java style. Use autoproperties a bit more, it will be much easier. And take note of your fields - you declared them but have not used...
Here is a cleaned up class:
class Circle
{
public double Radius {get; set;}
public double Diameter
{
get
{
return Radius * 2;
}
}
public double Area
{
get
{
return Radius * Radius * Math.PI;
}
}
public Circle()
{
this.Radius = 1;
}
}
And your main would be
static void Main(string[] args)
{
Circle create = new Circle();
create.Radius = 2;
WriteLine("Radius = {0}", create.Radius);
WriteLine("Diameter = {0}", create.Diameter);
WriteLine("Area = {0}", create.Area);
}
It looks like you intended to return a double, but you declared your method as returning int.
How can I call the parameter which is inside the method instance in the other class, as I want to make a calculation with the method and display it.
class Box
{
int width = 10;
int height = 15;
public int Area(int Area)
{
Area = width * height;
return Area;
}
public int Perimeter(int Para)
{
Para = 2 * (height + width);
return Para;
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
static void Main(string[] args)
{
Box b = new Box();
b.Area(Area);
b.Perimeter(Para);
Console.ReadLine();
}
It is giving me en error on b.Area(Area); and b.Perimeter(Para);
Maybe you wanted to do this:
class Program
{
static void Main(string[] args)
{
Box box = new Box(10, 15);
Console.WriteLine("Area is: " + box.CalculateArea());
Console.WriteLine("Perimeter is: " + box.CalculatePerimeter());
Console.ReadLine();
}
}
public class Box
{
public int Width { get; set; }
public int Height { get; set; }
public Box(int width, int height)
{
Width = width;
Height = height;
}
public int CalculateArea()
{
return Width * Height;
}
public int CalculatePerimeter()
{
return 2 * (Width + Height);
}
}
I would like to understand and make my own classes in the way like the common rectangle:
Rectangle r1 = new Rectangle();
Size s = r1.Size;
int size = r1.Size.Width;
I don't want to use methods, just simple property values.
public partial class Rectangle
{
private Size _size;
public Size Size
{
get { return _size; }
set { _size = value; }
}
}
So how to create the Width, Height, etc. properties?
And if i would like to create longer chain? e.g.:
r1.Size.Width.InInches.Color.
etc.
Probably you'll find yourself saying: I knew it.
Class properties can be associations with other classes:
public class Size
{
public Size(double width, double height)
{
Width = width;
Height = height;
}
public double Width { get; }
public double Height { get; }
}
Now you'll be able to get rectangle's size as follows: rect1.Size.Width.
About providing the size unit, I wouldn't create an InInches property, but I would create an enumeration:
public enum Unit
{
Inch = 1,
Pixel
}
...and I would add a property to Size as follows:
public class Size
{
public Size(double width, double height, Unit unit)
{
Width = width;
Height = height;
Unit = unit;
}
public double Width { get; }
public double Height { get; }
public Unit Unit { get; }
}
...and if you need to perform conversions, you could easily implement them in Size too:
public class Size
{
public Size(double width, double height, Unit unit)
{
Width = width;
Height = height;
Unit = unit;
}
public double Width { get; }
public double Height { get; }
public Unit Unit { get; }
public Size ConvertTo(Unit unit)
{
Size convertedSize;
switch(unit)
{
case Unit.Inch:
// Calc here the conversion from current Size
// unit to inches, and return
// a new size
convertedSize = new Size(...);
break;
case Unit.Pixel:
// Calc here the conversion from current Size
// unit to pixels, and return
// a new size
convertedSize = new Size(...);
break;
default:
throw new NotSupportedException("Unit not supported yet");
break;
}
return convertedSize;
}
}
What you call chain is what in object-oriented programming is called composition.
Thus, you can associate Size with other class, and associate another class to the other one, and so on...
Here's a part of my code:
class Tiger: Animal,IPredator
{
private double weight;
public static double AvgWeight;
public static int Population;
public static double TotalWeight;
public Tiger(double aWeight):this(aWeight,"Savannah"){}
public Tiger(double aWeight, params string[] aHabitat) : base(aWeight,"Panthera Tigris", "Mammalia", aHabitat)
{
Population++;
TotalWeight += Weight;
AvgWeight = TotalWeight / Population;
Console.WriteLine($"Let's all welcome our new Tiger: {Weight} kilos. Average pride weight: {AvgWeight}");
}
public override double Weight
{
get
{
return weight;
}
set
{
TotalWeight -= weight;
weight = value;
TotalWeight += weight;
AvgWeight = TotalWeight / Population;
}
}
Looks fine,but when I create an instance and check the constructor, it shows double the value of the new cat's weight.
Part of Animal's code:
abstract class Animal
{
private string kind;
private string #class;
public List<string> Habitat = new List<string>();
public virtual double Weight { get; set; }
public string Kind { get { return kind; } set { } }
public string #Class { get { return kind; } set { Console.WriteLine("Class cannot be changed"); } }
public Animal(double aWeight,string aKind, string aClass,params string[] aHabitat)
{
kind = aKind;
#class = aClass;
Habitat.AddRange(aHabitat);
Weight = aWeight;
}
I was trying to avoid these issues, that's why I added an extra double field in Tiger. But I need the Tiger's to work like this and Animals should definitely have a Weight property.
Ok, the problem was not the inheritance or static methods. It was just the same string in the setter and the constructor: TotalWeight += value;
why the result in the area of the rectangle and it's perimeter comes out by zero?
in using C#
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
rec r = new rec();
r.L = Convert.ToInt32(Console.ReadLine());
r.wi = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(r.area());
Console.WriteLine(r.per());
}
}
class rec
{
int l;
int w;
public int L
{
set
{
l = L;
}
get
{
return l;
}
}
public int wi
{
set
{
w = wi;
}
get
{
return w;
}
}
public rec()
{
}
public rec(int x, int y)
{
l = x;
w = y;
}
public double area()
{
return l * w;
}
public int per()
{
return 2 * l + 2 * w;
}
}
}
set should be using implicit value parameter. Your code sets properties to they current value instead:
private int width;
public int Width
{
get { return width; }
set { width = value; }
}
Note that since you don't use any logic in get/set you can use auto-implemented properties instead:
public int Width {get;set;} // no need to define private backing field.