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)
- In 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_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_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_forwardExperiment 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_forward
- PLEASE 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_forwardCreate a "book" class with relevant properties and methods. Be creative in "inventing" them. You must use "setter" and "getter" methods.arrow_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. in javaarrow_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. 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_forwardExamine the getBalance method's header and body, and then evaluate how they stack up against those of the getPrice method. I'm confused as to the differences between the two.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