Shreya

Shreya

  • NA
  • 2
  • 2.9k

exceptions

Apr 24 2013 10:45 PM
Write a program to define a class called PDA and a classes called RADIO and
CALCULATOR .Let the PDA class contain RADIO class and the CALCULATOR Class. RADIO
must capable to get connected to the following stations say 1.Mumbai 2.Delhi
.In addition the program must throw the OutOfRangeFrequency exceptions whenever the object
user tries to connect other frequency channel. CALCULATOR must be capable to perform the
rudimentary calculations say addition , subtraction , multiplication ,division ,modular division and
square root. The program must be capable to throw the exceptions say DivideByZeroException and Negative Number Exception .Write driver class which can perform the following tasks :
1.Creation of TWO objects to PDA class
2. One object Connects to Mumbai
3.The other objects Performs division of two numbers
4.Find the Generation of objects.


i have come up with half the solution. Help me complete it

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using A8;

namespace A8
{
class PDA
{
  
}

class Radio : PDA
{
    int ch;
    public Radio(int a)
    {
        ch=a;
        if(ch==1)
            Console.WriteLine("Connected to Mumbai station");
        else if(ch==2)
            Console.WriteLine("Connected to Delhi station");
        else
            throw new OutOfFrequencyException();
    }
}

class Calculator : PDA
{
    double num1;
    double num2;
    double res;
    int choice;
    public Calculator(double n1,double n2,int c)
    {
        num1=n1;
        num2=n2;
        choice=c;
        if(num2==0)
            throw new DivideByZeroException();
        if(choice<0)
            throw new NegativeNumberException();
        switch(choice)
        {
            case 1:
                    res=num1+num2;
                    Console.WriteLine("sum is {0}",res);
                break;
            case 2:
                    res=num1-num2;
                    Console.WriteLine("difference is {0}",res);
                break;
            case 3:
                    res=num1*num2;
                    Console.WriteLine("product is {0}",res);
                break;
            case 4:
                    res=num1/num2;
                    Console.WriteLine("quotient is {0}",res);
                break;
            case 5:
                    res=Math.Sqrt(num1);
                    Console.WriteLine("square root is {0}",res);
                break;
            default:
                    Console.WriteLine("invalid option");
                break;

    }
}

class MainClass
{
    public static void Main()
    {
        try{
            PDA p1= new PDA(1);
        }
        catch(OutOfFrequencyException e)
        {
            Console.WriteLine("Exception caught");
        }
       
        try
        {
            PDA p2= new PDA(6,3,4);
        }
        catch(DivideByZeroException e)
        {
            Console.WriteLine("Cannot divide by zero");
        }
        catch(NegativeNumberException e)
        {
            Console.WriteLine("Cannot accept negative number");
        }
    
    }
}

     class OutOfFrequencyException  : System.Exception
    {
    }
     class NegativeNumberException : System.Exception
     {
     }
}

}