Java Programming Examples - Ticket Booking, Printing, Polymorphism, and Static Keywords

Summary

This document provides several Java code examples demonstrating different programming concepts. It includes examples for ticket booking using final variables, printer implementations with methods, polymorphism, and static keywords with multiple ticket booking scenarios. These examples are useful for learning Java programming.

Full Transcript

**[\*\* FinalVariableExample.java\*\*]** package javaPrjChap1; import java.util.Scanner; /\*\* \* This class is used to understand the use of final keyword with instance and local variables. \* We use the example of a ticket booking window. \*/ public class FinalVariableExample { public fina...

**[\*\* FinalVariableExample.java\*\*]** package javaPrjChap1; import java.util.Scanner; /\*\* \* This class is used to understand the use of final keyword with instance and local variables. \* We use the example of a ticket booking window. \*/ public class FinalVariableExample { public final int TOTAL\_TICKETS; //we can skip initializing final variable here. //or we can use the following syntax which initializes the final variable at the time of declaration // public final int totalTickets=200; public int tktsAvailable=0; /\*\* \* The class constructor is used to initialise the final instance variable, TOTAL\_TICKETS \* \@param totTkc \*/ public FinalVariableExample(int totTkc) { //initialising a final variable. this.TOTAL\_TICKETS = totTkc; } /\*\* \* This method takes the day of the week from the user and sets the no of available tickets for sale on \* that day. \* Monday,Tuesday,Wednesday,Thursday - 150 \* Friday - ALL TICKETS \* Saturday, Sunday - 175 \*/ public void setAvailableTkts(String wkDy) { //Depending on the day of the week, we set the available tickets count. For this we are //using switch case implementation of case list and arrow case as per JDK21 version switch(wkDy) { case \"Mon\",\"Tue\",\"Wed\",\"Thur\"-\> { tktsAvailable = 150; } case \"Fri\"-\> { tktsAvailable = 200; } case \"Sat\",\"Sun\"-\> { tktsAvailable = 175; } default-\> tktsAvailable = 0; } } public boolean bookTickets(final int noOfTkts) { final double TKTPRC =150.50; //assign some ticket price to the local variable. double tktAmount=0.0; //check if the noOfTkts are less than the total tickets. proceed with the booking then. else return false if(noOfTkts \< tktsAvailable) { tktAmount = TKTPRC\*noOfTkts; System.out.println(\"Please pay: \"+tktAmount+\" at the ticket counter.\"); //updating available tickets count. tktsAvailable = tktsAvailable - noOfTkts; } else return false; return true; } public static void main(String arg\[\]) { FinalVariableExample fnVrObj = new FinalVariableExample(120); fnVrObj.setAvailableTkts(\"Mon\"); System.out.println(\"Available tickets are:\"+ fnVrObj.tktsAvailable); if(fnVrObj.bookTickets(100)) System.out.println(\"Tickets are booked!! Please pay the amount\"); else System.out.println(\"Tickets could not be booked. Better luck next time\"); System.out.println(\"Available tickets are:\"+ fnVrObj.tktsAvailable); if(fnVrObj.bookTickets(70)) System.out.println(\"Tickets are booked!! Please pay the amount\"); else System.out.println(\"Tickets could not be booked. Better luck next time\"); } } **[\*\*Printer.java\*\*\ ]**\ package javaPrjChap1; import java.util.Scanner; public class Printer { String model; String location; String choice; public void print() { System.out.println(\"I will print all the pages in black and white\"); } public void print(boolean flag) { System.out.println(\"I will print all the pages in colours!!\"); } public void printChoice() { System.out.println(\"Hello user! Enter Yes/yes if you want a colour print. Else enter No/no\"); Scanner sc = new Scanner(System.in); choice = sc.next(); if(choice.equalsIgnoreCase(\"Yes\")) { //call print() for colour copies print(true); } else if(choice.equalsIgnoreCase(\"No\")) { //call print for black and white print(); } else System.out.println(\"You have entered some incorrect input choice.\"); } } **[\*\*PolymorphismDemo.java\*\*]** package pkgPolymorphism; import java.util.\*; public class PolymorphismDemo { public static void main(String ar\[\]) { String chc; //create object of printer class Printer prtntObj = new Printer(); System.out.println(\"Enter YES if you want a colour print else enter NO\"); Scanner sc = new Scanner(System.in); chc = sc.next(); if(chc.equalsIgnoreCase(\"YES\")) { //this is when the user wants a colour print prtntObj.printPaper(true); } else if(chc.equalsIgnoreCase(\"NO\")) { //thisis when the user wants a black and white print prtntObj.printPaper(); } else System.out.println(\"Your input does not match\"); } }//end of class **[\*\*StaticExample.java\*\*]** package chap1; import java.util.Scanner; /\*\* \* This class is used to understand the working of static keyword with a variable using multiple ticket \* booking windows scenario \*/ public class StaticExample { public final int TOTAL\_TICKETS = 200; public int tktsAvailable=0; /\*\* \* This method takes the day of the week from the user and sets the no of available tickets for sale on \* that day. \* Monday,Tuesday,Wednesday,Thursday - 150 \* Friday - ALL TICKETS \* Saturday, Sunday - 175 \*/ public void setAvailableTkts(String wkDy) { //Depending on the day of the week, we set the available tickets count. For this we are //using switch case implementation of case list and arrow case as per JDK21 version switch(wkDy) { case \"Mon\",\"Tue\",\"Wed\",\"Thur\"-\> { tktsAvailable = 150; } case \"Fri\"-\> { tktsAvailable = 200; } case \"Sat\",\"Sun\"-\> { tktsAvailable = 175; } default-\> tktsAvailable = 0; } } public boolean bookTickets(final int noOfTkts) { final double TKTPRC =150.50; //assign some ticket price to the local variable. double tktAmount=0.0; //check if the noOfTkts are less than the total tickets. proceed with the booking then. else return false if(noOfTkts \< tktsAvailable) { tktAmount = TKTPRC\*noOfTkts; System.out.println(\"Please pay: \"+tktAmount+\" at the ticket counter.\"); //updating available tickets count. tktsAvailable = tktsAvailable - noOfTkts; } else return false; return true; } public static void main(String ar\[\]) { StaticExample stExObj1 = new StaticExample(); StaticExample stExObj2 = new StaticExample(); StaticExample stExObj3 = new StaticExample(); stExObj1.setAvailableTkts(\"Mon\"); stExObj2.setAvailableTkts(\"Mon\"); stExObj3.setAvailableTkts(\"Mon\"); //invoke booking method on all the three objects. print the total tickets System.out.println(\"Available tickets are:\"+ stExObj1.tktsAvailable); if(stExObj1.bookTickets(100)) System.out.println(\"Tickets are booked!! Please pay the amount\"); else System.out.println(\"Tickets could not be booked. Better luck next time\"); System.out.println(\"Available tickets are:\"+ stExObj1.tktsAvailable); System.out.println(\"Available tickets are:\"+ stExObj2.tktsAvailable); if(stExObj2.bookTickets(100)) System.out.println(\"Tickets are booked!! Please pay the amount\"); else System.out.println(\"Tickets could not be booked. Better luck next time\"); System.out.println(\"Available tickets are:\"+ stExObj2.tktsAvailable); System.out.println(\"Available tickets are:\"+ stExObj3.tktsAvailable); if(stExObj3.bookTickets(100)) System.out.println(\"Tickets are booked!! Please pay the amount\"); else System.out.println(\"Tickets could not be booked. Better luck next time\"); System.out.println(\"Available tickets are:\"+ stExObj3.tktsAvailable); } } **[\*\*ThisExample.java\*\*]** package javaPrjChap1; public class ThisExample { int i,j; public ThisExample() { this(0,0); //i=0; //j=0; } public ThisExample(int val) { this(val,val); //i=j=val; } public ThisExample(int v1,int v2) { i=v1; j=v2; } public void showValues(){ this.showValues(\"both\"); } public void showValues(String s) { System.out.println(\"i is\...\"+i); System.out.println(\"j is\...\"+j); } public static void main(String arg\[\]) { ThisExample thsExObj = new ThisExample(); //ThisExample thsExObj1 = new ThisExample(10); //ThisExample thsExObj2 = new ThisExample(12,13); thsExObj.showValues(); //thsExObj1.showValues(); //thsExObj2.showValues(); } }

Use Quizgecko on...
Browser
Browser