really need help solving this problem :S

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • outofmymind
    New Member
    • Oct 2006
    • 45

    #1

    really need help solving this problem :S

    Hi every1,

    im trying to solve this question, i did some of it but i dont think that its correct or complete:

    this is the question:

    Write the definition of a class called Product. A Product object should represent a product stocked in a supermarket, e.g. a 500 gram pot of
    yogurt. It should contain the following information: a code for the
    product, the name of the product, the cost of the product, and the
    quantity of the product currently in stock. Assume the variables code
    and name are represented by strings of characters. Include the
    following constructor and methods in the class definition.

    i. A constructor Product(code, name) which creates a new product with the given code and name. Initially, the cost and the quantity of the product should be set to zero.
    ii. An instance method getName() that will return the name of the product.
    iii. An instance method addStock(int n) that will add n to the quantity of the product in stock. This method throws exception where n is negative number. Add finally block which prints the value of n.

    below is the code that i have attempted to do, im only having problems solving part iii, in which i need help in.

    Code:
    public class Product {
    
    	String code;
    	String name;
    	double cost;
    	long quantity;
    	
    	public Product(){
    		cost=0;
    		quantity=0;
    	}
    	
    	public Product(String code, String name){
    		this.code=code;
    		this.name=name;
    		}
    	
    	public String getname(){
    		return name;
    	}
    	
    	public void setname(String Name){
    		name=Name;
    	}
    Any help will be very much appreciated,
    outofmymind
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Originally posted by outofmymind
    iii. An instance method addStock(int n) that will add n to the quantity of the product in stock. This method throws exception where n is negative number. Add finally block which prints the value of n.

    Code:
    public class Product {
    
    	String code;
    	String name;
    	double cost;
    	long quantity;
    	
    	public Product(){
    		cost=0;
    		quantity=0;
    	}
    	
    	public Product(String code, String name){
    		this.code=code;
    		this.name=name;
    		}
    	
    	public String getname(){
    		return name;
    	}
    	
    	public void setname(String Name){
    		name=Name;
    	}
    OK, you need a method that will add to stock. This is relatively easy - the only tricky part is the Exception. You will have to import the necessary Exception classes from the Java library.

    Once you have the Exception class, the method definition is easy. Consider the following pseudocode:

    Code:
    public void addStock(int n) {
       if (n is a bad number) throw BadInputException; // or whatever exception is most relevant.
       else quantity is increased by n;
    }
    See if you can figure it out from here.

    Comment

    • outofmymind
      New Member
      • Oct 2006
      • 45

      #3
      hey Ganon11,

      thanks for ur tip!

      this is the code i came up with:

      Code:
      public long addStock(int n)
      	{
      		if (n<0)
      		throw new IllegalArgumentException();
      		else                         
      		quantity = quantity + n;	
      	    return quantity;
      	}
      THANX!

      Comment

      • outofmymind
        New Member
        • Oct 2006
        • 45

        #4
        Hi,

        I've been trying to add the finally block that prints n for this part of the question:

        -> add An instance method addStock(int n) that will add n to the quantity of the product in stock. This method throws exception where n is negative number. Add finally block which prints the value of n.

        im just having problems with inserting this finally block

        Here is the code:

        Code:
         public long addStock(int n)
        	{
        		if (n<0)
        		throw new IllegalArgumentException();
        		else                         
        		quantity = quantity + n;	
        	    return quantity;
        	}


        Thanks for the help!
        outofmymind

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          I'm not sure what you mean by "Add finally block..." Could you elaborate as to what you mean by this?

          Comment

          • outofmymind
            New Member
            • Oct 2006
            • 45

            #6
            Hi,

            This is what i meant by the finally block:

            Code:
            Code:
            public class Product
            {
                
                String code;
                String name;
                double cost;
                long quantity;
                
                public Product ()
                {
                    cost=0;
                    quantity=0;
                }
                
                public Product (String code, String name)
                {
                    this.code=code;
                    this.name=name;
                }
                
                public String getname ()
                {
                    return name;
                }
                
                public void setname (String Name)
                {
                    name=Name;
                }
                void addStock(long n){
                    if(n<0)
                        throw new IllegalArgumentException();
                    quantity+=n;
                }
                public static void main (String[] args)    {
                    Product obj=new Product();
                    int n=-20;
                    try{
                        obj.addStock(n);
                    }
                    catch(IllegalArgumentException e){
                    System.out.println ("ExceptionCaught:" + e);
                    }
                    finally{
                    System.out.print("Value of n:" + n);
                    }
                    }
                }
            Thanks im glad that i finally finished it.
            outofmymind

            Comment

            Working...