CodeGym /Java Course /New Java Syntax /Practicing initializing objects

Practicing initializing objects

New Java Syntax
Level 10 , Lesson 4
Available

"Hi, Amigo! I already miss our lessons. Here are a few tasks to practice object initialization:"

10
Task
New Java Syntax, level 10, lesson 4
Locked
Populate the Friend class
A friend (Friend class) must have three initializers (three initialize methods): - Name - Name, age - Name, age, sex. Note: name is a String, age is an int, and sex is a char.
10
Task
New Java Syntax, level 10, lesson 4
Locked
Populate the Dog class
A dog (Dog class) must have three initializers: - Name - Name, height - Name, height, color.
10
Task
New Java Syntax, level 10, lesson 4
Locked
Let's put together a rectangle
The data for the rectangle (Rectangle class) will be top, left, width, and height. Create as many initialize(...) methods as possible Examples: - 4 parameters are specified: left, top, width, height - width/height is not specified (both are 0); - height is not specified (it is equal to the width), w
10
Task
New Java Syntax, level 10, lesson 4
Locked
Initializing objects
A person (Person class) should have a String name and int age. Add the initialize(String name, int age) method where you will initialize the variables name and age. In the main method, create a Person object and store a reference to it in the variable person. Call the initialize method with any valu
10
Task
New Java Syntax, level 10, lesson 4
Locked
Initializing cats
A cat (Cat class) must have five initializers: - Name - Name, weight, age - Name, age (standard weight) - Weight, color (unknown name, address and age, i.e. a homeless cat) - Weight, color, address (someone else's pet). The initializer's job is to make the object valid. For example, if the weight i
10
Task
New Java Syntax, level 10, lesson 4
Locked
Populate the Circle class
The Circle class must have three initializers: - centerX, centerY, radius - centerX, centerY, radius, width - centerX, centerY, radius, width, color.
10
Task
New Java Syntax, level 10, lesson 4
Locked
Initializing objects
Study the Person class carefully. Correct the class so that only one initialize method initializes all of the Person class's instance variables.
Comments (54)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Michael Amann Level 22, United States of America, United States
13 April 2022
The instructions say 4 parameters are specified: left, top, width, height Then it says..."height is not specified (it is equal to the width)" Height is one of the parameters. So which one is it?
Justin Smith Level 41, Greenfield, USA, United States
5 July 2021
There's a hidden lesson to notice with "Initializing Cats". if you look at what you have for the 5 initialize methods, you'll notice that they all have a unique sequence of variable types. They are, in order: string string, int, int string, int int, string int, string, string This is because this is how it knows which initialize method you are calling. If you had two initialize methods that both took a string and an int in that order, then when you called the initialize method and had the string and int arguments, it wouldn't know which one you were calling.
Jonaskinny Level 25, Redondo Beach, United States
11 February 2022
Well, while that is true, the compiler wont let you make a mistake that would not enable it to choose. try compiling a class with two such methods and you would get java: method initialize(java.lang.String,int) is already defined in class
Drzazgen Level 8, Warsaw, United Kingdom
12 February 2021
I'm not sure I understand: initialize method is another way to given variable to object? Previously we did: Person person = new Person(); person.name = "Mark"; etc. or with getName and now we make initialize. Where is difference?
John Solomon Legara Level 8, Makati, Philippines
7 May 2021
They are virtually the same with the fact that they help you assign values to the variables, but I think the initializers are bigger than that. The way they are described here, they are supposedly a way to make the class useful. I think they are even bigger than the concept of setters which, currently, they look like one. To understand what I mean, I am going to use an example used in some previous articles they posted here: Imagine a class named Cat with variables String name, and int age. Virtually, you can do this:

Cat cat = new Cat();
cat.age = -1000
However, making a cat that is -1000 years old doesn't make sense, hence you are to make setters:

private int age;
public void setAge(int age) {
    if (age > 0)
        this.age = age
    else
        System.out.println("That age is impossible!")
}
So here, we created a setter function setAge, which is used so that we could set age according to proper conditions. Now, we also set the int age variable to private so that it cannot be accessed outside the class if not through the setAge method. This basically guarantees they use the setAge method else there's really no point if they can edit the value beyond the setter. With this in mind, we can imagine that if multiple variables have required rules, you can have multiple setter methods, and then one initializer method where you can collate all of them. Of course, at this level, only setters makes sense to help initialize the class, but I think there are more to come. If so, these methods can also be included in the initializer method.
luis_rmr Level 8, San Ramon, Costa Rica
10 October 2020
Can someone please remind me the difference between a constructor and an initializer?
Michael Amann Level 22, United States of America, United States
13 April 2022
Constructors don't necessarily initialize anything.
Mihai Bone Level 8, Bucharest, Romania
8 October 2020
One single mistake instead of

initialize 
I did write

initializer
. (it did took me 30 minutes to figure out were is the problem)....... just one F... letter. 😡
Maryem Vickers Level 7, HT..., United Kingdom
19 August 2020
"...the mysterious and beautiful creatures we call cats." - CodeGym Hey. After I read one book they were not as "mysterious" as they once were, and for many other people too. But they certainly are BEAUTIFUL-- or, rather, CUTE. - MV Maryem V
Agent Smith Level 38
10 August 2020
"Let's create a new person... Actually, that would be better done elsewhere". I see what you did there. :-D
Maryem Vickers Level 7, HT..., United Kingdom
19 August 2020
"I see what you did Not so kind there." - Maryem V Mhm.
Johannes Level 27, Centurion, Pretoria, South-Africa
3 March 2020
So there cannot be 2 initialize methods in the class with the same vairable type (e.g. int) but for different fields ? e.g. private void initialize (int top){ } and private void initialize (int left){ }
Nickolas Johnson Level 8, St. Louis, United States of America
9 March 2020
Exactly. The compiler doesn't know how to tell the two apart because they both pass the same type of value. However, the below code would still work because it matters what types of values are being passed, not what the actual values are.

private void initialize (int top){
}
and 
private void initialize (String name){
}
Erik Kristofer Anderson Level 15, Chicago, United States
15 April 2020
I just learned this the hard way; I worked way harder than I needed to. I though there would be lots of intialize methods and I didn't want to type them all out. So I wrote a python script to generate what I though was all possible initializers. I didn't realize that java can't tell the different between the following:

    public void initialize(int left, int top) {
        this.left = left;
        this.top = top;
    }
and

    public void initialize(int left, int width) {
        this.left = left;
        this.width = width;
    }
So I generated 12 initialize methods when only four were possible. Oh well, it was a good learning exercise.
Maryem Vickers Level 7, HT..., United Kingdom
19 August 2020
Aikes.
10 January 2020
You have a nice felling when you see all of those check marks green. If they will do something similar at work the productivity it will go 500% :D .
Maryem Vickers Level 7, HT..., United Kingdom
19 August 2020
*feling you mean... you spelt it wrong. And haha yeah.
Andrei Level 41
5 October 2020
*feeling you mean. ;) you spelt it wrong too, Maryem.
Maryem Vickers Level 7, HT..., United Kingdom
9 October 2020
I know! It's a good old joke! And I tricked you! rwr ACTUALLY-- I shoulda said; "feeline". rwr rwr rwr!
Krig Raseri Level 38, Dallas, United States
23 August 2022
Maryem has been hitting the dab pen
matt Level 24, Springfield, United States
6 January 2020
int color? that's weird isn't it
Daniel Level 15, Colnbrook, United Kingdom
7 January 2020
no, it is not. I thought so too for a second, then i remembered about hex codes for colours. https://htmlcolorcodes.com/