In: Computer Science
Put In Java Programming
The TicketMachine class: Design a class named TicketMachine that
contains:
• A double data field named price (for the price of a ticket from
this machine).
• A double data field named balance (for the amount of money
entered by a customer).
• A double data field named total (for total amount of money
collected by the machine).
• A constructor that creates a TicketMachine with all the three
fields initialized to some values.
• A constructor that creates a TicketMachine with price and balance
fields initialized to some value and the total field initialized to
zero.
• A default no-argument constructor that creates a TicketMachine
with all three fields initialized to zero.
• Accessor and setter methods to get and set all three class
fields.
• A printTicketMachineInfo() method that prints the fields of a
TicketMachine object.
• Make all data fields private, and all methods and constructors
public.
• Write a test program that creates 3 TicketMachine objects.
o One object should be created by using the three-argument
constructor.
o One object should be created by using the two-argument
constructor.
o One object should be created by using the default
constructor.
o Invoke the printTicketMachineInfo() method on all the three
objects to print each object’s field values.
For different constructors, used the concept of constructor chaining.
Constructor chaining is the process of calling one constructor from another constructor with respect to the current object.
It can be done using this() keyword for constructors in the same class.
In the method, printTicketMachineInfo invoked the toString method of the object to get a string representation of the object.
A toString() is an in-built method in Java that returns the value given to it in string format. Hence, any object that this method is applied on, will then be returned as a string object.
Test.java
class TicketMachine{
// Instance variables representing price, balance and total
private double price;
private double balance;
private double total;
// Parameterized constructor with price, balance and total as arguments
TicketMachine(double price, double balance, double total) {
this.price = price;
this.balance = balance;
this.total = total;
}
// Parameterized constructor with price and balance as arguments
TicketMachine(double price, double balance) {
this(price, balance, 0);
}
// Default constructor
TicketMachine() {
this(0,0,0);
}
// Getter method for private field price
public double getPrice() {
return price;
}
// Setter method for private field price
public void setPrice(double price) {
this.price = price;
}
// Getter method for private field balance
public double getBalance() {
return balance;
}
// Setter method for private field balance
public void setBalance(double balance) {
this.balance = balance;
}
// Getter method for private field total
public double getTotal() {
return total;
}
// Setter method for private field price
public void setTotal(double total) {
this.total = total;
}
// Prints the ticket machine info
public void printTicketMachineInfo() {
System.out.println(this);
}
// String representation of object
// Overrides toString method of Object class
@Override
public String toString() {
return "Ticket Machine information:\nPrice = " + price + "\nBalance = " + balance +
"\nTotal = " + total + "\n";
}
}
// Driver class
public class Test {
public static void main(String[] args) {
TicketMachine obj1 = new TicketMachine(10, 20, 30);
TicketMachine obj2 = new TicketMachine(100, 200);
TicketMachine obj3 = new TicketMachine();
obj1.printTicketMachineInfo();
obj2.printTicketMachineInfo();
obj3.printTicketMachineInfo();
}
}