"I can see that you've already rested. Great! Here are some tasks to practice creating constructors:"
5
Task
New Java Syntax,
level 5,
lesson 9
Locked
Nerd break
Did you know that before the final power lift and transition to Level 5, you need to relax a bit and watch a video from the "Did You Know?" series. This isn't just entertainment. It's part of the comprehensive curriculum developed by the pedagogical board at the secret CodeGym center. The video is dedicated to the technologies and progress achieved by mankind.
estoy confundida. que es esto?
public Color color;
no es una variable porque no tiene el tipo.
no es un constructor porque le falta (){
Alguien me puede decir que es:
gracias...
public Color color es una variable. Es una variable del tipo Color. Es una variable de la clase Color.
Al igual que String color seria una variable del tipo String, Color color seria una variable del tipo Color.
I don't think the "Constructor" task is "Medium": it is more likely "Hard" or even "Insane" :-)
Btw. if you are, like me, puzzled with two classes within the same class, this is called "nested classes" and this topic is explained later in "Java Multithreading" course (level 20+). Or you can peep at this article:
https://codegym.cc/groups/posts/262-nested-inner-classes
Best exercise was calling a constructor with another constructor. So "this" is also a method that can be called. I feel like I just begin to understand the power of this...
El ejercicio «Constructor» me pareció algodifícil de ver. Debería ser de difícultad media o mayor. Muy aparte de eso, qué gran ejercicio; me hizo pensar bastante.
Yes Dave ... I had 5 including default (since we provide non-default we need to manually add back the default if we want max) and using Circle as parameter. Make sure to check if null != Circle param before using circle.attribute
Justin SmithLevel 41, Greenfield, USA, United States
6 July 2021
Found something that I would consider a bug in "Constructor". If you change the sole class variable from public to private (we were taught previously that they should be private and only accessed by getter and setter methods), it doesn't affect the ability of the program to run as intended, but it will fail validation for that reason alone.
Justin SmithLevel 41, Greenfield, USA, United States
6 July 2021
It may seem super-tedious to repeat the exact same tasks we did with initialize, but the point is to make some of these processes automatic for the learner.
I found the bug in this problem. But I have a basic question. Arent we to initialize the object with something like
this.color = color
public class Circle {
public Color color;
public static void main(String[] args) {
Circle circle = new Circle();
circle.color.setDescription("Red");
System.out.println(circle.color.getDescription());
}
public void Circle() {
color = new Color();
}
public class Color {
String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
}
I am not clear how this initializing the object with a new works inside a constructor. Can someone please help clarify ?
Could be done also the way you say, if the constructor of Circle class would take any parameter (in that case the parameter should be "Color" class).
The actual constructor of Circle class doesnt´take any Color parameter, but directly creates a new instance of Color and asigns it to color variable of Circle object, it is only left to "setDescription" of the color object.
I am surprised with the code in line 10;
public void Circle(){
color = new Color();
}
Is this a method or a constructor? Technically Java allows a method declaration with the same name as the class name, but this privilege is reserved to constructors. Constructors are declared with the same name as that of the class name, and they don't have a return type, not even void. So the above code must be treated as a method, not a constructor. This is a bad practice ie naming the methods with the class name and should be discouraged.
I have been using a different naming convention to help differentiate the parameters. Is this considered bad form and if so is there a better way to do it.
Thanks in advance.
EXAMPLE.
GO TO FULL VERSION