Michael S.

asked • 02/08/20

Java Programming Question: Intro to Computer Science

I'm working on a project for my computer science class in java and it is a travel agency. In one of the classes, Basket, I need to make an array of objects of type Reservation. But I also have to have the following methods:

  1. A constructor that takes no inputs and initialize the field with an array with no reservations.
  2. A getProducts method which takes no inputs and returns a shallow copy of the array of Reservations of the basket. This array should contain all the reservations in the basket in the same order in which they were added.
  3. An add method which takes as input a Reservation. The method adds the reservation at the end of the list of reservation of the basket and returns how many reservations are now there.
  4. A remove method which takes as input a Reservation and returns a boolean. The method removes the first occurrence of the specified element from the array of reservation of the basket. If no such reservation exists, then the method returns false, otherwise, after removing it, the method returns true. Note that this method removes a reservation from the list if and only if such reservation is equal to the input received. For example, two flight reservations from Montreal to Vancouver are not considered equal if they were created under two different names. After the reservation has been removed from the array, the subsequent elements should be shifted down by one position, leaving no unutilized slots in the middle array. 

Here's what I have so far:

public Basket() {

ResList=null;

}

public Reservation [] getProducts() {

Reservation [] copy=ResList;

return copy;

}

public int add(Reservation r) {

int count=0;

Reservation newResList[] = new Reservation[ResList.length +1];

for(int i=0;i<ResList.length;i++) {

newResList[i]=ResList[i];

count++;

}

newResList[ResList.length]=r;

count++;



return count;

}

public boolean remove(Reservation r) {


int removeIndex=0;


Reservation newResList[] = new Reservation[ResList.length -1];


for(int i=0;i<ResList.length;i++) {


if(ResList[i].equals(r)) {

removeIndex=i;

}

}

for(int j = removeIndex; j < ResList.length -1; j++){

    newResList[j] = ResList[j + 1];

    return true;

   }

return false;

}


I tried to test these but I realized I can't call a method on an array. Like if I make an array of Reservations called rList and fill it with Reservations, I can't do rList.add(newReservation);

So I have 2 questions.


How can I actually use these methods to add or remove reservations in an array of reservations?

Does my code look somewhat correct?


Thank you so much I know it's a long question but any help is greatly appreciated because I've been stuck on this for several days now!


2 Answers By Expert Tutors

By:

Oleksii L. answered • 02/10/20

Experienced Computer Science Tutor (C++, Java, Python, JS...)

Patrick B. answered • 02/09/20

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.