Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Help with assignment?

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Help with assignment?

    Hey!

    So I'm a beginner at java and I'm working on this assignment and kinda need help.. I have tried attempting it and attached it here..

    I just kinda get stuck when it comes to passing values into constructors, using main method or static method functionality. In theory i kind of understand how it work but when i type it, it's totally different! I have to have a junit test too but i guess i could do that in the end.

    I have attached the assignment. So, if you guys can give me any help on how to proceed with this; I would greatly appreciate that!

    Sincerely,
    Anna




     
    public class Flight {
     
     
    int flight_number, capacity, number_of_seats_left;
     
    String origin, destination;
     
    String departure_time;
     
    double original_price;
     
     
        //constructor with 7 arguments
     
     
    public  Flight (int flight_number, String origin, String destination, String departure_time, int capacity, int number_of_seats_left, double original_price) {
     
     
    //intializing all the instance variables
     
    this.flight_number=flight_number;
     
    this.origin=origin;
     
    this.destination=destination;
     
    this.departure_time=departure_time;
     
    this.capacity=capacity;
     
    this.number_of_seats_left=number_of_seats_left;
     
    this.original_price=original_price;
     
     
        //throwing the exception using if else statements
     
     
    if (origin.equals(destination)) {
     
    throw new IllegalArgumentException("Origin and Destination should be diffrent");
     
    }
     
    else {
     
    number_of_seats_left = capacity-1;
     
    }
     
    }
     
     
    public boolean bookASeat() {
     
    if (number_of_seats_left>0) 
     
     {
        number_of_seats_left=number_of_seats_left-1;
     
        return true;
     
       }
     
    else {
     
        return false;
     
       }
     
     
    }
     
    @Override
    public String toString() {
     
    return ("Flight " + flight_number + "\n" + origin + " to " + destination + "\nDeparture Time " + departure_time + "\nPrice " + original_price);
     
     
     
     
     
            }
     
     
     
        //getters for the instance variables    
     
     
    public int getFlight_Number() {
     
    return flight_number; }
     
     
    public String getOrigin() {
     
    return origin; }
     
     
    public String getDestination() {
     
    return destination; }
     
     
    public String getDeparture_Time(){
     
    return departure_time; }
     
     
    public int getCapacity() {
     
    return capacity; }
     
     
     
    public int getNumber_Of_Seats_Left() {
     
    return number_of_seats_left; }
     
     
     
    public double getOriginal_Price(){
     
    return original_price; }
     
     
     
     
    public void setFlight_Number(int flight_number) {
     
    this.flight_number=flight_number; }
     
     
    public void setOrigin(String origin) {
     
    this.origin=origin; }
     
     
    public void setDestination(String destination) {
     
    this.destination=destination; }
     
     
    public void setDeparture_Time(String departure_time) {
     
    this.departure_time=departure_time; }
     
     
    public void setCapacity(int capacity) {
     
    this.capacity=capacity; }
     
     
    public void setNumber_Of_Seats_Left(int number_of_seats_left) {
     
    this.number_of_seats_left=number_of_seats_left; }
     
     
    public void setOriginal_Price(double original_price) {
     
    this.original_price=original_price; }
    public class Ticket {
     
        Passenger passenger = new Passenger ();
     
        Flight flight = new Flight ();
     
        double price;
     
        static int number;
     
     
        public Ticket (Passenger p, Flight flight, double price){
     
        passenger = p;
     
        this.flight=flight;
     
        this.price= price;
     
     
        }
     
                public double getPrice(){
     
                return price;
     
                }
     
                   public int getNumber(){
     
                   return number;
                }     
     
     
                   public void setPrice(double price){
     
                   this.price=price;
                   }
     
     
                   public void setNumber(int number){
     
                   this.number=number;
                   }
     
     
     
    }
     
     
     
    public abstract class Passenger {
     
     
     
        String name;
     
        int age;
     
        public Passenger (int age, String name){
     
        this.age=age;
     
        this.name=name;
     
        }
     
     
        protected abstract double applyDiscount (double p);
     
     
             public String getName(){
     
             return name;
             } 
     
     
     
             public int getAge(){
     
             return age;
             }
     
    public void setName(String name){
     
     
        this.name=name;
        }
     
     
     
    public void setAge(int age){
     
        this.age=age;
    }
     
     
    }
     
     
     
     
     
    public class NonMember extends Passenger {
     
     
     
        //Also needs to implement the abstract method from the passenger class
     
     
        public double applyDiscount(double p){
     
     
        }
     
    }
     
     
     
    public class Member extends Passenger{
     
     
        int yearsofMembership;
     
        // NEED to override any abstract method present in the parent class
     
        @Override
         public double applyDiscount(double p){
     
     
     
             if (yearsofMembership>5)
     
                 p=0.5;
     
             else if (yearsofMembership > 1&& yearsofMembership<=5)
     
                 p=0.9;
     
             else p=1;
     
     
        }
     
     
    }
     
     
     
     
    public class Manager {
     
     
     
     
    public static void main (String[] args) {
     
     
     
    Flight f1 = new Flight (1200, "Toronto", "Kolkata", "2300", 100, 5, 1500.08);
        System.out.println(f1.toString());
     
     
    }
     
     
    }
    Attached Files Attached Files


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with assignment?

    Please don't post duplicate posts. I see you've improved this one, so I'm going to leave it and close the other.

    Now, can you describe what you mean by "kinda get stuck when it comes to passing values into constructors, using main method or static method functionality?" Let's pick one at a time.

    What don't you understand about passing values to a constructor? Show where you've tried that and got results other than what you expected or hoped for. Provide a sample run, if possible. If you're getting errors, post those.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with assignment?

    Yes please do that! I tried deleting the other one, but couldn't figure that out!

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with assignment?

    I was updating while you were typing. Review my post and answer my question.

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with assignment?

    So in the main class there are quite a few methods, how would i populate an array of flights in the createFlights() method and the arguments in the rest of the methods; are they suppose to set the parameters to some value? also, polymorphism in bookSeat method, why is that used here?

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author user
     */
    public class Manager {
     
     
          String[] flight= {};
     
          String[] IssuedTickets = {};
     
          public void createFlights(){
     
     
     
     
          }
     
    public void displayAvailableFlights (String origin, String destination){
     
     
     
    }
     
     
    public Flight getFlight(int flightNumber){
     
     
     
     
    }
     
    public void bookSeat(int flightNumber, Passenger p){
     
     
    }
     
     
     
     
     
    public static void main (String[] args) {
     
     
    Flight f1 = new Flight (1200, "Toronto", "Kolkata", "2300", 100, 5, 1500000);
        System.out.println(f1.toString());
     
     
    }
     
     
     
     
     
    }

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with assignment?

    Again, you ask several questions when I asked you to focus on one, specifically about constructor parameters. You didn't mention constructor parameters. Slow down, focus, and ask specific questions. This sentence, "how would i populate an array of flights in the createFlights() method and the arguments in the rest of the methods," is pretty much gibberish.

  7. #7
    Junior Member
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with assignment?

    Ok so I think constructor parameters are there to assign a value to the variables, right? Or can a constructor do something more than that?

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with assignment?

    You're essentially correct, but you've mixed 'constructor' and 'constructor parameters' together. The constructor is used to create an instance of, or an object from, the class, and the parameters, if any, can be used to pass values to the object being created. There can be multiple constructors, some that accept parameters and one that doesn't. The values passed to the object being created as parameters can be used in a variety of ways.

    If you're ready, let's move to your next question. Either the one below from your first post, or pick another:
    I just kinda get stuck when it comes to . . . using main method or static method functionality.
    What do you mean by "using main() or static method functionality?" Give an example of what confuses you or gets you stuck. Perhaps the example can be tied to your later post about assigning values to an array.

  9. #9
    Junior Member
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with assignment?

    Thanks so much! That clears it up.

    So what confuses me is implementing the methods. In the main, I have createFlights() and it should populate the array of flights. There are other bunch of other methods that need implementation. I just get stuck on where to start. How should I go about implementing any method? We can start with createFlights(). I'll try the other ones.

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with assignment?

    Okay, let's go. I looked through your code see that you've started createFlights() by writing the method signature. Have gone any further than that? If so, post updated code. If not, think about what the method should do. You've said: "createFlights() . . . should populate the array of flights." I see where you've declared the array flight[], though incorrectly. Instead, declare the array as containing Flight objects without a specific size:

    Flight[] flights;

    And I recommend you change the name of the array to flights as I did to make its purpose more clear.

    How do you fill an array? That's what should happen in the method createFlights(). Can you take a stab at that and post your code?

  11. #11
    Junior Member
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with assignment?

    Well, usually we use for method to fill the array right? Well, what is the type of the array we created as Flight[] flight (i.e String or Int)?
    So, should I be using for loop here?

  12. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with assignment?

    Yes, a loop.

    The flights array, declared as:

    Flight[] flights;

    holds Flight objects, not Strings or ints. Isn't that what you needed? An array of Flight objects?

  13. #13
    Junior Member
    Join Date
    Jan 2014
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with assignment?

    Ok. So, I'm filling the array with flight objects? How should I do that in the for loop?

  14. #14
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with assignment?

    To put one Flight object in the first element of an array, flights[], you'd do something like:

    flights[0] = new Flight(); // using the appropriate Flight() constructor

    How could you build a loop to fill the whole flights[] array?

Similar Threads

  1. I need some help with an assignment.
    By Rain_Maker in forum What's Wrong With My Code?
    Replies: 16
    Last Post: September 14th, 2013, 09:24 PM
  2. ASSIGNMENT HELP
    By ThePrince in forum Java Theory & Questions
    Replies: 33
    Last Post: April 21st, 2013, 04:04 PM
  3. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  4. Assignment please Help :(
    By pagalas07 in forum Collections and Generics
    Replies: 3
    Last Post: March 30th, 2011, 08:52 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM