This tutorial is the first step to creating attributes (features) in our classes.
Variables methods and Variables Classes
That is, method local variables can only be accessed within that method, and when it ends, its value is lost.
When we declare an object of a class, its variables come into existence from that moment until the end of the object existence.
About its access, Java comes with very interesting security devices. We can declare variables of a class as 'public' (which can be accessed from anywhere in the program), 'private' (only elements of the class itself can access this data) and 'protected' (only elements of their own class and subclasses that may see this data).
We will see this in Encapsulation in a future section of our online Java course, when we talk more about Object Orientation. For now, just know that it exists and what are they for - safety and organization.
Creating classes with attributes
Returning to our class "Student", we will create three variables: "Name", which will receive the student's name and "mathGrade" and "physGrade" that will receive the student grade of mathematics and physics.
Our class will be:
public class Student {
public String name;
public double mathGrade,
physGrade;
}
Yes, just that.
Declare variables as 'public' because we access it from another class, the class "FirstClass" which contains the method 'main'.
When we will begin to study methods later in our online Java course, we will see how to declare these attributes in a 'private' way so that they are inaccessible to other classes. And how do we access that data then? Through methods of this class.
So we can control how these variables are used.
Accessing and Changing attributes of a class
Now that variables are created, let's give value to them. Let's use the Scanner class to ask the user the name of the student and their grades.Note that, if these variables were in the main class, that contain the main method, simply assign these values as follows:
mathGrade = [number]
However, these variables are not in that class. Are in another.
So we have access through the object name.
In my case, I declared the Student object with the name "neil", then to access its elements, we simply use:
neil.mathGrade
Syntax: objectName.variableName;
So, in our main class, instantiate an object that fills the name and the Student two notes, then shows these values and the average:
FirstClass.java
import java.util.Scanner;
public class FirstClass {
public static void main(String[] args) {
Aluno neil = new Aluno();
Scanner input = new Scanner (System.in);
System.out.print("Student name: ");
neil.name = input.nextLine();
System.out.print("Math grade: ");
neil.mathGrade = input.nextDouble();
System.out.print("Physics grade: ");
neil.physGrade = input.nextDouble();
//Displaying the information
System.out.printf("The student \"%s\" took %2.2f in Math, "
+ "%2.2f in Physics and his average is %2.2f\n", neil.nome,
neil.notaMat,
neil.notaFis,
(pneil.mathGrade + neil.physGrade)/2);
}
}
Student.java
public class Aluno {
public String name;
public double mathGrade,
physGrade;
}
No comments:
Post a Comment