2.16 | To what class does the following constructor belong? public Student (String name)
| It belongs to class “Student.” |
2.17 | How many parameters does the following constructor have and what are their types? public Book(String title, double price)
| It has two parameters in the constructor. One is of type string, and the other is of type double. |
2.18 | Can you guess what types some of the Book class's fields might be? Can you assume anything about the names of its fields?
| Some of the Book class’s fields might be the name of the book, a string type, the author of the book, another string type, and the date it was published, which would again be a string type. If it was a book to be bought it could have an integer type for the field price, or if it was a library book it could have a code in integer type. |
2.19 | Suppose that the class Pet has a field called name that is of type String. Write an assignment in the body of the following constructor so that the name field will be initialized with the value of the constructor's parameter. public Pet (String petsName) { >> }
| public Pet(String petsName) { name = petsName; } |
2.20 | What is wrong with the following constructor of TicketMachine? public TicketMachine (int ticketCost) { int price = ticketCost; balance = 0; total = 0; } Once you have spotted the problem, try out this version in the naive-ticket-machine project. Does this version compile? Create an object, and then inspect its fields. Do you notice something wrong about the value of the price fiield in the inspector with this version? Can you explain why this is?
| Yes this version compiles; however, when a new object is created from the class, and a price parameter is set, there is no price value in the field. This is probably because the program could not understand the “int” part of the code before the “price = ticketCost” and simply put that field at the default integer zero as a result. |
2.21 | Compare the getBalance() method with the getPrice() method. What are the differences between them?
| The getBalance() and getPrice() methods, which are extremely similar in almost every way, are different in that invoking getPrice() simply returns the price a ticket, while getBalance() returns the total amount of money inside the ticket-machine at that point in its lifetime. |
2.22 | If a call to getPrice() can be characterized as "What do tickets cost?", how would you characterize a call to getBalance()? | I would characterize getBalance() as “How much money is in the machine right now?” |
2.23 | If the name of getBalance() is changed to getAmount(), does the return statement in the body of the method need to be changed too? Try it out within BlueJ.
| No it doesn’t, everything that was balance simply changed to amount. |
2.24 | Define an accessor method, getTotal(), that returns the value of the total field. | Done. |
2.25 | Try removing the return statement from the body of getPrice(). What error message do you get when you try compiling the class?
| The compiler simply responds by giving an error saying it is “missing return statement/” |
2.26 | Compare the method signatures of getPrice() and printTicket() in code 2.1. Apart from their names, what is the main difference between them?
| The main difference between the method signatures of getPrice() and printTicket() is that getPrice() is of integer type, and printTicket() is of void type, meaning it does not return any value, rather it prints out a ticket. |
2.27 | Do the insertMoney and printTicket methods have return statements? Why do you think this might be? Do you notice anything about their headers that might suggest why they do not require return statements? | No, they don’t, because the word void in their headers means that it they will not have any return statements. |
2.28 | Create a ticket machine with a ticket price of your choosing. Before doing anything else, call the getBalance method on it. Now call the insertMoney method (Code 2.6) and give a non-zero positive amount of money as the actual parameters. Now call getBalance again. The two calls to getBalance should show different output because the call to insertMoney had the effect of changing the machine’s state via it’s balance field. | Done. |
|
|
|
2.29 | How can we tell from just its header that setPrice() is a method and not a constructor? public void setPrice(int ticketCost)
| The word void in the setPrice() method proves that it it is a mutator method, and therefore it’s possible to know that it only alters the fields, but doesn’t create them. |
2.30 | Complete the body of the setPrice() method so that it assigns the value of its parameter to the price field. | Done. |
2.31 | Complete the body of the following method, whose purpose is to add the value of its parameter to a field named score. public void increase (int points)
| /** - Increase score by the given number of points.
- /
public void increase(int points) { score = score + points; } |
2.32 | Can you compile the following method, whose purpose is to subtract the value of its parameter from a field named price? public void discount (int amount)
| Yes, done. |
2.33 | Add a method called prompt() to the TicketMachine class. This should have a void return type and take no parameters. The body of the method should print something like:
"Please insert the correct amount of money."
| Done. |
2.34 | Add a showPrice() method to the TicketMachine class. This should have a void return type and take no parameters. The body of the method should print something like: "The price of a ticket is xyz cents" where xyz should be replaced by the value held in the price field when the method is called.
| Done. |
2.35 | Create two ticket machines with differently priced tickets. Do calls to their showPrice() methods show the same output or different? How do you explain this effect?
| No they don’t because each one has a different set amount, and in my showPrice() method, I made the printed string show xyz, being the amount, as a parameter and not a set amount. Therefore, now when I make two different machines, they show different outputs, because they show different prices. |
2.36 | What do you think would be printed if you altered the fourth statement of printTicket() so that the price also has quotes around it as follows? System.out.println("# " + "price" + " cents.");
| If you put quotation marks around price, it would no longer be a parameter, but a string, and all that would show up would be the word price itself instead of the integer parameter that is meant to be there. |
2.37 | What about the following version? System.out.println ("# price cents.");
| This is a more simplified version of the last, the word price is still in quotations, meaning it will be a string and not a variable. |
2.38 | Could either of the previous two versions be used to show the price of tickets in different ticket machines? Explain your answer.
| No because they placed the price variable inside the string, making it no longer a variable. To make price different on each machine, it should stay the way it was, outside of quotations, so that it will still be considered a variable. |
2.39 | Modify the constructor of TicketMachine so that it no longer has a parameter. Instead, the price of tickets should be fixed at 1000 cents. What effect does this have when you construct ticket machine objects within BlueJ.
| Instead of having a variable, price, it is now set at 1,000 cents. Therefore each method that uses price as a variable will now have the integer value, 1,000, in its place. |
2.40 | Implement a method, empty(), that simulates the effect of removing all money from the machine. This method should have a void return type, and its body should simply set the total field to zero. Does this method need to take any parameters? Test your method by creating a machine, inserting some money, printing some tickets, checking the total then emptying the machine. Is this method a mutator or an accessor?
| No this method takes no parameters, and it is a mutator, as it changes the balance’s value to 0. |
2.41 | Implement a method setPrice(), that is able to set the price of tickets to a new value. The new price is passed in as a parameter value to the method. Test your method by creating a machine, showing the price of tickets, changing the price, and then showing the new price. Is this method a mutator? Explain.
| Yes this method is a mutator as it changes the price field’s value from one into another. |
2.42 | Give the class two constructors. One should take a single parameter that specifies the price, and the other should take no parameter and set the price to a default value of your choosing. Test your implementation by creating machines via the two different constuctors.
| Done. |