#writing a static method called copyTheObjects()
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
That's one weird request. Inheritance plays no role here, since constructors aren't inherited.
So a generalised solution, in which you can't refer to the possible set of types to expect, would be forced to use reflection. Which is never where we should start - it's the escape hatch to use when the type-system can't help.
As to where to start...
- Do you know how to declare a local variable of an array type and initialise it with values?
- Do you know how to declare a method (
mainis a static method, taking an array of String as an argument but not returning a result)? - Do you know how to get the length of an array and iterate the array elements?
You could start with
- Declare the method, have it copy the array, but just return the original elements
- Create the array of inputs and use it to call your method.
Then you can move on to - given an element in the original array, how do I make a copy.
yeah i did what you said
but how do i write the method so that it calls every copy constructor of each object
there are 5 different objects
in the array and it does not want me to specify the objects
so it doesnt want me to call the copy constructors manually
can you post 2 or 3 of the different classes that are supposed to go in the array? @sour python
not sure if this helps ><
ok but are the "copy constructor"s like
public GraduateProgram(GraduateProgram gp) {
// deep copy
}```
yeah I'm with talden on this, idk how you're going to do this without reflectively calling that constructor based on the elements .getClass()
yeah thats where im stuck on as well
but using reflection (which generally should be avoided) is using "the build in java classes" to do so
yeah we are not allowed to use that either i think
and so the array can contain Coures and Programs?
yeah exactly
the array has courses, graduate programs, undergradprograms, certificateprograms, and specialprograms
yeah without writing explicit type checking I only see this being possible from reflection.
Just as an educational sidenote this assignment is horrible and worthless
incase you couldnt tell 
LOL bro i couldnt agree more
It'd at least be an improvement if it used .clone()...
on classes explicitly implementing Cloneable
yeah the funny part is they taught us .clone() so idk why we cant use it
So looking at the instructions you posted, and nothing says you cant use the java API
only your text before it
maybe that only applies to part 1
thats easy to do
you just modify the references in the original after copying
or print the hashcodes lol
I would email them asking if the "no java classes" applies to this part as well, since u dont see a way to do it under the restrictions without reflection
yeah i asked my ta and she said no 😭
well TAs are usually stupid
I used to have TAs that are in nursing and mechanical engineering
like huh!?
yeah my teacher was like 'how is my TA supposed to grade a network configuration when they dont know what IP stands for'
So I'd ask the professor directly if you're able to use reflection to complete it
okay yeah i will do that
if not i think im just going to make a shallow copy of the array and explain why thats incorrect like it instructs here
thank you for helping man! wish you the best
yeah but it said to use the copy constructor. Unless they thing = means copy constructor in java...
good luck with this bad instructor 
hmmm how so?
we're able to modify the existing classes?
interface Copyable {
Copyable copy();
}
Eeeh you can add in stuff i think but not change the required stuff
Hmm. Cause if so then ethan's idea is a cheap non-reflection one
The only thing I can see that might be a problem is their "will automatically depend on the copy constructors". Which is indirectly true
I would lean towards emccue's. But i'd still ask to verify that you can add extra code to the classes in part 1: Particularly implementing a common interface