-1

There is defined class Saturn. Define class SolarSystem which contain as a private field an object from Saturn class. In class SolarSystem: define constructor which could initialize private field which will be an object from Saturn class.

public class Saturn    
{
    private int masa;
    public Saturn() { masa = 0; }
}

I don't know how to solve this task or maybe it is incorrectly defined. Could someone give me some clu or better: an code, please?

krzyhub
  • 6,285
  • 11
  • 42
  • 68

3 Answers3

2

You currently have a class called Saturn, which has a private field of type int and a public constructor. The task is asking you to define a new class, called SolarSystem, which contains a private field of type Saturn and a public constructor. Within that constructor, the private field of type Saturn should be initialized to a new object.

So your SolarSystem class will look very similar to your Saturn class in structure. Same number of lines of code, same layout, same everything. The only difference will be the type of the private field and the line which initializes that field to a value.

David
  • 208,112
  • 36
  • 198
  • 279
0
public class Saturn
{
    private int masa;
    public Saturn() { masa = 0; }
}

public class SolarSystem {
    private Saturn saturn;

    public SolarSystem(Saturn saturn)
    {
        this.saturn = saturn;
    }
}
Oscar
  • 13,594
  • 8
  • 47
  • 75
  • Answers which are nothing more than code are generally frowned upon without any explanation of the code or what it means. This is especially true of homework questions. – David May 29 '13 at 15:05
  • But after all it seems to be what I was looking for so thank you. – krzyhub May 29 '13 at 15:20
  • 1
    @Cris: One thing to note in Oscar's implementation is how his constructor accepts an instance of a `Saturn` object as a parameter instead of instantiating it manually in the constructor. While your task didn't specifically call for this, it is worth understanding and investigating the impact of this design decision. This is a step toward the Dependency Inversion Principle in object oriented design, which indicates that objects should require that dependencies be provided for them instead of internally creating those dependencies. In short, this is a good design. – David May 29 '13 at 15:25
  • I don't know much about object oriented programming. Could you give me proper example of code solution for my task so I could see difference, please? – krzyhub May 29 '13 at 15:28
  • @Cris: My answer describes a literal interpretation of the task, whereby the constructor for `SolarSystem` would be parameterless and would internally instantiate a new `Saturn` object for the private property. The `Saturn` class in your example is itself an example of this behavior. Rather than require an `int` it internally creates one. For primitive types such as `int` this isn't often a bad thing. (In fact, it isn't even necessary in `Saturn` because the default value for an otherwise un-initialized `int` is `0`.) As types becomes more complex, a pattern emerges to require dependencies. – David May 29 '13 at 15:47
-1

You can define a property for your private field saturn :

public class Saturn    
{
    private int m_masa;
    public int masa
    {
          get { return m_masa ; }
          set { m_masa = value ;}
    }
    public Saturn() { masa = 0; }
}

value is a keyword in c# so if you type :

masa = 5;

it's the same thing as m_masa = 5; but as your property is public you can call it from where you want.

user2417992
  • 204
  • 1
  • 6
  • 16