Skip to content

Commit

Permalink
Re-formatted E3, minor tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
maxygdell committed Sep 13, 2024
1 parent 101f33c commit 22736d0
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions exercises/standard_exercises/E3.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,55 @@ Create a Car class with the following properties: make (String), model (String),
</ol>

## Creating and Manipulating Objects in Java
In this exercise, we will explore the concept of classes and constructors by imagining and creating objects in Java. Remember, coding is not just about technical skills; it's also a creative process that allows us to bring our ideas to life.
>In this exercise, we will explore the concept of classes and constructors by imagining and creating objects in Java. Remember, coding is not just about technical skills; it's also a creative process that allows us to bring our ideas to life.
### Task 1: Create a Class Describing an Object

Create a new Java file (.java) with a class that represents a physical object of your choice. It could be a person, an animal, an airplane, or anything else you can imagine. Inside the class:
<ol>
<li>Declare three variables of three different types that are relevant to your chosen object.</li>
<li>Initialize these variables with reasonable values. For example, if you chose a "Person" class, you might have variables for name (String), age (int), and height (double).</li>
</ol>
>Create a new Java file (.java) with a class that represents a physical object **of your choice**. It could be a person, an animal, an airplane, or anything else you can imagine. Inside the class:
>
><li>Declare three variables of three different types that are relevant to your chosen object.</li>
><li>Initialize these variables with reasonable values. For example, if you chose a "Person" class, you might have variables for name (String), age (int), and height (double).</li>
### Task 2: Define a Container Class

Create another Java file (.java) to define a "container" class for your objects. Think of this as a vessel for storing instances of your chosen object. Inside this class:
>**Create another Java file (.java) to define a "container" class for your objects. Think of this as a vessel for storing instances of your chosen object. Inside this class:**
>
><li>Create two separate instances of the class you defined in Task 1. These instances represent >two unique objects of the same type.</li>
<ol>
<li>Create two separate instances of the class you defined in Task 1. These instances represent two unique objects of the same type.</li>
</ol>

### Task 3: Create a main()-method to create instances of your classes

Create a third Java file (.java) that serves as your main executor-class. In this class:
>**Create a third Java file (.java) that serves as your main executor-class. In this class:**
><li>Create a new instance of your Container Class</li>
><li>Print the details of the two objects in your container.</li>
><li>Change the values of one of the instances such that the two objects have unique instance variable values. Print the details again.</li>
><li>Create a third object of your intial class from task 1, outside of your container (i.e. directly in the main()-method). Assign reasonable values to it</li>
><li>Create a fourth object of your initial class from task 1, outside of your container. Assign it with one of the objects inside the Container object. (<code>Object newObject = object1;</code>)</li>
>You should now have four variables of your initial class. Two inside of the container, and two outside of the container.
<ol>
<li>Create a new instance of your Container Class.</li>
<li>Print the details of the two objects in your container.</li>
<li>Change the values of one of the instances such that the two objects have unique instance variable values. Print the details again.</li>
<li>Create a third object of your intial class from task 1, outside of your container (i.e. directly in the main()-method). Assign reasonable values to it</li>
<li>Create a fourth object of your initial class from task 1, outside of your container. Assign it with one of the objects inside the Container object. (<code>Object newObject = object1;</code>)</li>
</ol>
You should now have four variables of your initial class. Two inside of the container, and two outside of the container.


### Task 4: Understanding References

<ol>
<li>Print the reference values of all four variables (the two instances from Task 2 and the new variable from Task 3) to the terminal. This can be done by simply using print(variablename).</li>
<li>Analyze the results: What do you observe? Why do the reference values behave the way they do?</li>
</ol>

><li>Print the reference values of all four variables (the two instances from Task 2 and the new variable from Task 3) to the terminal. This can be done by simply using print(variablename).</li>
><li>Analyze the results: What do you observe? Why do the reference values behave the way they do?</li>

### Task 5: Modifying Variables and Observing Changes

<ol>
<li>Re-assign the values of the two variables in the Container with new values. For example, if you have a "Person" class, change the name, age, and other attributes.</li>
<li>Observe the values of the other two variables (the ones outside of the Container). Explain the behavior you see. How do changes to one object affect the others?</li>
</ol>

><li>Re-assign the values of the two variables in the Container with new values. For example, if you have a "Person" class, change the name, age, and other attributes.</li>
><li>Observe the values of the other two variables (the ones outside of the Container). Explain the behavior you see. How do changes to one object affect the others?</li>

### Task 6: Constructors

Refactor your code in the Classes and the Main-program such that you have parameterised constructors that you use to create your objects. Let your creativity run free!
>Refactor your code in the Classes and the Main-program such that you have parameterised constructors that you use to create your objects. Are you able to implement something that you've learned during the previous lectures into this exercise?
> ### Let your creativity run free!
> (((
> )))
> +-----+
> | |] Java is fun!
> `-----'

0 comments on commit 22736d0

Please sign in to comment.