MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_NextPart_01C46F40.95BDF1E0" This document is a Single File Web Page, also known as a Web Archive file. If you are seeing this message, your browser or editor doesn't support Web Archive files. Please download a browser that supports Web Archive, such as Microsoft Internet Explorer. ------=_NextPart_01C46F40.95BDF1E0 Content-Location: file:///C:/22475234/FinalS04.htm Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="us-ascii"
Problem 1 (35 points): Short Answer
1. (4 points) Write the boolean expressions that represent the following conditions. Assume that the variables have been declared and initialized appropriately. Be careful to write out the expres= sions as you would type them in C++.
(= a) n is not between 5 and 15 inclusi= ve
__________________________________=
_______
(=
b) Either x and y are both divi=
sible
by 3 or both divisible by 5 (remember, a % b gives the remainder when a is divided by=
b)
__________________________________=
_______
2. (6
points) The following expressions are intended to convert a distance measur=
ed
in light-years and stored in an int
variable called =
lyr
to the equivalent distance in parsecs stored in an int variable called par. The
expression should round to the nearest light-year. There are 3.25 (or
) light-years in a parsec. For each of the following expressi=
ons,
write the letter of the correct response in the blank provided:
a) = Does not produce the correct result
b) = Produces the correct result
i.&n=
bsp;
par
=3D lyr / 3.25 + 0.5; =
//______=
______________
ii.&=
nbsp;
par
=3D (int) (lyr / 13 * 4 + 1 / 2); =
span> //___=
_________________
iii.=
par
=3D (int) ((double)lyr * 4 / 13 + 0.5); //____________________
(2
points) What happens when you open a write file with the parameter ios::binary,
=
4. (5 points) twoD is a 2-dimensional array of size
= 5. (3 points) Find the error in eac= h of the following segments. Assum= e the following declarations and statements:
int *zPtr; // zPtr will reference=
array
z
void *sPtr =3D 0;
int number;
int z[5] =3D {1 , 2, 3, 4, 5};
sPtr =3D z;&nbs=
p;
zPtr =3D z;
= a) number =3D zPtr; // Use pointe= r to get first value of array
_______________________________= ___________________________________
= b) number =3D *zPtr[2]; // Assign array= element 2 to number
_______________________________= ___________________________________
= c) number =3D *sPtr; // Assign th= e value pointed to be sPtr to number
_______________________________= ___________________________________
6.
(5 points) Write a method
______________________=
___________________________________________________________________________=
___________________________________________________________________________=
___________________________________________________________________________=
_____
7. (10 points) Consider the following method, which is valid despite being poorly formatted:
public
void foo(char ch)
{
if (ch !=3D ‘l’)=
if (ch >=3D ‘a̵=
7;
&& ch <=3D ‘z’)
if (ch !=3D ‘r’)=
cout << “dogR=
21;
<< endl;
else
cout << “catR=
21;
<< endl;
else
cout << “mouse=
8221;
<< endl;
}
(a) Rewrite the above method using proper indentation.
__________________________________=
___________________________________________________________________________=
___________________________________________________________________________=
___________________________________________________________________________=
___________________________________________________________________________=
___________________________________________________________________________=
_____________________________________________________
(b)&= nbsp; Give a value of ch for which foo displays cat. = &nb= sp; //____________
(c)&= nbsp; Give a value of ch for which foo displays dog. //____________
(d)&= nbsp; Give a value of ch for which foo displays mouse. = //____________
(e)&=
nbsp;
Give a value of ch for which foo does not display anything. //____________
Problem 2 (15 points): Complete the following program. It will first open a file called “physics.dat”. The first line of the file contains the initial velocity (v0) and in= itial position (x0) of an object, separated by a space. The object’s velocity (v) and position (x) were measured at regular intervals and recorded on subsequent lines as shown:
0.0004 1
0.00089 2
0.0019 2.5
…
Use the formula below to calculate the acceleration fo= r each measurement.

Finally, the program writes a file “newPhys.dat&= #8221; which includes the acceleration as a third piece of data on each line as sh= own:
0.0004 1
0.00089 2 1.0535e-07= p>
0.0019 2.5 3.28571e-07=
…
#include =
// Necessary header file
#include <=
;cmath>
using std::e=
ndl;
int main() {=
// Declare variables
=
a,
v, v0, x, x0; // Store =
data
=
&nb=
sp;
=
&nb=
sp;
// Open the read file
=
&nb=
sp;
=
&nb=
sp;
// Open the write file
// Get the initial data
// Continue Next Page
// Calculates
acceleration for each line of data
while ( =
=
)
{
// Get the data
// Calculate the accel=
eration
&n= bsp; // Output the result <= o:p>
}
// Close the files
}
Problem 3 (20 points): For this problem, you are given the files Card.h, Card.cpp, and main.cpp. Card.h is given below and declares the class Card. Add the following methods to the h= eader file:
Modify the Card.cpp file = on the next page to define the method isOneEyedJack and the method c= ontainsSuit. Assume that the constructor, const= ants, and other methods are also defined in the .cpp file. You may use them when writing your code. In main you will add a test case to m= ore completely test the method containsSuit.
//
file: Card.h
#include
<string>
using
std::string;
//
Creates cards and provides a few method for the cards
#ifndef
CARD_H
#define
CARD_H
class
Card {
private:
// Data Fields
int rank;
// Numeric value of the card
int suit;
// Suit of the card
public:
// Constants for names of ranks -
example use Card::JACK
const static int JACK; =
// value 11
const static int QUEEN; =
// value 12
const static int KING; =
// value 13
const static int ACE; =
// value 14
// Constants for suits
const static int CLUBS; =
// value 0
const static int DIAMONDS; =
// value 1
const static int HEARTS; =
// value 2
const static int SPADES; =
// value 3
// Creates a card
// @param r - rank of the card; @p=
aram s
- suit of the card
Card(int r, int s);
// Returns true exactly when the c=
ard
the method is invoked on is the
// the Jack of Spades or the Jack =
of
Hearts – a one-eyed Jack
// Returns true iff the hand (stor=
ed in
an array) contains a card of
// the same suit as the card the m=
ethod
is invoked on.
// Returns the value of the rank
int getRank();
// Returns the value of the suit
int getSuit();
// Returns the name of the rank of=
the
card: 2, 3, …, King, Ace
string getRankName();
// Returns the name of the suit of=
the
card: Clubs, …, Spades
string getSuitName();
};
//
file: Card.cpp
#include
“Card.h” =
&nb=
sp;
// Necessary file
// Returns
true exactly when the card the method is invoked on is the
//
the Jack of Spades or the Jack of Hearts – a one-eyed Jack
__________________isOneEyedJack()
{
}
//
Returns true iff the hand (stored in an array) contains a card of
//
the same suit as the card the method is invoked on. _____________________<=
/span>containsSuit
{
}
//
file: main.cpp
// Tests
the methods in the card class to make sure the code works
//
correctly. Add another test o=
f the
containsSuit method to more
//
completely test it. For full
credit, use constants.
#include
<iostream>
using
std::cout;
using
std::endl;
#include =
&nb=
sp;
//
Necessary header file
int
main()
{
Card jackS(Card::JACK,
Card::SPADES);
Card queenS(Card::QUEEN,
Card::SPADES);
Card hand[] =3D {Card(2,
Card::HEARTS), Card(5, Card::HEARTS),
=
Card(7, Card::CLUBS), Card(10, Card::DIAMONDS),
=
queenS};
cout <<
jackS.isOneEyedJack() << endl;
cout << queenS.isOneEy=
edJack()
<< endl;
cout << jackS.contains=
Suit(hand,
5) << endl;
// Add your test here
return 0;
}
Problem 4 (15 points): What is the output of the following program?
#include
<iostream>
using
std::cout; using std::endl;
#include
“Card.h”
const
int ASIZE =3D 5;
void
mystery(Card[], int);
int
main() {
Card hand[] =3D {Card(2, Card::HEA=
RTS),
Card(10, Card::HEARTS),
=
Card(7, Card::CLUBS), Card(10, Card::SPADES),
=
Card(6, Card::CLUBS)};
mystery(hand, ASIZE);
for (int i =3D 0; i < ASIZE; i+=
+) {
cout <<
hand[i].getRankName() << " of "
=
<< hand[i].getSuitName() << ", ";
}
cout << endl;
}
void
mystery(Card hand[], int size) {
if (size !=3D 0) {
Card c =3D hand[=
0];
for (int i =3D 1; i <=
; size;
i++) {
if(c.getRank() < hand[i].getRank())
=
c =3D hand[i];
else
if (c.getRank() =3D=3D hand[i].getRank() &&
=
c.getSuit() < hand[i].getSuit())
=
c =3D hand[i];
}
int j =3D 0;
for (int k =3D 0=
; k <
size-1; k++) {
if
(c.getRank() =3D=3D hand[j].getRank() &&
=
c.getSuit() =3D=3D hand[j].getSuit())
=
j++;
hand[k] =3D hand[j];
j++; =
}
cout << si=
ze
<< ": ";
for (int i =3D 0=
; i <
size-1; i++) {
cout
<< hand[i].getRankName() << " of " =
<< hand[i].getSuitName() << ", "; } cout << en=
dl; mystery(hand, si=
ze-1); hand[size-1] =3D=
c; } } ________________________________________________=
___________________________________________________________________________=
___________________________________________________________________________=
___________________________________________________________________________=
___________________________________________________________________________=
___________________________________________________________________________=
___________________________________________________________________________=
___________________________________________________________________________=
___________________________________________ Problem 5 (15=
points):
What is the output of this program?
(Enter your answers at the end of this problem.) #include
<iostream> using
std::cout; using
std::endl; class
Circle { private: int centerX; int centerY; int radius; public: // Constructors Circle(); Circle(int x, int y, int r); // Mutator void setRadius(int r); // Accessors int getRadius(); int getDiameter(); }; //
Constructor Circle::Circle() { &n=
bsp;
centerX =3D 0; centerY =3D 0; radius =3D 1; } //
Constructor Circle::Circle(int
x, int y, int r) { centerX =3D x; centerY =3D y; radius =3D r; } //
Sets the radius void
Circle::setRadius(int r) { radius =3D r; } //
Returns the radius int
Circle::getRadius() { return radius; } //Returns
the diameters int
Circle::getDiameter() { return 2 * radius; } int
main() { int x =3D 3; int y =3D 6; int r =3D 2; Circle cir1; Circle cir2(x, y, r); cout << "a) "=
; <<
cir1.getDiameter() << endl; cout << "b) "=
; <<
cir2.getDiameter() << endl; y =3D 5; Circle cir3(r, x, y); cout << "c) "=
; <<
cir1.getRadius() << endl; cout << "d) "
<< cir3.getDiameter() << endl; cir2.setRadius(4); cout << "e) "=
; <<
cir2.getDiameter() << endl; cir1.setRadius(cir3.getDiame=
ter()); cout << "f) "
<< cir1.getDiameter() << endl; Circle *copy =3D &cir1;<=
o:p> (*copy).setRadius(5); cout << "g) "=
; <<
cir1.getRadius() << endl; cout << "h) "=
; <<
cir3.getRadius() << endl; cir3 =3D cir1; cir3.setRadius(9); cout << "i) "=
; <<
cir1.getDiameter() << endl; cout << "j) "
<< cir3.getDiameter() << endl; copy =3D &cir2; cout << "k) "
<< cir1.getDiameter() << endl; cout << "l) "
<< cir2.getDiameter() << endl; cout << "m) "
<< cir3.getDiameter() << endl;; cout << "n) "=
; <<
(*copy).getDiameter() << endl; } Output:
a)
b)
c)
d)
e)
f)
g)
h)
i)
j)
k)
l)
m)
n)
Spring 2004 = &nb= sp; = &nb= sp; Name: ________________________________
CS630: Fina= l
Spring 2004 = = &nb= sp; = Name: ___________________________
CS630: Fina= l Exam