In: Computer Science
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.