Results 1 to 16 of 16
- 03-23-2011, 04:49 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 93
- Rep Power
- 0
Beginner Question About Packages, etc
Hello. I'm trying to complete an exercise in a book about Java coding, and I'm having a problem not explained in the book.
The exercise involves creating two classes in a package, and then using a short program to display a print out. I've set up the file structure for the packages as explained in the book, and created the two classes
This is the "Item" class
This is the "Storefront" classJava Code:package eCommerce; import java.util.*; public class Item implements Comparable { private String id; private String name; private double retail; private int quantity; private double price; Item(String idIn, String nameIn, String retailIn, String quanIn) { id = idIn; name = nameIn; retail = Double.parseDouble(retailIn); quantity = Integer.parseInt(quanIn); if (quantity > 400) price = retail * .5D; else if (quantity > 200) price = retail *.6D; else price = retail * .7D; price = Math.floor( price * 100 + .5 ) / 100; } public int compareTo(Object obj) { Item temp = (Item)obj; if (this.price < temp.price) return 1; else if (this.price > temp.price) return -1; return 0; } public String getId() { return id; } public String getName() { return name; } public double getRetail() { return retail; } public int getQuantity() { return quantity; } public double getPrice() { return price; } }
The "Item" class compiled fine, no problems, and was moved into the proper file. The "Storefront" class gets error messages when I compile it:Java Code:package eCommerce; import java.util.*; public class Storefront { private LinkedList catalog = new LinkedList(); public void addItem(String id, String name, String price, String quant) { Item it = new Item(id, name, price, quant); catalog.add(it); } public Item getItem(int i) { return (Item)catalog.get(i); } public int getSize() { return catalog.size(); } public void sort() { Collections.sort(catalog); } }
Note: Storefront.java uses unchecked of unsafe operations
Note: Recompile with -Xlint:unchecked for details
And when I compiled it with -Xlint:unchecked, I got these messages:
Warning: unchecked call to add(E) as a member of the raw type java.util.LinkedList
catalog.add(it);
Warning: unchecked conversion
found : java.util.LinkedList
required : java.util.List(T)
Collections.sort(catalog);
Warning: unchecked method invocation:(T)sort(java.util.List(T)) in java.util.Collections is applied to (java.util.LinkedList)
I've checked and double checked the code and spelling, etc. My book says this should compile, but I can't get it to go. This problem is beyond my meager computer skills. Can anyone help me complete this? Thanks. Bogie.Last edited by Humphrey Bogart; 03-23-2011 at 04:51 PM.
- 03-23-2011, 05:06 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Generics.
The book may be pre-1.5, or simply not be bothering with generics.
Essentially your LinkedList should really be LinkedList<Item>, thus limiting it (at compile-time anyway) to only storing Items.
That message, however, is simply a warning and you could ignore it...but it would probably be a good idea to do it properly.
ETA: Oh, and it also means (if you do it properly) that you don't have to do the cast to (Item) in the getItem() method.
- 03-23-2011, 05:26 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 93
- Rep Power
- 0
Hello. Thank you for your helpful reply. I did notice that "Storefront" did indeed compile, and I moved the resulting class file to the proper folder, and then wrote the code that is supposed to use this package
Code for "GiftShop"
And now get the following error message:Java Code:import eCommerce.*; public class GiftShop { public static void main(String[] arguments) { Storefront store = new Storefront(); store.addItem("C01", "MUG", "9.99", "150"); store.addItem("C02", "LG MUG", "12.99", "82"); store.addItem("C03", "MOUSEPAD", "10.49", "800"); store.addItem("D01", "T SHIRT", "16.99", "90"); store.sort(); for (i = 0; i < store.getSize(); i++) { Item show = (Item)store.getItem(i); System.out.println("\nItem ID: " + show.getID() + "\nName: " + show.getName() + "\nRetail Price: $ " + show.getRetail() + "\nQuantity " + show.getQuantity()); } } }
cannot access Storefront
bad class file: .\Storefront.java
file does not contain class Storefront
So I guess I haven't set up the correct CLASSPATH, folders, and files.
- 03-23-2011, 05:30 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 93
- Rep Power
- 0
The eCommerce folder has two files in it: Item.class, and Storefront.class.
So I don't understand why the compiler is telling me "file does not contain class Storefront.
- 03-23-2011, 06:41 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
You might need to show us the directory contents where you are trying to compile the GiftShop class.
- 03-23-2011, 08:40 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 93
- Rep Power
- 0
Hello. Thank you again for your reply
On my desktop (C:\Users\jmp\Desktop) I have a folder in which I put all the work I've done from this book (Java 6). In that folder, I created a subfolder (named eCommerce) to hold the class files for the package. So the address for the package is: C:\Users\jmp\Desktop\Java 6\eCommerce. I have already added this address to the CLASSPATH environmental variable.
Any ideas what I'm doing wrong?
- 03-24-2011, 02:27 AM #7
The package root is the folder that needs to be on the classpath. In this case, it's C:\Users\jmp\Desktop\Java 6So the address for the package is: C:\Users\jmp\Desktop\Java 6\eCommerce. I have already added this address to the CLASSPATH environmental variable.
Any ideas what I'm doing wrong?
The fully qualified names of classes in the eCommerce package will be of the form eCommerce.ClassName. The compiler and runtime will locate such class in a folder named eCommerce relative to the package root.
dbLast edited by DarrylBurke; 03-24-2011 at 03:17 AM.
- 03-24-2011, 08:22 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
In addition to what Darryl says, it's generally not a good idea to set a system classpath. If your CLASSPATH is local to a batch file you use for compiling then that's OK, but setting an environment variable is not a good idea. Use the -cp switch the set the classpath, or simply compile/run from the "C:\Users\jmp\Desktop\Java 6" directory, since you are doing a fairly simple app at the moment.
- 03-24-2011, 08:40 AM #9
Agreed. A CLASSPATH envar just muddies the waters even more.If your CLASSPATH is local to a batch file you use for compiling then that's OK, but setting an environment variable is not a good idea
db
- 03-24-2011, 03:11 PM #10
Member
- Join Date
- Mar 2011
- Posts
- 93
- Rep Power
- 0
Thanks for your reply. I'm still not understanding.
The name for the package is eCommerce. The folder C:\users\jmp\Desktop\Java 6\eCommerce has the two class files "Item" and "Storefront" in it. I have changed the CLASSPATH to include C:\Users\jmp\Desktop\Java 6. And when I run the command prompt, I am running it in C:\Users\jmp\Desktop\Java 6, which is the folder in which I have the "GiftShop.java" file.
And yet, when I compile the GiftShop code, I get the "cannot acces Storefront, bad class file, file does not contain the class Storefront" error message.
What am I doing wrong? thanks.
- 03-24-2011, 03:23 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Don't use environment variables for your classpath.
That's step 1.
Step 2 is to recompile your other java files, with the Java files located in the eCommerce directory. Compile from the "Java 6" directory by using "javac eCommerce\*.java".
Then compile your GiftShop, using "javac GiftShop.java".
Tell us what happened.
- 03-24-2011, 03:58 PM #12
Member
- Join Date
- Mar 2011
- Posts
- 93
- Rep Power
- 0
Aha!! Better.
So I
1) removed C:\Users\jmp\Desktop\Java 6 from the CLASSPATH
2) moved the Item. java file and the Storefront.java file to the eCommerce folder
3) run the command prompt in C:\Users\jmp\Desktop\Java 6 and compiled the Item and Storefront .java files - getting the "Storefront.java has unchecked or unsafe operations" error message.
4) Then compiled in GiftShop.java, and there WERE NO error messages saying "file does not contain Storefront class".
So that's better.
Compiling GiftShop.java resulted in error messages
"cannot find symbol: variable i (in the line: for (i = 0, i < store.getSize, i ++) {)
and
"cannot find symbol: method getID()"
So now I have to look back and see if I have made a typo somewhere.
I appreciate your help. Bogie
- 03-24-2011, 04:11 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
No problem.
Just one question, is it true you had to wear really hefty platform shoes in Casablanca so as not to look short against Bergman?
- 03-24-2011, 04:34 PM #14
Member
- Join Date
- Mar 2011
- Posts
- 93
- Rep Power
- 0
- 03-24-2011, 04:37 PM #15
- 03-24-2011, 04:45 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Beginner - question of 'if' statement
By hayden06f4i in forum New To JavaReplies: 6Last Post: 11-08-2010, 02:45 AM -
Beginner issues: Packages, classpaths and JARs
By circle in forum New To JavaReplies: 3Last Post: 05-31-2010, 03:43 AM -
Beginner question about ArrayList
By kesi in forum New To JavaReplies: 3Last Post: 09-19-2009, 11:30 PM -
Question about packages.
By dragonchr15 in forum New To JavaReplies: 7Last Post: 08-07-2009, 11:34 PM -
Beginner Java question
By DanK in forum New To JavaReplies: 3Last Post: 04-27-2009, 04:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks