My program generates new errors
I have solved the previous errors in my school assignment. However, my program generates new errors. Can someone please help me solve all these errors so I can get this assignment turned in? Thank you so much :)
Errors:
InventoryAdvanced Class:
Line 14: Incompatible types
Line 16: Array required, but InventoryDefine found
Line 17: Array required, but InventoryDefine found
Line 18: Array required, but InventoryDefine found
Line 21: method Sort in InventorySort cannot be applied to given types
Line 26: Array required, but InventoryDefine found
InventorySort Class:
Line 22: bad operand types for binary operator
Line 24: incompatible types
Line 26: incompatible types
Code:
public class InventoryAdvanced {
public static void main(String args[]){
int size = 4;
InventoryDefine swordstone = new InventoryDefine("Sword In The Stone Movie", 1, 10,13.00);
InventoryDefine hercules = new InventoryDefine("Hercules", 2, 20, 5.00);
InventoryDefine mermaid = new InventoryDefine("The Little Mermaid", 3, 30, 10.00);
InventorySort methodCall = new InventorySort();
InventoryDefine inventoryArray = new InventoryDefine[3];
inventoryArray[0] = swordstone;
inventoryArray[1] = hercules;
inventoryArray[2] = mermaid;
//Call the sort method
methodCall.Sort(inventoryArray);
//Display the sorted array
for (int i = 0; i < size; i++){
System.out.println(inventoryArray[i]);
}
}
}
Code:
/**
* @(#)InventoryAdvanced.java
*
* InventoryAdvanced application
*
* @author
* @version 1.00 2012/1/26
*/
public class InventoryDefine {
//Define the variables
public String inventoryName;
public int inventoryNumber;
public int inventoryStock;
public double inventoryPrice;
//Declare the constructor
public InventoryDefine(String name, int productNumber, int productStock, double productPrice){
this.inventoryName = name;
this.inventoryNumber = productNumber;
this.inventoryStock = productStock;
this.inventoryPrice = productPrice;
}
}
Code:
/**
* @(#)InventorySort.java
*
*
* @author
* @version 1.00 2012/1/28
*/
public class InventorySort {
public void Sort(InventorySort[] array){
int a, b, t;
int size;
size = 4;
for (a=1; a < size; a++)
for (b=size-1; b >= a; b--){
if(array[b-1] > array[b]) {
t = array[b-1];
array[b-1] = array[b];
array[b] = t;
}
}
}
}
Re: My program generates new errors
You are missing brackets where you declare your inventoryArray
Code:
InventoryDefine inventoryArray = new InventoryDefine[3]
should be
Code:
InventoryDefine[] inventoryArray = new InventoryDefine[3]
Re: My program generates new errors
Thank you! How do I fix the error that says method Sort in class InventorySort cannot be applied to given types? I passed the array correctly, I think.
Re: My program generates new errors
What are you trying to sort? The '>' operator is for comparing numeric values. The way your code is written you are saying "is this object (InventoryDefine) greater than that object" (which doesn't compute in Java). You either need to compare one of the InventoryDefine's fields or implement Comparable and use compareTo() method
Re: My program generates new errors
Thank you! How do I compare InventoryDefines fields? I am trying to sort my array.
Re: My program generates new errors
1. what field are you trying to sort by? price?
2. You can access an arrays value by arrayName[indexValue] which returns a value, in your case an InventoryDefine object. Therefore, you can access its public fields and methods with dot notation. Something like array[b].inventoryPrice
3. why are you passing InventorySort[] into your sort method? Don't you want to sort an array of InventoryDefine objects -- InventoryDefine[]?
4. your declared t as int. This is your temporary object that will hold an object of InventoryDefine. You need to declare it as such.
5. Why are you hardcoding the size of your arrays. You are going to have issues with the bounds of the array. You can access the length property of an array like yourArrayName.length
Re: My program generates new errors
I am trying to sort by name. I changed the passing to InventoryDefine. How do I declare t as int to hold the object of InventoryDefine? What do you mean by hardcoding? Can you explain further on that? Can you give me an example please? Thank you so much for your help. I greatly appreciate it! I have been stuck on this project for 5 days! How do I do a valid comparison with my arrays in the sort method?
Re: My program generates new errors
I am trying to sort by name. I described how to access the field. The problem you will have is that you are comparing with the '>' operator in your sort algorithm which doesn't work on strings. You will need to look into the String class's compareto method.
I changed the passing to InventoryDefine.
How do I declare t as int to hold the object of InventoryDefine? -- You would declare it as InventoryDefine object and not as an int
What do you mean by hardcoding? int size = 4; is hard coding, you should allow for a varying size array as I described in my earlier post. (Especially because your array's size is 3)
Can you explain further on that?
Can you give me an example please? Thank you so much for your help. I greatly appreciate it! I have been stuck on this project for 5 days! Just imagine how nice it will be to finish this on your own.
How do I do a valid comparison with my arrays in the sort method? -- refer to above.
Keep at it. The more you do on your own the easier it gets. Good luck.
Re: My program generates new errors
I have a question: Is there anyway to declare a method in the main program and use that method in the main program? Basically, use a method that you declared in a class and then call it in the same class?