Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Project 02 Materials / C10_YourID_Exercise10

.docx
Скачиваний:
22
Добавлен:
14.10.2016
Размер:
1.25 Mб
Скачать

exercise 10: calculator constructors

Application Development

Name: <your name goes here>

Purpose: When a program value is a single simple value (e.g. a single integer), it is easy to create a primitive variable to hold the value. It’s also easy to allocate memory for the value, and easy to associate a name with the value. When a program needs more complex values, such as those a calculator needs, a primitive variable is not adequate. This exercise explores the various aspects of class constructors for the calculator project.

Task: Perform each of the following activities. If you have questions, issues or doubts, please ask for help and do not just guess!

For this exercise, assume the following three fields of the class (some people may refer to the fields of a class as “attributes”):

private double value; // The representation of the value

private double errorTerm; // Specifies the value’s error term

private String units; // Specifies the value’s units

1. What is the primary purpose of a constructor method?

2. Given a class, what are two ways you can distinguish a constructor methods from the other class methods?

3. If we are going to create a class named “CalculatorValue” to represent the complex values that calculators manipulate, write a default constructor (one with no parameters) that would initialize the two double fields of the class to zero and the String field to an empty string.

4. Parameters are used to pass values into a constructor in those cases where the default constructor is not adequate. These parameters are given a type and a name within the opening and closing parenthesis following the constructor’s name. How important is the name that is given to the parameter? Are there any restrictions on the type of value that can be a parameter?

5. It is not uncommon for a Class to have more than one constructor method, all with the same name. How does the computer know which constructor to use?

6. A “fully-parameterized” constructor provides a parameter for each and every field of the class. Please create fully-parameterized constructor for the CalculatorValue class.

7. A “copy constructor” is a form of constructor that produces a new instance of the Class with fields that have the same value as a template object passed in to the constructor as a parameter. A programmer would use a copy constructor to produce a new instance of a Class when passing all of the variables individually for initialization would be cumbersome and tedious. In this case it would be much easier to just pass a template object to the constructor as a parameter. Please create a copy constructor for the CalculatorValue class.

8. There are no restrictions on the kinds of things that can be done within a constructor. A constructor may employ conditional statements, loops, call methods, display error messages, etc. This makes it possible for a constructor to verify that the provided parameter values are consistent. For example, given that the error term should not be larger than the absolute value of a value, explain some options that a programmer might choose to employ, should the provided error term be larger than the absolute value of the provided value and why.

This is the end of the exercise. Save your work and submit this document to the LMS as you did for other Exercises. (Please be sure you have changed the name at the top of this document and properly renamed it to start with your StudentID.)

Congratulations! You have just completed the 10th exercise.

©iCarnegie - Distribution or copying without permission is prohibited.

Соседние файлы в папке Project 02 Materials