8. Methods: I (do not understand why my code works, but throws 9 errors)


#1



Hi there. So I got this code to work, in that the 2 objectives in the instructions are green-checked, and I am able to click next. It looks pretty good to me, however I am receiving 9 errors and don't understand a few things.

First off, how the heck does this code work if it is throwing 9 errors? I feel like it shouldn't run with 9 errors. Secondly, what are these errors telling me, and what can I do to clear them?


https://www.codecademy.com/en/courses/learn-java/lessons/object-oriented-programming/exercises/methods-1?action=lesson_resume&link_content_target=interstitial_lesson


Dog.java:9: error: illegal start of expression
public void bark() {
^
Dog.java:9: error: illegal start of expression
public void bark() {
^
Dog.java:9: error: ';' expected
public void bark() {
^
Dog.java:15: error: illegal start of expression
public static void main(String[] args) {
^
Dog.java:15: error: illegal start of expression
public static void main(String[] args) {
^
Dog.java:15: error: ';' expected
public static void main(String[] args) {
^
Dog.java:15: error: '.class' expected
public static void main(String[] args) {
^
Dog.java:15: error: ';' expected
public static void main(String[] args) {
^
Dog.java:20: error: reached end of file while parsing
}
^
9 errors


class Dog {
  
  int age;

	public Dog(int dogsAge) {
    
  	age = dogsAge;
  
  public void bark() {
    
    System.out.println("Woof!");
  
  }

	public static void main(String[] args) {
    
    Dog spike = new Dog(5);
    spike.bark();
	}
}


#2

All those 9 errors arise because of just one thing:

public Dog(int dogsAge) {
    
  	age = dogsAge;

a missing closing brace }.


#3

@moonsocket,
Look at the code imagine.....

With

class Dog  {

}

you prepare the creation of a playing-ground which is bordered by a pair of curly-brackets-{ }

Within this playing-ground preperation we use

  • int age;
    which is declaring a variable age which is of the data-type int (integer)
  • then we create a Dog =function= with its code-block encapsulated by its own pair of curly-brackets-{ } (this a so-called object constructor)

      public Dog(int dogsAge) { 
          age = dogsAge; 
      }
  • then we create the bark =function= with its code-block encapsulated by its own pair of curly-brackets-{ } (this is a so-called Method which will be available to the object)

      public void bark() {
          System.out.println("Woof!");
      }
  • then we create an execution-area with with its code-block encapsulated by its own pair of curly-brackets-{

      public static void main(String[] args) {
        //here we place the code we want to execute ....
      }
=======================================================================

So what we have done upuntill now
Class: a blueprint for how a data structure should function ( class Dog { } )

Constructor: instructs the class to set up the initial state of an object ( Dog( int dogsAge) { } )

Method: set of instructions that can be called on an object ( bark() { } )

=== in the execution-area ========================================
  • Dog spike = new Dog(5);
    Object: instance of a class that stores the state of a class
    By using the new operator we create an object spike

  • spike.bark();
    we call the bark() Method of this spike object

=======================================================

Parameter: values that can be specified when
- creating an object ( we used the =number= Value 5 )
or
- calling a method ( we defined the bark() Method as having NO parameter )

Return value: specifies the data type that a method will return after it runs (use void if you don't return data)

https://docs.oracle.com/javase/tutorial/java/javaOO/classdecl.html
https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
https://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html