Skip to main content

Post content has been hidden

To unblock this content, please click here

Doughnut
Beginner June 2008

Any Java programmer people about?

Doughnut, 10 July, 2009 at 12:35 Posted on Off Topic Posts 0 27

This is nearly over, I promise ?

27 replies

Latest activity by Doughnut, 11 July, 2009 at 14:24
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content

    Bouncing, sorry, but I am completely useless at this and banging my head against the desk.

    • Reply
  • Dr Svensk Tiger
    Beginner
    Dr Svensk Tiger ·
    • Report
    • Hide content

    Hey there, my H is a programmer, he says his Java is a bit rusty but ask away!

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content

    Oh thank you! I'm a beginner.

    This assignment is about coach trips - I've got a Trip class with stuff about destination, price and an ArrayList to store Bookings, from a Booking class. In my Booking class I've got an array to store seats - people must be able to book a specific seat. So, I need to get my seat bookings into a particular index in the array. Only I haven't got a fcuking clue how to do this ?

    To complicate matters further, I must put an A in the array if it's an adult and a C if it's a child. There may be several seats per booking IYSWIM.

    Any help is greatly appreciated. I am losing it a bit here!

    • Reply
  • Dr Svensk Tiger
    Beginner
    Dr Svensk Tiger ·
    • Report
    • Hide content

    If I understand correctly, your array is a set size; the number of seats on the coach? If so, simply add an A or C at that index to say it's booked (default it to zero). You can then write a method to give seat numbers and adult/child. Likewise, a method could be written to set the seats.

    Problem comes when you do another booking - you'll need to go through all your bookings, and check for empty seats before adding any more. You could simply start adding at 0 to make life easier ;-)

    Another thought is to use a Vector class, rather than an array and simply append A/C's to the end.

    You could also create a Seat class and have an array of those - they could then handle the booking?

    Does that help at all or I have simply confused you more?

    SvenskTiger's Better Half

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content
    View quoted message

    Thank you for your help.

    Yes the array is set to 32, the number of seats on the coach. I don't know the code to write is the problem - I know what to do but not how to do it! Simply add an A or C at that index - how do I do this?? Sorry, I really am a beginner, I'm 5 weeks into a from-scratch part-time course.

    How do I write the method to give seat numbers and A/C? I guess I do a 'for' loop and System.out.println?

    I don't know what a vector is I'm afraid.

    • Reply
  • SamanthaJane81
    Beginner July 2007
    SamanthaJane81 ·
    • Report
    • Hide content

    Hey,

    J said let him think about it for a bit - he knows how to do it but doesnt want to do it for you, but wants to hlep you get there iyswim.

    Can I email you?

    x

    • Reply
  • Dr Svensk Tiger
    Beginner
    Dr Svensk Tiger ·
    • Report
    • Hide content

    Okee dokee. Something like (excuse my rusty Java - my syntax might be a bit off):

    Class Booking { String seats = new String[32]; ... // returns the seat number booked, or -1 if the coach is full int addBooking(String PassengerType) { // loop over seats and check if they're empty for (int i=0; i<seats.length(); i++) { if (seats[ i ] ==0) seats[ i ] = PassengerType return i } } return -1; }

    You could then do something similar to print out the seats

    Does that help more?

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content

    Thanks SJ. I feel like I mostly get it there's just a link missing between what I want to do and how I do it - a big link I guess! Do you have my email address?

    • Reply
  • SamanthaJane81
    Beginner July 2007
    SamanthaJane81 ·
    • Report
    • Hide content

    Think so, check your email....

    • Reply
  • SamanthaJane81
    Beginner July 2007
    SamanthaJane81 ·
    • Report
    • Hide content

    Email bounced back so here goes...........

    // Defines the type of seat
    public enum SeatType { ADULT, CHILD }


    // Contains basic information about the trip and every booking.
    Class Trip ()
    {
    string destination,
    Booking bookings[], // Collection of bookings
    int seatsInCoach // Number of places available on the coach when empty.
    }

    // Contains basic booking info. Could be modified by creating a
    Passenger class.
    Class Booking()
    {
    string FirstName
    string LastName
    Seat Seats[] // Add a seat class for each passenger.
    }

    // Contains info about actual seat.
    Class Seat()
    {
    decimal price,
    SeatType seatType,
    int seatNumber // You may wish to reserve a specific seat?
    }

    Before you add the booking, verify that the number of already booked
    seats + this booking don't exceed the number of seats in the coach.

    This approach doesn't use arrays but uses collections instead. It is
    then expandable if necessary.

    The price should probably be per seat not per trip (which you suggested)

    I have also suggested you use an enum instead of a char/string(A or C)
    becuase it is better practice.

    I've also not written any validation for you. Not sure how
    complicated your end result should be?

    I've only given this a few mins thought so there may be design faults
    and I have never programmed in Java. You should get the idea though.

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content
    View quoted message

    Thank you! It makes more sense now I've swapped the lightbulbs for [ i ] ?

    Thanks so much.... I feel like I'm never going to 'get it'. I keep waiting for it to click... no clicking is taking place as yet!

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content

    Thanks SJ, that's great. I did get an email and I replied! The hotmail address you used too though isn't mine.

    Thanks again ?

    • Reply
  • Dr Svensk Tiger
    Beginner
    Dr Svensk Tiger ·
    • Report
    • Hide content
    View quoted message

    ST back again... if it makes you feel any better H says its mean of them to teach you Java as a first language. He says its not easy at all for beginners. And on the "clicking" point I've had the same problem, I've been making H write all my Perl programs for years now (with due credit of course) and have finally started learning it myself, it's taking a while! I made the mistake of going on a C++ course first, what a disaster! ?

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content

    It's evil I tell you! I'm doing it online, learning all alone at home. Our tutor hasn't taught Java to distance learners before, and at the start posted about how he was very concerned about it as it was going to be extremely difficult for people without programming experience to get it. Boy he wasn't wrong ?

    I want someone to come and talk to me about it and explain things to me. The only person I know who knows it isn't an entirely suitable tutor ?

    I have heard C++ is bad. What happened with the course?

    • Reply
  • Dr Svensk Tiger
    Beginner
    Dr Svensk Tiger ·
    • Report
    • Hide content

    Sounds like a nightmare! You can borrow H if you're in Oxford, I'm always loaning him out. It's so useful being married to a scientific programmer who specialises in High Performance Computing (he told me to say that ? ).

    Luckily my C++ course was only three afternoons and cost me an entire 8 quid (evidently Oxford uni is good for something at least). I can just about manage to add two numbers together, it's still gone on my CV though ? I did a Perl course recently and that was so much nicer, you can still do OOP with it too so it's useful for a lot of things. I also did Python and that was evil, I did not get it at all.

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content
    View quoted message

    I'm only in Swindon! ?

    This course is bloody expensive. I think I've chosen the wrong subject (Computer Science) as my main specialities are makeup (purchase and application of), going out boozing, buying and selling old cars and a wide knowledge of popular weight loss plans ?

    I did a bit of Pascal at uni, but the main reason I went was because our tutor was fit ? I passed that module though so I must somewhere, deep down, be able to program!

    • Reply
  • Dr Svensk Tiger
    Beginner
    Dr Svensk Tiger ·
    • Report
    • Hide content

    Didn't realise you were that close! Well if you have any further probs you should shout out, he can't complain too much, he's met some pretendy people from a really weird forum before ?

    Good luck with the rest of your course, and, seriously, let us know if you need real-life help, I could do with some new slippers ?

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content

    Thanks for pimping out your H for Java help, might take you up on that offer ? I have 6/7 more weeks of it, and it can only get worse ? I worked to the west of Oxford up until the end of last year.

    Good luck with your Perl.

    • Reply
  • Dr Svensk Tiger
    Beginner
    Dr Svensk Tiger ·
    • Report
    • Hide content

    Thanks, at least if I turn out to be useless at it I can keep making H do it!

    Do let us know if you need any help, I naturally assume you will be able to learn to program whilst drinking vast quantites of wine ?

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content
    View quoted message

    Funny you should say that, I had a bucketful last night and did no work, 2 glasses the night before and did little work so, erm, no I can't program and drink (yet!) I obviously need more practice ?

    • Reply
  • Dr Svensk Tiger
    Beginner
    Dr Svensk Tiger ·
    • Report
    • Hide content

    Yes, much more practice needed. It has the added bonus of making whatever you're doing seem sooooo much more interesting ? H took loads of (unflattering) pictures of me when I was frantically trying to finish my PhD thesis, every single one had a half empty glass of wine in the background ?

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content

    ?

    Until we get pissed program together, can I post some code for him to look over for me? I find I get stuck and make a sandwich/ cup of tea/ put a wash on/ straighten my hair/ go for a long bike ride and so every little bit takes me ages.

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content

    You might have gone to bed now, but I thought I'd try before I head off. This is my Trip class:

    import java.util.ArrayList;

    /**
    * Write a description of class Trip here.
    *
    * @author
    * @version v0.1 05.07.09
    */
    public class Trip
    {
    private double price;
    private String destination;
    private Date date;
    private String code;
    private ArrayList<Booking> b;

    /**
    * Constructor for objects of class Trip
    */
    public Trip(double tripPrice, String tripDestination, int d, int m, int y, String tripCode)
    {
    price = tripPrice;
    destination = tripDestination;
    date = new Date (d, m, y);
    code = tripCode;
    b = new ArrayList<Booking>();
    }

    /**
    * An example of a method - replace this comment with your own
    *
    * @param y a sample parameter for a method
    * @return the sum of x and y
    */
    public double getPrice ()
    {
    return price;
    }

    public String getDestination ()
    {
    return destination;
    }

    public Date getDate ()
    {
    return date;
    }

    public String getCode ()
    {
    return code;
    }

    public String toString ()
    {
    return ("Trip code: " + code +
    " Destination : " + destination +
    " Date " + date.getAsString() + "Price " + price);
    }

    public void addBooking (Booking b)// this bit is from John
    {

    //In here I need to copy into the appropriate place in the array of 32 seats
    //the details about who booked it and either A or C
    //b.add(newBooking); - I don't know how to write the main method which is addBooking!!
    }

    public void peopleBooked()
    {
    //JP said need filed headCount which gets incremented by the number of seats
    //being booked each time a booking is made
    }



    public double getTotalMoneyCollected()
    {
    double totalMoneyCollected = 0.00;
    for (int i = 0; i < bookings.size(); i++)
    {
    Booking thisBooking = bookings.get(i);
    totalMoneyCollected = totalMoneyCollected + thisBooking.getAdultsBooked() * price;
    totalMoneyCollected = totalMoneyCollected + thisBooking.getChildrenBooked() * (price/2);
    }
    return totalMoneyCollected;
    }

    }

    And this is my Booking class:

    public class Booking
    {
    private String name;
    private String telephone;
    private int [] childSeat;
    private int [] adultSeat;

    /**
    * Constructor for objects of class Booking
    */
    public Booking(String name, String phone, int [] adultSeat, int [] childSeat)
    {
    name = name;
    phone = phone;
    childSeat = new int [32];
    adultSeat = new int [32];
    }

    /**
    * An example of a method - replace this comment with your own
    *
    * @param y a sample parameter for a method
    * @return the sum of x and y
    */
    public String getCustomerName()
    {
    return name;
    }

    public String getTelNumber()
    {
    return telephone;
    }

    public int[] childrenBooked ()
    {
    return childSeat;
    }

    public int[] adultsBooked ()
    {
    return adultSeat;
    }

    I think I'm missing some fundamental nugget of Java knowledge which is holding me back...

    Thanks again for all your help.

    • Reply
  • Dr Svensk Tiger
    Beginner
    Dr Svensk Tiger ·
    • Report
    • Hide content

    Sorry I missed this last night, will show him this now.

    • Reply
  • Dr Svensk Tiger
    Beginner
    Dr Svensk Tiger ·
    • Report
    • Hide content

    H has written this, hope it helps! He says the formatting might go a bit weird on Hitched so let me know if you have an email address you don't mind posting on here and I can send it via that too. I did try the Hitched email function but it made my computer go weird!

    import java.util.ArrayList;

    /**
    * Write a description of class Trip here.
    *
    * @author
    * @version v0.1 05.07.09
    */
    public class Trip
    {
    private double price;
    private String destination;
    private Date date;
    private String code;
    private ArrayList<Booking> b;
    private int headCount;

    /**
    * Constructor for objects of class Trip
    */
    public Trip(double tripPrice, String tripDestination, int d, int m, int y, String tripCode)
    {
    price = tripPrice;
    destination = tripDestination;
    date = new Date (d, m, y);
    code = tripCode;
    b = new ArrayList<Booking>();
    headCount = 0;
    }

    /**
    * An example of a method - replace this comment with your own
    *
    * @param y a sample parameter for a method
    * @return the sum of x and y
    */
    public double getPrice ()
    {
    return price;
    }

    public String getDestination ()
    {
    return destination;
    }

    public Date getDate ()
    {
    return date;
    }

    public String getCode ()
    {
    return code;
    }

    public String toString ()
    {
    return ("Trip code: " + code +
    " Destination : " + destination +
    " Date " + date.getAsString() + "Price " + price);
    }

    public void addBooking (String name, String phone, int nAdults, int nChildren)// this bit is from John
    {

    // create a new booking using this classes data
    Booking _b = new Booking(name, phone, nAdults, nChildren);
    // note the underscore - this is not the same
    // Booking object we have as a field in this class

    // add it to the list
    b.add();

    // now increment the headcount
    peopleBooked(nAdults+nChildren);

    }

    public void peopleBooked(int nPeople)
    {
    //JP said need filed headCount which gets incremented by the number of seats
    //being booked each time a booking is made
    heatCount = headCount + nPeople;
    }



    public double getTotalMoneyCollected()
    {
    double totalMoneyCollected = 0.00;
    for (int i = 0; i < bookings.size(); i++)
    {
    Booking thisBooking = bookings.get(i);
    totalMoneyCollected = totalMoneyCollected + thisBooking.getAdultsBooked() * price;
    totalMoneyCollected = totalMoneyCollected + thisBooking.getChildrenBooked() * (price/2);
    }
    return totalMoneyCollected;
    }

    }

    public class Booking
    {
    private String name;
    private String telephone;
    private String [] seats;
    private const int nSeats = 32;
    private int seatCount;

    /**
    * Constructor for objects of class Booking
    */
    public Booking(String name, String phone, int adultSeats, int childSeats)
    {
    name = name;
    phone = phone;
    seats = new String [nSeats];
    seatCount = 0;
    // init seats
    for (int i=0; i<nSeats; i++) {
    seats? = "empty";
    //childSeat = new int [32];
    //adultSeat = new int [32];

    // add some people to seats
    for (int i=0; i<adultSeats; i++) {
    _addAdult();
    // check if successful - really should throw an exception here and catch higher up
    // for now just print "Error" or soemthing
    }

    for (int i=0; i<childSeats; i++) {
    _addChild();
    // as above

    }

    }

    private int _addChild() {
    // add check
    seats[seatCount] = "C";
    seatCount++;
    return 1;
    }

    private int _addAdult() {
    // add a check for seatCount > nSeats - return 0 if not
    seats[seatCount] = "A";
    seatCount++;
    return 1;
    }

    /**
    * An example of a method - replace this comment with your own
    *
    * @param y a sample parameter for a method
    * @return the sum of x and y
    */
    public String getCustomerName()
    {
    return name;
    }

    public String getTelNumber()
    {
    return telephone;
    }

    public int childrenBooked ()
    {
    int childSeat;
    // loop through seats and retun number of children booked
    for (int i=0; i<nSeats; i++) {
    if (seats?.equals("C")) {
    childSeat++;
    }
    }

    return childSeat;
    }

    public int adultsBooked ()
    {
    // as above
    return adultSeat;
    }

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content

    Gosh, thanks, I didn't expect him to do my assignment for me ? Can I be reeeally cheeky and annoying and ask him to explain what he's done?

    ??

    • Reply
  • Dr Svensk Tiger
    Beginner
    Dr Svensk Tiger ·
    • Report
    • Hide content

    No worries - I find it easier to write code than explain it ;-) It's not complete (and I haven't tried to compile it either ;-) )

    I've changed the Booking class to store an Array of strings to represent the seats - it stores "empty", "A" or "C", depending on who's there. I modified the constructor and to accommodate this, and the getAdultSeats and getChildrenSeats to return the number of adults (by counting "A"s in the seats) and children (count "C"s) respectively. This makes your method that calculates price work now (previously, it would not have compiled as getAdultSeat would return an array of ints, rather than an int...).

    I'm not sure this is correct in terms of the assignment you have been set, but would work from the snippet of code I had.

    Hope it's useful, anyway Smiley smile

    We're off out now (shoppping - how dull), but no doubt ST will be on here later...

    Dr ST (the male one)

    • Reply
  • Doughnut
    Beginner June 2008
    Doughnut ·
    • Report
    • Hide content

    Many thanks for all your help. I'm going to read through it and use the bits I understand, as I'm not comfortable with passing someone else's work off as my own and taking the credit/ marks for it!

    ?

    • Reply

You voted for . Add a comment 👇

×


Related articles

Premium members

  • Q
    Qa Test I got married in August - 2022 North Yorkshire

General groups

Hitched article topics

Contest icon

Win £3,000 for your wedding

Join Hitched Rewards, where you can win £3,000 simply by planning your wedding with us. Start collecting entries, it's easy and free!

Enter now