Solutions Manual for
Data Structures and
Algorithms in Java, 6e
Michael Goodrich,
Roberto Tamassia (All
Chapters)
, Chapter
1 Java Primer
8
Hints and Solutions
8 8
Reinforcement
R-
1.1)8 Hint8Use8 the8 code8 templates8 provided8 in8 the8 Simple8 Input8 and8O
utput8section.
R-1.2)8Hint8You8may8read8about8cloning8in8Section83.6.
R-
1.2)8Solution8Since,8after8the8clone,8A[4]8and8B[4]8are8both8pointing8to8t
he8same8GameEntry8object,8B[4].score8is8now8550.
R-1.3)8Hint8The8modulus8operator8could8be8useful8here.
R-1.3)8Solution
public8boolean8isMultiple(long8n,8long8m)8{
return8(n%m8==80);
}
R-1.4)8Hint8Use8bit8operations.
R-1.4)8Solution
public8boolean8isEven(int8i)8{
return8 (i8 &8 18 ==8 0);
}
R-
1.5)8Hint8The8easy8solution8uses8a8loop,8but8there8is8also8a8formula8for8t
his,8which8is8discussed8in8Chapter84.
R-1.5)8Solution
public8int8sumToN(int8n)8{
int8 total8=8 0;
for8(int8j=1;8j8<=8n;8j++)8tot
al8+=8j;
return8 total;
}
,2 Chapter81.8 Java8Primer
R-1.6)8Hint8The8easy8thing8to8do8is8to8write8a8loop.
R-1.6)8Solution
public8int8sumOdd(int8n)8{
int8 total8=8 0;
for8(int8j=1;8j8<=8n;8j8+=82)8
total8+=8j;
return8 total;
}
R-1.7)8Hint8The8easy8thing8to8do8is8to8write8a8loop.
R-1.7)8Solution
public8int8sumSquares(int8n)8{
int8 total8=8 0;
for8(int8j=1;8j8<=8n;8j++)8tot
al8+=8j∗j;
return8 total;
}
R-1.8)8Hint8You8might8use8a8switch8statement.
R-1.8)8Solution
public8int8numVowels(String8text)8{
int8 total8=8 0;
for8(int8j=0;8j8<8text.length();8j++)8{
switch8(text.charAt(j))8{
case8'a':
case8'A':
case8'e':
case8'E':
case8'i':
case8'I':
case8'o':
case8'O':
case8'u':
case8'U':8tot
al8+=81;
}
}
return8 total;
}
R-1.9)8Hint8Consider8each8character8one8at8a8time.
, 3
R-
1.10)8Hint8Consider8using8get8and8set8methods8for8accessing8and8mod-
8ifying8the8values.
R-
1.11)8 Hint8The8traditional8 way8 to8 do8 this8 is8 to8use8 setFoo8 methods,8w
here8Foo8is8the8value8to8be8modified.
R-1.11)8Solution
public8void8setLimit(int8lim)8{
limit8=8lim;
}
R-1.12)8Hint8Use8a8conditional8statement.
R-1.12)8Solution
public8void8makePayment(double8amount)8{
if8(amount8>80)8balanc
e8− =8amount;
}
R-1.13)8Hint8Try8to8make8wallet[1]8go8over8its8limit.
R-1.13)8Solution
for8(int8val=1;8val8<=858;8val++)8{8wallet[0
].charge(3∗val);8wallet[1].charge(2∗val);8
wallet[2].charge(val);
}
This8change8will8cause8wallet[1]8to8attempt8to8go8over8its8limit.
Creativity
C-1.14)8Hint8The8Java8method8does8not8need8to8be8passed8the8value8of8n
as8an8argument.
C-1.15)8Hint8Note8that8the8Java8program8has8a8lot8more8syntax8require-
8ments.
C-
1.16)8Hint8Create8an8enum8type8of8all8operators,8including8=,8and8use8a
n8array8of8these8types8in8a8switch8statement8nested8inside8for-
loops8to8try8all8possibilities.
C-
1.17)8Hint8Note8that8at8least8one8of8the8numbers8in8the8pair8must8be8eve
n.
C-1.17)8Solution
Data Structures and
Algorithms in Java, 6e
Michael Goodrich,
Roberto Tamassia (All
Chapters)
, Chapter
1 Java Primer
8
Hints and Solutions
8 8
Reinforcement
R-
1.1)8 Hint8Use8 the8 code8 templates8 provided8 in8 the8 Simple8 Input8 and8O
utput8section.
R-1.2)8Hint8You8may8read8about8cloning8in8Section83.6.
R-
1.2)8Solution8Since,8after8the8clone,8A[4]8and8B[4]8are8both8pointing8to8t
he8same8GameEntry8object,8B[4].score8is8now8550.
R-1.3)8Hint8The8modulus8operator8could8be8useful8here.
R-1.3)8Solution
public8boolean8isMultiple(long8n,8long8m)8{
return8(n%m8==80);
}
R-1.4)8Hint8Use8bit8operations.
R-1.4)8Solution
public8boolean8isEven(int8i)8{
return8 (i8 &8 18 ==8 0);
}
R-
1.5)8Hint8The8easy8solution8uses8a8loop,8but8there8is8also8a8formula8for8t
his,8which8is8discussed8in8Chapter84.
R-1.5)8Solution
public8int8sumToN(int8n)8{
int8 total8=8 0;
for8(int8j=1;8j8<=8n;8j++)8tot
al8+=8j;
return8 total;
}
,2 Chapter81.8 Java8Primer
R-1.6)8Hint8The8easy8thing8to8do8is8to8write8a8loop.
R-1.6)8Solution
public8int8sumOdd(int8n)8{
int8 total8=8 0;
for8(int8j=1;8j8<=8n;8j8+=82)8
total8+=8j;
return8 total;
}
R-1.7)8Hint8The8easy8thing8to8do8is8to8write8a8loop.
R-1.7)8Solution
public8int8sumSquares(int8n)8{
int8 total8=8 0;
for8(int8j=1;8j8<=8n;8j++)8tot
al8+=8j∗j;
return8 total;
}
R-1.8)8Hint8You8might8use8a8switch8statement.
R-1.8)8Solution
public8int8numVowels(String8text)8{
int8 total8=8 0;
for8(int8j=0;8j8<8text.length();8j++)8{
switch8(text.charAt(j))8{
case8'a':
case8'A':
case8'e':
case8'E':
case8'i':
case8'I':
case8'o':
case8'O':
case8'u':
case8'U':8tot
al8+=81;
}
}
return8 total;
}
R-1.9)8Hint8Consider8each8character8one8at8a8time.
, 3
R-
1.10)8Hint8Consider8using8get8and8set8methods8for8accessing8and8mod-
8ifying8the8values.
R-
1.11)8 Hint8The8traditional8 way8 to8 do8 this8 is8 to8use8 setFoo8 methods,8w
here8Foo8is8the8value8to8be8modified.
R-1.11)8Solution
public8void8setLimit(int8lim)8{
limit8=8lim;
}
R-1.12)8Hint8Use8a8conditional8statement.
R-1.12)8Solution
public8void8makePayment(double8amount)8{
if8(amount8>80)8balanc
e8− =8amount;
}
R-1.13)8Hint8Try8to8make8wallet[1]8go8over8its8limit.
R-1.13)8Solution
for8(int8val=1;8val8<=858;8val++)8{8wallet[0
].charge(3∗val);8wallet[1].charge(2∗val);8
wallet[2].charge(val);
}
This8change8will8cause8wallet[1]8to8attempt8to8go8over8its8limit.
Creativity
C-1.14)8Hint8The8Java8method8does8not8need8to8be8passed8the8value8of8n
as8an8argument.
C-1.15)8Hint8Note8that8the8Java8program8has8a8lot8more8syntax8require-
8ments.
C-
1.16)8Hint8Create8an8enum8type8of8all8operators,8including8=,8and8use8a
n8array8of8these8types8in8a8switch8statement8nested8inside8for-
loops8to8try8all8possibilities.
C-
1.17)8Hint8Note8that8at8least8one8of8the8numbers8in8the8pair8must8be8eve
n.
C-1.17)8Solution