Homework Help Question & Answers

Assignment # 6              ...

Assignment # 6                      CSCI 215   

Due:   04/11/2019

50   pts.                                       

In this program assignment, you will construct two different exception classes: ClassNotFoundException and ObjectNotFoundException.

A static method search that takes an array of objects and a target object as arguments and throws two types of exceptions must be defined. An object of ClassNotFoundException should be thrown if the array doesn’t contain an object which is from the same class as the target; if the array contains an object which is the same type of the target but an object with same values as the target can’t be found in the array, an object of ObjectNotFoundException should be generated; otherwise the method should return the index of the first occurrence of the target object.

For example, let AO be the array in next paragraph, if D is a Double and I is an Integer with a value of 25, search(AO, D) will throw a ClassNotFoundException and search(AO, I) will throw an ObjectNotFoundException. search(AO, “hello”) should return 2.

In your main method, an array of Objects AO should be created, including 6 items:

          Class                 Value

  • Integer                5
  • Integer                42
  • String                 “hello”
  • String                 “there”
  • Person                “Laura” 40
  • Person                “Peter”   35

Also in your main method, try to search the following:

      Integer      5

      Person      “steve”   5

      Double      42

Note:   Operator instanceof can be used to check if an object is a member of certain class. Or the method getClass can be used to check the class identity of an object. You should also implement a class Person whose member variables are:

              String name;

              int      age;

A Person object equals another if and only if the two objects have the same name and age. Please override equals method for user defined classes.

0 0
ReportAnswer #1

Code:

class ClassNotFoundException:Exception
{
public override string ToString()
{
return "class Not Found Exception";
}
}

class ObjectNotFoundException : Exception
{
public override string ToString()
{
return "Object Not Found Exception";
}
}

class Person
{
string name;
int age;

public override bool Equals(object obj)
{
if(obj is Person)
{
Person person = obj as Person;
return ((this.age == person.age) && (this.name == person.name));
}
return false;
}

}
class Program
{
static int search(object[] arrayofObjects,object target)
{
bool bFoundEqualType = false;
foreach (object item in arrayofObjects)
{
if(item.GetType() == target.GetType())
{
if(item.Equals(target))
{
return Array.IndexOf(arrayofObjects, item);
}
bFoundEqualType = true;
}
}
if(!bFoundEqualType)
{
throw new ClassNotFoundException();
}
else
{
throw new ObjectNotFoundException();
}
}

}

Add Homework Help Answer
Add Answer of:
Assignment # 6              ...
Your Answer: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
More Homework Help Questions Additional questions in this topic.
Need Online Homework Help?
Ask a Question
Get FREE EXPERT Answers
WITHIN MINUTES
Related Questions