OBJECTS FIRST WITH JAVA A
PRACTICAL INTRODUCTION
USING BLUEJ 6TH EDITION
BARNES SOLUTIONS MANUAL
:
Exercise 2.2
Zero.
Exercise 2.3
If too much money is inserted the machine takes it all - no refund.
If there isn't enough money inserted, it still prints out the ticket.
Exercise 2.5
It looks almost completely the same. Only the price on the ticket is different.
Exercise 2.6
The outer part of the student class:
public class Student
{
}
The outer part of the LabClass class:
public class LabClass
{
}
Exercise 2.7
Yes, the order of public and class matters.
Exercise 2.8
It is possible to leave out the word public.
Exercise 2.9
It is not possible to leave out the word class.
Exercise 2.10
Fields:
price
balance
total
Constructors:
TicketMachine
Methods:
getPrice
getBalance
insertMoney
printTicket
Exercise 2.11
It does not have any return type. The name of the constructor is the same as the
name of the class.
Exercise 2.12
int
Student
Server
Exercise 2.13
alive
tutor
game
Exercise 2.14
Student, Server, Person and Game
Exercise 2.15
The exact order matters.
Exercise 2.16
Yes, it always necessary to have a semicolon after a field declaration.
Exercise 2.17
private int status;
Exercise 2.18
It belongs to the class Student.
Exercise 2.19
It has two parameters. One is of type String and the other of type double.
Exercise 2.20
It would be reasonable to expect the types to be the same as the two parameters
(String and double).
Can't really assume anything about the names, but probably something like title
and price.
Exercise 2.21
name = petsName;
Exercise 2.22
public Date(String month, int day, int year)
Exercise 2.23
Aside from their names, the only difference is that getPrice() returns the value of
the price field whereas getBalance() returns the value of the balance field.
Exercise 2.24
How much money have I inserted into the machine?
Exercise 2.25
No. There is no direct link between the name of a method and the name of the
field. However, it is a convention to use a name for the method that clearly links it
to the field.
Exercise 2.26
public int getTotal()
{
return total;
}
Exercise 2.27
Missing return statement.
Exercise 2.28
The header for getPrice() has an int as return type.
The header for printTicket() has void as return type.
Exercise 2.29
No.
Because they don't need to return anything.
Yes. Both headers have void as return types.
Exercise 2.31
It has a return type (void) and constructors do not have return types. It also does
not have the same name as the class it is in.
Exercise 2.32
price = cost;
Exercise 2.33
score = score + points;
Exercise 2.34
It is a mutator.
Use an inspector to view the current score, then call the increase method with a
positive parameter value and observe that score increases by that value.
Alternatively, if score has an accessor method; call the accessor, then call
increase, and then call the accessor once again to verify that it returns the
updated value, indicating that the field has been modified.
Exercise 2.35
price = price - amount;
Exercise 2.36
Note that no quote marks are printed, just the following:
My cat has green eyes.
Exercise 2.37
public void prompt()
{
System.out.println("Please insert the correct amount of
money.");
}
Exercise 2.38
Instead of printing out the actual price of the ticket, it displays the word "price":
# price cents.
Exercise 2.39
Prints out the exact same string as in exercise 2.38
Exercise 2.40
No, because neither uses the value of the price field in the println statement.
Exercise 2.41
public void showPrice()
{
System.out.println("The price of a ticket is " + price + "
cents.");
}
Exercise 2.42
They display different prices. This is because each ticket machine object has its
own price field.
The price that was set in one ticket machine does not affect the other ticket
machine’s price.
The unique value of each ticket machine's price field is substituted into the println
statement when
the method is called.
Exercise 2.43
public TicketMachine()
{
price = 1000;
balance = 0;
total = 0;
}
When constructing a TicketMaching object you will not be prompted for a
parameter value.
The tickets always have a price of 1000 cents.
Exercise 2.44
public TicketMachine()
{
price = 1000;
balance = 0;
total = 0;
}
public TicketMachine(int ticketCost)
{
price = ticketCost;
balance = 0;
total = 0;
}
Exercise 2.45
public void empty()
{
total = 0;
}
It needs no parameters.
It is a mutator.
Exercise 2.46
The balance does not change when an error message is printed.
Inserting zero results in an error message.
Exercise 2.47
This version does not print an error message when zero is inserted.
Other than that, it does not change the observable behavior of the method.
Exercise 2.48
Note the importance of using budget) {
System.out.println("Too expensive. Your budget is only: " +
budget);
}
else {
System.out.println("Just right.");
}
Exercise 2.58
Because balance is set to zero and then this new value is returned rather than the
old one. The method will always return zero. It can be tested by inserting an
amount, and then calling refundBalance(). The original would then return the
amount inserted, but the new method returns 0.
Exercise 2.59
An error is printed: unreachable statement.
A return statement ends (exits) the method. Code after a return statement can
therefore never be executed.
Exercise 2.60
The variable price is being re-declared as a local variable in the constructor – this
is the effect of the word int in front of it. This local variable ‘hides’ the field of the
same name. So the ticket price is never assigned to the price field.
Exercise 2.61
public int emptyMachine()
{
int oldTotal = total;
total = 0;
return oldTotal;
}
Exercise 2.62
public void printTicket()
{
int amountLeftToPay = price - balance;
if(amountLeftToPay 0 ) {
refNumberString = refNumber;
}
else {
refNumberString = "ZZZ";
}
System.out.println("Reference number: " + refNumberString);
}
Exercise 2.90
public void setRefNumber(String ref)
{
if(ref.length() >= 3) {
refNumber = ref;
}
else {
System.out.println("Error! The reference number must be at
least 3 characters long.");
}
}
Exercise 2.91
Add the field:
private int borrowed;
Add the methods:
/**
* Borrows the book. Increments the number of times the book has
been borrowed.
*/
public void borrow()
{
borrowed++;
}
/**
* Gets the number of times the book has been borrowed.
*/
public int getBorrowed()
{
return borrowed;
}
Add this line to printDetails method:
System.out.println("Borrowed: " + borrowed);
Exercise 2.92
private boolean courseText;
public boolean isCourseText()
{
return courseText;
}
Exercise 2.93
public class Heater
{
private double temperature;
/**
* Creates a new Heater with an initial temperature of 15.
*/
public Heater()
{
temperature = 15.0;
}
/**
* Increases the temperature by 5 degrees
*/
public void warmer()
{
temperature += 5.0;
}
/**
* Decreases the temperature by 5 degrees
*/
public void cooler()
{
temperature -= 5.0;
}
/**
* Gets the current temperature of the heater
*/
public double getTemperature()
{
return temperature;
}
}
Exercise 2.94
public class Heater
{
private double temperature;
private double min;
private double max;
private double increment;
/**
* Creates a new Heater with an initial temperature of 15.
*/
public Heater(double minimum, double maximum)
{
min = minimum; max
= maximum;
temperature = 15.0;
increment = 5.0;
}
/**
* Increases the temperature
*/
public void warmer()
{
double newTemperature = temperature + increment;
if(newTemperature = min) {
temperature = newTemperature;
}
}
/**
* Sets the increment, which determines how much the two
methods
* warmer() and cooler() changes the temperature.
*/
public void setIncrement(double inc)
{
if(inc >= 0) {
increment = inc;
}
}
/**
* Gets the current temperature of the heater
*/
public double getTemperature()
{
return temperature;
}
}
If the setIncrement method does not check for negative values, and a negative
value is passed, then the program will not work as expected. The minimum and
maximum values can be exceeded.