-2

I'm trying to instantiate three objects in a loop so that I don't reuse the constructor text. This is because, for Uni, we get marked down if we reuse the same line of code (it's marked automatically).

My code is as follows:

EnemyShip enemy1;
EnemyShip enemy2;
EnemyShip enemy3;

public Game()
{
    for (int i = 1; i <= 3; i++) {
        getEnemyRef(i) = new EnemyShip(); //getEnemyRef unexpected type - required:variable found:value
        //enemy1 = new EnemyShip(); works normally
    }
}

The i is underlined and I am told that it is a value not variable.

Please note that I use the following code instead of a list or array because we are not allowed to use them for this task.

public EnemyShip getEnemyRef(int enemy) {
    switch (enemy) 
    {
        case 1:
        return enemy1;

        case 2:
        return enemy2;

        case 3:
        return enemy3;
    }
    return null;
}

The marking system bases how many times a line is reused from how many times it is written in source code, not how many times it is executed

  • you need the exact variable name when assigning values. do `EnemyShip e1 = getEnemyRef(i);` before and then `e1 = new enemyShip()` – XtremeBaumer Jun 01 '18 at 13:42
  • 1
    you probably wanted something like this: `EnemyShip enemy = getEnemyRef(i);` – Lino Jun 01 '18 at 13:43
  • 3
    `getEnemyRef(i)` is a method, thus returns a value. You cannot assing something to a value (thus the compiler message: `required:variable found:value`). You constructor call is not the problem, the assignment (or more precise: the left hand side of the assignment) is the problem. – Turing85 Jun 01 '18 at 13:44
  • It looks like you need to learn about arrays and / or Lists. – John Bollinger Jun 01 '18 at 13:45
  • What are `enemy1`, `enemy2`, `enemy3`? Where do they come from? – Mikhail Batcer Jun 01 '18 at 13:48
  • I have them declared at the top of the code, forgot to include them but just edited it now @Mikhail – Harry Allen Jun 01 '18 at 13:51
  • 4
    I love when classes teach you bad coding practice in order to get you to do something a certain way using a certain method when in reality you would never do it that way – Zannith Jun 01 '18 at 14:16
  • @HarryAllen I have updated my answer once again. You may want to have a look at it – Lino Jun 04 '18 at 06:16

2 Answers2

0

This snippet should do the trick:

public void setEnemyRef(int enemyFlag, EnemyShip enemy){
    switch (enemyFlag){
        case 1:
            enemy1 = enemy;
            break;
        case 2:
            enemy2 = enemy;
            break;
        case 3:
            enemy3 = enemy;
            break;
    }
}

And then in the for-loop:

for (int i = 1; i <= 3; i++) {
    setEnemyRef(i, new EnemyShip());
}
Lino
  • 19,604
  • 6
  • 47
  • 65
  • This does work and meet my requirements but sadly I still lose the marks because it uses the createNew() method three times. (I lose marks for reuse (more than one usage) of equality checks, sub/add, constructor use, method calls and communication between classes) – Harry Allen Jun 01 '18 at 14:11
  • 1
    props for creativity, but you are using an array which OP said was not allowed – Zannith Jun 01 '18 at 14:19
  • @TravisHerbert I've updated, still not sure if that's allowed – Lino Jun 04 '18 at 06:40
-1

You're not instantiating it correctly, try e.g.

EnemyShip variableName = getEnemryRef(i); variableName = new EnemyShip();

https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html

Julian Alwandy
  • 321
  • 1
  • 4
  • 13
  • 3
    this code is equivalent to `EnemyShip variableName = new EnemyShip();` (under the assumption that `getEnemyRef(i)` is a method that never throws) and I do not think that this is what OP asks for. Please remember that [Java passes by value always](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value), and it returns by value in the same manner. – Turing85 Jun 01 '18 at 13:47
  • Fair enough but his solution to tackle the problem does not seem optimal. – Julian Alwandy Jun 01 '18 at 13:52
  • @JulianAlwandy The predicament that I'm in is that I need to reduce the use of the constructor '= new EnemyShip();' so that I'm not using three times to create three obejcts. Is there a way to do that? – Harry Allen Jun 01 '18 at 13:55
  • @HarryAllen No. If you want three distinct objects, you need at least three constructor calls. – Turing85 Jun 01 '18 at 13:56
  • @Turing85 So the only possible way is to use an array/list? – Harry Allen Jun 01 '18 at 13:58
  • @HarryAllen I am not sure I follow your train of thoughts. Even in an array/`List<...>`, if you want to store three distring objects in them, you need to first instantiate those three objects (via three constructor calls). – Turing85 Jun 01 '18 at 13:59
  • @Turing85 Maybe I am using the wrong terminology. A loop such as: while (true) { EnemyShip e = new EnemyShip() list.add(e) } Could create more than one object while only using the line once? – Harry Allen Jun 01 '18 at 14:04
  • @HarryAllen Well... you WRITE the line only once, but the line (in your example) is executed as often as your heap memory allows it. – Turing85 Jun 01 '18 at 14:05
  • @Turing85 the marking system we are submitting to only counts a use as when it is written, not how many times it is used/executed. It's a bit daft – Harry Allen Jun 01 '18 at 14:13
  • 2
    @HarryAllen you may want to edit your question and add this information. It is quite crucial to answer it correctly. – Turing85 Jun 01 '18 at 14:15