File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Create a class called Appliance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Create a class called Appliance " Watch "Create a class called Appliance " New topic
Author

Create a class called Appliance

Emmanuel Constant
Greenhorn

Joined: Oct 20, 2009
Posts: 14
This is a homework project but my teacher rarely calls me back when I send questions can somebody explain this to me so i can best understand. I have the Java file below for it, plus the Appliance class is
almost complete. Again I'm just looking to understand


Create a class called Appliance that has the following:
1) One String type field called modelNumber that can not be modified after initialization and it is visible to outside world without any getter.
2) One int style field called serailNumber that can not be modified after initialization and it is visible to outside world without any getter.
3) One Customer type field called 'owner' that is NOT visible to the outside world without using Getter and Setter.
4) One Dimension type field called 'dimension' that is NOT visible to the outside world without using Getter and Setter.
5) Three public constructors – the first one only takes model number, the second one takes model number and owner and the third one takes model number, owner and dimension.
6) Public Getters and Setters for owner and dimension.
7) A method getNumberOfApplianceCreated() that will return object count.

Please note that you will need additional fields to meet the requirements about and the rules below but I am not going to specify them, neither do I expect them to be visible directly or via getters and setter.


Rules:

a) Serial number should be automatically assigned by all three constructors by incrementing a number by one.
b) Set the starting serial number to 100. Use constant 'STARTING_SERIAL_NUMBER ' to store starting serial number.
c) Constructors should not repeat the same code. They should call one another for execution of the same code.
d) getNumberOfApplianceCreated() should be a class method not object method


So far I have this:

Appliance.java



and here is the JUnit test
ApplianceTest.java


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm not sure what the question is.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
As David has said, we need to know what the problem is.

But welcome to JavaRanch
Emmanuel Constant
Greenhorn

Joined: Oct 20, 2009
Posts: 14
My problems is that I don't know how to do the following:

1) One String type field called modelNumber that can not be modified after initialization and it is visible to outside world without any getter.
2) One int style field called serailNumber that can not be modified after initialization and it is visible to outside world without any getter.

Our teacher gave us a Juint class and our job was to create the the Apliance class to confirm with the Junit class
in the JUnit class I have errors specific to these lines:

assertEquals(modelNumber,appliance.modelNumber);
assertTrue(appliance2.serialNumber==appliance1.serialNumber+1);
Hariharan Ram Kumar
Greenhorn

Joined: Oct 19, 2009
Posts: 3
Your code is right till Rule b).

For rule c)
Constructors should not repeat the same code. They should call one another for execution of the same code.

do the following



public Appliance(String anyModelNumber) {
modelNumber = anyModelNumber;
serialNumber = nextSerialNumber++;
}
//Static variable and non static final search in google
public Appliance(String anyModelNumber, Customer anyOwner) {
this(anyModelNumber);
owner = anyOwner;
}

public Appliance(String anyModelNumber, Customer anyOwner, Dimension anyDimension) {
this(anyModelNumber,anyOwner);
dimension = anyDimension;
}

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4176
    
  21

Emmanuel Constant wrote:My problems is that I don't know how to do the following:

1) One String type field called modelNumber that can not be modified after initialization and it is visible to outside world without any getter.
2) One int style field called serailNumber that can not be modified after initialization and it is visible to outside world without any getter.

Our teacher gave us a Juint class and our job was to create the the Apliance class to confirm with the Junit class
in the JUnit class I have errors specific to these lines:

assertEquals(modelNumber,appliance.modelNumber);
assertTrue(appliance2.serialNumber==appliance1.serialNumber+1);


You should review your book for Access Modifiers. Alternatively, you could look through this section of the Java Tutorial. You have two parts to those requirements
1) the variables have to be visible to the outside world without methods. Look for 'Access Modifiers' and read up on which you think would be most appropriate
2) the variable can not be modified after initialization. There is a keyword which performs this function as well. If you are looking through the java tutorials you might see the term 'constant' used.


Steve
Emmanuel Constant
Greenhorn

Joined: Oct 20, 2009
Posts: 14
Ok I took your advice steve, I read up on Access modifiers and this is what I came up with:




The Junit class ran with no errors; however, for this piece of code in the ApplianceTest.java Junit class:


I get the warning 'The local variable appliance1 is never read', 'The local variable appliance2 is never read', 'The local variable appliance3 is never read'
I don't understand why if the Junit class ran with no errors, why I get these warnings?
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Emmanuel:

Those warnings indicate that you haven't called any methods on those references. The Java compiler is just saying "Hey you created these, but you don't do anything with them. Did you really mean to create them?".

John.
jami siva
Ranch Hand

Joined: Oct 16, 2009
Posts: 63
Hi,

Basically i dont know JUnit, and how to use this. I just to know only this is used to test unit test cases,but i don't know how to use this and how to configure in Eclipse

Please any one tell me
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Jami:

You can go here for more information on JUnit. JUnit tests are normal Java classes that follow a few rules imposed by the framework.

John.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4176
    
  21

Emmanuel Constant wrote:Ok I took your advice steve, I read up on Access modifiers and this is what I came up with:


Whereas that fulfills the requirement that the variables be accessed from an external class without a method, it does not meet the requirement that they can not be assigned to after initialization - if some external code wanted to set modelNumber after the constructor was called they could with:



You will have to look a little deeper to get to the keyword you need to add which prevents this.
Emmanuel Constant
Greenhorn

Joined: Oct 20, 2009
Posts: 14

Adding the final to both modelNumber and SerialNumber will do it?


Emmanuel Constant
Greenhorn

Joined: Oct 20, 2009
Posts: 14
I'm wrong again. It should be this:





Is this correct?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4176
    
  21

Hi Emmanuel,

I am not bothered that you sent me an email about a forum question, but the best policy is to UseTheForumNotEmail so the entire community can benefit from the answer.

The email:
Emmanuel Constant wrote:
Hi Steve,

I hope I'm not bothering you, but I just wanted to know you said that for this:
Steve Luke wrote:


Whereas that fulfills the requirement that the variables be accessed from an external class without a method, it does not meet the requirement that they can not be assigned to after initialization - if some external code wanted to set modelNumber after the constructor was called they could with:


You will have to look a little deeper to get to the keyword you need to add which prevents this.





So I did look deeper and I can up with this:

Is this correct?


The final keyword is correct, and the access modifier (in this case 'default' no value modifier) depends on how/where you need to access the value from. If this passes your tests then the answer is yes, it is correct. A good rule to follow is to keep the scope (accessibility) as small as possible while still meeting your requirements. The requirement is that it has to be accessed from an outside class, and the default access is definitely the smallest scope that allows that, so it is the correct route to go - as long as the class that needs to access it is in the same package.
Emmanuel Constant
Greenhorn

Joined: Oct 20, 2009
Posts: 14
Thanks Steve. Yes the JUnit test passed. I really appreciate the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Create a class called Appliance