Question

In: Computer Science

Create a class Ticket that assigns a ticketNumber to each new ticket in order, starting with...

  • Create a class Ticket that assigns a ticketNumber to each new ticket in order, starting with 1000 and increasing consecutively (ie 1001, 1002 …).
  • It should have methods:
    • public Ticket(String ticketHolder)
    • public int getTicketNumber()
    • public String getTicketHolder()
    • public static int getTicketCount() // return number of Tickets created
  • Fill in the code needed for each of these methods.
  • Define whatever private variables needed to implement this.
  • You do not need to create additional public methods outside of those listed above.

Solutions

Expert Solution

Hello dear....

Here is the java program for the given task. I have also written comments for better understanding. Also attached images of output and code for comfort of reading. Hope you will like it.

Ticket.java

//Driver class to test the Ticket class methods
class Driver{
        //main method
        public static void main(String args[]){
                //creating two tickets with 2 different ticketHolders
                Ticket t1 = new Ticket("Uday");
                Ticket t2 = new Ticket("Kiran");

                //printing the returned value of our class functions of 2 Tickets
                System.out.println( t1.getTicketNumber() + "-" + t1.getTicketHolder() );
                System.out.println( t2.getTicketNumber() + "-" + t2.getTicketHolder() );
                //print number of tickets created
                System.out.println( "Number of Tickets created - " + t1.getTicketCount() );
                //creating another Ticket
                Ticket t3 = new Ticket("Jayanthi");
                //printing the returned value of our class functions of 3rd Ticket
                System.out.println( t3.getTicketNumber() + "-" + t3.getTicketHolder() );
                //print number of tickets created
                System.out.println( "Number of Tickets created - " + t1.getTicketCount() );

        }
}

//Creating class Ticket
public class Ticket{
        //intializing private varibles to store data
        private static int count = 0;
        private int ticketNumber = 1000;
        private String ticketHolder = "";
        //Ticket class construtor
        public Ticket(String ticketHolder){
                this.ticketHolder = ticketHolder;
                //assign ticket number according to ticket count
                ticketNumber += count;
                count++;
        }
        //method to return ticket number
        public int getTicketNumber(){
                return ticketNumber;
        }
        //method to return ticket holder name
        public String getTicketHolder(){
                return ticketHolder;
        }
        //method to return ticket count
        public static int getTicketCount(){
                return count;
        }

}

Output:

Code image:

Hope you understood the program. If you have any doubts ask me in comments.

If you like my work, please give me a like and feedback. That helps me a lot. Thank you.

All the best.


Related Solutions

android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called DataBaseManager as in Lecture 5 and create a database table in SQLite, called StudentInfo. The fields for the StudentInfo table include StudentID, FirstName, LastName, YearOfBirth and Gender. Include functions for adding a row to the table and for retrieving all rows, similar to that shown in lecture 5. No user interface is required for this question, t -Continuing from , follow the example in...
Create a Class to contain a customer order Create attributes of that class to store Company...
Create a Class to contain a customer order Create attributes of that class to store Company Name, Address and Sales Tax. Create a public property for each of these attributes. Create a class constructor without parameters that initializes the attributes to default values. Create a class constructor with parameters that initializes the attributes to the passed in parameter values. Create a behavior of that class to generate a welcome message that includes the company name. Create a Class to contain...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
1. when you create a class by making it inherit from another class, the new class...
1. when you create a class by making it inherit from another class, the new class automatically, contains the data fields and _____ of the original class a. fonts b. methods c. class names d. arrays 2. if a programming language does not support ________, the language is not considered object oriented a. syntax b. applets c. loops d. polymorphism 3. when you create a class and do not provide a ______, java automatically supplies you with a default one...
Create a new Class Payroll that contains our ClassEmployee.
JAVACreate a new Class Payroll that contains our ClassEmployee.we are adding a copy constructor to our Employee Classwe are adding a toString() method to our Employee Classour company has grown so we can no longer avoid deducting taxes from our workers, so we moved the PrintPayStub() method to our newPayroll Class.we will need 2 static members to our Payroll Class to help us compute State and Federal tax deductions from our Employee PaychecksDelving deeper into the concepts of Object Oriented...
Suppose each day (starting on day 1) you buy one lottery ticket with probability 1/2; otherwise,...
Suppose each day (starting on day 1) you buy one lottery ticket with probability 1/2; otherwise, you buy no tickets. A ticket is a winner with probability ? independent of the outcome of all other tickets. Let ?? be the event that on day ? you do not buy a ticket. Let ?? be the event that on day ?, you buy a winning ticket. Let ?? be the event that on day ? you buy a losing ticket. a....
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT