bartleby

Concept explainers

bartleby

Videos

Question
Book Icon
Chapter 2, Problem 1E
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
You are tasked with developing a portable system that can be worn to collect health and fitness data. The challenge is to integrate all functions into the smaller form of an ear clip. The device should include heart rate, movement and temperature sensor and wireless communication with a mobile app. Draw a diagram- hardware architecture of the system- including the selection of suitable sensors, communication modules, and an energy-efficient microcontroller. (visualize the components and their connections)
Draw out an example of 3 systems using Lamport’s logical clock and explain the steps in words.
"Systems have become very powerful and sophisticated, providing quality information fordecisions that enable the firm to coordinate both internally and externally."With reference to the above statement compare the operations of any three data gatheringsystems today’s organisations use to aid decision making.

Chapter 2 Solutions

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

Chapter 2, Problem 11E Chapter 2, Problem 12E Chapter 2, Problem 13E Chapter 2, Problem 14E Chapter 2, Problem 15E Chapter 2, Problem 16E Chapter 2, Problem 17E Chapter 2, Problem 18E Chapter 2, Problem 19E Chapter 2, Problem 20E Chapter 2, Problem 21E Chapter 2, Problem 22E Chapter 2, Problem 23E Chapter 2, Problem 24E Chapter 2, Problem 25E Chapter 2, Problem 26E Chapter 2, Problem 27E Chapter 2, Problem 28E Chapter 2, Problem 29E Chapter 2, Problem 30E Chapter 2, Problem 31E Chapter 2, Problem 32E Chapter 2, Problem 33E Chapter 2, Problem 34E Chapter 2, Problem 35E Chapter 2, Problem 36E Chapter 2, Problem 37E Chapter 2, Problem 38E Chapter 2, Problem 39E Chapter 2, Problem 40E Chapter 2, Problem 41E Chapter 2, Problem 42E Chapter 2, Problem 43E Chapter 2, Problem 44E Chapter 2, Problem 45E Chapter 2, Problem 46E Chapter 2, Problem 47E Chapter 2, Problem 48E Chapter 2, Problem 49E Chapter 2, Problem 50E Chapter 2, Problem 51E Chapter 2, Problem 52E Chapter 2, Problem 53E Chapter 2, Problem 54E Chapter 2, Problem 55E Chapter 2, Problem 56E Chapter 2, Problem 57E Chapter 2, Problem 58E Chapter 2, Problem 59E Chapter 2, Problem 60E Chapter 2, Problem 61E Chapter 2, Problem 62E Chapter 2, Problem 63E Chapter 2, Problem 64E Chapter 2, Problem 65E Chapter 2, Problem 66E Chapter 2, Problem 67E Chapter 2, Problem 68E Chapter 2, Problem 69E Chapter 2, Problem 70E Chapter 2, Problem 71E Chapter 2, Problem 72E Chapter 2, Problem 73E Chapter 2, Problem 74E Chapter 2, Problem 75E Chapter 2, Problem 76E Chapter 2, Problem 77E Chapter 2, Problem 78E Chapter 2, Problem 79E Chapter 2, Problem 80E Chapter 2, Problem 81E Chapter 2, Problem 82E Chapter 2, Problem 83E Chapter 2, Problem 84E Chapter 2, Problem 85E Chapter 2, Problem 86E Chapter 2, Problem 87E Chapter 2, Problem 88E Chapter 2, Problem 89E Chapter 2, Problem 90E Chapter 2, Problem 91E Chapter 2, Problem 92E Chapter 2, Problem 93E Chapter 2, Problem 94E

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Male and Female Percentages Program plan: Declare the required variables in the program. Call the "input" funct...

Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)

The vertical deflection at A (υA).

Mechanics of Materials (10th Edition)

The statement <? super Double> represents the object passed as an argument of type Double and super class of Do...

Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)

Knowledge Booster
Background pattern image
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
    Programming Logic & Design Comprehensive
    Computer Science
    ISBN:9781337669405
    Author:FARRELL
    Publisher:Cengage
    Text book image
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781305480537
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
    Text book image
    Microsoft Visual C#
    Computer Science
    ISBN:9781337102100
    Author:Joyce, Farrell.
    Publisher:Cengage Learning,
    Text book image
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781337671385
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
    Text book image
    Programming with Microsoft Visual Basic 2017
    Computer Science
    ISBN:9781337102124
    Author:Diane Zak
    Publisher:Cengage Learning
    Text book image
    Np Ms Office 365/Excel 2016 I Ntermed
    Computer Science
    ISBN:9781337508841
    Author:Carey
    Publisher:Cengage
    Java Math Library; Author: Alex Lee;https://www.youtube.com/watch?v=ufegX5o8uc4; License: Standard YouTube License, CC-BY