473,569 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

really need help solving this problem :S

45 New Member
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 1755
Ganon11
3,652 Recognized Expert Specialist
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
outofmymind
45 New Member
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
outofmymind
45 New Member
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 Recognized Expert Specialist
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
outofmymind
45 New Member
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
1707
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 requires a web server and has more layers than a .NET Windows application 3. a .NET windows app .DLL's and .EXEs are very small So which is better,...
9
2064
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
1881
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 group ownership), but when the db2-instance starts running (manually started via "su" as the instance-owner), the db2sysc process requests root access....
1
1709
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 align it correctly this time. Ok... this is what I want it to get done. Where the X and Y are, I need to have showing is the values, $250.00 and...
1
1467
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 to reset the array. The program would run faster if I don't need to reset. ------------------------------------------ int Array(void) ...
3
3566
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 VB.NET (meaning they understand the .NET Framwork, how to use the IDE, the classes and components available, etc.) then would they really need to...
3
1925
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 to attributes in tags (think Style tag -> Style object). The problem that I am chasing is: Html side:
4
3428
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 I get this exception:
1
3180
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: http://www.kirupa.com/developer/mx/fadegrid2.htm It looks awesome! I absolutly love it but i'm not quite sure how to apply it to my banner. My banner consists of 3 movie clips...
0
7694
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.