Exercise 4.
private ArrayList<Book> library;
Exercise 4.3
10
Exercise 4.4
[Link](4);
Exercise 4.5
14
Exercise 4.6
[Link](meeting);
Exercise 4.7
[Link](2);
Exercise 4.8
Exercise 4.9
public void removeNote(int noteNumber) {
if(noteNumber < 0) {
// This is not a valid note number, so do nothing.
}
else if(noteNumber < numberOfNotes()) {
// This is a valid note number, so we can remove it.
[Link](noteNumber);
}
else {
// This is not a valid note number, so do nothing.
}
}
Exercise 4.10
public void listAllNotes()
Exercise 4.11
No. We have no idea how many lines we would need.
Exercise 4.15
public void showNote(int noteNumber)
{
if(noteNumber < 0) {
[Link]("This is not a valid note number");
}
else if(noteNumber <= numberOfNotes()) {
// This is a valid note number, so we can print it.
[Link]([Link](noteNumber));
}
else {
[Link]("This is not a valid note number");
}
}
Exercise 4.16
public void test() { int i = 10;
while(i <= 95) {
[Link](i);
i = i + 5;
}}
Exercise 4.17
public void sum(int a, int b) {
int number = a + 1;
int sum = 0;
while(number < b) {
sum = sum + number;
number = number + 1;
}
[Link](sum);
}
Exercise 4.18
public boolean isPrime(int n) {
int divisor = 2;
while(divisor < n ) {
if(n % divisor != 0) {
return false;
}
divisor = divisor + 1;
}
return true;
}
Exercise 4.19
public boolean search(String searchString) {
int index = 0;
boolean found = false;
while(index < [Link]() && !found) {
String note = [Link](index);
if([Link](searchString)) {
found = true;
}
else {
index++;
}
}
if(found) {
[Link]("Found search term in note: " +
[Link](index));
} else {
[Link]("Search term not found.");
}
}
Exercise 4.20
public void listNotes()
{
int index = 0;
for(String note : notes) {
[Link](index + ": " + note);
index++;
}
}
Exercise 4.21
The value does not vary.
public boolean search(String searchString) {
int index = 0;
boolean found = false; int size = [Link]();
while(index < size && !found) {
String note = [Link](index);
if([Link](searchString)) {
found = true;
}
else {
index++;
}
}
if(found) {
[Link]("Found search term in note: " +
[Link](index));
} else {
[Link]("Search term not found.");
}
}
Exercise 4.22
/**
* Show a note.
* @param noteNumber The number of the note to be shown.
*/
public void showNote(int noteNumber)
{
if(noteNumber < 1) {
// This is not a valid note number, so do nothing.
}
else if(noteNumber <= numberOfNotes()) {
// This is a valid note number, so we can print it.
[Link]([Link](noteNumber - 1));
}
else {
// This is not a valid note number, so do nothing.
}
}
/**
* List all notes in the notebook.
*/ public void listNotes()
{
int index = 0;
for(String note : notes) {
[Link]((index + 1) + ": " + note);
index++;
}
} /**
* Remove a note from the notebook if it exists.
* @param noteNumber The number of the note to be removed.
*/
public void removeNote(int noteNumber)
{
if(noteNumber < 1) {
// This is not a valid note number, so do nothing.
}
else if(noteNumber < numberOfNotes()) {
// This is a valid note number.
[Link](noteNumber-1);
}
else {
// This is not a valid note number, so do nothing.
}
}
Exercise 4.23-4.25
import [Link];
/**
* Store details of club memberships.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Club
{
ArrayList<Membership> members;
/**
* Constructor for objects of class Club
*/
public Club()
{
members = new ArrayList<Membership>();
}
/**
* Add a new member to the club's list of members.
* @param member The member object to be added.
*/
public void join(Membership member)
{
[Link](member);
}
/**
* @return The number of members (Membership objects) in
* the club.
*/
public int numberOfMembers()
{
return [Link]();
}
}
Exercise 4.26
public void close(){
for(Lot lot : lots) {
[Link]([Link]() + ": " +
[Link]());
// Include any details of a highest bid.
Bid highestBid = [Link]();
if(highestBid != null) {
[Link](" Highest bidder: " +
[Link]().getName());
[Link](" Bid: " +
[Link]());
}
else {
[Link](" Not sold");
}
}
}
Exercise 4.27
/**
* Returns a list of unsold lots
*/
public ArrayList<Lot> getUnsold() {
ArrayList<Lot> unsoldLots = new ArrayList<Lot>();
for(Lot lot : lots) {
Bid highestBid = [Link]();
if(highestBid == null) {
[Link](lot);
}
}
return unsoldLots;
}
Exercise 4.28
If the method getLot is called with a lot-number that have been removed the following
message is printed in the terminal window:
Internal error: Wrong lot returned. Number: 1
Exercise 4.29
/**
* Return the lot with the given number. Return null
* if a lot with this number does not exist.
* @param number The number of the lot to return.
*/
public Lot getLot(int number)
{
for(Lot lot : lots) {
if([Link]() == number) {
return lot;
} else if ([Link]() > number) {
[Link]("Lot number: " + number +
" does not exist.");
return null;
}
}
return null; //if there are no lots at all
}
Exercise 4.30
/**
* Remove the lot with the given lot number.
* @param number The number of the lot to be removed
* @return The Lot with the given number, or null if
* there is no such lot.
*/
public Lot removeLot(int number) {
//First we find the lot with the given number
Lot lot = getLot(number);
if(lot != null) {
//Then we can use the method remove with lot as argument
[Link](lot);
}
return lot;
}
Exercise 4.31
The documentation for LinkedList can be found here:
[Link]
The LinkedList have these methods that ArrayList does not have:
void addFirst(Object o)
void addLast(Object o)
Object getFirst()
Object getLast()
ListIterator listIterator(int index)
Object removeFirst()
Object removeLast()
The ArrayList have these methods that LinkedList does not have:
void ensureCapacity(int minCapacity)
protected void removeRange(int fromIndex, int toIndex)
void trimToSize()
Exercise 4.32
/**
* Determine the number of members who joined in the
* given month
* @param month The month we are interested in.
* @return The number of members.
*/
public int joinedInMonth(int month) {
if(month < 1 || month > 12) {
[Link]("Month " + month + " out of range. Must
be in the range 1 ... 12");
}
int count = 0;
while(Membership member : members) {
if([Link]() == month) {
count++;
}
}
return count;
}
Exercise 4.33
public ArrayList purge(int month, int year) {
if(month < 1 || month > 12) {
[Link]("Month " + month + " out of range. Must
be in the range 1 ... 12");
}
ArrayList<Membership> purged = new ArrayList<Membership>();
Iterator<Membership> it = [Link]();
while([Link]()) {
Membership member = [Link]();
if([Link]() == month && [Link]() == year)
{
// Must use the remove method from the iterator.
// Check the documentation for the Iterator for more
info.
[Link]();
[Link](member);
}
}
return purged;
}
Exercise 4.34
public void printProductDetails()
{
for(Product product : stock) {
[Link](product);
}
}
Exercise 4.35
public Product findProduct(int id)
{
for(Product product : stock) {
if([Link]() == id) {
return product;
}
}
return null;
}
Exercise 4.36
public int numberInStock(int id)
{
Product product = findProduct(id);
if(product != null) {
return [Link]();
}
else {
return 0;
}
}
Exercise 4.37
public void delivery(int id, int amount)
{
Product product = findProduct(id);
if(product != null) {
[Link](amount);
}
}
Exercise 4.38
/**
* Print details of all the products which has stock
* levels below the given amount
*/
public void printLowStockProducts(int upperLimit)
{
for(Product product : stock) {
if([Link]() < upperLimit) {
[Link](product);
}
}
}
/**
* Add a product to the list.
* @param item The item to be added.
*/
public void addProduct(Product item)
{
if( ! [Link](item)) {
[Link](item);
}
}
/**
* Try to find a product in the stock with the given name.
* @return The identified product, or null if there is none
* with a matching name.
*/
public Product findProduct(String name)
{
for(Product product : stock) {
if([Link]().equals(name)) {
return product;
}
}
return null;
}
Exercise 4.39
The busiest time of day: 18
Exercise 4.40
Person[] person;
Exercise 4.41
boolean[] vacant;
Exercise 4.43
int[] counts;
boolean[] occupied = new boolean[5000];
Exercise 4.44
readings = new double[60];
urls = new String[90];
machines = new TicketMachine[5];
Exercise 4.45
None. It only creates an array to hold String objects.
Exercise 4.46
double[] prices = new double[50]
Exercise 4.47
It throws an ArrayIndexOutOfBoundsException: 24
Exercise 4.48
/**
* Print the hourly counts.
* These should have been set with a prior
* call to analyzeHourlyData.
*/
public void printHourlyCounts()
{
[Link]("Hr: Count");
int hour = 0;
while(hour<[Link]) {
[Link](hour + ": " + hourCounts[hour]);
hour++;
}
}
Exercise 4.49
public void printGreater(double[] marks, double mean) {
for(int index = 0; index < [Link]; index++) {
if(marks[index] > mean) {
[Link](marks[index]);
}
}
}
Exercise 4.50
public void listNotes()
{
for(int index=0; index < [Link](); index++) {
[Link]([Link](index));
}
}
Exercise 4.51
public void listNotes()
{
for(String note : notes) {
[Link](note);
}
}
Exercise 4.52
/**
* Return the number of accesses recorded in the log file
*/
public int numberOfAccesses() {
int total = 0;
// Add the value in each element of hourCounts to total.
for(int hourCount : hourCounts) {
total = total + hourCount;
}
return total;
}
Exercise 4.54
/**
* Return the busiest hour of day
*/
public int busiestHour() {
int busiestHour = 0;
for(int hour = 1; hour < [Link]; hour++) {
if(hourCounts[hour] > hourCounts[busiestHour]) {
busiestHour = hour;
}
}
return busiestHour;
}
Exercise 4.55
/**
* Return the quietest hour of day
*/
public int quietestHour() {
int quietestHour = 0;
for(int hour = 1; hour < [Link]; hour++) {
if(hourCounts[hour] < hourCounts[quietestHour]) {
quietestHour = hour;
}
}
return quietestHour;
}
Exercise 4.56
In the above implementation, it is the first one that is found.
Exercise 4.57
/**
* Return the two-hour period which is busiest.
*/
public int busiestTwoHourPeriod() {
int busiestPeriod = 0;
int busiestPeriodCount = 0;
for(int hour = 0; hour < [Link]-1; hour++) {
int periodCount = hourCounts[hour] + hourCounts[hour+1];
if(periodCount > busiestPeriodCount) {
busiestPeriod = hour;
busiestPeriodCount = periodCount;
}
}
return busiestPeriod;
}
Exercise 4.58
Reasons for choosing a fixed size array could be:
Performance is slightly better.
Avoids casting objects to Student when retrieving objects from the array.
Reasons for keeping the dynamically sized list:
No need to keep track of the current number of students.
Good for future enhancements (for instance if we want to have a method to
remove a student from the list).
Exercise 4.60
public void listNotes()
{
int index = 0;
do{
if(![Link]()) {
[Link]([Link](index));
index++;
}
} while(index < [Link]());
}