Notes
Notes - notes.io |
/**
* CheckEventPass is about checking the number of tickets assigned ticket ID's.
* Topic: Java Error Handling and Assertions.
*/
public class CheckPassEvent {
public static void main(String[] args) {
//TODO 1. Declare an integer for the number of children attending the event
//TODO 2. Declare an integer for the number of adults attending the event.
//TODO 3. Declare an integer for the number of tickets.
//TODO 4. Declare a single dimensional integer array for ticket ID's.
//TODO 5. Declare an integer average visitor.
//TODO 6. Declare an integer no of days and initialize it with zero.
//TODO 7. Initialize the number of children to 2.
//TODO 8. Initialize the number of adults to 6.
//TODO 9. Initialize the number of tickets with the sum of number of children and number of adults.
//TODO 10. Initialize the ticket ID array with number of tickets as the size.
//Note:Try changing value of "ticketID" index from [noOfTickets] to [noOfTickets-1] in next run and see the output.
int noOfChildren;
int noOfAdults;
int noOfTickets;
int ticketID[];
int averageVisitor;
int noOfDays = 0;
noOfChildren = 2;
noOfAdults = 6;
noOfTickets = noOfChildren + noOfAdults;
ticketID = new int[noOfTickets];
//TODO 11. Declare a try/catch/finally block.
//TODO 11.a In try block, print the last element of ticketID array.
//TODO 11.b In try block, calculate the average visitor value by dividing number of tickets by no of days.
//TODO 11.c Surround the try block with catch block by passing ArithmeticException and ArrayIndexOutOfBoundsException as parameter.
//TODO 11.d In catch block, print the type of exception thrown by printing the value of exception parameter
//TODO 11.e In finally block, print the message “Executing the finally block.”.
//TODO 11.f In finally block, set the number of days to the value 2.
//TODO 11.g In finally block, calculate the average visitor value by dividing number of tickets by no of days.
//TODO 11.h In finally block, print the value of average visitor.
try{
System.out.print("Access element of ticket id := ");
System.out.println(ticketID[noOfTickets-1]);
System.out.print("Exception thrown :");
averageVisitor = noOfTickets / noOfDays;
}
catch(ArithmeticException e){
System.out.println(e);
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println(e);
}finally{
System.out.println("Executing the finally block.");
noOfDays = 2;
averageVisitor = noOfTickets / noOfDays;
System.out.println("Average visitor per day: " + averageVisitor);
}
//Assign IDs to tickets
//TODO 12.Declare an integer for pass id and initialize it with zero.
int passID = 0;
/*TODO 13. Use for loop to populate the ticket ID array.
* a. Declare an integer i for the loop index and assign it value zero inside the loop .
* b. Use 'No of tickets' as upper bound.
* c. Increment the loop index 'i' by 1.
*/
for(int i=0; i < noOfTickets-1; i++){
passID = i + 1;
ticketID [i] = passID;
}
//TODO 13.i.Inside the for loop, pass ID can be generated by adding 1 to the loop index.
//TODO 13.ii.Inside the for loop, assign pass id generated to the respective element of the ticket ID array .
// Verify all the tickets issued assigned with ticket ID's using assert.
//TODO 14. Declare an integer variable for the loop index and initialize with zero.
//TODO 15. Print “Verifying tickets assigned with ticket ID's” message to the console.
System.out.println("Verifying tickets assinged with ticket ID's");
//TODO 16. Loop through the ticketID array using a do..while loop with array size as the boundary.
int z =0;
do{
assert(ticketID[z] != 0):"Mismatch in issuance of ticket ID's";
System.out.println("Ticket ID: " + ticketID[z]);
z++;
}while(z < ticketID.length);
/*TODO 17. Inside the do..while loop
*i. Verify if value of each element of the ticket id array is not equal to zero using assert statement.
*ii. If the value of ticket id array element is equal to zero, print 'Mismatch in issuance of ticket ID's' to the console as an assertion error.
*iii. Print ticket ID value to console.
*iv. Increase the loop index and verify all the values in the array.*/
//TODO 18.Print 'Ticket ID's are assigned to total number of tickets issued.' to the console after exiting the do..while loop.
System.out.println("Ticket ID's are assigned to total number of tickets issued.");
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
