Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help lol

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Thanked 0 Times in 0 Posts

    Default Help lol

    Hi there,

    my background is totally c based but I just started looking into Java, I came across an example which im trying to code in java,

    i got a load of data, people with their attributes (hair colour, eye colour etc) and i need to ask questions and then work out who they are thiknig about. The questions part i got and working out who they on bout i got but how do i store the data. In c i would just use a structure

    struct attributes{
    char eyecolour
    char haricolour

    but i cant seem to do this in java, any advice lol

  2. #2
    Junior Member
    Join Date
    Nov 2011
    My Mood
    Thanked 1 Time in 1 Post

    Default Re: Help lol

    To learn how to do this in java you have to really understand how object orientated programming works, I will try and explain it here but watching some tutorials or reading some reference books will give you a more full understanding.
    So in java you have a class, and this is something generic (like for instance a person), that class contains attributes that all people would have, called variables, and actions that all people can do, called methods, you then create objects of this class, and set the specific variables for that object, so I could create an object of the class person, and set it's hair color to brown and it's eyes to green. Then I can make that person do stuff by calling the persons methods, for your program the stuff you want them to do is answer questions, so you would have a method called ask question, this would take in a string parameter of the question that is being asked, and the method body would contain something that works out what the question is and asks according to the attributes that have been set. So an example of your person class would be
    public class Person{
           String hairColor = null;
           String eyeColor = null;
           public void askQuestion(String question){
                   //check to see if the attributes have been set yet
                   if(hairColor == null || eyeColor == null){
                          System.out.println("you must set variables before asking a question");
                   //check to see what the question was
                   if(question.equals("How are you feeling")){
                          //no idea why blonde people feel fine and others don't lol
                                System.out.println("I am fine thankyou");
                                //were done with the method, get out of here
                                System.out.println("I feel so down and depressed with life");
                                //were done with the method, get out of here
                          //execute this if the hair color wasn't blonde or brown (a cleaner way to do this repeated if's followed by an else if none were true is in a switch 
                          //statement, this bit of code goes in the defualt part (google it))
                                  System.out.println("Sorry, I don't know what to do if my hair color is" + hairColor);
                          System.out.println("I couldn't be bothered to code in any more questions lol");

    Now to use that code we need to create an object of person, we do this in a main method (this is the method in java that runs first, you cannot run a program if it doesn't contain one of these, it is static which means it isn't object orientated (it isn't something that all objects of a generic type can do it's just a utility method put in that class for organizational purposes).
    So we will create a new class for that method to be in like so:
    public class main{
           //main method
           public static void main(String args[]{
                  //create an object of person
                  Person examplePerson = new Person();
                  //set example persons attributes
                  examplePerson.hairColor = "brown";
                  examplePerson.eyeColor = "green";
                  //ask example person a question
                  examplePerson.askQuestion("How are you feeling");
    So the output in the console from this would be: I feel so down and depressed with life