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();
}
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'm learning inheritance and I understand the code below.
namespace InheritanceApplication {
class Shape {
public void setWidth(int w) {
width = w;
}
public void setHeight(int h) {
height = h;
}
protected int width;
protected int height;
}
// Base class PaintCost
public interface PaintCost {
int getCost(int area);
}
// Derived class
class Rectangle : Shape, PaintCost {
public int getArea() {
return (width * height);
}
public int getCost(int area) {
return area * 70;
}
}
class RectangleTester {
static void Main(string[] args) {
Rectangle Rect = new Rectangle();
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
// Print the area of the object.
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
Console.ReadKey();
}
}
}
However, why have they created the set height and set width functions. Would it not be better practice to simply just do this:
public int width {get;set;}
public int height {get;set;}
and then in the main class just do something like below:
rect.width = 5;
rect.height = 7;
Many thanks,
Amir
I'm sure others will provide different points, but here are my main 2 reasons for using gets/sets. If these don't apply for a given property, chances are I won't use getters/setters.
1 - Debugging
It makes it significantly easier to debug data propagation (how data gets passed around) if you can debug a setter that you're concerned about. You can easily throw in a Debug.Print call and debug the value being set if you're concerned it's being passed the wrong value. Or you could place break points and actually debug through the stack trace. For example:
class Shape {
public void setWidth(int w) {
if(w < 0)
Debug.Print("width is less than 0!");
width = w;
}
public void setHeight(int h) {
height = h;
}
protected int width;
protected int height;
}
2 - Value Change Actions
There may be better ways to achieve this, but I like being able to add simple logic to setters to ensure that any logic that needs to run when a value changes does so. For instance I may use the following:
public void SetWindowHeight(int newHeight)
{
if(WindowHeight == newHeight)
return;
WindowHeight = newHeight;
UpdateWindowDisplay();
}
public int GetWindowHeight()
{
return WindowHeight;
}
private int WindowHeight;
public void UpdateWindowDisplay()
{
Window.UpdateHeight(WindowHeight);
// Other window display logic
}
Although personally I prefer to use property gets/sets, but that's just my preference.
public int WindowHeight
{
get
{
return windowHeight;
}
set
{
if(windowHeight == value)
return;
windowHeight = value;
UpdateWindowDisplay();
}
}
private int windowHeight;
public void UpdateWindowDisplay()
{
Window.UpdateHeight(WindowHeight);
// Other window display logic
}
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
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;
}