C# Create a Rectangle class that holds width and height. - c#

This is for C#
Create a Rectangle class that holds width and height. Provide a constructor that accepts width and height. The Rectangle class contains three methods, to calculate the perimeter, to calculate the area, and to check whether it is square or not respectively.
So far I got this done..
public double width;
public double height;
public Rectangle(double w, double h)
{
width = w;
height = h;
}
public double perimeter()
{
return 2 * (width + height);
}
public double area()
{
return width * height;
}
public boolean isSquare()
{
if (width == height)
{
return true;
}
else
{
return false;
}
}
}
}
I get an error for this line
}
public boolean isSquare()
{
What is the problem?

You should use bool instead of boolean. Try this code:
public bool isSquare()
{
if (width == height)
{
return true;
}
else
{
return false;
}
}
Hope it helps!

Related

Creating and comparing objects based on user input

I'm pretty new to C# and I have a task to solve.
I need to create a console app that asks the user to make 2 figures(either rectangle or hexagon again selected by the user input) to enter their properties and compare which one is bigger. For some reason I get not all code paths return a value in SelectAndCreateFigure even after using the else statement returning null.
This is my code:
figure class:
namespace PU_task
{
public class Figure
{
public virtual double Area()
{
return 0;
}
}
}
hexagon class:
using System;
namespace PU_task
{
public class Hexagon :Figure
{
private double side;
public double Side { get => side; set => side = value; }
public Hexagon()
{
}
public override double Area()
{
double area = 3 * Math.Sqrt(3 * Math.Pow(side, 2)) / 2;
return area;
}
public Figure CreateFigure(double side)
{
Hexagon hexagon = new Hexagon
{
Side = side
};
return hexagon;
}
}
}
rectangle class:
namespace PU_task
{
public class Rectangle : Figure
{
private double length;
private double width;
public double Length { get => length; set => length = value; }
public double Width { get => width; set => width = value; }
public Rectangle()
{
}
public override double Area()
{
double area = length * width;
return area;
}
public Figure CreateFigure(double width,double length)
{
Rectangle rectangle = new Rectangle
{
Width = width,
Length = length
};
return rectangle;
}
}
}
The code in my main file:
using System;
namespace PU_task
{
internal class Program
{
static void Main(string[] args)
{
Figure figure1 = new Figure();
figure1.SelectAndCreateFigure();
Figure figure2 = new Figure();
figure2.SelectAndCreateFigure();
SmallerArea(figure1, figure2);
}
public Figure SelectAndCreateFigure(Figure figure)
{
Console.WriteLine("Select figure type:");
string input = Console.ReadLine().ToLower();
if (input == "rectangle")
{
Console.WriteLine("Enter length:");
double length = double.Parse(Console.ReadLine());
Console.WriteLine("Enter width:");
double width = double.Parse(Console.ReadLine());
Rectangle rectangle = new Rectangle();
rectangle.CreateFigure(width, length);
return rectangle;
}
else if (input == "hexagon")
{
Console.WriteLine("Enter the the side length:");
double side = double.Parse(Console.ReadLine());
Hexagon hexagon = new Hexagon();
hexagon.CreateFigure(side);
}
else return null;
}
public void SmallerArea(Figure figure1, Figure figure2)
{
if (figure1.Area() > figure2.Area())
{
Console.WriteLine(figure1.Area());
}
else
{
Console.WriteLine(figure2.Area());
}
}
}
}
You have an issue with initializing your objects with the correct data. Each class needs a constructor to fill in the data. Also C# supports properties, that can either store values (like the side of the hexagon), or that can return calculated values (like the area of the figure).
A basic skeleton for the above functionality is shown below:
public abstract class Figure
{
public abstract double Area { get; }
}
public class Rectangle : Figure
{
public Rectangle(double length, double width)
{
Length = length;
Width = width;
}
public double Length { get; }
public double Width { get; }
public override double Area => Length * Width;
}
public class Hexagon : Figure
{
public Hexagon(double side)
{
Side = side;
}
public double Side { get; }
public override double Area => 3 * Math.Sqrt(3 * Math.Pow(Side, 2)) / 2;
}
class Program
{
static void Main(string[] args)
{
Figure rect = new Rectangle(12.0, 5.5);
Figure hex = new Hexagon(8.0);
if (rect.Area > hex.Area)
{
Console.WriteLine("Rectangle is bigger");
}
else
{
Console.WriteLine("Hexagon is bigger");
}
}
}
It is up to you to design the required functionality based on the class hierarchy above.
try this
static void Main(string[] args)
{
double hexagonArea=0;
double rectangleArea=0;
CreateFigures(ref hexagonArea, ref rectangleArea);
Console.WriteLine("rectangle area - "+rectangleArea.ToString());
Console.WriteLine("hexagon area - "+hexagonArea.ToString());
if (rectangleArea > hexagonArea)
Console.WriteLinge ("rectangle area is bigger!");
else Console.WriteLinge ("hexagon area is bigger!");
}
public void CreateFigures(ref double hexagonArea, ref double rectangleArea)
{
for (int i = 0; i < 2; i++)
{
Console.WriteLine("Select figure type:");
string input = Console.ReadLine();
if (input == "rectangle")
{
Console.WriteLine("Enter length:");
double length = double.Parse(Console.ReadLine());
Console.WriteLine("Enter width:");
double width = double.Parse(Console.ReadLine());
rectangleArea= CreateRectangle(width, length);
}
else if (input == "hexagon")
{
Console.WriteLine("Enter width:");
double side = double.Parse(Console.ReadLine());
hexagonArea = CreateHexagon(side);
}
}
}
public void CreateRectangle(double length,double width)
{
Rectangle rectangle = new Rectangle
{
Width = width,
Length = length
};
return rectangle.Area();
}
public double CreateHexagon(double side)
{
Hexagon hexagon = new Hexagon
{
Side = side
};
return hexagon.Area();
}
The reason you getting 'not all code paths return a value' error in SelectAndCreateFigure is because in your else if statement you not returning figure. this will solve that particular error.
public Figure SelectAndCreateFigure(Figure figure)
{
Console.WriteLine("Select figure type:");
string input = Console.ReadLine().ToLower();
if (input == "rectangle")
{
Console.WriteLine("Enter length:");
double length = double.Parse(Console.ReadLine());
Console.WriteLine("Enter width:");
double width = double.Parse(Console.ReadLine());
Rectangle rectangle = new Rectangle();
rectangle.CreateFigure(width, length);
return rectangle;
}
else if (input == "hexagon")
{
Console.WriteLine("Enter the the side length:");
double side = double.Parse(Console.ReadLine());
Hexagon hexagon = new Hexagon();
hexagon.CreateFigure(side);
return hexagon;
}
else return null;
}

Zoom in and out an image on mouse click using MVVM

Here in this application, I've used buttons to zoom in and out an image using MVVM in WPF. On clicking zoom in button, the height and width are increased by a particular amount and vice versa for the zoom out. but I want to achieve the same using a mouse double click. Is there any way to achieve the same using MVVM?
private ICommand zoomOutCommand;
public ICommand ZoomOutCommand {
get {
if (zoomOutCommand == null) {
zoomOutCommand = new RelayCommand < object > (ZoomOutExecute, OutReturnBool, false);
}
return zoomOutCommand;
}
}
private ICommand zoomInCommand;
public ICommand ZoomInCommand {
get {
if (zoomInCommand == null) {
zoomInCommand = new RelayCommand < object > (ZoomInExecute, InReturnBool, false);
}
return zoomInCommand;
}
}
private void ZoomInExecute(object obj) {
// Scale += stepScale;
Height *= 1.2;
Width *= 1.2;
}
private bool InReturnBool(object obj) {
if (Height > 6 * iniWidth) return false;
else return true;
}
private bool OutReturnBool(object obj) {
if (Height < 0.1 * iniHeight) return false;
else return true;
}
private void ZoomOutExecute(object obj) {
Height *= 0.8;
Width *= 0.8;
}
private const double iniWidth = 500;
private double width = iniWidth;
public double Width {
get {
return width;
}
set {
width = value;
NotifyPropertyChanged("Width");
}
}
private const double iniHeight = 500;
private double height = iniHeight;
public double Height {
get {
return height;
}
set {
height = value;
NotifyPropertyChanged("Height");
}
}
For a double click you can bind the command this way
<Button>
<Button.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding YourCommand}" />
</Button.InputBindings>
</Button>

There is no argument that corresponds to the required formal paramter

I have only just started learning C# so excuse this basic question. I am experimenting with C# inheritance and want to inherit the properties from Shape class into the Rectangle class. The Rectangle class below gives me an error:
"There is no argument that corresponds to the required formal
parameter 'height' of 'Shape.Shape(double.double)'
Is anyone able to tell me why this is happening?
class Shape
{
public double Height { get; set; }
public double Width { get; set; }
public Shape (double height, double width)
{
Height = height;
Width = width;
}
public double calculateArea()
{
double Area = (Height * Width);
return Area;
}
}
class Rectangle : Shape
{
public Rectangle(double height, double width)
{
Height = height;
Width = width;
}
static void Main(string[] args)
{
Rectangle rectangle = new Rectangle(15, 19);
double areaOfRectangle = rectangle.calculateArea();
Console.WriteLine(areaOfRectangle);
}
}
In c#, the base keyword is used to access base class members such as properties, methods, etc. in the derived class. so you have to act like below:
public Rectangle(double height, double width) : base(height, width) { ... }
for learning more about base you can follow this. good luck.

How do I introduce an initialization constructor? C# CS0236

I'm getting multiple errors but I don't know why. The errors are introduced after the GetArea method.
namespace Lesson02
{
class Rectangle
{
static void Main(string[] args)
{
}
private double length;
private double width;
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public double GetArea()
{
return length * width;
}
public Rectangle rect = new Rectangle(10.0, 20.0);
double area = rect.GetArea();
Console.WriteLine("Area of Rectagle: {0}", area);
You can't just put execution
Console.WriteLine("Area of Rectagle: {0}", area);
within the class scope as if it's declaration. Move it to the Main method:
namespace Lesson02
{
class Rectangle
{
// Method, here we execute
static void Main(string[] args)
{
// Executions are within the method
Rectangle rect = new Rectangle(10.0, 20.0);
double area = rect.GetArea();
Console.WriteLine("Area of Rectagle: {0}", area);
}
// Declarations
private double length;
private double width;
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public double GetArea()
{
return length * width;
}
}
}
As mentioned in the comment, you have class body mixed up with program code. It's also a bad idea to have everything in one class.
Your Rectangle class should be separate:
public class Rectangle
{
private double length;
private double width;
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public double GetArea()
{
return length * width;
}
}
And your program code separate:
public class Program
{
static void Main(string[] args)
{
Rectangle rect = new Rectangle(10.0, 20.0);
double area = rect.GetArea();
Console.WriteLine("Area of Rectagle: {0}", area);
}
}
Either make class Rectangle as public otherwise change
public Rectangle rect = new Rectangle(10.0, 20.0); as Rectangle rect = new Rectangle(10.0, 20.0);
public class Rectangle
{
private double length;
private double width;
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public double GetArea()
{
return length * width;
}
}
static void Main(string[] args)
{
Rectangle rect = new Rectangle(10.0, 20.0);
double area = rect.GetArea();
Console.WriteLine("Area of Rectagle: {0}", area);
}

Why am I getting a value of zero for my circle?

Here is my Circle class code.
class Circle
{
private double radius;
private double area;
public Circle(double radius)
{
this.radius = radius;
}
public double Area
{
set { area = Math.PI * Math.Pow(radius, 2); }
get { return area; }
}
}
This is test code.
Circle circle1 = new Circle(3);
MessageBox.Show("Circle 1 Area: " + circle1.Area);
So for some reason, when I use the MessageBox.Show(), it seems to give me values of zero instead. I gave the circle a value of 3 so shouldn't my constructor set the value of the radius that?
Because you haven't ever called the setter on Area. Perhaps you want something like this instead?
class Circle
{
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
public double Area
{
get { return Math.PI * Math.Pow(radius, 2); }
}
}
This will compute the Area every time it is requested.
Your Area property should be:
public double Area
{
get { return Math.PI * Math.Pow(radius, 2); }
}
and you don't need the area field.
I'm not sure you need a set in this instance (You didn't use it)
try
get { return Math.PI * Math.Pow(radius, 2); }

Categories