473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. public class Product {
  2.  
  3.     String code;
  4.     String name;
  5.     double cost;
  6.     long quantity;
  7.  
  8.     public Product(){
  9.         cost=0;
  10.         quantity=0;
  11.     }
  12.  
  13.     public Product(String code, String name){
  14.         this.code=code;
  15.         this.name=name;
  16.         }
  17.  
  18.     public String getname(){
  19.         return name;
  20.     }
  21.  
  22.     public void setname(String Name){
  23.         name=Name;
  24.     }
Any help will be very much appreciated,
outofmymind
Oct 29 '06 #1
5 1747
Ganon11
3,652 Expert 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. public class Product {
  2.  
  3.     String code;
  4.     String name;
  5.     double cost;
  6.     long quantity;
  7.  
  8.     public Product(){
  9.         cost=0;
  10.         quantity=0;
  11.     }
  12.  
  13.     public Product(String code, String name){
  14.         this.code=code;
  15.         this.name=name;
  16.         }
  17.  
  18.     public String getname(){
  19.         return name;
  20.     }
  21.  
  22.     public void setname(String Name){
  23.         name=Name;
  24.     }
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:

Expand|Select|Wrap|Line Numbers
  1. public void addStock(int n) {
  2.    if (n is a bad number) throw BadInputException; // or whatever exception is most relevant.
  3.    else quantity is increased by n;
  4. }
See if you can figure it out from here.
Oct 29 '06 #2
hey Ganon11,

thanks for ur tip!

this is the code i came up with:

Expand|Select|Wrap|Line Numbers
  1. public long addStock(int n)
  2.     {
  3.         if (n<0)
  4.         throw new IllegalArgumentException();
  5.         else                         
  6.         quantity = quantity + n;    
  7.         return quantity;
  8.     }
  9.  
THANX!
Oct 29 '06 #3
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:

Expand|Select|Wrap|Line Numbers
  1.  public long addStock(int n)
  2.     {
  3.         if (n<0)
  4.         throw new IllegalArgumentException();
  5.         else                         
  6.         quantity = quantity + n;    
  7.         return quantity;
  8.     }


Thanks for the help!
outofmymind
Oct 29 '06 #4
Ganon11
3,652 Expert 2GB
I'm not sure what you mean by "Add finally block..." Could you elaborate as to what you mean by this?
Oct 29 '06 #5
Hi,

This is what i meant by the finally block:

Expand|Select|Wrap|Line Numbers
  1. Code:
  2. public class Product
  3. {
  4.  
  5.     String code;
  6.     String name;
  7.     double cost;
  8.     long quantity;
  9.  
  10.     public Product ()
  11.     {
  12.         cost=0;
  13.         quantity=0;
  14.     }
  15.  
  16.     public Product (String code, String name)
  17.     {
  18.         this.code=code;
  19.         this.name=name;
  20.     }
  21.  
  22.     public String getname ()
  23.     {
  24.         return name;
  25.     }
  26.  
  27.     public void setname (String Name)
  28.     {
  29.         name=Name;
  30.     }
  31.     void addStock(long n){
  32.         if(n<0)
  33.             throw new IllegalArgumentException();
  34.         quantity+=n;
  35.     }
  36.     public static void main (String[] args)    {
  37.         Product obj=new Product();
  38.         int n=-20;
  39.         try{
  40.             obj.addStock(n);
  41.         }
  42.         catch(IllegalArgumentException e){
  43.         System.out.println ("ExceptionCaught:" + e);
  44.         }
  45.         finally{
  46.         System.out.print("Value of n:" + n);
  47.         }
  48.         }
  49.     }
Thanks im glad that i finally finished it.
outofmymind
Oct 30 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Rob R. Ainscough | last post by:
This is more of a conceptual question: 1. More and more companies are using VPN's and locking out internet connectivity (for a host of reasons, security, productivity, etc.). 2. ASP.NET...
9
by: jon wayne | last post by:
OK! I had this nagging doubt Consider (without worrying abt access specifiers) class Kid : public Parent{...}; Parent::someFunc() { Kid k; }
4
by: _link98 | last post by:
Which of the UDB 8.2.2 ESE unix executables really need to gain root privilege while they are running? In my installation, the db2sysc file is owned by the instance-owner (and relevant admin...
1
by: Paul Mendez | last post by:
I really need your assistance. I tried what you gave me and it did not work and I am thinking that the formatting that showed up when u saw my posting might have confused you. So I made sure to...
1
by: John Smith | last post by:
I have a two dimentional char array. Before filling it using strtok(), I reset its elements to '\0' using two nested for loops. The code works as I hope it would but I wonder whether I really need...
3
by: Chris | last post by:
Wait.. before you flame.. If someone can program in Java, or Javascript, or C, or (insert your language here that uses basically the same syntax as C#), and that person knew how to program in...
3
by: Sky Sigal | last post by:
I coming unglued... really need some help. 3 days chasing my tail all over MSDN's documentation ...and I'm getting nowhere. I have a problem with TypeConverters and storage of expandableobjects...
4
by: Pipo | last post by:
The problem: I create a very simple custom control: public class cLabel : System.Web.UI.WebControls.Label { } I place the cLabel in a user control. When I place the user control on a page...
1
lolixpop
by: lolixpop | last post by:
Hi everybody thanks for reading! i have a few actionscript questions that i really need help on...any help would be greatly appreciated! Okay so i came upon this tutorial:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.