Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use this (incomplete) code in the 4 questions that follow. OtherClass is a user-

ID: 663814 • Letter: U

Question

Use this (incomplete) code in the 4 questions that follow. OtherClass is a user-defined class. It has a default constructor and a copy constructor, overrides equals(), and is mutable. (You’re not to write this class, but that is sufficient information to use the class in what you do have to write.)

public class MyParent {
     private int someNum;
     private OtherClass obj;
  
     public MyParent(int i) {
           someNum = I;
           obj = null;
     }
     
     public toString() {
           return “This is a MyParent.”;
}          
}

1) For MyParent, create an (int, OtherClass) constructor, a copy constructor, and properly override equals(). The latter should consider its argument to be equal to the MyParent in question if the argument is a MyParent (but not a subclass of MyParent), has the same value for someNum, and has an OtherClass which is equal to obj based on OtherClass’s definition of equals().

2)Create a subclass of MyParent called MyChild. MyChild has a character instance variable called type and a method getType() that returns it. Include a copy constructor, a (char, int) constructor, and a (char, int, OtherClass) constructor (thus matching the constructors in MyParent).Do not override equals().

3)

Create a class with a main() method in it. Inside main, create an array of 10 MyParent references. Use a loop to populate this array with a mix of MyParents and MyChilds. Use (int)(Math.random()*2) to get a value of 0 or 1 and use that to determine whether a MyParent or a MyChild is put into each location. For MyParents, create them with an int corresponding to the array index they’re being put into and a default OtherClass. Create MyChilds the same way, but with a char as well. Use “A” for the first MyChild created, “B” for the second, and so on (note that the letters increase for each MyChild, not for each element placed in the array).

4)

Write a loop as it would appear at the end of main() above that goes through the array and, for all MyChilds or potential subclasses of MyChild,outputs the result of calling getType().

The following code for OtherClass should make your code for this compilable:

class OtherClass {

public OtherClass() {}
public OtherClass(OtherClass o){}
public equals(Object o) {
return this != o;
}

}

It doesn't need to do anything, since the code itself doesn't make use of it. If you want to check your deep copying, the equals here will return true if the copy was deep and false if it wasn't. Of course, it will also return true if they're completely unrelated.

Explanation / Answer

MYPARENT CLASS

package mani;
public class MyParent {
private int someNum;
private OtherClass obj;
public MyParent(int k,OtherClass f){
   someNum=k;
   obj=f;
}
public MyParent(){
  
}
public MyParent(int i) {
someNum = i;
obj = null;
}
public boolean equals(Object o){
      
   return this!=o;
}
public String toString() {
return "This is a MyParent.";
}
}

OTHER CLASS

package mani;

class OtherClass {
   public OtherClass() {}
   public OtherClass(OtherClass o){}
   public boolean equals(Object o) {
   return this != o;
   }
   }

MYCHILD

package mani;

public class MyChild extends MyParent{
char type;
   public MyChild(char t,int i) {
       super(i);
       type=t;
      
       // TODO Auto-generated constructor stub
   }
   public MyChild(char s,int t,OtherClass y){
       super(t,y);
       type=s;
      
   }
public char getType(){
   return type;
  
}
public String toString() {
return "This is a MyChild";
}
}

MAIN METHOD

package mani;

public class MainMethod {

   /**
   * @param args
   */
   public static void main(String[] args) {
       MyParent[] myparent=new MyParent[10];
       int k;
       int j=0;
       char t;
       for(int i=0;i<10;i++){
           k=(int) (Math.random()*2);
           if (k==0){
           myparent[i]=new MyParent(i,new OtherClass());  
           }
           if(k==1){
               t=(char) ('A'+j);
              
               myparent[i]=new MyChild(t,i,new OtherClass());
               j++;
           }
       }
       String m;
       for(int i=0;i<10;i++){
           m=myparent[i].toString();
           if(m.equals("This is a MyParent.")){
              
               continue;
           }
           else{
               System.out.println(((MyChild) myparent[i]).getType());
           }
       }
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote