Question: Problem 3: Static vs. non-static 14 points total; individual-only When designing a blueprint class, we have seen that we can include both static and non-static

Problem 3: Static vs. non-static

14 points total; individual-only

When designing a blueprint class, we have seen that we can include both static and non-static variables. Static variables (also known as class variables) belong to the class as a whole. Non-static variables (also known as instance variables or fields) belong to individual objects of the class; each object gets its own copy of those variables.

We have also seen that a blueprint class can include both static and non-static methods. A non-static method (also known as an instance method) is required if the method must have access to the fields of a particular called object. However, if a method doesnt need a called object i.e., if all of the information that it needs is supplied by its parameters or by the static variables of the class then we typically make it static. In the same way that static variables are also known as class variables, static methods are sometimes referred to as class methods.

Whether a method is static or non-static has an impact on how we call it. Non-static methods must be called on an object of the class (e.g., r1.area(), where r1 is a reference to a Rectangle object). Static methods may be called on an object of the class, but it is better style to call them using the name of the class instead.

In Part II, you will be implementing a Card class that serves as a blueprint for objects that encapsulate the rank and suit of a given playing card. Each Card will have two fields:

- an integer field called rank that stores the rank of the card; for example, in a Card object that represents the 10 of Diamonds, this field would have the value 10

- a field called suit that stores a single character representing the suit of the card; for example, in a Card object that represents the 10 of Diamonds, this field would have the value 'D'.

In the rest of this problem, you will be thinking through some possible changes to the Card class. Note that these changes are hypothetical; you wont actually be making these changes when you implement the class in Part II!

1. (5 points) Imagine that after creating the version of the Card class that is described in Part II, we decide that:

- We want to keep track of how many Card objects have been created for each of the four suits (Clubs, Diamonds, Hearts, and Spades).

- In addition to its rank and suit, we want each Card object to store its value (an integer like 4 or 11). Note that the value of a Card may not be the same as its rank. For example, a Queen has a rank of 12, but its value in a game like Blackjack is 10.

What variables (static and/or non-static) would we need to add to the Card class as part of our implementation of these changes?

In section 2-1 of ps3_partI, we have included a table that you should complete to describe the necessary variables. For each of your proposed variables, you should specify:

- its type and name (make it descriptive!)

- whether it will be static or non-static

- a brief description of its purpose, and why it needs to be static or non-static.

As an example, we have filled in the first row of the table to describe the field called rank that you will be defining in Part II. You should add the descriptions of your proposed new variables. You may not need all of the rows in the table.

2. (6 points) The version of the Card class that you will implement in Part II will not have any mutator methods, so it wont be possible to change the suit of an existing Card object. Imagine that we want to add a mutator method called setSuit that takes in a char and uses it to change the suit of the called Card object. For example, you might change the suit of a Card from 'D' (for Diamonds) to 'C' (for Clubs).

a. What type of method should setSuit be static or non-static? Explain briefly.

b. Assume we have a Card object whose current suit is 'H' (i.e., it has the suit character that corresponds to Hearts). We decide to change the suit of the Card to 'S' (for Spades) by making the appropriate call to our new setSuitmethod. During that method call, what changes will the method need to make to the values of the variables in the class? You should include (1) any changes to the fields that were already present in the original version of the class (see above), and (2) any changes to the variables that you proposed above. Be as specific as possible.

c. Give an example of how you would call this method from outside the Card class. If you need a Card object to call the method, assume that the variable c represents that object, and that the object has already been created. However, you should only use c if doing so is absolutely necessary.

3. (3 points) Finally, lets say that we want to add another method callednumFaceCards. It takes a parameter that represents an array of Card objects, and it computes and returns the number of face cards in that array.

a.What type of method should numFaceCards be static or non-static? Explain briefly.

b.Give an example of how you would call this method from outside the Cardclass to determine the number of face cards in an array of Card objects called myCards. If you need a Card object to call the method, assume that the variable c represents that object, and that the object has already been created. However, you should only use c if doing so is absolutely necessary.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!