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
Create an original network topology consisting of at least seven routers and twelve links, assigning arbitrary positive weights to each link. Using this topology, apply Dijkstra's Link-State Algorithm to compute the shortest paths from a source router of your choice to all other routers in the network. Your topology must be entirely your own design and should not resemble any examples from the textbook, lecture slides, or other students' work. Al-generated topologies are not permitted. Create a PowerPoint presentation that follows the format and style of slides 11 to 23 from Lecture Slide Set 06 (LS06). You should copy those slides and make any necessary changes, additions, or deletions to reflect your own topology, shortest-path calculations, and update tables. Do not alter the original slide style, layout, or formatting.
Create an original network topology consisting of at least seven routers and twelve links, assigning arbitrary positive weights to each link. Using this topology, apply Dijkstra's Link-State Algorithm to compute the shortest paths from a source router of your choice to all other routers in the network. Your topology must be entirely your own design and should not resemble any examples from the textbook, lecture slides, or other students' work. Al-generated topologies are not permitted. Create
x3003 x3008 1110 0000 0000 1100 1110 0010 0001 0000 0101 0100 1010 0000 x3004 0010 0100 0001 0011 x3005 0110 0110 0000 0000 X3006 0110 1000 0100 0000 x3007 0001 0110 1100 0100 0111 0110 0000 What does the following LC-3 program do? Trace Step by Step, SHOW ALL YOUR WORK. x3001 x3002 0000 x3009 0001 0000 0010 0001 X300A 0001 0010 0110 0001 x300B 0001 0100 1011 1111 x300C 0000 0011 1111 1000 X300D 1111 0000 0010 0101 x300E 0000 0000 0000 0101 x300F 0000 0000 0000 0100 x3010 0000 0000 0000 0011 x3011 0000 0000 0000 0110 x3012 0000 0000 0000 0010 x3013 x3014 0000 0000 0000 0000 0000 0100 0000 0111 x3015 0000 0000 0000 0110 x3016 0000 0000 0000 1000 x3017 0000 0000 0000 0111 x3018 0000 0000 0000 0101

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