Chapter 2 of the BlueJ book. Walk through many of the chapters exercises.
Nicholas Satinover 4278709a91 Text Answered in edited READ.ME file 6 years ago
doc added doc 6 years ago
.DS_Store added doc 6 years ago
.gitignore Initial commit 6 years ago
README.TXT checkpoint 6 years ago
README.md Text Answered in edited READ.ME file 6 years ago
TicketMachine.java added doc 6 years ago
bluej.pkg Text Answered in edited READ.ME file 6 years ago
bluej.pkh added doc 6 years ago
package.bluej Text Answered in edited READ.ME file 6 years ago

README.md

Naive Ticket Exercise's

Exercise 2.1 Requested price as argument for parameter upon creating new instance of TicketMachine getPrice returns price entered upon initialization of object insertMoney requests int argument for its parameter getBalance returns int equal to amount entered into insertMoney's parameter printTicket prints price of ticket and sets balance field to 0

Exercise 2.2 getBalance method returns 0 after printTicket method is called

Exercise 2.3 Naive TicketMachine does not validate correct amount entered, each call to printTicket will print a ticket with the listed price and set balance to 0

Exercise 2.4 The constructor requires input to set price = ticketCost Balance and total are set to 0 upon initialization

Exercise 2.5 The only difference is the price printed on the ticket

Exercise 2.6 ?

Exercise 2.7 Yes, order matters. Public modifier must come before class, will not successfully compile if reversed.

Exercise 2.8 Yes, there are no syntax errors if public modifier is not present

Exercise 2.9 Constructors have no return type, they do not require a public modifier and can only be run when object is created

Public class TicketMachine {

private int balance;
private int price;
private int deposit;

public TicketMachine(int ticketCost)
{
    price = ticketCost;
    balance = 0;
    deposit = 0;
}
public void printTicket()
{
    System.out.println(*****);
    System.out.println("Your ticket price is:");
    System.out.println(price + " cents.");

    balance = 0;
}
public void setBalance(int newBalance)
{
    balance = newBalance;
}
public int getBalance()
{
    return balance;
}
public int getPrice()
{
    return price;
}

}

Exercise 2.11 Int Student Server

Exercise 2.12 Alive Tutor game

Exercise 2.13 Yes order matters, modifier + type + name

Exercise 2.14 It is not always necessary to have a semicolon at the end of a statement but is bad syntax to not include

Exercise 2.15 Private int name;

Exercise 2.16 Student

Exercise 2.17 Two parameters of type String and double

Exercise 2.18 ?

Work all Exercises from 2.19 to 2.58 that are NOT marked Challenge exercise. READ upto and INCLUDING section 2.15 of this chapter.

Exercise 2.19 Two parameters of type String and double

Exercise 2.20

Exercise 2.21 Public Pet(String petsName) {

name = petsName;

}

Exercise 2.23 Only difference is the field they interact with

Exercise 2.24 Tickets cost value set when object created getBalance is total amount entered into machine before calling getTicket

Exercise 2.25 No, name of method being changed does not effect return statement

Exercise 2.26 Public int getTotal() {

return total;

}

Exercise 2.27 Must return value of type int

Exercise 2.28 Return type is main difference

Exercise 2.29 No return statements, these methods complete their statements without returning a value

Exercise 2.30 The value of the balance field changed as expected after each call

Exercise 2.31 Constructor's do not have return types

Exercise 2.32 Public void setPrice(int newPrice) {

price = newPrice;

}

Exercise 2.33 Score = points;

Exercise 2.35 Yes, if changes field value, is a mutator

Exercise 2.36 My cat has green eyes.

Exercise 2.37 Public void prompt() {

System.out.println("Please insert the correct amount of money.");

}

Exercise 2.38 The word price rather than the value of the variable price

Exercise 2.39 Would only print a string

Exercise 2.40 No, would only print string

Exercise 2.41 Public void showPrice() {

System.out.println("The price of the ticket is " + price + " cents.");

}

Exercise 2.42 Each object has it's own copy of the price field and will display it's own assigned value

Exercise 2.43 You are no longer prompted for input, price variable is set at 1000 when object created

Exercise 2.44 TicketMachine() {

price = 1000;
balance = 0;
total = 0;

}

Exercise 2.45 This method needs no parameters or return statement

Exercise 2.46 Balance is not changed if error message is printed

Exercise 2.47 No error will be thrown if 0 is entered and balance will not be changed as 0 is being added to current balance

Exercise 2.48 if(amount < ) {

System.out.println("Use a positive amount rather than: " + amount);

} else {

balance += amount;

}

Exercise 2.49

Exercise 2.50 We use conditional statements to check input and then choose an outcome base on boolean expression

Exercise 2.51 It still compiles but no action is taken if conditional statement does not evaluate to true for the if statement to run

Exercise 2.52 No, boolean expression evaluates balance and price fields to be sure balance is <= price before making changes (subtracting) from balance field

Exercise 2.53

  • / %

Exercise 2.54 Saving = price * discount;

Exercise 2.55 Mean = total / count;

Exercise 2.56 if(price > budget) {

System.out.println("Too expensive");

} Else {

System.out.println("Just right");

}

Exercise 2.57 if(price > budget) {

System.out.println("Too expensive, budget is: " + budget);

} Else {

System.out.println("Just right");

}

Exercise 2.58 Balance is set to 0 before returning