Getting so confusing this... Have many questions. PLS Help


#1

Hi guys!

Im a very beginner. I could figurite out most of the lesson even if i did not understand half of it. I dont know the basics specially when/ where i need to put brackets, write in a new line, delete an existing word to run the code or just insert a new word in methods/booleane/object ect.
Why do we write the first letters with lower case and change it to upper ones. example: startEngine or startEnginemethod? what happen if its 3 words or more?
I cant figurite out where to write the new features and why....
I think we should understand the basics with very poor basic language and get a good explanation so all the beginners will understand easier.

Im gonna insert my code but even if i get the result it wont make change based on i dont have a clue why and what im doing.

So thank you very much your help in advance. I hope this topic is going to help many beginners.

class Dog {

    int age; 
    
    public Dog(int dogsAge) {
        age = dogsAge;
    }

    public void  bark startEngine() {
        System.out.println("Woof!");
    }
  
    public static void main(String[] args) {
        Dog spike = new Dog(6);
        dogSpike.startEngine();    
    }
}


#2

Hey there. I would be happy to help clear up any confusions you may have.

Why do we write the first letters with lower case and change it to upper ones


This type of naming style is part of a very popular naming convention called: camelCase.

Almost every language uses camelCase whether it be for variable names, method names etc. Each language will have a best practice guide for what type of naming convention they use for the various elements in the programming language.

For camelCase, the first letter of the first word is always lower case. Every subsequent word thereafter must begin with an uppercase letter. This is the way.

For example:

  1. In Java we use camelCase for variables and methods: int myFavNum; or startEngineMethod();
  2. In the c# language we use camelCase for variables and PascalCase for methods: int myFavNum and StartEngine();

As you can see it differs from language to language. It's important to figure out which apply the language you're coding in.

Why follow naming conventions?

Naming conventions are monumentally important in being able to read and maintain code. By looking at how things are named we can clearly tell right away what this thing we are looking at is.

I encourage you to google naming conventions for Java so that you can better understand this important topic.


When do we need to use brackets?

Curly braces - ie { }:

Is used to contain code. Also known as a code block. We use them as a divider in java to separate code from each other. There's a more technical discussion to be had here. But all you need to know for now as a beginner is what I have described. You may wish to look this up if you'd like.

Parenthesis, Parentheses(plural) - ie ( ):

Is used to mark the start and end of a parameter list in Java. When we want our methods to use variables and values we place them in these because it's how we allow them to use it.

For example lets say we have a method called bakePie(). We may want to add parameters to bakePie() to let the method know how to bake a pie. We could set up the method to accept two parameters. One parameter to let the method know what kind of pie and another parameter to let the method know for how long we wish to bake. This can look something like this: bakePie(string kindOfPie, int howLongToBake);

Just to reiterate, this is a brief overview of the question at hand. There's a lot more than can be said. But for now, just to have some idea. This is a good start.


I can see for a fact that you have more questions, but I don't really understand them. Those were the two main ones that stuck out to me.

Is there anything else you need clarification on?


#3

Thank you so much! You are extremly helpful!

Could you please explain me why you put int there "bakePie(string kindOfPie, int howLongToBake)" and why you did not put int in front of the the first word.

I know my other questions were confusing but i try to explain my issue with an example.:

Instructions
1.
In between the constructor and the main method, add a method called bark to the Dog class by typing:

public void bark() { **ok but where is the contractor and the main method? which line/row should I input it?**

}
2.
Inside the bark method, type:

System.out.println("Woof!"); Where is the bark method again? Is it the public void bark?

3, add a method called bark to the Dog class by typing:

class Dog { (

int age;

public Dog(int dogsAge) {
age = dogsAge;

}
public void bark startEngine() {

System.out.println("Woof!");
}

public static void main(String[] args) {

Dog spike = new Dog(6);
dogSpike.startEngine();

}
}

At least i do understand each meaning of the symbols thank for you, but i cant find out if i have a new idea or i wanna be more specific and detail with the object (e.g.: rose->rose is red-> roses are red and beautiful ) then which line belongs to. Like above, I should have input the result but did not know where to write so i was just guessing and it was worked.

So please forgive me to bothering you with all these silly questions, but i really want to understand it.

Thank you very much in advance!


#4

No such thing as a silly question. I'll try my best here.


bakePie(string kindOfPie, int howLongToBake)

The reason I specified int here is because when we pass a value into this method I want to pass a whole number so that the method knows how long to bake for. As for string kindOfPie, this is the variable I pass to the method to tell it what kind of pie I want to bake. It's not an int because I want to pass word(s) not numbers.

Lets say for example I want to use this method somewhere else in the program I can refer to the defintion I set up before. I could do something like this

bakePie("Blueberry", 120) // kindOfPie = blueberry, howLongToBake = 120(mins)

Now if this was a real method, the method would know that I wish to create a blueberry pie because I specified it appropriately as a string, and that I wish to cook it for 120 mins because i specified it as an int. It'll take those parameters and do whatever the method is coded to do.


Ok but where is the contractor and the main method? which line/row should I input it?

This is the constructor.

public Dog(int dogsAge) {
age = dogsAge;

}

Just in case you don't know: A constructor is a special kind of method. As you may have noticed it has the exact same name as Dog class. This is what makes it a constructor.

When we make an object of the Dog class the constructor is automatically called to initialize fields in our object. We can see this happening with age = dogsAge;

So when we say

Dog spike = new Dog(6);

spike is the object being created here. And Dog(6) is the constructor that is being called to initialize our new Dog object. 6 will be passed through in the constructor params and spike's age will be set.

And remember classes are essentially our own data types. So basically.. spike is of type Dog.
As age is of type int.


This is the main method:

public static void main(String[] args) {

Dog spike = new Dog(6);

}

We can tell it's the main method because it's called main. The main method is another special method. . When java programs run there is usually a main method that the Java Runtime Environment will look for first. It's the entry point. It's what allows other stuff to happen. It's the key to the rest of the program. Without it, Java is lost.

Now as for where do we put the bark() method? Between the constructor and the main. Just be careful not to put the bark method inside of the constructor. It's should be on it's own just below the constructor.

The bark() method should look like this:

public void  bark() {

  System.out.println("Woof!");
}

Hopefully that covers everything. If you're really stuck I'll include the full code below. Study it if you have to. It only gets more challenging from here on out.

class Dog { // dog class starts

   int age; // set by Dog(int dogsAge)

   public Dog(int dogsAge) { // Constructor. Intializes our Dog objects
   age = dogsAge; // see above

   } // constructor ends

   public void  bark() { // this is our bark method. It prints woof when called

     System.out.println("Woof!");
   } // bark method ends
  
   public static void main(String[] args) { // this is the special main method. It's the entry point of the program

   Dog spike = new Dog(6); 
   // this is where we create a variable called spike. spike is an object of type Dog. We pass 6 into the constructor so that spike's age can be set to 6 upon object creation.
  
   } // main method ends
} // dog class ends

Hope that helps!


#5

Thank you so much. I wish you were a teacher and i could join your class. I'm really preciate all your patient and time what you spent on my struggles. I really want to study java even if i have never met with any coding language before. I wont give up and practice day and night.It's quiet necessary to learn it because I work for Oracle and they have offered me a position; but they are asking me to learn Java coding.

So Dear.angusBeef () :slight_smile: You are awesome and very helpful. Thank you so much one more time!!! Have a wonderful day


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.