Results 1 to 16 of 16
  1. #1
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default 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

    Java 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;
    	}
    }
    This is the "Storefront" class
    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);
    	}
    }
    The "Item" class compiled fine, no problems, and was moved into the proper file. The "Storefront" class gets error messages when I compile it:
    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.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,950
    Rep Power
    19

    Default

    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.

  3. #3
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default

    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"
    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());
    		}
    	}
    }
    And now get the following error message:
    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.

  4. #4
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default

    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.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,950
    Rep Power
    19

    Default

    You might need to show us the directory contents where you are trying to compile the GiftShop class.

  6. #6
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default

    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?

    Quote Originally Posted by Tolls View Post
    You might need to show us the directory contents where you are trying to compile the GiftShop class.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    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?
    The package root is the folder that needs to be on the classpath. In this case, it's C:\Users\jmp\Desktop\Java 6

    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.

    db
    Last edited by DarrylBurke; 03-24-2011 at 03:17 AM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,950
    Rep Power
    19

    Default

    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.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    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
    Agreed. A CLASSPATH envar just muddies the waters even more.

    db

  10. #10
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default

    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.

    Quote Originally Posted by Darryl.Burke View Post
    The package root is the folder that needs to be on the classpath. In this case, it's C:\Users\jmp\Desktop\Java 6

    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.

    db

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,950
    Rep Power
    19

    Default

    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.

  12. #12
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default

    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

    Quote Originally Posted by Tolls View Post
    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.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,950
    Rep Power
    19

    Default

    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?

  14. #14
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default

    Sure thing, shweet-heart. Ain't she a dame?

    Herez looking at you.

    Quote Originally Posted by Tolls View Post
    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?

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Quote Originally Posted by Tolls View Post
    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?
    Careful, your age is showing ;)

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,950
    Rep Power
    19

Similar Threads

  1. Beginner - question of 'if' statement
    By hayden06f4i in forum New To Java
    Replies: 6
    Last Post: 11-08-2010, 02:45 AM
  2. Beginner issues: Packages, classpaths and JARs
    By circle in forum New To Java
    Replies: 3
    Last Post: 05-31-2010, 03:43 AM
  3. Beginner question about ArrayList
    By kesi in forum New To Java
    Replies: 3
    Last Post: 09-19-2009, 11:30 PM
  4. Question about packages.
    By dragonchr15 in forum New To Java
    Replies: 7
    Last Post: 08-07-2009, 11:34 PM
  5. Beginner Java question
    By DanK in forum New To Java
    Replies: 3
    Last Post: 04-27-2009, 04:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •