bubble sort problem please help!!
hi there, i have to do a school project and it's about a dvd rental system. I have the following classes ; Client, SalesPerson(both inherit from the class Person), Loan class, DVD class, Runner, DvdShop.
Now my problem is how do I sort the DVDs when the user inputs his choice in the menu to show all dvds .
I have tried to do the code but it gives me an error. I don't know what's wrong with it. Here is what I tried so far;
P.S. DVDs ARE ADDED TO THE ARRAYLIST NAMED dList which I have declared at the beginning as ; ArrayList dList = new ArrayList () ;
the following is in the DvdShop class;
public String showAllDvds()
{
int temp = 0;
int n = dList.size();
String tmpDvd = "";
if(n>0)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < (n-i); j++)
{
if(dList[j-1] > dList[j])
{
//swap the elements
temp = dList[j-1];
dList[j-1] = dList[j];
dList[j] = temp;
}
}
}
/* DVD tmpDVD = (DVD) dList.get(i);
tmpDvd += "DVD " + (id+1) + ": " + tmpDVD.toString() + "\n";*/
}
else
{
tmpDvd = "No DVDs found! ";
}
return tmpDvd;
}
while the following is in the runner class ;
case 7:
//Show all DVDs in DvdShop
System.out.println(myDvdShop.showAllDvds());
for(int i=0; i < dList.size; i++)
{
System.out.println(dList[i] + " ");
}
break;
Can you please help?? thanks.
Re: bubble sort problem please help!!
Quote:
I have tried to do the code but it gives me an error.
What error? Post it in its entirety, and please wrap your code in the code tags
Re: bubble sort problem please help!!
Could you be a little more specific, please? How exactly do you want your DVD's to be sorted? And does the user input criteria for the sorting, or just request to see all the DVD's?
Re: bubble sort problem please help!!
BUBBLE SORT (Java, C++) | Algorithms and Data Structures
Check that out and it might help you solve the problem. good luck.
Re: bubble sort problem please help!!
thanks for your interest :)
sorry about not wrapping my code in code tags but i am relatively new to this blog...
The error i receive are 2 ;
1) in this line in the DvdShop class - if(dList[j-1] > dList[j]) i receive the error ' array required, but java.util.ArrayList found'
2) in this line in the Runner class - for(int i=0; i < dList.size; i++) i receive the error ' cannot find symbol - variable dList '
I know that the second error is because the arraylist dList is not declared in the Runner Class but I don't know how to call the arraylist from the DvdShop to the Runner class!!
thanks again :)
Re: bubble sort problem please help!!
thanks I actually have visited this site before but in this case, the arraylist is in the same class while mine is in another class so I have to call it to use it in the second class etc.... so it doesn't help me so much
but thanks for the reply :) i appreciate it :)
Re: bubble sort problem please help!!
Your first issue (first error): you cannot access the elements of an ArrayList like you do in an array. Instead, use ArrayList's get() method.
The second issue: if dList is in the DvdShop class, you cannot access it another class simply by referring to its name. You must first call the class and then the variable name, which is dList in this case (if the class is static). However, if DvdShop is an Object, you must refer first to an instance of DvdShop and then the variable (dList).