Java Programming: Use this (incomplete) code in the 4 questions that follow. Oth
ID: 663801 • Letter: J
Question
Java Programming:
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;
}
}
Explanation / Answer
1)
/*The class MyParent contains the parameter constructor to set i and OhterClass object and copy coonstructor.
*This java class override the equals method with Object to check if given object is OtherClass object
*and returns true if somNum and obj are matched with object of type MyParent. */
//MyParent.java
public class MyParent
{
//private members of class
private int someNum;
private OtherClass obj;
//parameter constructor
public MyParent(int i,OtherClass obj)
{
someNum = i;
this.obj = obj;
}
public int getSomeNum()
{
return someNum;
}
public OtherClass getOtherClass()
{
return obj;
}
//copy constructor
public MyParent(MyParent myParent)
{
this.someNum=myParent.someNum;
this.obj=myParent.obj;
}
//Overrideing equals method
@Override
public boolean equals(Object obj)
{
if(obj instanceof MyParent)
{
MyParent myParent=(MyParent)obj;
return someNum==myParent.someNum && obj.equals(myParent);
}
else
return false;
}
//Override toString method that returns the private members of class
public String toString()
{
return "parent : someNum "+someNum+" MyParent : "+obj;
}
}
------------------------------------------------------------------------------------------------------------------------------------
2)
//MyChild.java
//The class inherits public methods from the parent class MyParenet
public class MyChild extends MyParent
{
//create private variable of class MyChild
private char ch;
//Create an constructor with argument character,
//integer and OtherClass object
public MyChild(char ch,int i,OtherClass otherObject)
{
//calling super class constructor
super(i, otherObject);
this.ch=ch;
}
//copy constructor with arguments character and integer
public MyChild(char ch,int i)
{
super(i, null);
this.ch=ch;
}
//Returns the character value
public char getType()
{
return ch;
}
//Override toString method that returns the chracter
//value of i and OtherObject details
@Override
public String toString()
{
return "Child : Character"+ch+" i "+getSomeNum()+" OtherObject "+getOtherClass();
}
}
------------------------------------------------------------------------------------------------------------------------------------
3)
/*The driver program that creates an array of ten MyParent
* and creates a random values either 0 or 1 to create
* objects of MyParent with index and default constructor
* of OtherClass and MyChild class object with character
* that starts from 'A' and index value and default OtherClass
* object as arguments.
* Then assign the objects into the myParent array and
* print the object values .
* On each increment of the character variable ch , the chracter 'A'
* becomes 'B','C' and so on.
* */
//Driver.java
public class Driver
{
public static void main(String[] args)
{
//set character starting from 'A'
char ch='A';
//Create an array of size 10 of type MyParent
MyParent[] myParent=new MyParent[10];
for (int index = 0; index < myParent.length; index++)
{
//create a random value either o or 1
int value=(int)(Math.random()*2);
if(value==0)
{
/*Create an instace of MyParent class with index and OtherClass default
constructor*/
MyParent parentObject=new MyParent(index, new OtherClass());
myParent[index]=parentObject;
}
else
{
/*Create an instace of MyChild class with character ,index and OtherClass default
constructor*/
MyChild child=new MyChild(ch, index, new OtherClass());
myParent[index]=child;
//increment the ch value to get next character from 'A'
ch++;
}
}
//print the object values in the myParent array
for (int index = 0; index < myParent.length; index++)
{
System.out.println(myParent[index]);
}
}
}
sample output:
parent : someNum 0 MyParent : OtherClass :
parent : someNum 1 MyParent : OtherClass :
parent : someNum 2 MyParent : OtherClass :
Child : CharacterA i 3 OtherObject OtherClass :
Child : CharacterB i 4 OtherObject OtherClass :
Child : CharacterC i 5 OtherObject OtherClass :
Child : CharacterD i 6 OtherObject OtherClass :
Child : CharacterE i 7 OtherObject OtherClass :
Child : CharacterF i 8 OtherObject OtherClass :
parent : someNum 9 MyParent : OtherClass :
------------------------------------------------------------------------------------------------------------------------------------
4)
//Since the OhterClass not have any private members
//the OtherClass has no assignments and values
//OtherClass.java
public class OtherClass
{
//Default constructor of OtherClass
public OtherClass()
{
//Not set
}
public OtherClass(OtherClass o)
{
//Not set
}
@Override
public boolean equals(Object o)
{
return this!=o;
}
@Override
public String toString()
{
return "OtherClass : ";
}
}
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.