Warm-up Exercises
Questions
-
Explain what a constructor is and what it does.
-
Consider a fragment of longer C# code, see below.
public void SetNumber(int tempNumber)
{ name = tempNumber; } // store the account name- Which of the following statements is false?
- the method returns no value
-
nameis a local instance variable - the parameter
tempNumberis ofstringtype - the method can be used outside of the class it is defined in
- C# is an object oriented language that has roots in:
- C
- C++
- Java
- All above.
- Fill in the blanks: “A class asserts that every object created using it should have _________ (i.e., ‘data’) and _________ (i.e., ‘operations’).”
- attributes, methods
- methods, attributes
- methods, properties
- properties, attributes
-
Give two examples of access modifiers.
-
What is the purpose of the keyword
new? -
What does the keyword
returndo? -
What does it mean to say that instance variables have a default initial value? How is that different from the variables we have been manipulating in the
Mainmethod?
Problems
- You are going to design a class named
Triangle. A triangle only has three angles, but knowing the value of only two angles is sufficient to determine the value of the third, since they always add up to 180°. Hence, it is sufficient to have only twodoubleattributes,angle1andangle2. We want to define several methods:
- a no-arg constructor that sets the value of
angle1to 60.0 and the value ofangle2to 60.0, - another constructor that takes two arguments, and assigns to
angle1the value of the first argument, and toangle2the value of the second argument, - getters for
angle1andangle2, - a method that computes and returns the value of the third angle, that
we name
ComputeAngle3, - a method that rotates the triangle: the value of the first angle should become the value of the second angle, and the value of the second angle should become the value of the third angle.
- Write the UML diagram for the
Triangleclass. - Write the full, compilable implementation of the
Triangleclass.