Wednesday, March 4, 2020

WEEK 5 ASSIGNMENT SOLUTION

QUESTION 1 :


//Create a class A which implements the interface Number.
class A implements Number
{
  public int findSqr(int i)
  {
    return i*i;
  }
}

QUESTION 2:

 
//Create a class B, which implements the interface GCD.
class B implements GCD
{
  public int findGCD(int a, int b)
  {
    if(b==0)
      return a;
    else
      return findGCD(b,a%b);
  }
}

QUESTION 3:


//Read any two values for a and b.
 //Get the result of a/b;
a=input.nextInt();
b=input.nextInt();
{
  try
  {
    if(b!=0)
    {
      System.out.print(a/b);
      System.exit(0);
    }
  }
  finally
  {
    System.out.print("Exception caught: Division by zero.");
  }
}

QUESTION 4:


//Define try-catch block to save user input in the array "name",if there is an exception then catch the exception otherwise print the total sum of the array.
try
{
  for(int i=0; i<length; i++)
  {
    name[i]=sc.nextInt();
    sum=sum+name[i];
  }
  System.out.print(sum);
  System.exit(0);
}
finally
{
  System.out.print("You entered bad data.");
}

QUESTION 5:


// Put the following code under try-catch block to handle exceptions
      try
      {
switch (i)
        {
    case 0 :
int zero = 0;
j = 92/ zero;
break;
        case 1 :
int b[ ] = null;
j = b[0] ;
break;
      default:
       System.out.print("No exception");
}
      }
     catch(Exception e)
        {
          System.out.print(e);
        }

5 comments:

  1. Ok...

    What I'm going to tell you might sound a little creepy, maybe even kind of "strange"

    BUT what if you could simply hit "PLAY" and listen to a short, "magical tone"...

    And miraculously bring MORE MONEY into your LIFE???

    And I'm really talking about BIG MONEY, even MILLIONS of DOLLARS!!!

    Sounds way too EASY??? Think this couldn't possibly be for REAL??

    Well then, I'll be the one to tell you the news.

    Sometimes the greatest miracles in life are the SIMPLEST!!!

    In fact, I will PROVE it to you by allowing you to listen to a REAL "magical money tone" I developed...

    YOU simply click "PLAY" and you will start having more money come into your life.. it starts right away..

    CLICK here NOW to PLAY this wonderful "Miracle Money-Magnet Sound Frequency" as my gift to you!!!

    ReplyDelete
  2. However, finding the best assignment writing service provider is not that Fastest PhD Thesis Writing Service easy of a task. The students need to take care of some important factors. We all know that there are some fraud assignment services providing companies.

    ReplyDelete
  3. Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! assignment代写

    ReplyDelete
  4. Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too. 作业代写

    ReplyDelete