From 2b2e39c8feca21db33008dfb3707265bb816300b Mon Sep 17 00:00:00 2001 From: Mo Russ Date: Wed, 5 Feb 2020 15:12:28 -0500 Subject: [PATCH] Maurice Russ --- CopyOfTicketMachine.java | 98 ++++++++++++++++++++++++++++++++++++++++ README.md | 39 ++++++++++++++++ TicketMachineTest.java | 12 +++++ bluej.pkg | 42 ++++++++++++----- package.bluej | 42 ++++++++++++----- 5 files changed, 209 insertions(+), 24 deletions(-) create mode 100644 CopyOfTicketMachine.java diff --git a/CopyOfTicketMachine.java b/CopyOfTicketMachine.java new file mode 100644 index 0000000..700e5db --- /dev/null +++ b/CopyOfTicketMachine.java @@ -0,0 +1,98 @@ +/** + * CopyOfTicketMachine models a naive ticket machine that issues + * flat-fare tickets. + * The price of a ticket is specified via the constructor. + * It is a naive machine in the sense that it trusts its users + * to insert enough money before trying to print a ticket. + * It also assumes that users enter sensible amounts. + * + * @author David J. Barnes and Michael Kolling + * @version 2008.03.30 + */ +public class CopyOfTicketMachine +{ + // The price of a ticket from this machine. + private Integer price; + // The amount of money entered by a customer so far. + private Integer balance; + // The total amount of money collected by this machine. + private Integer total; + // The number of tickets printed. + private Integer ticketNumber; + + /** + * Create a machine that issues tickets of the given price. + * Note that the price must be greater than zero, and there + * are no checks to ensure this. + */ + public CopyOfTicketMachine(Integer ticketCost) + { + price = ticketCost; + balance = 0; + total = 0; + ticketNumber = 0; + } + + /** + * Return the price of a ticket. + */ + public Integer getPrice() + { + return price; + } + + /** + * Return ticketNumber. + * (Increments on each print.) + */ + public Integer getTicketNumber() + { + return ticketNumber; + } + + /** + * Return the amount of money already inserted for the + * next ticket. + */ + public Integer getBalance() + { + return balance; + } + + /** + * Receive an amount of money in cents from a customer. + */ + public Integer insertMoney(Integer amount) + { + balance = balance + amount; + return balance; + } + + public Integer calculateTotal(){ + total = balance + total; + return total; + } + + public Integer incrementTicketNumber(){ + ticketNumber++; + return ticketNumber; + } + + + /** + * Print a ticket. + * Update the total collected and + * reduce the balance to zero. + */ + public String printTicket() + { + //Increment the number of tickets printed + incrementTicketNumber(); + // Update the total collected with the balance. + total = total + balance; + // Clear the balance. + balance = 0; + + return "Ticket price: " + price + " cents. " + "Your total is " + total + "."; + } +} diff --git a/README.md b/README.md index 4f153b0..22556b4 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,18 @@ Work through all these exercises. You edit this file with your answers for these ### Exercise 2.2 * What value is returned if you check the machine’s balance after it has printed a ticket? +/*0;*/ + + ### Exercise 2.3 * Experiment with inserting different amounts of money before printing tickets. * Do you notice anything strange about the machine’s behavior? * What happens if you insert too much money into the machine – do you receive any refund? * What happens if you do not insert enough and then try to print a ticket? + + /*Strings with the ticket price and your total. All of the money is taken. No refund is given. No balance was shown but it still pints a ticket.;*/ + ### Exercise 2.4 * Try to obtain a good understanding of a ticket machine’s behavior by interacting with it on the object bench before we start looking at how the `TicketMachine` class is implemented in the next section. @@ -32,9 +38,19 @@ Work through all these exercises. You edit this file with your answers for these * Buy a ticket from that machine. * Does the printed ticket look different? + ?*The ticket are the same. The sting with the total is the only difference.;*/ + ### Exercise 2.6 * Write out what you think the outer wrappers of the `Student` and `LabClass` classes might look like – do not worry about the inner part. + +/*public class Student(){ + +} + +public class LabClass(){ + +};*/ ### Exercise 2.7 Does it matter whether we write
`public class TicketMachine`
@@ -47,17 +63,28 @@ in the outer wrapper of a class? * What error message do you get when you now press the compile button? * Do you think this message clearly explains what is wrong? + /*Yes. Public must be in front of class. + "Error(s) found in class" "Press K or click link on right to go to next error." + Yes;*/ + + ### Exercise 2.8 * Check whether or not it is possible to leave out the word `public` from the outer wrapper of the `TicketMachine` class. +/*Yes, you can.;*/ + ### Exercise 2.9 * From your earlier experimentation with the ticket machine objects within BlueJ you can probably remember the names of some of the methods – `printTicket`, for instance. * Look at the class definition in Code 2.1 and use this knowledge, along with the additional information about ordering we have given you, to try to make a list of the names of the fields, constructors, and methods in the `TicketMachine` class. * Hint: There is only one constructor in the class. + /*The fields are price, balance, total. The constructor is TicketMachine. The methods are the getters getPrice, getTicketNumber and getBalance.;*/ + + ### Exercise 2.10 * Do you notice any features of the constructor that make it significantly different from the other methods of the class? + /*It is another TicketMachine and parameters and is assigning values to price, balance, total and ticketNumber.;*/ ### Exercise 2.11 * What do you think is the type of each of the following fields? @@ -66,6 +93,7 @@ private int count; private Student representative; private Server host; ``` +/*They are private fields.;*/ ### Exercise 2.12 * What are the names of the following fields? @@ -75,6 +103,8 @@ private boolean alive; private Person tutor; private Game game; ``` +/*The fields are alive, tutor, and game.;*/ + ### Exercise 2.13 In the following field declaration from the TicketMachine class
@@ -89,29 +119,38 @@ possible? * Check by pressing the compile button to see if there is an error message. * Make sure that you reinstantiate the original version after your experiments! + /*Yes.You private is class type. It must must have a class definition before int. int must then be declared as price.;*/ + ### Exercise 2.14 * Is it always necessary to have a semicolon at the end of a field declaration? * Once again, experiment via the editor. * The rule you will learn here is an important one, so be sure to remember it. + /*The answer is usually. It is not needed at the end of a block, }.;*/ ### Exercise 2.15 * Write in full the declaration for a field of type `int` whose name is `status`. +/*private int status;*/ + ### Exercise 2.16 * To what class does the following constructor belong? ``` public Student(String name) ``` +/*public*/ ### Exercise 2.17 * How many parameters does the following constructor have and what are their types? ``` public Book(String title, double price) ``` +/*2*/ ### Exercise 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? +/*author, genre, pageNumber, publisher, dateOfPublish*/ + READ upto and INCLUDING section 2.15 of this chapter. diff --git a/TicketMachineTest.java b/TicketMachineTest.java index 9d1e74f..e39282f 100644 --- a/TicketMachineTest.java +++ b/TicketMachineTest.java @@ -13,6 +13,9 @@ */ public class TicketMachineTest { + private TicketMachine ticketMa1; + private java.lang.String string1; + /** * Default constructor for test class TicketMachineTest */ @@ -23,6 +26,15 @@ public TicketMachineTest() @Before public void setUp() { + ticketMa1 = new TicketMachine(20); + ticketMa1.getTicketNumber(); + ticketMa1.insertMoney(300); + string1 = ticketMa1.printTicket(); + ticketMa1.getBalance(); + ticketMa1.incrementTicketNumber(); + ticketMa1.getTicketNumber(); + ticketMa1.calculateTotal(); + ticketMa1.getBalance(); } @After diff --git a/bluej.pkg b/bluej.pkg index bf25784..f1a5556 100644 --- a/bluej.pkg +++ b/bluej.pkg @@ -1,20 +1,23 @@ #BlueJ package file +dependency1.from=TicketMachineTest +dependency1.to=TicketMachine +dependency1.type=UsesDependency editor.fx.0.height=722 editor.fx.0.width=800 -editor.fx.0.x=709 -editor.fx.0.y=113 +editor.fx.0.x=1098 +editor.fx.0.y=203 objectbench.height=101 -objectbench.width=461 +objectbench.width=776 package.divider.horizontal=0.6 package.divider.vertical=0.8007380073800738 package.editor.height=427 -package.editor.width=674 -package.editor.x=1067 -package.editor.y=119 +package.editor.width=682 +package.editor.x=1093 +package.editor.y=57 package.frame.height=600 package.frame.width=800 -package.numDependencies=0 -package.numTargets=1 +package.numDependencies=1 +package.numTargets=3 package.showExtends=true package.showUses=true project.charset=UTF-8 @@ -24,9 +27,24 @@ readme.width=47 readme.x=10 readme.y=10 target1.height=50 -target1.name=TicketMachine +target1.name=TicketMachineTest target1.showInterface=false -target1.type=ClassTarget +target1.type=UnitTestTargetJunit4 target1.width=120 -target1.x=80 -target1.y=50 +target1.x=110 +target1.y=20 +target2.association=TicketMachineTest +target2.height=50 +target2.name=TicketMachine +target2.showInterface=false +target2.type=ClassTarget +target2.width=120 +target2.x=80 +target2.y=50 +target3.height=50 +target3.name=CopyOfTicketMachine +target3.showInterface=false +target3.type=ClassTarget +target3.width=150 +target3.x=10 +target3.y=110 diff --git a/package.bluej b/package.bluej index bf25784..f1a5556 100644 --- a/package.bluej +++ b/package.bluej @@ -1,20 +1,23 @@ #BlueJ package file +dependency1.from=TicketMachineTest +dependency1.to=TicketMachine +dependency1.type=UsesDependency editor.fx.0.height=722 editor.fx.0.width=800 -editor.fx.0.x=709 -editor.fx.0.y=113 +editor.fx.0.x=1098 +editor.fx.0.y=203 objectbench.height=101 -objectbench.width=461 +objectbench.width=776 package.divider.horizontal=0.6 package.divider.vertical=0.8007380073800738 package.editor.height=427 -package.editor.width=674 -package.editor.x=1067 -package.editor.y=119 +package.editor.width=682 +package.editor.x=1093 +package.editor.y=57 package.frame.height=600 package.frame.width=800 -package.numDependencies=0 -package.numTargets=1 +package.numDependencies=1 +package.numTargets=3 package.showExtends=true package.showUses=true project.charset=UTF-8 @@ -24,9 +27,24 @@ readme.width=47 readme.x=10 readme.y=10 target1.height=50 -target1.name=TicketMachine +target1.name=TicketMachineTest target1.showInterface=false -target1.type=ClassTarget +target1.type=UnitTestTargetJunit4 target1.width=120 -target1.x=80 -target1.y=50 +target1.x=110 +target1.y=20 +target2.association=TicketMachineTest +target2.height=50 +target2.name=TicketMachine +target2.showInterface=false +target2.type=ClassTarget +target2.width=120 +target2.x=80 +target2.y=50 +target3.height=50 +target3.name=CopyOfTicketMachine +target3.showInterface=false +target3.type=ClassTarget +target3.width=150 +target3.x=10 +target3.y=110