Results 1 to 6 of 6

Thread: Define Attributes of a java class

  1. #1
    Join Date
    Mar 2010
    Posts
    202

    Define Attributes of a java class

    Hello,
    I have the following classes:

    1) Class Card is a list of items (books, dishes ,...)
    2) Class EsembleCarton is a list of boxes
    3) The Article class

    1) private int weight; / / Weight in kg (1 to 50 kg) Is it a way to limit in java
    the weight of 1 to 50 kg

    2) private String degreResistance / / degree of resistance to shock is weak or not

    3) private String typeArticle / / or book, dishes, clothes, others ...) -> I set the type as string but I thought an ArrayList or a simple table.

    4) private String descriptionArticle / / (string may be empty).

    Now, my question is that is this the right way to define attributes of a java class.

  2. #2
    Join Date
    Nov 2009
    Posts
    583

    Re: members of a java class

    Hello,
    You can define as it is howing the preceding example
    Code:
    Public void setPoids(int wg){
    if ( (wg> TRP) | | wg <TRP){
    / / processing -> throw exception or truncate ...
    }
    }
    I think this is the perfect way of defining the attributes, though there may be other efficient ways, but I think this is the one.

  3. #3
    Join Date
    Nov 2009
    Posts
    347

    Re: Define Attributes of a java class

    Hello,
    A map that has relationship an element with a unique key, which is not your case. You should therefore use a List. To choose between a LinkedList or an ArrayList it depends greatly on the type of use setter and getter, go see the javadoc to understand the difference. Try also to use variables in lowercase and plural for collections.
    Code:
    private List it = <Article> new ArrayList <Article> ();
    Also use a more explicit name, then I do not see just how the article name.
    Here is the code for it
    Code:
    private List <Carton> bx = new ArrayList <Carton> ();

  4. #4
    Join Date
    Nov 2009
    Posts
    518

    Re: variables of a java class

    Hello,
    It depends on usage. If the types of items are finished and should be quit and private property variables, you should use object orientation by creating a class which inherit Item class books, dishes, ... and you would have them in checkup a toString method that will write the item description. But in your case I think the string is simple enough. Hope this information will help you. If you have any more problems then do post back.

  5. #5
    Join Date
    Nov 2009
    Posts
    359

    Re: members in a java class

    Hello,
    Follow the following
    1 - create a new set of boxes;
    Say in the main method that you put in a class that your name or Applications Main.
    Code:
    EnsembleCartons al = new EnsembleCartons();
    2- INSEE an object in a set of boxes;
    You therefore need a method add (Card card) in your class EnsembleCartons.
    Code:
    Public void add(Cardboard car){
     car.add(cardboard);
    }
    Cardboard is a list of type ArrayList <Carton> object.

    3- list the numerous cartons of a set of boxes;
    You create a eNews method in class EnsembleCartons:
    Here is the code for the same
    Code:
    Public void printCartons(){
       for(Cardboard car: car){
          System.out.System.out.println("Cardboard" + Cardboard.id);
       }
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    446

    Re: attributes of java class

    Hello,
    I give an example to clarify the thing in your case
    See this code
    Code:
    for(Cardboard car: a.car)
    To the left of the symbol ":" you have the type of object that contains the collection and a variable change value at each iteration. On the right you have the collection.
    The code is equivalent to:
    Code:
    Iterator itr = all.cartons.itr();
    while(itr.hasNext()){
       Cardboard b = itr.next();
    }

Similar Threads

  1. Explain how to define function outside the class in C++?
    By Palnila in forum Software Development
    Replies: 4
    Last Post: 29-12-2010, 03:11 AM
  2. Converting java class to java bean class
    By Ucchal in forum Software Development
    Replies: 6
    Last Post: 09-08-2010, 10:24 AM
  3. Define Manifest File in Java
    By Sandy22 in forum Software Development
    Replies: 3
    Last Post: 09-11-2009, 06:34 PM
  4. The keyword "static", attributes and methods of class in OOP
    By DotNetUser in forum Software Development
    Replies: 3
    Last Post: 05-01-2009, 11:37 AM
  5. Java Programming Language Basics: Reflection Basics and Class Class
    By mayuri_gunjan in forum Guides & Tutorials
    Replies: 6
    Last Post: 29-08-2005, 12:04 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,305,596.11446 seconds with 17 queries