Concept explainers
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.
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:
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());
}
}
Want to see more full solutions like this?
Chapter 2 Solutions
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
Additional Engineering Textbook Solutions
Concepts of Programming Languages (11th Edition)
C++ How to Program (10th Edition)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Python (4th Edition)
Problem Solving with C++ (9th Edition)
Starting Out with Java: From Control Structures through Objects (6th Edition)
- can you please help me with the last part of this code because I don't understand this; part of the code is below and the other part of the code is in the photo. question: In the LightsOutPuzzle class, write a method copy(self) that returns a new LightsOutPuzzle object initialized with a deep copy of the current board. Changes made to the original puzzle should not be reflected in the copy, and vice versa. >>> p = create_puzzle(3, 3)>>> p2 = p.copy()>>> p.get_board() == p2.get_board()True >>> p = create_puzzle(3, 3)>>> p2 = p.copy()>>> p.perform_move(1, 1)>>> p.get_board() == p2.get_board()False In the LightsOutPuzzle class, write a method successors(self) that yields all successors of the puzzle as (move, new-puzzle) tuples, where moves themselves are (row, column) tuples. The second element of each successor should be a new LightsOutPuzzle object whose board is the result of applying the corresponding move to the current…arrow_forwardPLEASE 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.arrow_forwardIn this exercise, you are going to build on your Circleclass from the previous exercise. You are going to add 2 method, areaDifference and perimeterDifference. Both methods take a doubleradius of a second circle and return the difference from the current circle. For example, if you create a Circle object with a radius of 4 and call areaDifference(3), you will return the diffence between the area of a circle with radius 4 and the area of a circle with a radius of 3. perimeterDifferencewould be the same. Make sure you create at least one Circle and test and print the results of your methods. in javaarrow_forward
- In this exercise, you are going to build on your Circleclass from the previous exercise. You are going to add 2 method, areaDifference and perimeterDifference. Both methods take a doubleradius of a second circle and return the difference from the current circle. For example, if you create a Circle object with a radius of 4 and call areaDifference(3), you will return the diffence between the area of a circle with radius 4 and the area of a circle with a radius of 3. perimeterDifferencewould be the same. Make sure you create at least one Circle and test and print the results of your methods. given: public class Circle{private double radius;public Circle(double theRadius){radius = theRadius;}// Add a method called area that returns the area of a circle// using Math.PIpublic double area(){return Math.PI*radius*radius;}// Add a method called perimeter that returns the perimeter of a// circle using Math.PIpublic double perimeter(){return Math.PI*2*radius;}}arrow_forwardWrite the code for the timeTick method in ClockDisplay that displays hours, minutes, and seconds, or even implement the whole class if you wish.arrow_forwardWrite a compareTo method that compares two Song objects based on the artist, and title within artist. For example, given the following Songobjects: "Glass Animals""Heat Waves" "Elton John, Dua Lipa""Cold Heart" "Adele""Easy on Me" "Doja Cat""Kiss Me More" "Adele""My Little Love" They will be put in the following order: "Adele""Easy on Me" "Adele""My Little Love" "Doja Cat""Kiss Me More" "Elton John, Dua Lipa""Cold Heart" "Glass Animals""Heat Waves" In which class does the compareTo method belong?arrow_forward
- Write a button-click method to retrieve the value from an EditText and send it to another activity called SecondActivity. Then, write an onCreate method to retrieve the value sent from MainActivity to SecondActivity.arrow_forwardAdd a playCount field to the Track class. Provide methods to reset the count to zero and to increment it by one.arrow_forwardRearrange the code below to provide two additional CashRegister methods: an enterPayment method that reduces the total price by the given payment, and a giveChange method that returns the change due and resets the total price and item count. Not all lines are useful.arrow_forward
- Add a new Java class to your project by right clicking on the src folder in the Project pane and select New > Java Class.Enter Hours as the name for this class, and IntelliJ will add it to the files in the src folder for your project. Remember tocopy and insert the standard heading for the main method as you did for Program 2.Write a program that prompts the user to enter a number of hours, and produces the equivalent number of days as a wholenumber along with the number of hours left over. Assume that a day has exactly 24 hours. The following example showsthe format to use for your output, where the input entered by the user is underlined.Enter the number of hours: 100100 hours is 4 days, plus 4 hours.Define Variables: The number of hours will be a whole number, and we would like to obtain results as whole numbers ofdays and hours, so use the int data type to declare the variables to hold the numbers of hours input, as well as the results(the number of days and number of hours left…arrow_forwardUse the debugger to examine the clock-display project. Set breakpoints in the ClockDisplay constructor and each of the methods, and then singlestep through them. How do you feel about the way it's behaving? Have you gained any fresh insights as a result of this? If this is the case, what exactly were they?arrow_forwardWhich is NOT true of the decorator design pattern? It requires modification of the way an object is used It allows us to detach new methods to an object at runtime It allows us to attach new methods to an object at runtime It allows us to combine several features in a scaleable mannerarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education