coders club java tutorial
TRANSCRIPT
Homework for August 9th 2015
JAVA
Shivaum Kumar and Aditya Aggarwal wrote the following tutorial for the Saratoga Young Coders Club.
Introduction
• In this tutorial, you will learn the fundamentals of JAVA.
• JAVA makes an Android app interactive.
• Interactivity is when the user touches the UI the UI changes.
o For example, if you hit a send button for a message box, you
want the UI to change so you know the message is sent.
Caution
If you want to continue Android Development after this tutorial, I strongly recommend taking a JAVA course or learning some more JAVA. I simplify JAVA fundamentals like variables and skip over concepts like
scope and method return types.
Nonetheless, this tutorial provides a good starting point if you have never programmed in JAVA.
Part 1 (Methods and Objects)
Objects are common nouns.
• A person is an object. There are many persons.
• A dog is an object. There are many dogs.
• A ball is an object. There are many balls. Objects can have attributes like elements in XML. These attributes are called variables. JAVA runs on making objects do stuff. Objects do stuff through methods.
• All people can move, so all people have a move method.
• All dogs can bark, so all dogs have a bark method.
• All balls can bounce, so all balls have a bounce method. Methods are your action verbs for your objects.
Open a text editor for Mac users TextEdit and for
Windows users Notepad . Type in public class Dog { } This is JAVA code that creates an object. Dog, highlighted in green, is the name of the object you just created. Since you have to write class when creating a new Object, programmers also call objects classes. You created a Dog class in your text editor. You place an object’s methods and variables inside the curly braces. Everything inside the curly braces is called the class definition.
In your text editor in the Dog class definition, type in public void bark(){ //Code that makes a Barking sound! } This code creates a bark method for the Dog class. For this tutorial, you do not need to know about the code above highlighted in red. Ignore it. The code highlighted in green is the method’s name. You will learn what the parentheses () in the bark method does in the Part 3 of this Step. The code inside the curly braces of the bark method is the bark method definition. It is where you define what a method does. You can change a value of a variable of in a method. In the bark method definition, I made a comment, code that doesn’t do anything. To make a line of code a comment, you start it with //.
The code in your editor should now look like the code below. public class Dog {
public void bark(){ //Code that makes a Barking sound! }
}
The code creates a dog class with a bark method.
Part 2 (Object Instances)
Remember, an object is any common noun. An object’s instance is a particular noun.
• I am an instance of the object person.
• My friend’s dog Spot is an instance of the object Dog.
• My football is an instance of the object ball. Just like a person can be different from another person, one object instance can be different than the other. Below the Dog class in your text editor, type in Dog spot=new Dog(); This code makes an instance of a Dog. The code highlighted in green is the instance name. The code highlighted in red is the instance’s object name. In the line of code above, Dog() looks like a method. It is a method. Dog() is a method that is called whenever you create a new instance of a Dog. Methods like Dog() are called constructors. Java automatically creates a constructor so you do not need to put it in the class definition. However, if you want the Dog object to do more than just create a new instance of a Dog, you need to put the constructor in the class definition.
In your text editor, under the line that creates the instance spot, type in spot.bark(); This code makes spot bark. You defined how all instances of Dog bark in your Dog class’ bark method. To make an instance of an object do its method, you write the instance name, a dot . , the method name, and then parentheses () followed by a semicolon ;. Instancename.methodname();
The code in your editor should now look like the code below. public class Dog {
public void bark(){ //Code that makes a Barking sound! }
} Dog spot= new Dog(); spot.bark();
• The code creates a Dog class with a bark method.
• The code makes a new instance of Dog named spot.
• The code makes spot do the bark method.
Part 3 (Methods in Depth) Objects have variables. Variables have values. These values can be numbers, text, and other object instances. An object’s methods use its variables.
• Methods that set the values of variables are called setters.
• Methods that get the values of variables are called getters. Let us say the Dog class in your text editor has an nose variable. In your text editor above your Dog class, type in: public class Nose{ } This creates a Nose object. Let us say the nose variable in your Dog class has a Nose object instance as a value.
Inside your Dog class definition and below all of its current methods, starting on a new line type in public Nose getNose(){
//code to get nose variable value } This method is used to get the nose variable value. Next, following the same instructions on where and how to place the getNose() method in your editor, type in: public void setNose(Nose nose) { //code to set Nose } setNose sets the nose variable to a value. The code inside the parentheses and highlighted in pink is called a parameter. setNose does not automatically know what Nose instance to set the nose variable to. In the parentheses you write in a nose object instance in order to solve this problem.
Under your last line of code in your text editor, type in Nose spotsnose= new Nose(); spot.setNose(spotsnose);
This code created spotsnose an instance of the Nose class and set spot’s (spot is an instance of Dog) nose variable to spotsnose.
The code in your editor should now look like the code below. public class Nose { } public class Dog {
public void bark(){ //Code that makes a Barking sound! } public Nose getNose(){
//code to get nose variable value } public void setNose(Nose nose) { //code to set Nose }
} Dog spot= new Dog(); spot.bark(); Nose spotsnose= new Nose(); spot.setNose(spotsnose);
• The code creates a Nose class.
• The code creates a Dog class with a bark method, a getNose method, and a
setNose method.
• The code makes a new instance of Dog named spot.
• The code makes spot do the bark method.
• The code creates spotsnose a new instance of Nose.
• The code sets spot’s nose variable to spotsnose.
Part 4 (Inheritance)
You can make a Dog class and an Animal class. They are both common nouns.
A dog is an animal. How do you tell the computer a Dog class is an
Animal class, when they are different objects? You use inheritance. Inheritance allows you to make one class a
category in another class.
• Teenager a category in Person
• Dog a category in Animal
• Basketball a category in Ball
In your text editor, above all your objects, type in public class Animal {
public void breathe(){ //code that makes the animal breathe }
} The code above creates an animal object with a method breathe. In your text editor, change the line public class Dog { to public class Dog extends Animal { This code makes the Dog object also an Animal object. Therefore, all instances of a Dog object are an Animal object. extends gets all methods and variables from the object on the right side of extends (highlighted in pink) and then sets them to the object on the left side of extends. Dog has all of the Animal’s attributes and methods. This is also called Dog inheriting the methods and attributes of Animal.
On a new line below all the code in your text editor, type in spot.breathe(); Since the Dog extends Animal, Dog inherits the method breathe which is why the method can be used. Dogs breathe in a particular way. They don’t breathe like all animals. Because dogs breathe differently than other animals, you would want to add code to the breathe method in the Dog class. In your text editor, on a new line in the dog class definition type in public void breathe(){
super.breathe(); //code to make a dog pant while breathing
} This code makes the Dog class’ breathe method different from the Animal class,’ while inheritance still happens. This is called overriding a method. The Dog class breathe method works like the Animal class breathe method, but you can also add the additional code to make the Dog class breathe method different. In the method definition, the line super.breathe(); tells the Dog class to include the code for the breathe method in the Animal class. The code after super.breathe(); is additional code that makes the Dog class breathe method different.
You can limit a Dog instance’s access to methods and attributes to that of an Animal. Below all your current code, on a new line in your text editor type in Animal animalDog= new Dog(); This code creates a Dog instance animalDog that can only use the methods and attributes from the extended Animal instance. Therefore, animalDog would be better considered an Animal instance. Below all your current code, on a new line in your text editor type in Dog regularDog=(Dog)animalDog; (Dog)animalDog allows you to access the methods and attributes of the Dog class again. It “unlimits” the instance. Dog regularDog= is used to name this instance with complete access regularDog. To “unlimit” an instance, you type in the category class’ (the child class’) name in parentheses () and then the name of the instance to be “unlimited.” (Class to be converted to)instanceName You can only do this when the instance is an instance of the class in parenetheses also called the class being casted to.
The code in your editor should now look like the code below. public class Animal {
public void breathe(){ //code that makes the animal breathe }
} public class Nose { } public class Dog extends Animal {
public void bark(){ //Code that makes a Barking sound! } public Nose getNose(){
//code to get nose variable value } public void setNose(Nose nose) { //code to set Nose } public void breathe(){
super.breathe(); //code to make a dog pant while breathing
} } Dog spot= new Dog(); spot.bark(); Nose spotsnose= new Nose(); spot.setNose(spotsnose); spot.breathe(); Animal animalDog= new Dog(); Dog regularDog=(Dog)animalDog;
• The code creates an Animal class with a breathe method.
• The code creates a Nose class.
• The code creates a Dog class that extends the Animal class and has a bark
method, a getNose method, a setNose method, and an overridden breathe
method.
• The code makes a new instance of Dog named spot.
• The code makes spot do the bark method.
• The code creates spotsnose a new instance of Nose.
• The code sets spot’s nose variable to spotsnose.
• The code makes spot breathe.
• The code creates the Dog instance animalDog with access limited
to Animal attributes and methods.
• The code then casts animalDog back to Dog and names this casted
instance regularDog.
Part 5 (Importing Classes)
You can import classes from files called libraries. Then, you can make instances of these classes without creating them.
The code for importing classes is at the top of a JAVA document (page of code).
At the top of your code in your editor start a new line and type in: import com.developer.cat;
The Cat class is created in the library com.developer.cat. The code in the library is: public class Cat { }
By using import com.developer.cat, you add all the code in the library. Therefore, you include public class Cat {}.
Now, you can instantiate (make instances of) cat without ever having to create it.
At the bottom of your code in your editor start a new line and type in: Cat cat=new Cat();
If com.developer.cat had more classes and Cat had more methods, this would save a lot of code. Often libraries have many objects that have many methods.
Libraries can be imported from a file on your computer or from online.
You can make Android Studio import important classes for Android programming automatically, so for this tutorial, you will not
Part 6 (Anonymous Classes) Anonymous classes are classes that are created during instantiation. At the bottom of your code in your editor start a new line and type in, Mouse mouse=new Mouse(){ public void squeak() { //code to make a mouse squeak
} };
The class definition is highlighted in yellow. Rather than using public class Mouse {}, this code creates Mouse right when you instantiate it.
The syntax for an anonymous class is the instance code (Object
objectname=new Object()), the class definition in curly braces {}, and then a semicolon ;.
Usually, you do not use an anonymous class, because you can only
make one instance of it; however, you will need to use it for this tutorial.
An import statement may require you to include certain methods
in any anonymous class definition with a certain name. The code in the import statement that causes this is called an interface.
For example, an interface can require you to make the method
squeak in any anonymous Mouse class.
The code in your editor should now look like the code below. import com.developer.cat; public class Animal {
public void breathe(){ //code that makes the animal breathe }
} public class Nose { } public class Dog extends Animal {
public void bark(){ //Code that makes a Barking sound! } public Nose getNose(){
//code to get nose variable value } public void setNose(Nose nose) { //code to set Nose } public void breathe(){
super.breathe(); //code to make a dog pant while breathing
} } Dog spot= new Dog(); spot.bark(); Nose spotsnose= new Nose(); spot.setNose(spotsnose); spot.breathe(); Animal animalDog= new Dog(); Dog regularDog=(Dog)animalDog; Cat cat=new Cat(); Mouse mouse=new Mouse(){ public void squeak() { //code to make a mouse squeak
} };
• The code imports com.developer.cat library with the Cat class
• The code creates an Animal class with a breathe method.
• The code creates a Nose class.
• The code creates a Dog class that extends the Animal class and has a bark method, a getNose
method, a setNose method, and an overridden breathe method.
• The code makes a new instance of Dog named spot.
• The code makes spot do the bark method.
• The code creates spotsnose a new instance of Nose.
• The code sets spot’s nose variable to spotsnose.
• The code makes spot breathe.
• The code creates the Dog instance animalDog with access limited to Animal attributes and
methods.
• The code then casts animalDog back to Dog and names this casted instance regularDog.
• The code creates a Cat instance from the class in the imported
library
• The code creates an anonymous Mouse class with a squeak
method