Jump to content

Hi guys am new to programming

- - - - -

  • Please log in to reply
2 replies to this topic

#1
kedah160

kedah160

    Newbie

  • Members
  • PipPip
  • 17 posts
i've just started to learn java yesterday from some books and am doing pretty good i guess i started doing some exercise but i came to this question where it asks me to create a class of student with methods and attributes so i know what is class n methods but i dont know how to do it any guideline?thank you

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Example:

public class ClassName{

   private int attribute1;                        //attribute

   private String attribute2;                     //attribute


   public ClassName(){                            //constructor

       attribute1 = 2;

       attribute2 = "word";

   }


   public void printAll(){                        //method

      System.out.println(attribute1 + " " + attribute2);

   }


   public int getAttribute1(){                    //method

      return attribute1;

   } 


   public void setAttribute2(String attribute2){  //method

      this.attribute2 = attribute2;

   }

}



#3
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
The syntax to do it is above, but if you are struggling on what exactly they should do/hold then it's best to think as real-world as possible (unless you are given exact attributes to create). So you should create attributes to hold things age, student number, name etc etc.

It is then best to have these as private attributes (so outside classes cannot tamper with them in a way which is out of your control), so you should create set and get methods as above. Get which returns the attribute, and set which changes an attribute to one given as a parameter. :)

You then add any methods that you need to, to carry out anything else you might need from a student object.
My Company - My Homepage - My Twitter - My Google+ - My LinkedIn

"Things don’t have to change the world to be important.” - Steve Jobs




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users