Page 2 of 2 FirstFirst 12
Results 21 to 38 of 38
  1. #21
    erikjd21 is offline Member
    Join Date
    Apr 2011
    Posts
    27
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    I highly suggest reading this Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    It's explains everything quite well and it should have working example code that you can "mimic" when making the DVD class.

    In this assignment you want to start small and work your way up. You start with the smallest item an inventory contains, in this case, a DVD. You create a class that represents an image. A class is like a blueprint, much like a houses blueprint, it shows how a house is made, but doesn't actually make it. You use the blueprint to make houses.

    In oop you use the class to make objects of that class.

    The next piece you want to make is an inventory; which represents an inventory of items, so it basically manages an array of DVD objects.
    So should each aspect be a class? Should there be a DVD class that contains the title, and price. Then an inventory class to manage the dvd class? Or should it all be contained within the DVD class?

    Also, I am trying to set the variables inside the DVD class like you suggested, but when I try to create an array using the variables, it gives me an error, suggesting that I make another class.

    I am very very very lost on this, and I dont understand why. I get the concept of making a class, I understand the concept of how arrays work. I guess one thing that is eluding me, is it is seeming like I am making multiple variables for one item. For instance:
    Java Code:
    class dvd {
    
          String name;
          int price;
          int inv;
         
    
         public dvd(){
            
    
    
         }
             public void setName(String name){
                 this.name = name;
             }
             public void setPrice(int price){
                 this.price=price;
             }
             public void setInv(int inv){
                 this.inv=inv;
             }
             public String getName(String name){
                 return (name);
             }
             public int getPrice(int price){
                 return (price);
             }
             public int getInv(int inv){
                 return (inv);
             }
    
        }
    Is what I made thus far. But when I attempt to create an array of the titles, using the String name, I get errors, saying that it is either an incompatiable type, or that it wants to create a name class. Im so confused, I appologize for the simplicity of these questions, but the instructor isnt helping, and aparently I am dumber than what the tutorials expect a person to be.

  2. #22
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Quote Originally Posted by erikjd21 View Post
    Should there be a DVD class that contains the title, and price. Then an inventory class to manage the dvd class?
    Now you're getting it! A real DVD has a title and price, so it makes sense for a DVD object to contain that data. Likewise, an inventory is a list of DVDs and how many of each you have, so that should suggest the implementation of your Inventory class.

    Your dvd class looks good so far. One thing I would suggest is to give your constructor some arguments, so you never have an "empty" dvd object. Now might also be a good time to get familiar with the standard naming conventions to make your code more readable.

    Show us the code you're trying to use to create an array of titles.

    BTW, I share your frustration with bad instructors. I can't believe they've given you this assignment without explaining the basic concepts of OOP. What the heck are people paying for at universities these days?

  3. #23
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are doing really well with this iteration. You need to supply more constructors, which take arguments and initialize the item. Like this
    Java Code:
    public DVD(String title, int quantity){
      this.title = title;
      this.quantity = quantity;
    }
    You can have as many different constructors as you want. The compiler will determine which to use based on the supplied arguments.

    The tutorials can be a bit hard to follow when you are new, do not be discouraged(don't panic), stick with it and they will make more sense.

    What text are you using for this class?

    When I'm at a computer I can show you an inventory class example of some other type to give you a stronger feel. Once you understand oop concepts it will be very satisfying.

  4. #24
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by sunde887 View Post
    Always remember, this gobbledegook: ClassName@186af6d means that the object toString method was called and your error lies in printing, either you tried to print an array or you didn't give the class an overridden version of toString.
    Yes; in this case, it was the former.

    For Erik's benefit - in this particular case, it wasn't a matter of overriding a toString method, it's just that the loop was repeatedly trying to print the array rather than an element of it. The loop should be more like:
    Java Code:
         public void sortInfo(){
           
             for (int i = 0; i <title.length; i++){
                    System.out.println(title[B][i][/B]); <--- print the i'th element
             }
             Arrays.sort(title);
        }
    I hope this helps.

  5. #25
    erikjd21 is offline Member
    Join Date
    Apr 2011
    Posts
    27
    Rep Power
    0

    Default

    OK, this is where I am running into problems.

    What is the transition to go from

    This
    Java Code:
     class Fruit{
      private String name;
    to this
    Java Code:
     public Fruit(){ //default constructor
      }
      public void setName(String name){
        this.name = name;
    to this

    Java Code:
    Fruit[] fruits = new Fruit[1];
    fruits[0] = new Fruit("Apple", 10, 10.0, Color.RED);
    When I attempt to make a title array similar to sunde's example, I get an error saying either to make a class called title, or if I name it "name" it says it is hiding a field.

    I dont think I would be having this many problems if it wasnt for the array implementation, even though I understand what arrays are. The object oriented portion is the part that I think is confusing me, even though I at first felt I understood it.

    To be honest, in the above examples, it feels like I am making redundant variables. Maybe that is what is confusing me. It seems like the variables are just repeating themselves.

    name > this.name > array name

    Why not skip the first two, and begin with the names in the array?

    I am not questioning but rather trying to understand, so please anyone who answers do not take it as so.

    Thanks to anyone in advance

  6. #26
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    [QUOTE=erikjd21;203074]
    Java Code:
     class Fruit{
      private String name;
    This is part of the class definition declaring that a Fruit has a name.

    Java Code:
     public Fruit(){ //default constructor
      }
    This defines a constructor for the Fruit class to alllow you to create a Fruit (without specifying a name, or anything - probably not such a great idea).
    Java Code:
     public void setName(String name){
        this.name = name;
    This defines a setName method for the Fruit class that allows you to set the name of a Fruit you have created.

    Java Code:
    Fruit[] fruits = new Fruit[1];
    This declares a new array to hold Fruits that has space for 1 Fruit.

    Java Code:
    fruits[0] = new Fruit("Apple", 10, 10.0, Color.RED);
    This creates a new Fruit and puts it into the array at position 0.

    I can't really grok your other questions...

  7. #27
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I can give you a much better explanation in about 2 hours when I am around a computer. When you create an array with new you are not using a fruit objects constructor, you are saying you are creating an array with a size of 2 whatever is in the brackets.

    My fruit class doesn't provide a constructor for anything but no arguments, and I used a constructor with 4 arguments, which may be a bit confusing, I will show you more on this later as well.

    The use of this in setters in constructors and methods differentiates between the classes instance variable and the argument name. You could do it just as well like this
    Java Code:
    public void setName(String newName){
      name = newName;
    }
    however, I prefer to be lazy and not make a new variable name in the method. Please do your best to clarify your misunderstandings. It is a little un clear currently. I am are this is just because you are still trying to understand everything.

  8. #28
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Here is a short expansion of the fruit class that you can run and look over to see what a class can look like. This class is similar to what your classes will look like, however it is a bit different. Mainly, you will be using an array instead of an array list, and you can't directly print an array like an array list.

    It also has a toString to let you see how they operate

    Java Code:
    public class Fruit{
    	private String name;
    	private int price;
    	private int weight;
    	
    	//I provided 4 constructors here, these are different overloaded versions
    	//the compiler will use the arguments passed to determine the correct 
    	//constructor to be used when creating a new fruit object
    	
    	public Fruit(){//default constructor
    	}
    	public Fruit(String name){
    		this.name = name;
    		price = 10;
    		weight = 10;
    	}
    	public Fruit(String name, int price){
    		this.name = name;
    		this.price = price;
    		weight = 10;
    	}
    	public Fruit(String name, int price, int weight){
    		this.name = name;
    		this.price = price;
    		this.weight = weight;
    	}
    	
    	//I have provided getters and setters which allow the outside world
    	//to have access to this classes instance variables. This way you 
    	//can change and query the value of some fruit object.
    	//you don't always want setters for you classes, however; I supplied
    	//them to show how they look/what they do.
    	public void setName(String name){
    		this.name = name;
    	}
    	public String getName(){
    		return name;
    	}
    	public void setPrice(int prive){
    		this.price = price;
    	}
    	public int getPrice(){
    		return price;
    	}
    	public void setWeight(int weight){
    		this.weight = weight;
    	}
    	public int getWeight(){
    		return weight;
    	}
    	public String toString(){
    		return name + ", " + weight + ", " + price;
    	}
    }
    //The fruit class does not have anything else in it, it could have methods
    //to perform other things, but what can a fruit really do?
    Java Code:
    import java.util.*;
    
    public class Person{
    	private ArrayList<Fruit> fruits;
    	
    	//I am hesitant to post this, it may be giving away too
    	//much, I am using an array list instead to vary it a bit
    	private int money;
    	
    	public Person(){}
    	public Person(ArrayList<Fruit> fruits){
    		this.fruits = fruits;
    	}
    	public Person(ArrayList<Fruit> fruits, int money){
    		this.fruits = fruits;
    		this.money = money;
    	}
    	
    	public void eatFruit(){
    		if(!fruits.isEmpty()){
    			Fruit f = fruits.get(0);
    			fruits.remove(0);
    			System.out.println("What a good " + f);
    		}
    		else{
    			System.out.println("No more fruits, you need to buy more");
    		}
    	}
    	public void buyFruit(Fruit fruit){
    		fruits.add(fruit);
    		money -= fruit.getPrice();
    		System.out.println("You now have one more " + fruit + ", and have " + money + " remaining");
    	}
    	public String toString(){
    		return "Money: " + money + "\nFruits: " + fruits;
    	}
    	public static void main(String[] args){
    		Person p = new Person(new ArrayList<Fruit>(), 100);
    		p.eatFruit();
    		p.buyFruit(new Fruit("Apple", 10, 5));
    		p.buyFruit(new Fruit("Orange"));
    		System.out.println(p);
    		p.eatFruit();
    		p.eatFruit();
    		p.eatFruit();
    		System.out.println(p);
    	}
    }
    There are two separate classes here. Save each in it's own file, then you can compile them and run them. I used comments to explain some stuff, but I'd like you to really analyze what each thing does.

  9. #29
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Just a general comment about constructors - it's worth considering that a constructor is fundamentally for initialising the object to a valid or usable state. In this example, I would question whether the default, no argument constructor should be provided; is it really valid to have a Fruit with no name, price, or weight? would it be usable? It's true that you can set the name later, but as sunde887 suggested, that might not always be a reasonable thing to do - after all, can a Fruit change its name? The point is that the constructor is there to ensure the object is valid at the outset.

    A second point is that where you have multiple constructors with varying numbers of parameters, it is good practice to chain them together. This reduces duplication, ensures all defaults are set, minimizes the potential for errors, and makes maintenance and enhancement easier:
    Java Code:
    public class Fruit{
    	private String name;
    	private int price;
    	private int weight;
    	
    	public Fruit() {           //default constructor
    		this("Apple");     // chain to name constructor passing default name (optional)
    	}
    
    	public Fruit(String name) {
    		this(name, 10);     // chain passing name & default price 
    	}
    
    	public Fruit(String name, int price) {  
    		this(name, price, 10);     // chain passing name, price, & default weight
    	}
    
    	public Fruit(String name, int price, int weight) {
    		this.name = name;
    		this.price = price;
    		this.weight = weight;
    	}
            ...
    }
    Last edited by dlorde; 04-28-2011 at 06:42 PM.

  10. #30
    erikjd21 is offline Member
    Join Date
    Apr 2011
    Posts
    27
    Rep Power
    0

    Default

    Ok guys, Ive been working a lot but I didn't give up on this.

    So far this is what I have came up with, and although I know its far from what the professional outcome would be, it seems to almost be working correctly for the exception of one part..

    Java Code:
    public class Inventory {
        private String[] movies;
        private int[] stockNum;
        private int[] price;
        private int [] in_stock;
        private int counter;
        private int[] total;
        
        public Inventory(){
            movies=new String[3];
            stockNum= new int[3];
            price = new int[3];
            in_stock = new int[3];
            total = new int[3];
            
        }
        
        public void displayInv(){
          
            counter=0;
            while(counter<3){
                
                setInv(movies);
                setStock(stockNum);
                setPrice(price);
                totalPrice(total);
                inStock(in_stock);
                System.out.printf("The name of the movie is: "+movies[counter]+"\n");
                System.out.printf("Stock number: "+stockNum[counter]+"\n");
                System.out.printf("The price of this movie is:$"+price[counter]+"\n");
                System.out.printf("There are currently "+in_stock[counter]+" of these movies in stock\n");
                System.out.printf("The total amount for the inventory of "+movies[counter]+ " is $"+total[counter]+"\n");
                System.out.println("");
                counter++;
              
            }
            
            }
        
        public void setInv(String movies[])
        {
            movies[0]=("The Social Network");
            movies[1]=("Hackers");
            movies[2]=("Anti-trust");
         
        }
        
        public void setStock(int stockNum[])
        {
            stockNum[0]=(2121);
            stockNum[1]=(2122);
            stockNum[2]=(2123);
        }
    
        public void setPrice(int price[])
        {
            price[0]=(25);
            price[1]=(15);
            price[2]=(25);
        }
    
        public void inStock(int in_stock[])
        {
    
           in_stock[0]=(150);
           in_stock[1]=(200);
           in_stock[2]=(300);
        }
    
        public void totalPrice(int total[])
        {
            total[0]=(in_stock[0]*price[0]);
            total[1]=(in_stock[1]*price[1]);
            total[2]=(in_stock[2]*price[2]);
        }
    }
    It has no errors listed in Netbeans, and compiles fine for the exception of one line:

    The name of the movie is: The Social Network
    Stock number: 2121
    The price of this movie is:$25
    There are currently 150 of these movies in stock
    The total amount for the inventory of The Social Network is $0

    The name of the movie is: Hackers
    Stock number: 2122
    The price of this movie is:$15
    There are currently 200 of these movies in stock
    The total amount for the inventory of Hackers is $3000

    The name of the movie is: Anti-trust
    Stock number: 2123
    The price of this movie is:$25
    There are currently 300 of these movies in stock
    The total amount for the inventory of Anti-trust is $7500

    Take note that the total for the inventory of Social network is 0. I have tried using doubles, ints, and combinations of both. The difficult part is that the math works perfect on the other 2. I dont even know how to google a question like this.

    Thank you all again for helping with this, it has been a great help.

    Any insight into this will be just as appreciated.

  11. #31
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I am glad to see you again Erik

    Maybe I have been drinking too much, but I don't see your main method.

    Also, you still haven't quite incorporated the use of a DVD class, which isn't a huge problem, perhaps you haven't learned it yet; when you do though, you will laugh at how much it simplifies the inventory class.

  12. #32
    erikjd21 is offline Member
    Join Date
    Apr 2011
    Posts
    27
    Rep Power
    0

    Default

    DOH! sorry, its late, I have been working like crazy here lately, and Im exhausted.

    The main class just has the class objects that are calling the methods from the inventory class. I attempted to make the DVD class, and although I got some of it to work, I was getting more and more confused, so I decided to make this work before getting in over my head. Ill post the main class for ya, just so youll see what i have done.

    Java Code:
    public class week6 {
    
        
        public static void main(String[] args) {
    
            Inventory inventoryOBJ = new Inventory();
            inventoryOBJ.displayInv();
    
            
    
      
    
        }
    }
    Like I said, I was getting very confused with the multiple class thing, which sucks because the minute I get this done, I have to make a subclass, so I can only imagine how much torture that is going to be for me heh.

  13. #33
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'd like to ask what you have done to try and debug this problem? To me it seems weird that it produces 0 for social network, try adding print statements to see the values of quantity, and price. With this you may be able to see what's wrong with the social network information.

    Perhaps after you have finished the project Ill give you a sample of what the DVD class would look like.


    I can't quite explain the behavior, but perhaps computing the price should be the ladt thing you do before the statements?
    Last edited by sunde887; 05-02-2011 at 07:13 AM.

  14. #34
    erikjd21 is offline Member
    Join Date
    Apr 2011
    Posts
    27
    Rep Power
    0

    Default

    At first I thought it may be a problem with the math because I originally had price set as a double, with in_stock set as an int.

    So I changed both to doubles, then both to ints.. didnt work.

    Then I figured it may not be able to perform the math on the first pass through of the while loop, so I tried setting the counter to 4, that didnt work.

    Ive commented out everything in the while loop, seeing if anything else messes up on the first run through, and it all prints fine.

    Ive tried googling 0.0 (the original number I was getting when price was set as a double) and it came back with NAN errors. But I dont know how to fix that, especially since it works on the other 2.

    Oddly, I am also unable to sort by movie title this way, so I am going to have to figure that out, I get a null pointer exception error when I add the sort method in the displayInv method, as well as adding it inside the while loop.

    P.S. I just added another array for a restocking fee, price * 0.05, and the math works perfect for it. Its just the first total that I am having problems with.

    But one problem at a time. Everything prints fine, they are all of the same type, and I have gone line by line to see if there was a syntax error that I am missing, which I cannot find, and would be weird to find one considering the other two add fine.
    Last edited by erikjd21; 05-02-2011 at 07:25 AM.

  15. #35
    erikjd21 is offline Member
    Join Date
    Apr 2011
    Posts
    27
    Rep Power
    0

    Default

    OK, update, I just made the while loop infinite, and on the second pass through, it did the math. So the problem is that it is not performing the math on the first pass through for whatever reason.

    Any Idea how to fix something like that?

    Sorry to spam, but fixed the problem. :)
    Last edited by erikjd21; 05-02-2011 at 07:38 AM. Reason: Fixed

  16. #36
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm not quite sure, however; I was able to get the correct output from your correct code.

    I think one thing is that the lines
    Java Code:
    setInv(movies);
                setStock(stockNum);
                setPrice(price);
                totalPrice(total);
                inStock(in_stock);
    don't really care what counter is. What happens if you move them out of the loop?

    Also, you are gonna have a huge problem when sorting by title. I believe I showed the reason why earlier. Creating more instance variable arrays will only make it more difficult.



    edit: I figured out what was causing the problem. You call total before you call in stock. When you do this it is happening from the loop. So the first pass through the loop it calculates the Total of all the movies THEN it calculates the amount of instock items. It continue through the loop and has the values of the count(the social network) calculated incorrectly. When you called total the in stock amount was 0 so it multiplied the price by 0, which produces 0. The second pass through the loop the instock values were already set, so the call to total worked a expected. Similar for the third pass.

    The change that made me realize this is:
    Java Code:
    public void displayInv(){
          
            counter=0;
                setInv(movies);
                setStock(stockNum);
                setPrice(price);
                totalPrice(total);
                inStock(in_stock);
            while(counter<3){
                System.out.printf("The name of the movie is: "+movies[counter]+"\n");
                System.out.printf("Stock number: "+stockNum[counter]+"\n");
                System.out.printf("The price of this movie is:$"+price[counter]+"\n");
                System.out.printf("There are currently "+in_stock[counter]+" of these movies in stock\n");
    			System.out.println(in_stock[counter] + " Price per: " + price[counter]);
                System.out.printf("The total amount for the inventory of "+movies[counter]+ " is $"+total[counter]+"\n");
                System.out.println("");
                counter++;
              
            }
    When I ran this EVERYTHING had a price of 0. I was able to conclude from this the logic error. If you don't quite understand what I mean above let me know, I will show some examples.
    Last edited by sunde887; 05-02-2011 at 07:50 AM.

  17. #37
    erikjd21 is offline Member
    Join Date
    Apr 2011
    Posts
    27
    Rep Power
    0

    Default

    I guess we posted around the same time. I tried moving the method call to where it was after the other amounts had been done, and it fixed the problem.

    Thank you again for the help.

    I am now attempting a subclass, Im hoping I am understanding it correctly, basically using the
    Java Code:
     public DVD extends Inventory{
    }
    line, and attempting to make it work that way. One part is, now Im getting more math problems.

    Thank you again for your help, you have no idea how much it has helped. I feel like I am spamming the forum though hehe. Trying my best to cut down as much hand holding as possible, but this stuff is very confusing.
    Last edited by erikjd21; 05-02-2011 at 08:07 AM.

  18. #38
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    As long as you don't make different posts for each question it's not a huge deal. Try your best to debug, think, and re think your strategy. If you are still stuck, come here and ask for help.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Concept question
    By erikjd21 in forum New To Java
    Replies: 3
    Last Post: 04-10-2011, 02:14 PM
  2. enter key concept......
    By silversurfer2in in forum AWT / Swing
    Replies: 5
    Last Post: 06-29-2010, 11:36 AM
  3. What is Seeding Concept?
    By bhupal4all in forum New To Java
    Replies: 1
    Last Post: 09-03-2008, 05:47 AM
  4. Map concept in JAVA
    By Mathan in forum New To Java
    Replies: 19
    Last Post: 06-27-2008, 06:02 AM
  5. mail concept
    By thamizhisai in forum Advanced Java
    Replies: 4
    Last Post: 04-11-2008, 07:19 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
  •