Upload
opal-jennings
View
231
Download
0
Embed Size (px)
Citation preview
What is a class?
public class _Alpha{
public static void main(String [] args){
}}You have been using classes all year – you just didn’t know
What is a class?
public class _Alpha{
public static void main(String [] args){
}}When a class file has a main you can execute the file
What is a class?
public class _AlphaClass{
}We are going to make class files without the main method.
What is a class?
public class Student{
}You still need to save the file as the class name with the .java extension – in this case it would be Student.java
Attributes / Fields
public class Student{
}What do we need to know about a student? These will be the ATTRIBUTES of the class.
Attributes / Fields
public class Student{
private String name;private int idNumber;private double gpa;
}All attributes are declared to be private. Attributes are the global variables of the class.
Attributes / Fields
public class Student{
private String name;private int idNumber;private double gpa;
}NOTICE – you only DECLARE the attributes you do NOT initialize them here…we will do that later.
Attributes / Fields
public class Student{
private String name;private int idNumber;private double gpa;
}So far we have DECLARED the global or instance variables of the class
Constructors
public class Student{
private String name;private int idNumber;private double gpa;
public Student( ){
}
}
To initialize the variables we have to write a method called a CONSTRUCTOR
Constructors
public class Student{
private String name;private int idNumber;private double gpa;
public Student( ){
}
}
CONSTRUCTORS are public and have the same name as the class – ALWAYS, NO EXCEPTIONS
Constructors
public class Student{
private String name;private int idNumber;private double gpa;
public Student( ){ name = “ “;
idNumber = 0;gpa = 0;
}
}
This constructor is called the DEFAULT constructor. We want to initialize the attributes to generic starting values.
Constructors
public class Student{
private String name;private int idNumber;private double gpa;
public Student( ){ name = “ “;
idNumber = 0;gpa = 0;
}
}
Classes can have more than 1 constructor. Next we are going to make a SECONDARY constructor
Constructorspublic class Student{
private String name;private int idNumber;private double gpa;
public Student( ){ name = “ “;
idNumber = 0;gpa = 0;
}
public Student( String n, int id, double g ){ name = n;
idNumber = id;gpa = g;
}
}
In this case the secondary constructor will allow your client to pass over values to the class and then store them in the attributes.
Constructorspublic class Student{
private String name;private int idNumber;private double gpa;
public Student( ){ name = “ “;
idNumber = 0;gpa = 0;
}
public Student( String n, int id, double g ){ name = n;
idNumber = id;gpa = g;
}
}
FLEXIBLITY – having more than 1 constructor makes your class more flexible. There are now 2 ways to initialize your attributes.
Constructorspublic class Student{
private String name;private int idNumber;private double gpa;
public Student( ){ name = “ “;
idNumber = 0;gpa = 0;
}
public Student( String n, int id, double g ){ name = n;
idNumber = id;gpa = g;
}
}
INITIALIZE ALL ATTRIBUTES IN EVERY CONSTRUCTOR
CLASSpublic class Student{
private String name;private int idNumber;private double gpa;
public Student( ){ name = “ “;
idNumber = 0;gpa = 0;
}
public Student( String n, int id, double g ){ name = n;
idNumber = id;gpa = g;
}
}
So – let’s look at a client program that might use this class.
Declaring and Instantiating Classes
public class Student{
private String name;private int idNumber;private double gpa;
public Student( ){ name = “ “;
idNumber = 0;gpa = 0;
}
public Student( String n, int id, double g ){ name = n;
idNumber = id;gpa = g;
}
}
public class Example{
public static void main (String [] args)
{ Scanner scan = new Scanner (System.in);
Student s1 = new Student ();
}}
Declaring and Instantiating Classes
public class Student{
private String name;private int idNumber;private double gpa;
public Student( ){ name = “ “;
idNumber = 0;gpa = 0;
}
public Student( String n, int id, double g ){ name = n;
idNumber = id;gpa = g;
}
}
public class Example{
public static void main (String [] args)
{ Scanner scan = new Scanner (System.in);
Student s1 = new Student ();
}}
s1
name “ “
idNumber 0
Gpa 0
s1 is a Student OBJECTthat was declared with the default constructorso all the attributes are set to the default values.
Declaring and Instantiating Classes
public class Student{
private String name;private int idNumber;private double gpa;
public Student( ){ name = “ “;
idNumber = 0;gpa = 0;
}
public Student( String n, int id, double g ){ name = n;
idNumber = id;gpa = g;
}
}
public class Example{
public static void main (String [] args)
{ Scanner scan = new Scanner (System.in);
Student s1 = new Student (); Student s2 = new Student(“Sookie”, 10025, 4.0);
}}s1
name “ “
idNumber 0
Gpa 0
name “ Sookie “
idNumber
10025
Gpa 4.0
s2
Declaring and Instantiating Classes
public class Student{
private String name;private int idNumber;private double gpa;
public Student( ){ name = “ “;
idNumber = 0;gpa = 0;
}
public Student( String n, int id, double g ){ name = n;
idNumber = id;gpa = g;
}
}
public class Example{
public static void main (String [] args)
{ Scanner scan = new Scanner (System.in);
Student s1 = new Student (); Student s2 = new Student(“Sookie”, 10025, 4.0);
}}s1
name “ “
idNumber 0
Gpa 0
name “ Sookie “
idNumber
10025
Gpa 4.0
s2
“Sookie” is passed over to n
Declaring and Instantiating Classes
public class Student{
private String name;private int idNumber;private double gpa;
public Student( ){ name = “ “;
idNumber = 0;gpa = 0;
}
public Student( String n, int id, double g ){ name = n;
idNumber = id;gpa = g;
}
}
public class Example{
public static void main (String [] args)
{ Scanner scan = new Scanner (System.in);
Student s1 = new Student (); Student s2 = new Student(“Sookie”, 10025, 4.0);
}}s1
name “ “
idNumber 0
Gpa 0
name “ Sookie “
idNumber
10025
Gpa 4.0
s2
The attribute name is assigned n
Accessorspublic class Student{
private String name;private int idNumber;private double gpa;
public Student( ){ name = “ “;
idNumber = 0;gpa = 0;
}
public Student( String n, int id, double g ){ name = n;
idNumber = id;gpa = g;
}
}
Now, let’s look at accessors.
Accessors are used to give information about the class.
What is your name?What is your idNumber?What is your gpa?
If I ask you, “what is your name?” what data type are you going to tell me?
Accessorspublic class Student{
private String name;private int idNumber;private double gpa;
public Student( ){ name = “ “;
idNumber = 0;gpa = 0;
}
public Student( String n, int id, double g ){ name = n;
idNumber = id;gpa = g;
}
}
Now, let’s look at accessors.
Accessors are used to give information about the class.
What is your name?What is your idNumber?What is your gpa?
If I ask you, “what is your name?” what data type are you going to tell me?
STRING – that is the return type of the accessor.
The return type is the data type of the value returned or given by the method
Accessorspublic class Student{
private String name;private int idNumber;private double gpa;
public Student( ){ name = “ “;
idNumber = 0;gpa = 0;
}
public Student( String n, int id, double g ){ name = n;
idNumber = id;gpa = g;
}
public String getName(){
return name;}
}
Now, let’s look at accessors.
Accessors are used to give information about the class.
What is your name?What is your idNumber?What is your gpa?
If I ask you, “what is your name?” what data type are you going to tell me?
STRING – that is the return type of the accessor.
The return type is the data type of the value returned or given by the method
Mutatorspublic class Student{
private String name;private int idNumber;private double gpa;
public Student( ){ name = “ “;
idNumber = 0;gpa = 0;
}
public Student( String n, int id, double g ){ name = n;
idNumber = id;gpa = g;
}
public void setName(String n){
name = n;}
}
Now, let’s look at mutators.
Mutators are used to change information about the class.
What if on the first day of class I check my roll sheet and you are listed as Orvil but you go by your middle name? I would want to change your name to Sam.
I could call a mutator method that would set your name to a new and improved value.
Mutatorspublic class Student{
private String name;private int idNumber;private double gpa;
public Student( ){ name = “ “;
idNumber = 0;gpa = 0;
}
public Student( String n, int id, double g ){ name = n;
idNumber = id;gpa = g;
}
public void setName(String n){
name = n;}
}
Now, let’s look at mutators.
Mutators are used to change information about the class.
What if on the first day of class I check my roll sheet and you are listed as Orvil but you go by your middle name? I would want to change your name to Sam.
I could call a mutator method that would set your name to a new and improved value.