#writing a static method called copyTheObjects()

1 messages · Page 1 of 1 (latest)

ancient hareBOT
#

<@&987246399047479336> please have a look, thanks.

ancient hareBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

quartz dock
#

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 (main is 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.

sour python
#

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

plush latch
#

can you post 2 or 3 of the different classes that are supposed to go in the array? @sour python

plush latch
#

ok but are the "copy constructor"s like

public GraduateProgram(GraduateProgram gp) {
   // deep copy
}```
sour python
#

more like this

plush latch
#

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()

sour python
#

yeah thats where im stuck on as well

plush latch
#

but using reflection (which generally should be avoided) is using "the build in java classes" to do so

sour python
#

yeah we are not allowed to use that either i think

plush latch
#

and so the array can contain Coures and Programs?

sour python
#

yeah exactly

#

the array has courses, graduate programs, undergradprograms, certificateprograms, and specialprograms

plush latch
#

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

plush latch
#

incase you couldnt tell LUL

sour python
#

LOL bro i couldnt agree more

plush latch
#

It'd at least be an improvement if it used .clone()...

#

on classes explicitly implementing Cloneable

sour python
#

yeah the funny part is they taught us .clone() so idk why we cant use it

plush latch
#

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

sour python
#

im pretty sure it applies to both parts 🥲

#

they also gave us this instruction

plush latch
#

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

sour python
#

yeah i asked my ta and she said no 😭

plush latch
#

well TAs are usually stupid

#

I used to have TAs that are in nursing and mechanical engineering

#

like huh!?

sour python
#

LOL rly

#

yeah this seems like it is literally impossible

plush latch
#

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

sour python
#

okay yeah i will do that

sour python
#

thank you for helping man! wish you the best

plush latch
#

good luck with this bad instructor o7

brazen glade
#

@sour python make an interface

#

That's the solution

sour python
#

hmmm how so?

plush latch
#

we're able to modify the existing classes?

brazen glade
#
interface Copyable {
    Copyable copy();
}
sour python
plush latch
#

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

sour python
#

yeahh thats true

#

i dont know what to do at this point😵‍💫

plush latch
#

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