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

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
Need help writing code to answer this question in Python! (image attached)
Need help with python code! How do I simplify my code for a beginner to understand, simple fixed format and centering? Such as:  print(f"As an int variable: {age_int:^7}") print(f"In numeric binary: {age_int:^7b}") My Code:name = input("Enter your name: ")print(f"In text name is: {' '.join(name)}")decimal_values = []binary_values = []for letter in name:   ascii_val = ord(letter)   binary_val = format(ascii_val, '08b')   decimal_values.append(str(ascii_val))   binary_values.append(binary_val)# Loop through each letter:print(f"In ASCII decimal: {' '.join(decimal_values)}")print(f"In ASCII binary: {' '.join(binary_values)}")# Ageage_str = input("Enter your age: ")age_int = int(age_str)print(f"As a string \"{age_str}\": {' '.join(age_str)}")age_decimal_values = []age_binary_values = []for digit in age_str:   ascii_val = ord(digit)   binary_val = format(ascii_val, '07b')   age_decimal_values.append(str(ascii_val))   age_binary_values.append(binary_val)print(f"In ASCII decimal: {'…
Don't use chatgpt or any other AI

Chapter 2 Solutions

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

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