0% found this document useful (0 votes)
9 views6 pages

Oop Prac

The document contains multiple C# code snippets demonstrating different functionalities. It includes a Windows Forms application for file handling, a console application for deleting an element from an array, and a shape hierarchy with area and perimeter calculations. Additionally, it features error handling in a console application for basic arithmetic operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Oop Prac

The document contains multiple C# code snippets demonstrating different functionalities. It includes a Windows Forms application for file handling, a console application for deleting an element from an array, and a shape hierarchy with area and perimeter calculations. Additionally, it features error handling in a console application for basic arithmetic operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

QUESTION 1

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using System.IO;

namespace WindowsFormsApplication4

public partial class Form1 : Form

private string f = "subject.txt";

public Form1()

InitializeComponent();

private void button1_Click(object sender, EventArgs e)

if(!File.Exists(f)){

File.Create(f).Close();
}

private void button2_Click(object sender, EventArgs e)

using (StreamWriter write = new StreamWriter(f,true)){

write.WriteLine(textBox1.Text);

private void button3_Click(object sender, EventArgs e)

using (StreamReader read = new StreamReader(f))

string line;

while ((line = read.ReadLine()) != null) {

listBox1.Items.Add(line);

}
QUESTION 2

2a)
using System;
using System.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] numbers = {10,20,30,40,50,60,70,80,90,100};
Console.WriteLine("Array: " + string.Join(",", numbers));

Console.Write("Enter a number to delete: ");


int numToDelete = int.Parse(Console.ReadLine());
numbers = numbers.Where(x => x != numToDelete).ToArray();
Console.WriteLine("Updated Array: " + string.Join(", ", numbers));
Console.ReadKey();
}
}
}
2b)
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// Abstract Shape class


public abstract class Shape
{
public abstract double CalculateArea();
public abstract double CalculatePerimeter();
}

// Rectangle class
public class Rectangle : Shape
{
public double Width { get; set; }
public double Height { get; set; }

public Rectangle(double width, double height)


{
Width = width;
Height = height;
}

public override double CalculateArea()


{
return Width * Height;
}

public override double CalculatePerimeter()


{
return 2 * (Width + Height);
}
}

// Square class
public class Square : Rectangle
{
public Square(double sideLength) : base(sideLength, sideLength)
{
}
}

// Circle class
public class Circle : Shape
{
public double Radius { get; set; }

public Circle(double radius)


{
Radius = radius;
}

public override double CalculateArea()


{
return Math.PI * Math.Pow(Radius, 2);
}

public override double CalculatePerimeter()


{
return 2 * Math.PI * Radius;
}
}

class Program
{
static void Main()
{
Rectangle rectangle = new Rectangle(4, 5);
Console.WriteLine("Rectangle Area: " + rectangle.CalculateArea() );
Console.WriteLine("Rectangle Perimeter:
"+rectangle.CalculatePerimeter());

Square square = new Square(4);


Console.WriteLine("Square Area: "+ square.CalculateArea());
Console.WriteLine("Square Perimeter: "+ square.CalculatePerimeter());

Circle circle = new Circle(3);


Console.WriteLine("Circle Area: "+ circle.CalculateArea());
Console.WriteLine("Circle Circumference: " +
circle.CalculatePerimeter());
}
}

QUESTION 5

a)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main()
{
try
{
Console.Write("Enter first number: ");
int a = int.Parse(Console.ReadLine());
Console.Write("Enter second number: ");
int b = int.Parse(Console.ReadLine());

Console.WriteLine("Addition: "+ a + b);


Console.WriteLine("Division: "+ a/b);
}
catch (DivideByZeroException) {
Console.WriteLine("Division by zero is not allowed.");
}
Console.ReadKey();
}

}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy