DrJava Coding Lab(Eclipse) (Instance Variable help)

In summary, the conversation discusses the task of changing the definition of two classes, EcoOne and EcoTwo, to be in composition relationships with the Terrarium class. In EcoOne, a void method called 'addCaterpillar' is defined to add a new Caterpillar to the composed Terrarium and start it, while a similar method called 'addButterfly' is also defined for adding and starting a Butterfly. In EcoTwo, the class is changed to have two composition relationships with Terrarium, and the methods 'addCaterpillar' and 'addButterfly' are modified to add the new object to a different Terrarium each time the method is called. The conversation also includes code attempts for both tasks.
  • #1
NightShift
7
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
  • #2


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.
 

1. What is DrJava Coding Lab?

DrJava Coding Lab is an interactive programming environment designed for Java developers. It provides a user-friendly interface for writing, debugging, and executing Java code.

2. What is Eclipse?

Eclipse is an integrated development environment (IDE) for various programming languages, including Java. It offers a range of features such as code editing, debugging, and version control to help developers write and manage code more efficiently.

3. What are instance variables in Java?

Instance variables, also known as non-static fields, are variables declared within a class that hold unique values for each object of that class. They are accessible from within any method or constructor of the class and can be used to store and retrieve data specific to each object.

4. How do I declare and initialize an instance variable in DrJava Coding Lab?

To declare an instance variable in DrJava Coding Lab, you need to first create a class and then declare the variable within the class using the appropriate data type. To initialize the variable, you can either assign a value to it directly or use a constructor method.

5. How can I access an instance variable in Eclipse?

In Eclipse, you can access an instance variable by creating an object of the class that contains the variable and using the dot operator (.) to access the variable. You can also use getter and setter methods to retrieve and modify the value of the instance variable.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
773
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
834
  • Programming and Computer Science
Replies
34
Views
2K
Back
Top