Java Help

Discussion in 'School Work Help' started by Woody, Sep 28, 2010.

  1. Woody

    Woody Well-Known Member

    213
    41
    0
    Code:
    
        int test = this.age;
    
    
    --So "this" refers to the attribute of the object named "helpMe".

    What is the difference if I write it as:

    Code:
    
        int test = helpMe. getAge();
    
    
    Thank you for helping.
     
    #1 Woody, Sep 28, 2010
    Last edited: Sep 28, 2010
  2. no, "this" does not refer to the attribute of the class "helpMe", "this" refers to the actual class "helpMe".

    "age" is an attribute of the class "helpMe", so when you say "int test = this.age", it means you're storing the value of class helpMe's age, into variable "test".

    the two lines you wrote are for wholly different problems....

    is used inside of class helpMe.. "this" means "use my current class that im in right now".. so somewhere in class "helpMe", if you have a method that implements "int test = this.age", it takes the class variable's age and stores it into test...

    however

    is assuming that you have initialized the "helpMe" class... so say you have something like:

    helpMe helpMe1 = new helpMe(20);
    helpMe helpMe2 = new helpMe (30);

    where helpMe's constructor stores the value into "this.age", calling helpMe1.getAge() returns 20, and helpMe2.getAge() returns 30... you see, there are two instances of helpMe, in which each store a different age............... it's used to get your information outside of the helpMe class, when you implement it....

    HOWEVER, in your helpMe class, if you have some other method that requires the age, "int test = this.age;" is equivalent to "int test = this.getAge();"... this is used if your class variable "age" is private, it is a convention to use get/set methods instead of calling the attribute directly.


    NOW, all i've said above is assuming that your class variable "age" is not "static". IF "age" is static, it means despite having multiple instances of helpMe (i.e. helpMe1, helpMe2 etc etc), all of those instances will share the SAME object "age".... so if age is static, and you call helpMe1.getAge() and helpMe2.getAge(), IT WILL RETURN THE SAME VALUE!!
     
  3. Woody

    Woody Well-Known Member

    213
    41
    0
    Hello. :smile:

    So, I believe in the main class. It should have a statement:

    CreditCard copy;
    // and copy have all the assigned attributes.
    CreditCard c = new CreditCard (copy)
    // assigns attributes of copy to c.
    // copy and c points to it's own attribute.

    Is this right?

    Also, what is a deep copy?
     

    Attached Files:

  4. dude... keep your questions in one thread... you made a previous thread which i replied to, and you never replied... keep your questions in one thread...........

    Mods, please merge his threads together....

    anyways as for this question, using what you said:

    CreditCard copy;
    CreditCard c = new CreditCard(copy);

    I'll translate that into english:

    You made a CreditCard called copy.. (it has nothing, because it has not been initialized).
    You made a new CreditCard from the CreditCard copy... and assigned it to c...

    I don't know if that's right, because you essentially made a credit card out of a credit card........ from that, I assume the class CreditCard takes a parameter CreditCard, which is kinda weird, because you're passing in a class into itself...

    as for types of copies, there are three types of copy methods... deep, lazy and shallow copy.

    shallow copy is when you're copying, but you're not really copying... in the computer memory, if you have a copy 'A', and you want to copy into 'B', both 'A' and 'B' are pointed to the same copy of 'A'... they point to the same memory block that holds the information of 'A'

    deep copy is when you actually make hard copies of 'A' into 'B', which means you have 2 hard copies of data...

    lazy copy is basically a combination of both, in which at the beginning, 'A' and 'B' are pointing to the same memory block containing the information (shallow copy)... when a modification is made to an item of 'B', a new copy of that item is created (deep copy)

    more info: http://en.wikipedia.org/wiki/Object_copy#Deep_copy
     
  5. Woody

    Woody Well-Known Member

    213
    41
    0
    Thanks. I understand it now.

    :p
     
  6. Woody

    Woody Well-Known Member

    213
    41
    0
    question about singleton.

    Maintaining a Singleton:

    Code:
    
    private static Rectangle instance = new Rectangle();
    
    ......
    
    public static Rectangle getInstance()
    {
    return Rectangle.instance;
    }
    
    

    Is the line: private static Rectangle instance = new Rectangle();
    in the Rectangle class? I'm thinking it should be in the main class.
    I don't know why this is a singleton. Thank you. This is hard. :p
     
  7. Knoctur_nal

    Knoctur_nal |Force 10 from Navarone|

    16,563
    662
    29
    Keep all the Java threads together or differentiate with names.

    Knoc
     
  8. The constructor method "Rectangle()" is a method of the Rectangle class, yes. However, the line Rectangle instance = new Rectangle(); can be called anywhere outside of the Rectangle class, not just in main.

    so regardless of whether it's in main or not, any other class that needs to make a Rectangle, you would call the line private static Rectangle instance = new Rectangle();

    A singleton is defined as something with one element... so a class with one element..

    But what im confused about is this... you have a conflict... you called a new Rectangle as "instance"... however the "instance" variable is static.... they're two conflicting attributes.... when you have a static variable, despite how many instances (copies) of a class you have, they will all share the one copy of the static variable.... but if you call it an instance, that assumes that there are multiple copies of "Rectangle" which is contradictory to the definition of "static"....
     
  9. Woody

    Woody Well-Known Member

    213
    41
    0
    I think I get it now. It seems like the rectangle class is the class that can only create the instance, since it's a private, so one instance can be created in that class. It's a static because it belongs to the rectangle class.

    There is a better example of singleton in the book, this is just one of them. Thanks.
    I have a computer science test today. So scared. :D
     
  10. hongkongboy

    hongkongboy Well-Known Member

    707
    68
    0
    I know what i say now got nothing to do with this topic but Dan can i ask or would you say/agree that you know decent amount of knowledge about Java

    I got a feeling my project idea is gonna be accept lol, and my idea is using Java SDK Sony Ericcsson I am gonna make a application that will encrypt data by inputing 4 digit code

    you think that be hard to do? ive downloaded all the software I need and atm Java and sdk all looks like Aliens
     
  11. Woody

    Woody Well-Known Member

    213
    41
    0
    The test was hard. :p

    What does it mean when:

    main class:
    Code:
    Rectangle r1 = new Rectangle(); // width and height is zero
    Rectangle. setDefaultWidth(3);
    Rectangle. setDefaultHeight(4);
    Rectangle r2 = new Rectangle();  //now, width is 3, and height is 4
    
    This was on the test. I believe setDefaultWidth is an attribute of the Rectangle class.

    The question asked me to write a constructor for this. Also mentions to write a method called setDefaultHeight and setDefaultWidth. So I was confused.:smile:
     
  12. I can't tell you if it's hard to do or not. it depends on two things: 1) your aptitude with java 2) the requirements of your application. the more requirements, the harder it is, but other than that, i really can't tell you if it's hard to do or not.

    setDefaultWidth is not an attribute of the rectangle class.. it's a method of the rectangle class.. it sets the defaultwidth given an integer parameter... initially, the two class variables of rectangles are:

    defaultWidth = 0;
    defaultHeight = 0;

    when you call the constructor new Rectangle(), it creates an instance of the rectangle class, and uses the default dimensions of 0... when you call rectangle.setdefault____(int), it sets the variables to whatever specified, and the next time you instantiate the class, it uses the new default values....
     
  13. Woody

    Woody Well-Known Member

    213
    41
    0
    Awww man.
    I was thinking """Rectangle. setDefaultHeight(4);""" means Class.Attribute (like a static attribute).<_<
    So, I guess it's a method. Thanks. I get it now.
     
  14. i see where you're getting confused............

    so here's an example of the Rectangle class:

    Code:
    public class Rectangle {
         private static int defaultHeight = 0; // static variable
         private static int defaultWidth = 0; // static variable
    
         public Rectangle() { // constructor
              // implement constructor
         }
         public static void setDefaultHeight(int i) {
              Rectangle.defaultHeight = i; // keep this line in mind... let's call it line (a)
         }
         public static void setDefaultWidth(int i) {
              Rectangle.defaultWidth = i;
         }
    }
    
    and in your main method:

    Code:
    Rectangle r1 = new Rectangle(); // width and height is zero
    Rectangle. setDefaultWidth(3);
    Rectangle. setDefaultHeight(4); // keep this line in mind... let's call it line (b)
    Rectangle r2 = new Rectangle();  //now, width is 3, and height is 4
    
    you see, you're mixing up line (a) and line (b).... Rectangle.defaultHeight is a class variable (or as you call them attributes), whereas Rectangle.setDefaultHeight(int) is a method.......... the way you distinguish one from the other is with the parentheses "()" . variables do not have parentheses, whereas methods do.

    parentheses allows for parameters to be specified, such as "public static void setDefaultHeight(int i)". In some IDEs (code editors), they will even color code the class variables for you, so that you may distinguish it easily.

    on a different note, you were thinking that setDefault_____(int) was an attribute. Keep in mind that you do not want your class attributes to be public, as in, you do not want to be able to call Rectangle.defaultHeight outside of the rectangle class itself. The reason for this is because you want to be able to control who gets to use your variables... if you set it to public, everyone can see it, and generally that's not good coding. The correct way of manipulating variables, is as described above. Make your variables PRIVATE, and create get and set methods to manipulate the variables in a controlled manner. So setDefault______(int) is called a "setter" method. a getter method can be written as well, by adding something like this to the rectangle class:

    Code:
    public static int getDefaultHeight() {
         return Rectangle.defaultHeight;
    }
    public static int getDefaultWidth() {
         return Rectangle.defaultWidth;
    }
    
    I know this is a lot of stuff to absorb, but a last note.... when do you use Rectangle.defaultHeight, or this.defaultHeight. Since defaultHeight is static, we must use the class name. if a variable is not static, we can use "this"
     
  15. hongkongboy

    hongkongboy Well-Known Member

    707
    68
    0
     
  16. this is why you plan before you execute........

    when you create a product, you don't just jump into it and start coding.

    1) that will take you like 4 times as long, because you are doing everything on the fly, and probably rewriting code duplicates....
    2) it's going to be as messy as hell
    3) you will have no idea how your end product will look like, in which case, you would probably fail to meet the criteria.

    there are phases that you need to do:

    Requirements
    Specifications
    Architecture
    Design
    Implementation
    Testing
    Deployment
    Maintenance

    what you need to do, is plan your project. get a list of all the requirements that you have to meet. then, use those requirements as specifications for your project. which means, you need to use those specifications as guidelines for your project. you then come up with a structure for your project, figure out all the components of your project, and then you design your project around it.

    at this point, you should have a blueprint of what you need to do before you start coding. at this point, you tell yourself if this is something you can do or not. (in a corporation though, they wont give two shits if you don't know how to do it. they expect you to find ways to make it work.)

    so start gathering all the data, and start planning. figure out if that is something that you can do or not.
     
  17. hongkongboy

    hongkongboy Well-Known Member

    707
    68
    0
    100% agree but also disagree lol, what you said is great I think I should follow those steps in order to achieve something, but planning something could be time consuming

    for example my situation now, my project idea was to design application on Sony Ericcson application phone to encrpty files etc at first I though this task would be easy to do I design to email my lectures n forward this idea.

    note: the project idea is overdue i should be starting it NOW tbh but yeh since i dont have any idea im abit stuck.. but back to my point, i spent A WEEK doing research well what you call it the first stage of what you said:

    Requirements
    Specifications
    Architecture

    after doing mass research gathering all information n software I need it becomes more clear what im trying to achieve is much more complicated than i first expected as a result i dropped the idea AGAIN -surr

    you see where im coming from dan, i agree what you said is right, have to plan etc.. but sometimes you got learn to run before you walk

    p.s my current idea i have from lectures is to make a interactive website that will allow user to learn topic of computer security all a wide range e.g wireless connection key, WPA WPA2 WEP, encrpying files, deleting history on web browswer etc,, all basic stuff

    atm im doing my research and it seem do-able for MY ability -blush2
     
  18. lol. it's up to you if you want to agree or disagree with the concept i mentioned. the mentioned concept is something that's being applied in all companies all over the world. so if you disagree with the standard concept, then that's up to you lol

    other than that, there's really nothing i can tell you, because if you think running before walking is suitable for you, then go ahead and do it. i'm just saying that running before walking takes much more time, but if it floats your boat, then do it. nothing much more that i can tell you lol
     
  19. understand what override is first:

    http://www.java-tips.org/java-se-tips/java.lang/what-is-overriding.html
     
  20. are you sure your prof hasn't covered this in lecture? this is pretty important stuff for him to miss................