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;
}
Related
I have a C#-Class Point with two Subclasses ColorPoint and AmountPoint.
Point-Class
public class Point
{
double x; // Position x
double y; // Position y
public Point(double pos_x, double pos_y) // Constructor
{
this.x = pos_x;
this.y = pos_y;
}
}
public class ColorPoint : Point
{
double color; // White value (0 to 255)
}
public class AmountPoint : Point
{
int amount; // Amount of Persons standing at this point
}
Now in my main class I want to create a new Point in the List
This should than look something like this:
public class main
{
public main()
{
List<ColorPoint> colorList = new List<ColorPoint>(4);
AddPoint<ColorPoint>(colorList);
}
public List<T> AddPoint<T>(List<T> pointList)
where T : Point
{
pointList.Add(new T(0, 0)); // DOES NOT WORK (Cannot create instance of variable type 'T')
pointList.Add(new Point(0, 0)); // DOES NOT WORK (Cannot convert Point to T)
}
}
The variables coloror amount can be left as null in both cases.
Your code doesn't compile. I can't imagine why you would want to do what you are trying to do. But the closest you can get to a legitimate implementation is:
class Program
{
static void Main(string[] args)
{
List<ColorPoint> colorList = new List<ColorPoint>(4);
AddPoint<ColorPoint>(colorList);
}
public static List<T> AddPoint<T>(List<T> pointList)
where T : Point, new()
{
pointList.Add(new T());
return pointList;
}
}
public class Point
{
double x; // Position x
double y; // Position y
public Point() : this(0, 0)
{
}
public Point(double pos_x, double pos_y) // Constructor
{
this.x = pos_x;
this.y = pos_y;
}
}
public class ColorPoint : Point
{
double color; // White value (0 to 255)
public ColorPoint()
{
}
public ColorPoint(double pos_x, double pos_y) : base(pos_x, pos_y)
{
}
}
public class AmountPoint : Point
{
int amount; // Amount of Persons standing at this point
public AmountPoint()
{
}
public AmountPoint(double pos_x, double pos_y) : base(pos_x, pos_y)
{
}
}
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!
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);
}
I'm not sure why but anytime I'm working with collision it seems like that everything goes well for the top and left properties of a control, but for the right and bottom properties not so much. I'm not sure if its because they are read only properties or not, but someone please help.
heres the code
playground(panel) class
namespace final_pong_game_phase_3
{
class PlayGround
{
public Panel Panel;
Size PlayGroundSize;
public int Top;
public int Bottom;
public int Right;
public int Left;
public PlayGround(Panel panel,Size size, Point location)
{
this.Panel = panel;
this.PlayGroundSize = size;
this.Panel.Size = this.PlayGroundSize;
this.Panel.BackColor = Color.Black;
this.Panel.Location = location;
this.Panel.SendToBack();
this.Top = panel.Top;
this.Bottom = panel.Bottom;
this.Right = panel.Right;
this.Left= panel.Left;
}
}
}
Ball(picturebox) Class
namespace final_pong_game_phase_3
{
class Ball
{
Size BallSize;
Point Location;
int TopSpeedInterval;
int LeftSpeedInterval;
PictureBox BallGraphic;
public int Top;
public int Left;
public int Bottom;
public int Right;
public Ball(Point location, int topspeedinterval,int leftspeedinterval, Size ballsize, PictureBox ballgraphic)
{
this.Location = location;
this.TopSpeedInterval = topspeedinterval;
this.LeftSpeedInterval = leftspeedinterval;
this.BallSize = ballsize;
this.BallGraphic = ballgraphic;
this.BallGraphic.Location = this.Location;
this.BallGraphic.BackColor = Color.White;
this.BallGraphic.Size = this.BallSize;
this.BallGraphic.BringToFront();
this.Top = BallGraphic.Top;
this.Left = BallGraphic.Left;
this.Bottom = ballgraphic.Bottom;
}
public void start()
{
Top -= TopSpeedInterval;
Left -= LeftSpeedInterval;
BallGraphic.Top = Top;
BallGraphic.Left = Left;
Bottom = BallGraphic.Bottom;
Right = BallGraphic.Right;
}
public void SwitchTopDirection()
{
TopSpeedInterval *=-1;
}
public void SwitchLeftDirection()
{
LeftSpeedInterval *= -1;
}
public void Hit()
{
TopSpeedInterval+= 5;
LeftSpeedInterval+= 5;
}
}
}
GameWorld(controls and monitors the game everything in the method of the class happens every time the timer ticks) Class
//ball to wall collision
if (playground.Top == ball.Top)
{
ball.SwitchTopDirection();
Console.Beep(1000, 100);
}
if (playground.Left == ball.Left)
{
Console.Beep(1500, 700);
MessageBox.Show("Player 2 Wins!!!");
}
if (playground.Right == ball.Right)
{
MessageBox.Show("Player 2 Wins!!!");
}
if (ball.Bottom >= playground.Bottom)
{
ball.SwitchTopDirection();
Console.Beep(1000, 100);
}
This would probably do the trick for you
if (Math.Abs(playground.Right - ball.Right)<=7)
{
MessageBox.Show("Player 2 Wins!!!");
}
What we are doing in this code is that we are checking the range of +-7 of both Playground and Ball it collides.
I'm working on a game, and I made all the building blocks. now I'm working on the game logic and rendering.
I have abstract Monster class and a class call GreenMonster that inherits from it.
Now the weird thing is, when I try to init a GreenMonster object.
when I do this:
private void initGreenMonsters()
{
for (int i = 0; i < greenMonsters.Length; i++)
{
greenMonsters[i] = new GreenMonster(new Point(0,40),new Size(40, 40));
}
}
every thing works like I planned and I can render the images on the form.
but when I try to init like that:
private void initGreenMonsters()
{
for (int i = 0; i < greenMonsters.Length; i++)
{
greenMonsters[i] = new GreenMonster();
greenMonsters[i].Position = new Point(0, 40);
greenMonsters[i].Size = new Size(40, 40);
}
}
I don't get any errors, and the app runs, but I can render the monsters.
This is the Monster class constructor and the Draw Method I use to draw a Monster:
public Monster(Point _startPosition,Size _size)
{
this.size = _size;
this.position = _startPosition;
}
public virtual void Draw(Graphics g)
{
Rectangle monsterRec = new Rectangle(position, size);
g.DrawImage(img, monsterRec);
}
and this is the GreenMonster class constructor:
public GreenMonster(Point _startPosition, Size _size)
: base(_startPosition, _size)
{
this.img = new Bitmap(SpaceInvadersGame.Properties.Resources.NormalMonster);
this.hp = 1;
this.speed = 1;
}
public GreenMonster()
{
this.img = new Bitmap(SpaceInvadersGame.Properties.Resources.NormalMonster);
this.hp = 1;
this.speed = 1;
}
the only thing that bothers me is, that when I'm looking at both ways I init the objects, it just looks the same..
I just can't find any different between in both of the ways.
someone have any idea how its different?
If you need more code so the question is more clear, I would be happy to add!
this is the Monster class and its properties
public abstract class Monster
{
protected Point position;
public Point Position { get { return position; } set { position = value; } }
protected Size size;
public Size Size { get { return size; } set { value = size; } }
public int speed;
protected Bitmap img;
protected int hp;
public int HP { get { return hp; } }
public void SetStartingPosition(int x, int y)
{
this.position = new Point(x, y);
}
public virtual void Draw(Graphics g)
{
Rectangle monsterRec = new Rectangle(position, size);
g.DrawImage(img, monsterRec);
}
}
You are setting your incoming value to the current size, rather than setting the current size to the incoming value, in the method below:
public Size Size { get { return size; } set { value = size; } }
should be
public Size Size { get { return size; } set { size = value; } }
Your code for Position looks OK though:
public Point Position { get { return position; } set { position = value; } }