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
Problem Solving with C++ (10th Edition)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)
Web Development and Design Foundations with HTML5 (8th Edition)
Mechanics of Materials (10th Edition)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
- Add a playCount field to the Track class. Provide methods to reset the count to zero and to increment it by one.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_forwardIn this task by using java language , you should create the moving box example belwo, then you should extend the box class and create a new one called ColoredBox, in this class you should override the drawing method, the first thing you should do is recalling the original draw method (as we don't want to change any of the previous behaviours), then you should check if the box collided on the X or Y axis, if so change the colour of the box randomly colour. o 3:53 / Now we have a fully functional animated box :D TV Screen D. Figure 2: TV Screen App.arrow_forward
- Please help me create a cave class for a Hunt the Wumpus game. You can read the rules in it's entirety of the Hunt the Wumpus game online to get a better idea of the specifications. It's an actual game. INFORMATION: The object of this game is to find and kill the Wumpus within a minimum number of moves, before you get exhausted or run out of arrows. There is only one way to win: you must discover which room the Wumpus is hiding in and kill it by shooting an arrow into that room from an adjacent room. The Cave The Wumpus lives in a cave of 30 rooms. The rooms are hexagonal. Each room has up to 3 tunnels, allowing access to 1, 2 or 3 (out of 6) adjacent rooms. The attached diagram shows the layout of rooms in the cave. The map wraps around such that rooms on the edges (white cells) have neighbors from the opposite edge (blue cells). E.g., the neighbors of room 1 are rooms 25, 26, 2, 7, 6, and 30, and you could choose to connect room 1 to any of these rooms. Observe how room 1…arrow_forwardAdd a method to search about a particular student using his/her name. The method should display all student details if found or display a suitable massage otherwisearrow_forwardWrite a main method, in which you should have an Arralylist of students, in which you should include every student. You should have a PhD student named Joe Smith. In addition, you should create an Undergraduate student object for each of your team members (setting their names). If you’re only one person doing the assignment, then you should just create one undergraduate object. For each undergraduate student, you should add add two courses (COIS 2240 and COIS 1020). Loop over the arraylist of students, print their names, if the student is an undergraduate student, print their courses.arrow_forward
- Write 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_forwardSearch a graphic method online and take a screenshot of it. Cite your reference.arrow_forwardImplement following three methods that passes sturctural tests: getXStep and getYStep: helper methods, they indicate how far (-1, 0, or 1) have to be taken to take x and y coordinates. moveAndSet: it moves the piece and changes the value of the new position. Use the above helper methods in the code of the method. If the move would make the figure leave the board, cancel the move: the figure remains where it was, the state of the board doesn’t change at all, and the method immediately returns 0. Otherwise, the method returns the old value of the new position, and its value is set to the second argument. Különben a metódus térjen vissza az új mező régi értékével, és a mező értéke álljon át a paraméterben megadottra. setAndMove: it is similar to moveAndSet but it changes the value of the position before changing position @Test @DisabledIf(notApplicable) publicvoidmethodGetX() { it.hasMethod("getXStep", withParams("direction: walking.game.util.Direction")) .thatIs(FULLY_IMPLEMENTED,…arrow_forward
- 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?arrow_forwardPLEASE ENSURE TO USE THE FRAMEWORK PROVIDED IN THE IMAGES, AND THAT IT WORKS WITH THE TESTER CLASS. PLEASE DONT EDIT THE TEST CLASS. Simulate a circuit for controlling a hallway light that has switches at both ends of the hallway. Each switch can be up or down, and the light can be on or off. Toggling either switch turns the lamp on or off. Provide methods public int getFirstSwitchState() // 0 for down, 1 for up public int getSecondSwitchState()public int getLampState() // 0 for off, 1 for onpublic void toggleFirstSwitch() public void toggleSecondSwitch()arrow_forwardPLEASE ENSURE TO USE THE FRAMEWORK PROVIDED IN THE IMAGES, AND THAT IT WORKS WITH THE TESTER CLASS. PLEASE DONT EDIT THE TEST CLASS. Simulate a circuit for controlling a hallway light that has switches at both ends of the hallway. Each switch can be up or down, and the light can be on or off. Toggling either switch turns the lamp on or off. Provide methods public int getFirstSwitchState() // 0 for down, 1 for up public int getSecondSwitchState()public int getLampState() // 0 for off, 1 for onpublic void toggleFirstSwitch() public void toggleSecondSwitch()arrow_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