Question

1) (10 points) Create a new Java project, name it Quiz02. Create a java class Traveler that has four data members, String nc. If the availability is equal to OR more than the requested tickets (traveler + his noOfDependens) then, accept this reserv

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code for the above question

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date; //import Date Class

//Traveler class
class Traveler
{
        private String name;                    //variable to store name
        private Date bdate;                             //variable to store DOB
        private int id;                                 //variable to store id
        private int noOfDependens;              //variable to store no of dependets
        
        //constructor
        public Traveler(String n, Date d, int id, int NOD)
        {
                this.setName(n);
                this.setBdate(d);
                this.setId(id);
                this.setNoOfDependens(NOD);
        }

        //getter and setters
        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public Date getBdate() {
                return bdate;
        }

        public void setBdate(Date bdate) {
                this.bdate = bdate;
        }

        public int getId() {
                return id;
        }

        public void setId(int id) {
                this.id = id;
        }

        public int getNoOfDependens() {
                return noOfDependens;
        }

        public void setNoOfDependens(int noOfDependens) {
                this.noOfDependens = noOfDependens;
        }
        
        //function to print details of travleler
        public void tostring()
        {
                
                SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");  
            String DOB = formatter.format(this.bdate);  
                String fin =  "Name: " + this.name + "\nDate of Birth: " + DOB + "\nID: " + String.valueOf(this.id) + "\nNo of Dependents: " + String.valueOf(this.noOfDependens);
                System.out.println(fin);
        }
        
}

//Flight class

class Flight
{
        private String number;                          //variable to store flight no
        private String destination;                     //variable to store destination
        private int capacity;                           //variable to store flight capacity
        ArrayList<Traveler> travelers;            //list of travelers
        
        //constructor
        public Flight(String n, String des, int cap)
        {
                this.number = n;
                this.destination = des;
                this.capacity =cap;
                travelers = new ArrayList<Traveler>();
        }
        
        //constructor
        public String getNumber() {
                return number;
        }
        public void setNumber(String number) {
                this.number = number;
        }
        public String getDestination() {
                return destination;
        }
        public void setDestination(String destination) {
                this.destination = destination;
        }
        public int getCapacity() {
                return capacity;
        }
        public void setCapacity(int capacity) {
                this.capacity = capacity;
        }
        
        //function to add traveler
        public void addTraveler(Traveler traveler)
        {
                //variable to store total passengers
                int totaltraveler = 0;
                for(int i=0;i<this.travelers.size();i++)
                {
                        Traveler trav = this.travelers.get(i);
                        totaltraveler += trav.getNoOfDependens() +1;
                }
                //variable to count remaing seats
                int remaining = this.capacity - totaltraveler;
                //if seats availabe
                if(remaining>= traveler.getNoOfDependens()+1)
                {
                        this.travelers.add(traveler);
                        System.out.println("Flight Capacity: " + this.capacity + "\nNo. of current travelers: " + totaltraveler +"\nFlight availability: "+ remaining);
                        System.out.println("=======================\nAccepted reservation.");
                        //variable to store no of tickkets to be booked
                        int ttravelers = traveler.getNoOfDependens()+1;
                        System.out.println("Total No. of reserved tickets are: " + ttravelers);
                        
                
                }
                else
                {
                        System.out.println("This reservation is not accepted. Total No of requested tickets are:"+String.valueOf(traveler.getNoOfDependens()+1)+ "while the availability is: "+ String.valueOf(remaining));
                }
                
        }
        //print details of flight
        public void tostring()
        {
                System.out.println("***** Flight Details*****");
                System.out.println("=========================");
                String f =  "Flight No.: "+ this.number + "\nFlight Destination: " + this.destination + "\nFlight Capacity: " + String.valueOf(this.capacity);
                System.out.println(f+ "\n");
                System.out.println("List of Travelers:");
                for(int i=0;i<this.travelers.size();i++)
                {
                        
                        Traveler trav = this.travelers.get(i);
                        trav.tostring();
                }
        }
        
}

//AppSytem class
public class AppSystem
{
        //main function
    public static void main(String args[]) throws ParseException
    {
        
        SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy");
        Date d1 = sd.parse("17/05/1950");
        Date d2 = sd.parse("2/12/1975");
        Date d3 = sd.parse("13/03/1981");
        Date d4 = sd.parse("9/10/1979");
        Date d5 = sd.parse("25/05/1963");
        //initializing travelers
                Traveler t1 = new Traveler("Traveler1", d1, 1950517, 7);
        Traveler t2 = new Traveler("Traveler2",d2, 1975122, 5);
        Traveler t3 = new Traveler("Traveler3",d3, 1981313, 3);
        Traveler t4 = new Traveler("Traveler4",d4, 1979109, 4);
        Traveler t5 = new Traveler("Traveler5",d5, 1950517, 6);
        
        //initializing flight
        Flight f1 = new Flight("QAR246","Kuwait", 70);
        Flight f2 = new Flight("Kuw579", "Doha", 60);

        //adding travelers
        f1.addTraveler(t1);
        //print flight details
        f1.tostring();
    }

}

The output of the code:

Problems @ Javadoc e Declaration Console X Coverage <terminated > AppSystem (Java Application] C:\Program Files\Java jre 1.8.

Add a comment
Know the answer?
Add Answer to:
1) (10 points) Create a new Java project, name it "Quiz02". Create a java class Traveler...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • create a java class named Person that has 3 attributes: name, age and weight. include a...

    create a java class named Person that has 3 attributes: name, age and weight. include a default and parametrize constructor. include all setters and getters needed and a method that is used to display the information about the person

  • Create a java project and create Student class. This class should have the following attributes, name...

    Create a java project and create Student class. This class should have the following attributes, name : String age : int id : String gender : String gpa : double and toString() method that returns the Student's detail. Your project should contain a TestStudent class where you get the student's info from user , create student's objects and save them in an arralist. You should save at least 10 student objects in this arraylist using loop statement. Then iterate through...

  • PART 1: GETTING STARTED 1. Create a new Java Project. 2. Create a new Tree class...

    PART 1: GETTING STARTED 1. Create a new Java Project. 2. Create a new Tree class and enter this code. You do not have to type in the comments public class Tree { private String name; private int age; private bogJsAn evergreen; // true if evergreen // false if deciduous //CONSTRUCTORS public Tree( { name 0; evergreen = true; age } // you fill this one public Tree (String n, jnt a, bgalean e) //PUT YOUR CODE HERE } //...

  • java with Part 1 Write the Java classes given with their fields. Implement their constructors, toString...

    java with Part 1 Write the Java classes given with their fields. Implement their constructors, toString methods, getters and setters. Then create three Ticket instances. • Airport: String name, String city, String country • Route: Airport departure , Airport destination • Date: int day, int month, int year (Day must be between 0-31, month must be between 0-12, year must be between 2020-2025; please consider this when you're implementing setters and constructors). • ClockTime: int hour, int minute (Hour must...

  • using CSCI300 java Given the following UML Class Diagram Club_Card # card_num : String # name...

    using CSCI300 java Given the following UML Class Diagram Club_Card # card_num : String # name : String # age: int # year: int + Club_Card (all parameters) + Setters & Getters + toString() : String + equals(x: Object): boolean [10 pts] Define the class Club_Card, which contains: 1. All arguments constructor which accepts all required arguments from the main and instantiate a Club_Card object. 2. Set & get method for each data attribute. 3. A toString() method which returns...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • [JAVA] Program: Design a Ship class that the following members: A field for the name of...

    [JAVA] Program: Design a Ship class that the following members: A field for the name of the ship (a string) o A field for the year the the ship was built (a string) o A constructor and appropriate accessors and mutators A toString method that displays the ship's name and the year it was built Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers...

  • Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a...

    Write the following program in Java. Create an abstract Vehicle class - Vehicle should have a float Speed and string Name, an abstract method Drive, and include a constructor. Create a Tesla class - Tesla inherits from Vehicle. - Tesla has an int Capacity. - Include a constructor with the variables, and use super to set the parent class members. - Override Drive to increase the speed by 1 each time. - Add a toString to print the name, speed,...

  • Create a NetBeans project called "Question 1". Then create a public class inside question1 package called Author according to the following information (Make sure to check the UML diagram)

    Create a NetBeans project called "Question 1". Then create a public class inside question1 package called Author according to the following information (Make sure to check the UML diagram) Author -name:String -email:String -gender:char +Author(name:String, email:String, gender:char) +getName():String +getEmail):String +setEmail (email:String):void +getGender():char +tostring ):String . Three private instance variables: name (String), email (String), and gender (char of either 'm' or 'f'): One constructor to initialize the name, email and gender with the given values . Getters and setters: get Name (), getEmail() and getGender (). There are no setters for name and...

  • With Java Language: In this assignment, you are to create a class named Payroll. In the...

    With Java Language: 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) .İd: 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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT