Warm-up Exercises
- Consider the following partial class definition:
public class Book
{
private string title;
private string author;
private string publisher;
private int copiesSold;
}- Write a statement that would create a
Bookobject. - Write a “getter” and a “setter” for the
titleattribute. - Write a constructor for the
Bookclass taking at least one argument.
Solution for Part 1
Book myBook = new Book();
Solution for Part 2
public string GetTitle()
{
return title;
}
public void SetTitle(titleP)
{
title = titleP;
}
Solution for Part 3
public Book(string titleP, string authorP)
{
title = titleP;
author = authorP;
}Questions
- How do you make reference to a
publicpropertyNameoutside of the class (for instance, in theMainmethod)?
- *Name
- +Name
- .Name
- None of these
- In C#, you should think of the class’s properties as the class’s attributes.
- Yes
- No
- The property notation allows the client to directly manipulate the
privateinstance variable.
- Yes
- No
- Consider the code:
public void SetName(string tempAccountName)
{
name = tempAccountName; // store the account name
}Which of the following statements is false? - [ ] The first line of
each method declaration is the method header. - [ ] The method’s
return type specifies the type of data the method returns to its caller
after performing its task. - [ ] The return type void indicates that
when SetName() completes its task, it does not return any information
to its calling method. - [x] All methods require at least one
parameter to provide data to perform tasks.
- A return type of _____ is specified for a method that does not return a value.
- int
- double
- void
- None of the above.
- Methods are called by writing the name of the method followed by _____ enclosed in parentheses.
- a condition
- argument(s)
- a counter
- None of the above.
- The parameter list in the method header and the arguments in the method call must agree in:
- Number
- Type
- Order
- All of the above
- Suppose
method1is declared as
public void method1(int a, float b, string c)Which of the following methods does not overload method1? - [x]
void method2(int a, float b, char c) - [ ]
int method1(float a, int b, string c) - [ ]
float method1(int a, float b) - [ ]
string method1(string a, float b, int c)
- Write a get method for an instance variable named
totalof typeint.
Solution
public int GetTotal()
{
return total;
}- Write a getter for an attribute of type
stringnamedmyName.
Solution
public string GetMyName()
{
return myName;
}- Write a setter for an attribute of type
intnamedmyAge.
Solution
public void SetMyAge(int age)
{
myAge = age;
}- Assuming
nameis astringinstance variable, there is a problem with the following setter. What is the problem, and how would one fix it?
public int SetName1(string var){
name = var;
}
Solution
The keyword var is being used as an identifier.
public int SetName1(string nameVar)
{
name = nameVar;
}- Is it possible to have more than one constructor defined for a class? If yes, how can C# know which one is called?
Solution
Yes, C# can identify which constructor is called based on that constructor’s method signature, that is, the combination of parameters associated with it.
- What is the name of a constructor method? What is the return type of a constructor?
Solution
The name of a constructor method is the name of the class that contains it, and a constructor’s return type is the class that contains it.
- Write a constructor for a
Sodaclass with onestringattribute calledname.
Solution
public Soda(string nameP)
{
name = nameP;
}- What is the “default” constructor? Do we always have the possibility of using it?
Solution
The default constructor is one without any parameters. The only case in which it may not be called is if it has not been explicitly defined while other constructors have been defined.
- Why would one want to define a constructor for a class?
Solution
By defining a constructor for a class, one can specify which values to assign to the instance variables upon instantiation.