Homework #3

CSCI 1301 - Principles of Computer Programming I – Fall 2020

Exam #1 will consist of questions taken or inspired from this homework, the two previous homework, and

from the labs:

• For Section A (Tuesday), you should study the lab up to Week #6 (included), and this homework up to

question 16 (included), as well as the problem (which is difﬁcult),

• For Section B (Thursday), it is enough to stop at the lab of Week #5 (even if having a look at Week #6 will not
hurt), and to stop at question 13 (included) in this homework, as well as the problem (which is difﬁcult).

Part I – Questions

1. What is “an instance of the class”?

2. Fill in the blanks:

“A class asserts that every objects created using it should have

(i.e., ‘data’) and

(i.e., ‘operations’).”

3. Give two access modiﬁers.

4. What, if any, is the difference between a parameter and an argument?

5. Write a statement that creates a new object from the Rectangle class.

6. What is the purpose of the keyword new?

7. Do different objects from the same class share their instance variable?

8. Suppose we have a Circle class containing

public void SetRadius(double RadiusArgument)
{

radius = RadiusArgument;

}

}

Write a statement that create a Circle object, and one statement that sets its radius to 3.5.

9. What does the keyword return do?

10. Write a get method for a total instance variable of type int.

11. Write a getter for an attribute of type string named myName.

12. Write a setter for an attribute of type int named myAge.

13. Assuming name is a string instance variable, there is problem with the following setter. Fix it.

public int SetNamel(string var){

name = var;

September 12, 2020

spots.augusta.edu/caubert/pcp/

Page 1 of 3

Homework #3

CSCI 1301 - Principles of Computer Programming I – Fall 2020

14. Draw the UML diagram of a class named “Student” with a single attribute, “name”, of type string, and two
methods, SetName and GetName.

15. Brieﬂy describe what a format speciﬁer is. Write a statement that uses one.

16. Consider the following UML diagram:

Circle

- radius : ﬂoat
+ setRadius(radiusParam : ﬂoat) : void
+ getRadius(): ﬂoat
+ getArea(): ﬂoat

What is the name of the class, what are the methods and attributes of the class?

17. 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 Main method?

18. Is it possible to have more than one constructor deﬁned for a class? If yes, how can C# know which one is
called?

19. What is the name of a constructor method? What is the return type of a constructor?

20. Write a constructor for a Soda class with one string attribute called name.

21. What is called the “default” constructor? Do we always have the possibility of using it?

22. Consider the following partial class deﬁnition:

public class Book
{

private string title;
private string author;
private string publisher;
private int copiesSold;

1

2

3

4

5

6

7

}

1. Write a statement that would create a Book object.

2. Write a “getter” and a “setter” for the title attribute.

3. Write a constructor for the Book class taking at least one argument (you’re free to decide which one(s)).

23. Why would one want to deﬁne a constructor for a class?

A

Part II – Problems

There is only one problem this time, and it is harder than what you’ll be asked to do during the exam. Being able
to solve it is an excellent sign that you are ready.

September 12, 2020

spots.augusta.edu/caubert/pcp/

Page 2 of 3

Homework #3

CSCI 1301 - Principles of Computer Programming I – Fall 2020

Problem 1
You are going to design a class named Triangle. A triangle has three angles, but knowing the value of only two
angles is sufﬁcient to determine the value of the third, since they always add up to 180˝. Hence, it is sufﬁcient to
have only two double attributes, angle1 and angle2. We want to deﬁne several methods:

• a no-arg constructor that sets the value of angle1 to 60.0 and the value of angle2 to 60.0,

• another constructor, that takes two arguments, and assigns to angle1 the value of the ﬁrst argument, and

assigns to angle2 the value of the second argument,

• getters for angle1 and angle2,

• a method that computes and returns the value of the third angle, that we name ComputeAngle3,

• a method that rotate the triangle: the value of the ﬁrst angle should become the value of the second angle,

and the value of the second angle should become the value of the third angle.

1. Write the UML diagram for the Triangle class.

2. Write the full, compilable implementation of the Triangle class.

A

September 12, 2020

spots.augusta.edu/caubert/pcp/

Page 3 of 3

