DrJava Coding Lab(Eclipse) (Instance Variable help)

  • Context: Comp Sci 
  • Thread starter Thread starter NightShift
  • Start date Start date
  • Tags Tags
    Coding Variable
Click For Summary
SUMMARY

The forum discussion focuses on implementing composition relationships in Java using the DrJava Coding Lab within Eclipse. Specifically, it addresses modifying the lab3.EcoOne and lab3.EcoTwo classes to manage instances of example1.Terrarium, example1.Caterpillar, and example1.Butterfly. The solution for EcoOne is provided, while the user seeks guidance on completing EcoTwo, which requires maintaining two Terrarium instances and a counter to alternate between them when adding new objects.

PREREQUISITES
  • Understanding of Java programming and object-oriented concepts
  • Familiarity with composition relationships in software design
  • Knowledge of instance variables and method definitions in Java
  • Experience with the Eclipse IDE and DrJava for Java development
NEXT STEPS
  • Implement a counter variable in lab3.EcoTwo to track method calls
  • Define a second instance variable of type example1.Terrarium in lab3.EcoTwo
  • Research Java method overloading to enhance the functionality of addButterfly and addCaterpillar
  • Explore design patterns related to composition in Java for better code organization
USEFUL FOR

Students learning Java programming, software developers working with object-oriented design, and educators teaching composition relationships in coding.

NightShift
Messages
7
Reaction score
0

Homework Statement


Lab Tasks

1. Change the definition of the lab3.EcoOne class so that it is in a composition
relationship with the example1.Terrarium class. Define a void method named
‘addCaterpillar’ which adds a new example1.Caterpillar to the composed
Terrarium, and starts the Caterpillar. Also define a void method named
‘addButterfly’ which adds a new example1.Butterfly to the composed
Terrarium, and starts the Butterfly.

2. Change the definition of the lab3.EcoTwo class so that it is in two composition
relationships with the example1.Terrarium class. (Hint: this means that there
will be two instance variables of type example1.Terrarium in the lab3.EcoTwo
class.)
Define the methods ‘addCaterpillar’ and ‘addButterfly’ as described above, but
with the following modification. Define these methods so that each time
either method is called, the Terrarium that is added to is swapped. In other
words, if we label the two Terrariums t1 and t2, the first time a Butterfly or a
Caterpillar is added, it is added to t1, but the second time one of them is
added, it is added to t2, and the third time it is added to t1 again, and so on.
For example, the following code must result in two Butterflies and one
Caterpillar in t1, and two Caterpillars in t2:
lab3.EcoTwo et = new lab3.EcoTwo();
et.addButterfly();  added to t1
et.addCaterpillar();  added to t2
et.addCaterpillar();  added to t1
et.addCaterpillar();  added to t2
et.addButterfly();  added to t1


The Attempt at a Solution



Task 1

package lab3;

public class EcoOne {

public example1.Terrarium _et;

public EcoOne() {

_et = new example1.Terrarium();
}
public void addButteryfly(){

example1.Butterfly b;
b = new example1.Butterfly();

_et.add(b);
b.start();
}
public void addCaterpillar(){

example1.Caterpillar c;
c = new example1.Caterpillar();

_et.add(c);
c.start();
}
}

\\This is Part 1 of the Tasks.________________________________________________________________
Task 2package lab3;

public class EcoTwo {

public example1.Terrarium _et;

public void addButterfly(){

example1.Butterfly b;
b = new example1.Butterfly();

_et = temp;

_et.add(b);
temp.add(b);
}
public void addCaterpillar(){

example1.Caterpillar c;
c = new example1.Caterpillar();

_et.add(c);
temp.add(c);

}
public EcoTwo() {

_et = new example1.Terrarium();

lab3.EcoTwo _et2 = new lab3.EcoTwo();

_et.addButterfly();
_et.addCaterpillar();
_et.addCaterpillar();
_et.addCaterpillar();
_et.addButterfly();
}
}

\\This is my attempt on Task 2. I don't know how to complete task 2. My instructor said to declare a temp variable.
 
Physics news on Phys.org


In your attempt for Task 2, you are on the right track by declaring a temp variable, but you also need to have another instance variable of type example1.Terrarium in the lab3.EcoTwo class. This will allow you to swap between the two Terrariums when adding Butterflies and Caterpillars. Additionally, you should also consider using a counter variable to keep track of the number of times the methods have been called and use it to determine which Terrarium to add the new object to. I would recommend discussing this further with your instructor or classmates to fully understand the concept of composition relationships and how to implement them in your code.
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K