Perimeter Calculator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Perimeter Calculator

I have coded this program below, it works perfectly through my VS Code, but it doesn't here in this platform. As the test case is hidden, I can't figure why that is not passing. Does anyone have a clue? Add an abstract method Perimeter to class Figure in order to calculate the perimeters of given objects. class Program { static void Main(string[] args) { Figure rectangle = new Rectangle(5, 6); Figure triangle = new Triangle(4, 8, 3); Console.WriteLine(rectangle.Perimeter()); Console.WriteLine(triangle.Perimeter()); } } abstract class Figure { //define abstract method Perimeter with no body public abstract int Perimeter(); } class Rectangle : Figure { public int width; public int height; public Rectangle(int width, int height) { this.width = width; this.height = height; } //override Perimeter method for rectangle public override int Perimeter() { return (width * width) + (height * height); } } class Triangle : Figure { public int side1; public int side2; public int side3; public Triangle(int s1, int s2, int s3) { this.side1 = s1; this.side2 = s2; this.side3 = s3; } //override Perimeter method for triangle public override int Perimeter() { return side1 + side2 + side3; } }

3rd Mar 2021, 12:38 AM
Rafael F Pimentel
Rafael F Pimentel - avatar
7 Answers
+ 2
The formula used for rectangle perimeter needs attention. It should be the sum of the four sides, not the sum of squares of two sides. Try 2*(width + height).
3rd Mar 2021, 6:33 AM
Brian
Brian - avatar
+ 7
I Got an Answer by this: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { Figure rectangle = new Rectangle(5, 6); Figure triangle = new Triangle(4, 8, 3); Console.WriteLine(rectangle.Perimeter()); Console.WriteLine(triangle.Perimeter()); } } abstract class Figure { public abstract int Perimeter(); //define abstract method Perimeter with no body } class Rectangle : Figure { public int width; public int height; public Rectangle(int width, int height) { this.width = width; this.height = height; } public override int Perimeter() { return (2*width)+(2*height); } //override Perimeter method for rectangle } class Triangle : Figure { public int side1; public int side2; public int side3; public Triangle(int s1, int s2, int s3) { this.side1 = s1; this.side2 = s2; this.side3 = s3; } public override int Perimeter() { return side1+side2+side3; } //override Perimeter method for triangle } }
6th Jul 2022, 7:43 AM
Anandu S Kumar
+ 2
Why doesn't it work? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { Figure rectangle = new Rectangle(5, 6); Figure triangle = new Triangle(4, 8, 3); Console.WriteLine(rectangle.Perimeter()); Console.WriteLine(triangle.Perimeter()); } } abstract class Figure { public abstract void Perimeter(); } class Rectangle : Figure { public int width; public int height; public Rectangle(int width, int height) { this.width = width; this.height = height; } public override void Perimeter(){ return (2*width)+(2*height); } } class Triangle : Figure { public int side1; public int side2; public int side3; public Triangle(int s1, int s2, int s3) { this.side1 = s1; this.side2 = s2; this.side3 = s3; } public override void Perimeter(){ return s1+s2+s3; } } }
18th Apr 2022, 5:14 PM
rudes
+ 1
You were right man, thanks a bunch!
3rd Mar 2021, 5:09 PM
Rafael F Pimentel
Rafael F Pimentel - avatar
+ 1
You're welcome!
3rd Mar 2021, 5:20 PM
Brian
Brian - avatar
+ 1
Annoying when you cant see the results or errors.
10th May 2021, 9:44 AM
Daryl Wells
0
U forgot the public keyword in Figure
25th Apr 2022, 12:15 PM
Daniel Kraus
Daniel Kraus - avatar