Question

In: Computer Science

Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...

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.

Solutions

Expert Solution

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();
        }
}

Related Solutions

PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A...
PUT IN JAVA PROGRAMMING The Stock class: Design a class named Stock that contains: • A string data field named symbol1 for the stock’s symbol. • A string data field named name for the stock’s name. • A double data field named previousClosingPrice that stores the stock price for the previous day. • A double data field named currentPrice that stores the stock price for the current time. • A constructor that creates a stock with the specified symbol and...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following...
PUT IN JAVA PROGRAMMING The StockB class: Design a class named StockB that contains the following properties: • Name of company • Number of shares owned • Value of each share • Total value of all shares and the following operations: • Acquire stock in a company • Buy more shares of the same stock • Sell stock • Update the per-share value of a stock • Display information about the holdings • The StockB class should have the proper...
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle....
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area of...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT