27
More Object Concepts Chapter 4

More Object Concepts Chapter 4. Our class is made up of several students and each student has a name and test grades How do we assign the variables

Embed Size (px)

Citation preview

More Object Concepts

Chapter 4

Our class is made up of several students and each student has a name and test grades

How do we assign the variables for each student?

Student 1 Name: Anne ID#: 900001 Test1: 92 Test 2:85

Student 2 Name: Barry ID#: 900002 Test1: 88 Test 2:81

Student 3 Name: Carl ID#: 900003 Test1: 90 Test 2:95

The differences between one student and another is simply the variable assignments

Instead of creating multiple variables for each student, we could create one class with multiple objects

This produces a concept of an object within an object

What does it mean to be a student?

class Student {String name;String idnum;int test1;int test2;

}

How do we fill in the information? Let’s create a new student within the class

student1 = new Student( );student2 = new Student( );

Now we can fill in the desired variable assignment for each unique student

class Student {String name;String idnum;int test1;int test2;

}

class StudentInfo {public static void main(String[ ] args) {

Student student1;

student1 = new Student( );

student1.name = "Anne";student1.idnum = "900001";student1.test1 = 92;student1.test2 = 85;

System.out.println(student1.name);System.out.println(student1.test1);

}}

Create and compile the Student class

Create and compile the StudentInfo class

Declare a variable within the object Student student1;

This indicates that if and or when the variable student1 is assigned, that assignment will be an instance of the Student class

Reserves the variable name student1

The actual creation of the object occurs in the statement:

student1 = new Student( );

Our Student class works well, but has one serious flaw Once the data is assigned, anyone can view the

data for any student Student1 should not have access to student2

or student3 We can create an accessor method to hide

the data not assigned to that particular user

class Student {private String name;

public void setName(String n) {name = n;

}

public String getName( ) {return name;

}}

Note: private

variable can only be referenced within

the class

The accessor method for the Student class now limits access to each instance (in this case for each student along with their individual information)

In the main method we will replace the assignment student1.name = "Anne";with the reference

student1.setName( "Anne" ); andstudent1.getName( );

class Student {private String name;private String idnum;private int test1;private int test2;

public void setName(String n) {name = n;

}public String getName( ) {

return name;}public void setIdNum(String id) {

idnum = id;}public String getIdNum( ) {

return idnum;}public void setTest1(int t) {

test1 = t;}public int getTest1( ) {

return test1;}public void setTest2(int t) {

test2 = t;}public int getTest2( ) {

return test2;}

}

class StudentInfo {public static void main(String[ ] args) {

Student student1 = new Student( );

student1.setName ( "Anne" );student1.setIdNum ( "900001" );student1.setTest1 ( 92 );student1.setTest2 ( 85 );

System.out.println(student1.getName( ));System.out.println(student1.getIdNum( ));System.out.println(student1.getTest1( ));System.out.println(student1.getTest2( ));

}}

The Student class we created is called a constructor

Constructors are used to initialize the instance variables of an object

Constructors are similar to methods, but with some important differences Constructor name is the class name If a constructor does not exists, a default

constructor initializes the variables (zero, null, false)

Each constructor in a class may have the same name as the class

Which constructor to use depends on the parameter data type.

Class Student {private String name;private int test1;

public Student( ) {name = " " ;test1 = 0;

}

public Student(String student) {name = student;

}

public Student( int test ) {test1 = test;

}}

No parameter uses this method

String parameter uses this method

Integer parameter uses this method

Now we do not need to worry about multiple method names setName ( ) setTest1 ( )

Unfortunately, we still have multiple variable names. We can fix this using the this command this(...) - Calls another constructor in same class If a constructor uses this, it must be in the constructor's

first line; ignoring this rule will cause a compile error

Class Student {private String name;private int test1;

public Student( ) {name = " " ;test1 = 0;

}

public Student(String student) {name = student;

}

public Student( int test ) {test1 = test;

}}

Class Student {private String name;private int test1;

public Student( ) {this ( " " , 0 );

}

public Student(String name) { this ( name , 0 );

}

public Student( int test1 ) { this ( " " , test1 );

}}

this must be used if a parameter or other local variable with the same name is used in the method Otherwise, all instances of the variable name

will be interpreted as local int someVariable = this.someVariable

local instance

public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public Circle() { this(1.0); } public double getArea() { return this.radius * this.radius * 3.14159; }}

this must be explicitly used to reference the data field radius of the object being

constructed

this is used to invoke another

constructorEvery instance variable belongs

to an instance represented by this, which is normally omitted

Many classes commonly used by wide variety of programmers

Java contains nearly 500 classes Package or library of classes

Folder provides convenient grouping for classes Many contain classes available only if explicitly

named within program Some classes available automatically

Fundamental or basic classes Implicitly imported into every Java program java.lang package

Only automatically imported, named package

Optional classes Must be explicitly named

java.lang.Math class Contains constants and methods used to

perform common mathematical functions No need to create instance Imported automatically Cannot instantiate objects of type Math

Constructor for Math class private

public class MainClass {public static void main(String[ ] args) {

System.out.println(Math.E);System.out.println(Math.PI);

System.out.println(Math.abs(-1234)); System.out.println(Math.cos(Math.PI/4)); System.out.println(Math.sqrt(25)); System.out.println(Math.min(20, -2)); System.out.println(Math.round(1.573)); System.out.println(Math.pow(2, 4));

System.out.print(Math.random( )); }}

Use prewritten classes Use entire path with class name Import class Import package that contains class

To use import statements Place before any executing statement in Java file

Prior to import statement Use blank line or comment line but nothing else

Wildcard symbol Alternative to importing class

Import entire package of classes Use asterisk

Can be replaced by any set of characters Represents all classes in package No disadvantage to importing extra classes

Importing each class by name can be form of documentation

GregorianCalendar class Seven constructors Default constructor creates calendar object

containing current date and time in default locale (time zone)

Can also specify date, time, and time zone get() method

Access data fields