Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
6th Edition
ISBN: 9780134477367
Author: David J. Barnes, Michael Kolling
Publisher: PEARSON
bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 2, Problem 1E

Create a TicketMachine object on the object bench and take a look at its methods. You should see the following: getBalance, getPrice, insertMoney, and printTicket. Try out the getPrice method. You should see a return value containing the price of the tickets that was set when this object was created. Use the insertMoney method to simulate inserting an amount of money into the machine. The machine stores as a balance the amount of money inserted. Use getBalance to check that the machine has kept an accurate record of the amount just inserted. You can insert several separate amounts of money into the machine, just like you might insert multiple coins or bills into a real machine. Try inserting the exact amount required for a ticket, and use getBalance to ensure that the balance Is Increased correctly. As this is a simple machine, a ticket will not be issued automatically, so once you have inserted enough money, call the printTicket method. A facsimile ticket should be printed in the BlueJ terminal window.

Expert Solution & Answer
Check Mark
Program Plan Intro

Creating a TicketMachine object and using functions of the object class.

Program Plan:

Write a JAVA program to create an object of the class TicketMachine with the main function and the required set of statements to accomplish the following:

Use of the getPrice method to check the return value containing the price of a ticket

Use of insertMoney to inserting money into the machine

Check the balance using getBalance method to keep an accurate record of the money.

Generate a ticket using printTicket method when inserted enough money.

Program Description Answer

Program Description:

The following JAVA program prompts the user to insert enough money to a TicketMachine before trying to print a ticket.

Explanation of Solution

Program:

// TicketMachine is a working model of the ticket printing machine.

// Through constructor, the price of the ticket is passed.

// For printing tickets, enough money has to be entered into the machine.

class TicketMachine

{

// Cost per ticket.

private int price;

// Customer entered amount.

private int balance;

// The amount present in the machine.

private int total;

// Constructor to take and initialized the price of the ticket.

public TicketMachine(int cost)

{

// Cost of ticket allocated.

price = cost;

//declaring the value of variable

balance =0;

//declaring the value of variable

total = 0;

}

// Gets the ticket price

public int getPrice()

{

//return the value of price

return price;

}

// declaring the nee method .

public int getBalance()

{

//return the value of balance.

return balance;

}

// decaling method for money

public void insertMoney(int amount)

{

//add the balance

balance = balance + amount;

}

//Ticket has to be printed.

// Update the total money present in the machine and change the balance for // next ticket to zero.

public void printTicket()

{

//message for printing of a ticket.

System.out.println(“###################�);

//message for printing of a ticket.

System.out.println(“# The Bluej line�);

//message for printing of a ticket.

System.out.println(“# Ticket�);

//message for printing of a ticket.

System.out.println(“# “+ price + “ cents.�);

//message for printing of a ticket.

System.out.println(“###################�);

//message for printing of a ticket.

System.out.println();

// Total money is update for the machine.

total = total + balance;

// balance is cleared for the next ticket.

balance = 0;

}

}

/&

The main class which has the main method to create and

call the object of the TicketMachine.

*/

public class Main

{

// Main method to call the methods

public static void main(String[] args) {

/**

Creating object 'obj' of class TicketMachine.

and the cost of the ticket = 1000.

*/

TicketMachine obj = new TicketMachine(1000);

// To see the price of the ticket

System.out.println("The price of the ticket = " + obj.getPrice());

// Inserting amount into the TicketMachine.

obj.insertMoney(1000);

// Checking for the balance in the TicketMachine.

System.out.println("Balance = " + obj.getBalance());

/&

Balance should be enough to print the ticket

Assuming that cost of ticket is 1000.

*/

if(obj.getBalance()>=1000)

{

// Print the ticket

obj.printTicket();

}

// Checking the balance is increasing on inserting more money .

obj.insertMoney(100);

System.out.println("New Balance = " + obj.getBalance());

}

}

Sample Output

Objects First with Java: A Practical Introduction Using BlueJ (6th Edition), Chapter 2, Problem 1E

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
In this assignment, you will need to add a new method to the Date class, which you can find on D2L in the "Sample Code" section. The new method should make a Date object's day, month, and year value change to the next day. In other words, add one day to the object's current date. Do not use any part of the built-in DateTime class, but you may want to use the DaysInMonth() method from the Date class. Examples: • If the Date object stores the date 12/31/2021, then after calling your method it should store the date 1/1/2022 • If the Date object stores the date 2/28/2022, then after calling your method it should store the date 3/1/2022 As noted in the common instructions, you only need to turn in your new method, not the entire Date class.
Open the clock-display project and create a ClockDisplay object by selecting the following constructor: new ClockDisplay() Call its getTime method to find out the initial time the clock has been set to. Can you work out why it starts at that particular time?
PLEASE ENSURE TO USE THE FRAMEWORK PROVIDED IN THE IMAGES, AND THAT IT WORKS WITH THE TESTER CLASS. Write a class RangeInput that allows users to enter a value within a range of values that is provided in the constructor. An example would be a temperature control switch in a car that allows inputs between 60 and 80 degrees Fahrenheit. The input control has “up” and “down” buttons. Provide up and down methods to change the current value. The initial value is the midpoint between the limits. As with the preceding exercises, use Math.min and Math.max to limit the value. Write a sample program that simulates clicks on controls for the passenger and driver seats.

Chapter 2 Solutions

Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)

Ch. 2 - Prob. 11ECh. 2 - Prob. 12ECh. 2 - Prob. 13ECh. 2 - Prob. 14ECh. 2 - In the following field declaration from the...Ch. 2 - Prob. 16ECh. 2 - Prob. 17ECh. 2 - To what class does the following constructor...Ch. 2 - How many parameters does the following constructor...Ch. 2 - Prob. 20ECh. 2 - Suppose that the class Pet has a field called name...Ch. 2 - Challenge exercise The following object creation...Ch. 2 - Compare the header and body of the getBalance...Ch. 2 - If a call to getPrice can be characterized as...Ch. 2 - If the name of getBalance is changed to getAmount,...Ch. 2 - Prob. 26ECh. 2 - Try removing the return statement from the body of...Ch. 2 - Compare the method headers of getPrice and...Ch. 2 - Do the insertMoney and printTicket methods have...Ch. 2 - Create a ticket machine with a ticket price of...Ch. 2 - How can we tell from just its header that setPrice...Ch. 2 - Complete the body of the setPrice method so that...Ch. 2 - Prob. 33ECh. 2 - Is the increase method a mutator? If so, how could...Ch. 2 - Prob. 35ECh. 2 - Write down exactly what will be printed by the...Ch. 2 - Add a method called prompt to the TicketMachine...Ch. 2 - Prob. 38ECh. 2 - What about the following version? Ch. 2 - Prob. 40ECh. 2 - Add a showPrice method to the TicketMachine class....Ch. 2 - Create two ticket machines with differently priced...Ch. 2 - Modify the constructor of TicketMachine so that it...Ch. 2 - Give the class two constructors. One should take a...Ch. 2 - Implement a method, empty, that simulates the...Ch. 2 - Prob. 46ECh. 2 - Predict what you think will happen if you change...Ch. 2 - Rewrite the if-else statement so that the method...Ch. 2 - Prob. 49ECh. 2 - In this version of printTicket, we also do...Ch. 2 - Is it possible to remove the else part of the...Ch. 2 - After a ticket has been printed, could the value...Ch. 2 - So far, we have introduced you to two arithmetic...Ch. 2 - Write an assignment statement that will store the...Ch. 2 - Write an assignment statement that will divide the...Ch. 2 - Prob. 56ECh. 2 - Modify your answer to the previous exercise so...Ch. 2 - Why does the following version of refundBalance...Ch. 2 - What happens if you try to compile the...Ch. 2 - What is wrong with the following version of the...Ch. 2 - Add a new method, emptyMachine, that is designed...Ch. 2 - Rewrite the printTicket method so that it declares...Ch. 2 - Challenge exercise Suppose we wished a single...Ch. 2 - List the name and return type of this method: Ch. 2 - Prob. 65ECh. 2 - Write out the outer wrapping of a class called...Ch. 2 - Write out definitions for the following fields: Ch. 2 - Write out a constructor for a class called Module....Ch. 2 - Prob. 69ECh. 2 - Correct the error in this method:...Ch. 2 - Write an accessor method called getName that...Ch. 2 - Write a mutator method called setAge that takes a...Ch. 2 - Write a method called printDetails for a class...Ch. 2 - Draw a picture of the form shown in Figure 2.3,...Ch. 2 - Prob. 75ECh. 2 - Create a Student with name "djb" and id "859012"....Ch. 2 - Prob. 77ECh. 2 - Challenge exercise Modify the getLoginName method...Ch. 2 - Consider the following expressions. Try to predict...Ch. 2 - Open the Code Pad in the better-ticket-machine...Ch. 2 - Now add the following in the Code Pad:...Ch. 2 - Add the following: t1.InsertMoney500; What would...Ch. 2 - Prob. 83ECh. 2 - Prob. 84ECh. 2 - Add a field, pages, to the Book class to store the...Ch. 2 - Are the Book objects you have implemented...Ch. 2 - Add a method, printDetails, to the Book class....Ch. 2 - Add a further field, refNumber, to the Book class....Ch. 2 - Modify your printDetai 1 s method to include...Ch. 2 - Prob. 90ECh. 2 - Add a further integer field, borrowed, to the Book...Ch. 2 - Prob. 92ECh. 2 - Prob. 93ECh. 2 - Prob. 94E

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Java Math Library; Author: Alex Lee;https://www.youtube.com/watch?v=ufegX5o8uc4;License: Standard YouTube License, CC-BY