Question

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
Add a comment Improve this question Transcribed image text
Answer #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 a comment
Know the answer?
Add Answer to:
Assignment # 6              ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment # 6                      CSCI 215    Due:   04/11/2019 50   pts.       &nb

    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...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • Java Object Array With 2 Elements In 1 Object

    1. Create a UML diagram to help design the class described in Q2 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Person class object; should they be private or public? Determine what class methods are required; should they be private or public?2. Write Java code for a Person class. A Person has a name of type String and an age of type integer.Supply two constructors: one will be...

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • 1. Write a new class, Cat, derived from PetRecord – Add an additional attribute to store...

    1. Write a new class, Cat, derived from PetRecord – Add an additional attribute to store the number of lives remaining: numLives – Write a constructor that initializes numLives to 9 and initializes name, age, & weight based on formal parameter values 2. Write a main method to test your Cat constructor & call the inherited writeOutput() method 3. Write a new writeOutput method in Cat that uses “super” to print the Cat’s name, age, weight & numLives – Does...

  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...

    In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass. Code for Store below(Super class aka...

  • Create a UML diagram to help design the class described in exercise 3 below. Do this...

    Create a UML diagram to help design the class described in exercise 3 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Baby class object; should they be private or public? Determine what class methods are required; should they be private or public? 3. Write Java code for a Baby class. A Baby has a name of type String and an age of type integer. Supply two...

  • About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which...

    About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which has private data members for name, age, gender, and height. You MUST use this pointer when any of the member functions are using the member variables. Implement a constructor that initializes the strings with an empty string, characters with a null character and the numbers with zero. Create getters and setters for each member variable. Ensure that you identify correctly which member functions should...

  • Suppose we have the following Java Interface: public interface Measurable {    double getMeasure(); // An...

    Suppose we have the following Java Interface: public interface Measurable {    double getMeasure(); // An abstract method    static double average(Measurable[] objects) { // A static method    double sum = 0;    for (Measurable obj : objects) {    sum = sum + obj.getMeasure();    }    if (objects.length > 0) { return sum / objects.length; }    else { return 0; }    } } Write a class, called Person, that has two instance variables, name (as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT