undefined

Practicing initializing objects

Java Syntax
Level 5 , Lesson 7
Available

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

undefined
4
Task
Java Syntax, level 5, lesson 7
Locked
Create a Friend class
Let's make up our own invisible friends, give them some qualities, and then go play with them, just like the other loners do! No? Okay, another time. We'll get through today by creating a friend template, i.e. a Friend class. And we'll make up three initializers for it: name, age and sex; only name and age; and only name.
undefined
8
Task
Java Syntax, level 5, lesson 7
Locked
Initializing cats
This task is dedicated to the mysterious and beautiful creatures we call cats. Create a Cat class and define five initializers (only name; name plus weight and age; only weight and color; name and age; weight, color, and address). We need valid cats: they can't exist without a weight and age. Alas, there are cats without names and homes.
undefined
2
Task
Java Syntax, level 5, lesson 7
Locked
Create a Dog class
The "Man's Best Friend" Bureau has decided to conduct a full census of Terra's dog population. To do this, they need an application that has a special Dog class. The only thing we can learn (or assign) about some dogs is a name. For other pooches, we also know the height. Finally, complete information about other dogs also includes color. This means that we need three initializers.
undefined
2
Task
Java Syntax, level 5, lesson 7
Locked
Create a Circle class
A circle is the most perfect shape in the world. At least that's what the roly polies on the 2D planet 2piR think. The roly polies needed a registration program to identify fellow roly polies in the plane. The challenge is that different data is known about various roly polies. Let's help them: create a Circle class with three initializers.
undefined
4
Task
Java Syntax, level 5, lesson 7
Locked
Let's put together a rectangle
OKEA sells unassembled space shuttles. They are all rectangular (we don't know why). These guys ordered a program from Company X for automatically generating shuttle instructions. You've been assigned to create a rectangle class and initializer methods for whatever life might throw at it.
undefined
2
Task
Java Syntax, level 5, lesson 7
Locked
A programmer creates a person
Let's create a new person... Actually, that would be better done elsewhere. In our case, we'll just create a template for a person. That is, a Person class. We'll give it fields (name and age). To create new Person objects quickly and confidently, we'll write an initialize(String name, int age) method, where we will initialize these fields.
undefined
2
Task
Java Syntax, level 5, lesson 7
Locked
Initializing objects
We'll initialize objects however we want! In this task, someone previously created the Person class, but their implementation doesn't quite suit us. Something needs to be fixed: we need to rewrite the program so that one and only one initialize method initializes all the Person class's instance variables.
undefined
2
Task
Java Syntax, level 5, lesson 7
Locked
Code entry
Your attention, please! Now recruiting code entry personnel for CodeGym. So turn up your focus, let your fingers relax, read the code, and then... type it into the appropriate box. Code entry is far from a useless exercise, though it might seem so at first glance: it allows a beginner to get used to and remember syntax (modern IDEs seldom make this possible).
Comments (50)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Justin Smith Level 12, 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.
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?
luis_rmr Level 8, San Ramon, Costa Rica
10 October 2020
Can someone please remind me the difference between a constructor and an initializer?
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
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){ }
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 .
matt Level 11, Springfield, United States
6 January 2020
int color? that's weird isn't it
Nicholas Dragotta Level 7, Philadelphia, United States
6 January 2020
public class Learning { public static void main(String[] args) { System.out.println("I am having fun!"); } }