So i have made a small console calculator for working out the circumference of a circle. Yet i would like to rewrite it using methods. But i don't know where to start. Can anyone help me
using System;
class Circle
{
static void Main()
// Circumference of a circle: C=2πr
{
Console.WriteLine("What is the radius of your circle: ");
double radius = double.Parse(Console.ReadLine());
double pi = 3.1452;
double area = 2 * pi * radius;
Console.WriteLine("The Circumference of your circle is: "+ area);
Console.ReadKey();
}
}
using System;
class Circle
{
public static double GetCircumference(double radius)
{
return 2 * Math.PI * radius;
}
static void Main()
// Circumference of a circle: C=2πr
{
Console.WriteLine("What is the radius of your circle: ");
Console.WriteLine("The Circumference of your circle is: " + GetCircumference(Double.Parse(Console.ReadLine())).ToString());
Console.ReadKey();
}
}
If you just want to break out the calculation into a method, you just create the following method below your main method:
private static double CalculateCircumference(double radius)
{
return 2 * radius * Math.PI;
}
And then call it from within your main method like so:
double circumference = CalculateCircumference(radius);
All in all
static void Main()
{
Print("What is the radius of your circle: ");
double radius = ParseInputNumber();
double circumference = CalculateCircumference(radius);
Print("The Circumference of your circle is: " + circumference);
WaitForKeystroke();
}
private static void Print(string message)
{
Console.WriteLine(message);
}
private static double ParseInputNumber()
{
return double.Parse(Console.ReadLine());
}
private static void WaitForKeystroke()
{
Console.ReadKey();
}
private static double CalculateCircumference(double radius)
{
return 2 * radius * Math.PI;
}
Related
I have this code and I am trying to calculate the area and circumference of the circle. I have tried debugging and I can see that the radius was passed but the area and circumference is not, it's always 0.
using System;
class Circle{
double radius;
double area;
double circumference;
public double Radius{set{radius = value;}}
public double Area{set{area=value;}}
public double Circumference{set{circumference=value;}}
public Circle(double radius){
this.radius = radius;
area=0;
circumference=0;
}
void CalculateArea(){
area=Math.PI*Math.Pow(radius,2);
}
void CalculateCircumference(){
circumference = 2*Math.PI*radius;
}
public void DisplayArea(){
System.Console.WriteLine("Area is {0}",area);
}
public void DisplayCircumference(){
System.Console.WriteLine("Circumference is {0}",circumference);
}
}
class TestCircle{
public static void Main(string[] args)
{
System.Console.WriteLine("Enter radius: ");
double radius=Convert.ToDouble(Console.ReadLine());
Circle theCircle = new Circle(radius);
theCircle.DisplayArea();
theCircle.DisplayCircumference();
}
}
this is the output
Enter radius:
3
Area is 0
Circumference is 0
You have a very strange design: set only Radius (once set I can't read it), editable (both get and set) Area: one
can easily assign -1.0 to it...
Let's redesign it; we have Circle class with three properties only:
class Circle{
// Radius, the man property we can
// - read it (get)
// - assign it, but let it be just once (so set is private)
public double Radius {get; private set;}
// Area, which we can just read (get, no set)
public double Area => Math.PI * Radius * Radius;
// Circumference, which we can just read (get, no set)
public double Circumference => 2 * Math.PI * Radius;
public Circle(double radius) {
Radius = radius;
}
public void DisplayArea(){
System.Console.WriteLine($"Area is {Area}");
}
public void DisplayCircumference(){
System.Console.WriteLine($"Circumference is {Circumference}");
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm having trouble and I was wondering if there is something wrong with my code.
The code should execute the following
When executed within the terminal the MoveAmount and Move should end up equal to each other.
Side A should follow the equation
(x*a)/(a+b) and execute with the correct value. (x representing the move and A and B representing their perspective ratio).
Move - SideA = Side B
Move = SideA + SideB
Unfortunately it doesn't seem to be working and I am unable to get the code to create the proper output.
If I try to input my values based on this graph the Calculated Movement of Side A and B return incorrectly and the output does not follow the desired perameters above.
If anyone can give me any tips let me know.
using System;
namespace Example
{
class CalculatedMove
{
public double Move = 0.0;
public double SideARatio = 0.0;
public double SideBRatio = 0.0;
public CalculatedMove(double SideARatio, double SideBRatio, double Move)
{
this.Move = Move;
this.SideARatio = SideARatio;
this.SideBRatio = SideBRatio;
}
public virtual void SideA()
{
double SideA = 0.0;
SideA = (Move * SideARatio) /(SideARatio + SideBRatio);
Console.WriteLine("Calculated Side A Movement is {00:00.0000}", SideA);
}
public void SideB()
{
double SideB = 0.0;
SideB = Move - SideARatio;
Console.WriteLine("The Calculated Side B Movement is {00:00.0000}", SideB);
}
public void MovementAmount()
{
double MovementAmount = 0.0;
MovementAmount = SideARatio + SideBRatio;
Console.WriteLine("The Calculated Move Amount is {0:00.0000}", MovementAmount);
}
}
class Program
{
static void Main(string[] args)
{
double Move, SideARatio, SideBRatio = 0.0;
Console.WriteLine("Enter the Move Amount ");
Move = Double.Parse(Console.ReadLine());
Console.WriteLine("Side A Ratio");
SideARatio = Double.Parse(Console.ReadLine());
Console.WriteLine("Side B Ratio");
SideBRatio = Double.Parse(Console.ReadLine());
CalculatedMove objMove = new CalculatedMove(Move, SideARatio,SideBRatio);
objMove.SideA();
objMove.SideB();
objMove.MovementAmount();
Console.Read();
}
}
}
First of all, as JohnG said, the sequence of parameters is wrong when calling parameters.
Then when calculating SideB, SideARatio should not be subtracted, but SideA should be subtracted.
Finally, Move should be SideA+SideB, not SideARatio+SideBRatio.
using System;
namespace Example {
class CalculatedMove {
private double move =0.0;
public double Move {
get { return move; }
set { move=value; }
}
private double sideARatio=0.0;
public double SideARatio {
get { return sideARatio; }
set { sideARatio=value; }
}
private double sideBRatio = 0.0;
public double SideBRatio {
get { return sideBRatio; }
set { sideBRatio=value; }
}
private double sideA = 0.0;
public double SideA {
get { return sideA; }
set { sideA=value; }
}
private double sideB = 0.0;
public double SideB {
get { return sideB; }
set { sideB=value; }
}
private double movementAmount = 0.0;
public double MovementAmount {
get { return movementAmount; }
set { MovementAmount=value; }
}
public CalculatedMove(double SideARatio, double SideBRatio, double Move) {
this.move=Move;
this.sideARatio=SideARatio;
this.sideBRatio=SideBRatio;
}
public virtual void SideAMove() {
SideA=(Move*SideARatio)/(SideARatio+SideBRatio);
Console.WriteLine("Calculated Side A Movement is {00:00.0000}", SideA);
}
public void SideBMove() {
SideB=(Move*SideBRatio)/(SideARatio+SideBRatio);
Console.WriteLine("The Calculated Side B Movement is {00:00.0000}", SideB);
}
public void MovementAmountMove() {
MovementAmount=SideA+SideB;
Console.WriteLine("The Calculated Move Amount is {0:00.0000}", MovementAmount);
}
}
class Program {
static void Main(string[] args) {
double Move, SideARatio, SideBRatio = 0.0;
Console.WriteLine("Enter the Move Amount ");
Move=Double.Parse(Console.ReadLine());
Console.WriteLine("Side A Ratio");
SideARatio=Double.Parse(Console.ReadLine());
Console.WriteLine("Side B Ratio");
SideBRatio=Double.Parse(Console.ReadLine());
CalculatedMove objMove = new CalculatedMove( SideARatio, SideBRatio,Move);
objMove.SideAMove();
objMove.SideBMove();
objMove.MovementAmountMove();
Console.Read();
}
}
}
Output:
As 3.Move - SideA = Side B :
public void SideB()
{
double SideB = 0.0;
SideB = Move - SideARatio; // Here is the problem;
Console.WriteLine("The Calculated Side B Movement is {00:00.0000}", SideB);
}
Fix public void SideB()
public void SideB()
{
double SideB = 0.0;
SideB = (Move * SideBRatio) /(SideARatio + SideBRatio);
Console.WriteLine("The Calculated Side B Movement is {00:00.0000}", SideB);
}
I wrote a C# program to print the area, but when I run the program it always prints "The area is 0". Any idea what seems to be the problem?
class Circle
{
double radius;
// int color;
double area;
public void setCircleInfo()
{
radius = 15;
//color = 225;
}
public void calculateArea()
{
area = 3.142 * radius * radius;
}
public double getRadius()
{
return radius;
}
public void displayArea()
{
Console.WriteLine("The area is " + area.ToString());
Console.ReadLine();
}
}
}
Here's the code to the main method:
{
class Program
{
static void Main(string[] args)
{
Circle obj1 = new Circle();
obj1.displayArea();
}
}
}
Your code runs fine for me. The reason why you are always getting The area is 0 is probably you are missing any one of the following calls. Note that If you miss setCircleInfo or calculateArea, you will get a zero as the result.
Circle c = new Circle();
c.setCircleInfo();
c.calculateArea();
c.displayArea();
Hope this helps :-)
I would adjust the object a little bit to make it easier to use:
class Circle
{
public double Radius
{
get;
set;
}
public double Area
{
get
{
return 3.142 * radius * radius;
}
}
public Circle(double radius)
{
Radius = radius;
}
public void displayArea()
{
Console.WriteLine("The area is " + Area.ToString());
Console.ReadLine();
}
}
You can then use it like this:
static void Main(string[] args)
{
Circle obj1 = new Circle(15);
obj1.displayArea();
}
Change your code
static void Main(string[] args)
{
Circle obj1 = new Circle();
obj1.setCircleInfo();
obj1.calculateArea();
obj1.displayArea();
}
add in your Circle constructor method:
public Circle(double radius){
this.radius=radius;
c.setCircleInfo();
c.calculateArea();
c.displayArea();
}
I have write a code to calculate the area of different shapes using polymorphism (Virtua & Override Method) but not getting correct result. :(
Below is my code:
class Program
{
static void Main(string[] args)
{
double side = 0;
double length = 0;
double width = 0;
double height = 0;
double baseoftriangle = 0;
double radius = 0;
UserChoice:
Console.WriteLine("For what shape you want to calculate the Area:\n1. Sqaure\n2. Rectangle\n3. Triangle\n4. Circle");
Console.Write("Please Select the number from above options: ");
int choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
Console.Write("Please enter the side of square: ");
side = double.Parse(Console.ReadLine());
break;
case 2:
Console.Write("Please enter the length of rectangle: ");
length = double.Parse(Console.ReadLine());
Console.Write("Please enter the width of rectangle: ");
width = double.Parse(Console.ReadLine());
break;
case 3:
Console.Write("Please enter the height of triangle: ");
height = double.Parse(Console.ReadLine());
Console.Write("Please enter the base of triangle: ");
baseoftriangle = double.Parse(Console.ReadLine());
break;
case 4:
Console.Write("Please enter the radius of circle: ");
radius = double.Parse(Console.ReadLine());
break;
default:
Console.WriteLine("Incorrect Choice, please try again!");
goto UserChoice;
}
CalculateArea Sqa = new Square();
Sqa = new Rectangle();
Sqa = new Triangle();
Sqa = new Circle();
if (choice == 1)
{
Sqa.Area(side);
Sqa.ShowResult();
}
else if(choice==2)
{
Sqa.Area(length,width);
Sqa.ShowResult();
}
else if(choice==3)
{
Sqa.Area(height, baseoftriangle);
Sqa.ShowResult();
}
else
{
Sqa.Area(radius);
Sqa.ShowResult();
}
ChoiceOfAnotherCalculation:
Console.Write("\nDo you want to calculate area of any other shape? Give input in Yes or NO: ");
string choice1 = Console.ReadLine();
switch (choice1.ToUpper())
{
case "YES":
goto UserChoice;
case "NO":
break;
default:
Console.WriteLine("Incorrect Choice, please try again!");
goto ChoiceOfAnotherCalculation;
}
}
}
class CalculateArea
{
public double result;
public virtual void Area(double side)
{
}
public virtual void Area(double length, double width)
{
}
public void ShowResult()
{
Console.WriteLine($"Your Result is {result}");
}
}
class Square: CalculateArea
{
public override void Area(double side)
{
result = side * side;
}
}
class Rectangle:CalculateArea
{
public override void Area(double length, double width)
{
result = length * width;
}
}
class Triangle:CalculateArea
{
public override void Area(double height, double baseoftriangle)
{
result = (height * baseoftriangle)/2;
}
}
class Circle:CalculateArea
{
public override void Area(double radius)
{
result = 3.14159 * radius * radius;
}
}
For Rectangle & Triangle, I am always getting 0 as a result and for square I am getting result from circle. I think this issue is causing because of the way I have created objects for class (May be because of lack of understanding in polymorphism).
Can someone please have a look and let me know what is the actual problem here.
Apart from this also I would appreciate your efforts if you can provide me the solution with get & set properties for each class. I have tried to write the code (Where I have tried to define the property in each derive class as well as in base class) but was getting various errors that's why I have jump on above solution.
I will also request please do not downvote the question as system won't allow me ask question for a long time. As this is the only platform where I can understood the things properly.
Many Thanks.
Inside the switch statement, after case 1, your program will exit. This is because you have used return;, you might have intended to use break; instead. Break statement ensures that your switch wont fall through to the next statement, and a return statement would terminate the execution immediately.
Correct case statement should be like this
case 1:
Console.Write("Please enter the side of square: ");
side = int.Parse(Console.ReadLine());
break;
EDIT
Base class will only hold an abstract method to calculate area, which should be implemented by inheriting classes.
public abstract class Shape
{
// must implement by inheriting classes
public abstract double CalculateArea();
}
Each shape would calculate area using different parameters, therefore you should create properties that would be required by that specific shape.
public class Square : Shape
{
// properties differ based on the shape..
// eg: triangle needs height and base
// edit CalculateArea method based on the shape
public double Length { get; set; }
public override double CalculateArea()
{
return Length * Length;
}
}
Implement just like you did before, create an instance of the shape then pass the required parameters and invoke CalculateArea() method, that would return a double value:
double length;
Console.WriteLine("Enter length of one side: ");
if (!double.TryParse(Console.ReadLine(), out length))
Console.WriteLine("Invalid value, must be a number or decimal value.");
Square sq = new Square();
sq.Length = length;
Console.WriteLine(sq.CalculateArea().ToString("F"));
For polymorphism your classes should look like this:
abstract class Shape
{
public abstract double Area {get;}
}
class Square: Shape
{
public double Side {get;}
public override double Area => Side * Side;
public Square (double side)
{
this.Side = side;
}
}
class Rectangle:Shape
{
public double Width {get;}
public double Length {get;}
public override double Area => Width * Length;
public Rectangle (double width, double length)
{
this.Width = width;
this.Length = length;
}
}
etc.
With this in place, you can create the Shape once during the first case statement and can then use the .Area property on it in your output.
You should not have code to do output in your shape class, keep each class focused on one thing (SRP).
Your shapes should track their own properties because you may later want to add, say, a 'Description' property that uses those properties to describe the shape.
A friend recently had a telephone interview and he was asked a technical question:
Q) If I wanted to calculate the surface area of some 2D shapes then what "Bucket" would I use. He had 20 minutes to write some code and the interviewer called him back. He sent the code via email and the code was not discussed for the remainder of the interview (there were no other technical questions). He sent me the code:
Windows Forms app
namespace ShapesApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += form_load;
}
public void form_load (Object o, EventArgs e)
{
List<Shape> listShape = new List<Shape>();
Shapes.Circle circle = new Shapes.Circle();
Shapes.Rectangle rectangle = new Shapes.Rectangle();
Shapes.Square square = new Shapes.Square();
Shapes.Triangle triangle = new Shapes.Triangle();
listShape.Add(rectangle);
listShape.Add(square);
listShape.Add(triangle);
foreach (Shape shape in listShape)
{
double a = 10;
double b = 10;
double surfaceArea = shape.CalculateSurfaceArea(a,b);
Console.WriteLine("The surface area of a " + shape.GetType() + " is: " + surfaceArea);
}
}
}
}
Shapes - Class Library
namespace Shapes
{
public abstract class Shape
{
abstract public double CalculateSurfaceArea(double Double1, double Double2);
}
public class Circle : Shape
{
public override double CalculateSurfaceArea(double pi, double radius)
{
return (pi * radius) * (pi * radius);
}
}
public class Triangle : Shape
{
public override double CalculateSurfaceArea(double Base, double Height)
{
return (Base*Height)/2;
}
}
public class Rectangle : Shape
{
public override double CalculateSurfaceArea(double Length, double Width)
{
return Length * Width;
}
}
}
The interviewer has said that he "struggled" with the test. What is wrong with the code?
Calculating area is the behavior and every shape has his own formula for calculating it. Because calculating area can involve different amount of variables and constants method will not take any parameter and variables will be concern of class which implement interface .
So I think method of calculating area can be abstracted as interface:
public interface ICalculatingArea
{
double CalculateArea();
}
Then every shape will implement it on its own manner.
public class Rectangle:ICalculatingArea
{
public double Width {get; set;}
public double Length {get; set;}
public double CalculateArea()
{
return Length * Width;
}
}
In the main program it is enough to cast shape classes to the interface type and use CalculateArea method