0% found this document useful (0 votes)
10 views25 pages

ECEN324 - Computer System Software - Lecture 05

The lecture discusses the characteristics of class members in object-oriented programming, focusing on the use of the 'this' keyword to access member variables and methods. It also covers the implementation of file handling with examples of saving and loading student data using both object and discrete data types, as well as the importance of setter and getter methods for encapsulation. Additionally, it explains static members of a class and how they differ from instance members.

Uploaded by

shafee001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views25 pages

ECEN324 - Computer System Software - Lecture 05

The lecture discusses the characteristics of class members in object-oriented programming, focusing on the use of the 'this' keyword to access member variables and methods. It also covers the implementation of file handling with examples of saving and loading student data using both object and discrete data types, as well as the importance of setter and getter methods for encapsulation. Additionally, it explains static members of a class and how they differ from instance members.

Uploaded by

shafee001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Lecture (05)

Classes II

Prof. Ahmed ElShafee

1 NU : Spring 2025, ECEN324 - Computer System Software


Characteristics of Members of a Class this
Instance
• If a class contains member variables and methods, the (non-static) member
variables are automatically available to the method(s) of the class, even member
variables that are private.
• When accessing a member variable or a method from another method of the class,
to indicate that the member you are accessing belongs to the same class, you can
precede it with the this member and the period operator.
• When using the this member variable (in C/C++, it is a pointer), you can access any
member of a class within any method of the same class.

NU : Spring 2025,
ECEN324 -
2
Computer System
There are rules you must observe when using this:
• The this member can never be declared: it is automatically implied when you create
a Class this cannot be used in a class A to access a member of class B.
• this cannot be used in a static method

NU : Spring 2025,
ECEN324 -
3
Computer System
Working with files Example08 (object data type)
class Student implements Serializable
{
int •id;
String name;
boolean irregular;
boolean transferred;
String level;
String cc1;
String cc2;
String cc3;
String cc4;
String cc5;
String cc6;
void setStudent(int id,String name, boolean irregular, boolean
transferred, String level,
String cc1, String cc2, String cc3, String cc4, String cc5, String cc6 )
{
this.id=id;
this.name=name;
this.irregular=irregular;
4 NU : Spring 2025, ECEN324 - Computer System Software
this.transferred=transferred;
this.level=level;

Working with files Example06


this.cc1=cc1;
this.cc2=cc2;
this.cc3=cc3;
this.cc4=cc4;

this.cc5=cc5;
this.cc6=cc6;
}
String report()
{
String st;
st="ID : "+id+"\n";
st+="Name : "+name+"\n";
if(level!="") st+="Level : "+level+"\n";
if(irregular)st+="Student is Irregular\n";
else st+="Student is Regular\n";
if(transferred)st+="Student is Transferred\n";
else st+="Student is not Transferred\n";
if(cc1!="-") st+="Course1 : "+cc1+"\n";
if(cc2!="-") st+="Course2 : "+cc2+"\n";
if(cc3!="-") st+="Course3 : "+cc3+"\n";
if(cc4!="-") st+="Course4 : "+cc4+"\n";
5 if(cc5!="-") st+="Course5 NU
: : Spring
"+cc5+"\n";
2025, ECEN324 - Computer System Software
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{

Working with files Example06


if (jFileChooser1.showSaveDialog(this) == 0) {
try{

FileOutputStream fout = new



FileOutputStream(jFileChooser1.getSelectedFile().toString());
ObjectOutputStream oos = new ObjectOutputStream(fout);

oos.writeObject((Student)st1);

fout.flush();
fout.close();
}
catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex);
} }
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{
if (jFileChooser1.showOpenDialog(this) == 0) {
try{

FileInputStream fis = new


FileInputStream(jFileChooser1.getSelectedFile());
6 NU : Spring 2025, ECEN324 - Computer System Software
st1 = (Student) ois.readObject();

Working with files Example06


jTextField1.setText(Integer.toString(st1.id));
jTextField2.setText(st1.name);
jCheckBox1.setSelected(st1.irregular);
jCheckBox2.setSelected(st1.transferred);
• jComboBox7.setSelectedItem(st1.level);
jComboBox1.setSelectedItem(st1.cc1);
jComboBox2.setSelectedItem(st1.cc2);
jComboBox3.setSelectedItem(st1.cc3);
jComboBox4.setSelectedItem(st1.cc4);
jComboBox5.setSelectedItem(st1.cc5);
jComboBox6.setSelectedItem(st1.cc6);
fis.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex);}
}
}

7 NU : Spring 2025, ECEN324 - Computer System Software


Working with files Example07 (discrete data type)

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{
if (jFileChooser1.showSaveDialog(this) == 0) {
try{
FileOutputStream fout = new FileOutputStream(jFileChooser1.getSelectedFile().toString());
DataOutputStream dos = new DataOutputStream(fout);
dos.writeInt(st1.id);
dos.writeUTF(st1.name);
dos.writeBoolean(st1.irregular);
dos.writeBoolean(st1.transferred);
dos.writeUTF(st1.level);
dos.writeUTF(st1.cc1);
dos.writeUTF(st1.cc2);
dos.writeUTF(st1.cc3);
dos.writeUTF(st1.cc4);
dos.writeUTF(st1.cc5);
dos.writeUTF(st1.cc6);
fout.flush();
fout.close();
}
catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex);
} 8 }} NU : Spring 2024, ECEN324 - Computer System Software
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
if (jFileChooser1.showOpenDialog(this) == 0) {
try{

FileInputStream fis = new FileInputStream(jFileChooser1.getSelectedFile());


DataInputStream dis = new DataInputStream(fis);
st1.id=dis.readInt();
• st1.name=dis.readUTF();
st1.irregular=dis.readBoolean();
st1.transferred=dis.readBoolean();
st1.level=dis.readUTF();
st1.cc1=dis.readUTF();
st1.cc2=dis.readUTF();
st1.cc3=dis.readUTF();
st1.cc4=dis.readUTF();
st1.cc5=dis.readUTF();
st1.cc6=dis.readUTF();
jTextField1.setText(Integer.toString(st1.id));
jTextField2.setText(st1.name);
jCheckBox1.setSelected(st1.irregular);
jCheckBox2.setSelected(st1.transferred);
jComboBox7.setSelectedItem(st1.level);
jComboBox1.setSelectedItem(st1.cc1);
jComboBox2.setSelectedItem(st1.cc2);
jComboBox3.setSelectedItem(st1.cc3);
jComboBox4.setSelectedItem(st1.cc4);
jComboBox5.setSelectedItem(st1.cc5);
jComboBox6.setSelectedItem(st1.cc6);
fis.close(); } catch (Exception ex) {
9 JOptionPane.showMessageDialog(this,
NU : Spring 2024, ECEN324ex);}
- Computer System }
Software }
Working with files Example08 (object data type)
class Student implements Serializable
{
int •id;
String name;
boolean irregular;
boolean transferred;
String level;
String cc1;
String cc2;
String cc3;
String cc4;
String cc5;
String cc6;
void setStudent(int id,String name, boolean irregular, boolean
transferred, String level,
String cc1, String cc2, String cc3, String cc4, String cc5, String cc6 )
{
this.id=id;
this.name=name;
this.irregular=irregular;
10 NU : Spring 2025, ECEN324 - Computer System Software
this.transferred=transferred;
this.level=level;

Working with files Example06


this.cc1=cc1;
this.cc2=cc2;
this.cc3=cc3;
this.cc4=cc4;

this.cc5=cc5;
this.cc6=cc6;
}
String report()
{
String st;
st="ID : "+id+"\n";
st+="Name : "+name+"\n";
if(level!="") st+="Level : "+level+"\n";
if(irregular)st+="Student is Irregular\n";
else st+="Student is Regular\n";
if(transferred)st+="Student is Transferred\n";
else st+="Student is not Transferred\n";
if(cc1!="-") st+="Course1 : "+cc1+"\n";
if(cc2!="-") st+="Course2 : "+cc2+"\n";
if(cc3!="-") st+="Course3 : "+cc3+"\n";
if(cc4!="-") st+="Course4 : "+cc4+"\n";
11 if(cc5!="-") st+="Course5 NU
: : Spring
"+cc5+"\n";
2025, ECEN324 - Computer System Software
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{

Working with files Example06


if (jFileChooser1.showSaveDialog(this) == 0) {
try{

FileOutputStream fout = new



FileOutputStream(jFileChooser1.getSelectedFile().toString());
ObjectOutputStream oos = new ObjectOutputStream(fout);

oos.writeObject((Student)st1);

fout.flush();
fout.close();
}
catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex);
} }
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
{
if (jFileChooser1.showOpenDialog(this) == 0) {
try{

FileInputStream fis = new


FileInputStream(jFileChooser1.getSelectedFile());
12 NU : Spring 2025, ECEN324 - Computer System Software
st1 = (Student) ois.readObject();

Working with files Example06


jTextField1.setText(Integer.toString(st1.id));
jTextField2.setText(st1.name);
jCheckBox1.setSelected(st1.irregular);
jCheckBox2.setSelected(st1.transferred);
• jComboBox7.setSelectedItem(st1.level);
jComboBox1.setSelectedItem(st1.cc1);
jComboBox2.setSelectedItem(st1.cc2);
jComboBox3.setSelectedItem(st1.cc3);
jComboBox4.setSelectedItem(st1.cc4);
jComboBox5.setSelectedItem(st1.cc5);
jComboBox6.setSelectedItem(st1.cc6);
fis.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex);}
}
}

13 NU : Spring 2025, ECEN324 - Computer System Software


Set function
• in Java, a "setter" function (often called a set method) is a type of method that is
used to set or update the value of a private instance variable of a class.
• The primary purpose of setter methods is to control how important fields within an
object are set or modified.
• By using setters, you can encapsulate the fields (attributes) of a class, thereby
adhering to the principle of encapsulation—one of the key principles in object-
oriented programming.

14 NU : Spring 2024, ECEN324 - Computer System Software


Example 05 set
class• Student
{
int id;
String name;
boolean irregular;
boolean transferred;
String level;
String cc1;
String cc2;
String cc3;
String cc4;
String cc5;
String cc6;
void setStudent(int id,String name, boolean irregular,
boolean transferred, String level,
String cc1, String cc2, String cc3, String cc4, String
cc5, String cc6 )
{15 NU : Spring 2024, ECEN324 - Computer System Software
this.id=id;
this.name=name;
this.irregular=irregular;
this.transferred=transferred;
this.level=level; Example 05
this.cc1=cc1;
this.cc2=cc2;

this.cc3=cc3;
this.cc4=cc4;
this.cc5=cc5;
this.cc6=cc6;
}
String report()
{
String st;
st="ID : "+id+"\n";
st+="Name : "+name+"\n";
if(level!="") st+="Level : "+level+"\n";
if(irregular)st+="Student is Irregular\n";
else st+="Student is Regular\n";
if(transferred)st+="Student is Transferred\n";
else st+="Student is not Transferred\n";
if(cc1!="-") st+="Course1 : "+cc1+"\n";
if(cc2!="-") st+="Course2 : "+cc2+"\n";
16 if(cc3!="-") st+="Course3 NU:: Spring
"+cc3+"\n";
2024, ECEN324 - Computer System Software
if(cc4!="-") st+="Course4 : "+cc4+"\n";
if(cc5!="-") st+="Course5 : "+cc5+"\n";
if(cc6!="-") st+="Course6 : "+cc6+"\n";
return st; Example 05
}
}
• void
private
jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
Student st1=new Student();
st1.setStudent(Integer.parseInt(jTextField1.getText()),
jTextField2.getText(),
jCheckBox1.isSelected(),
jCheckBox2.isSelected(),
jComboBox7.getSelectedItem().toString(),
jComboBox1.getSelectedItem().toString(),
jComboBox2.getSelectedItem().toString(),
jComboBox3.getSelectedItem().toString(),
jComboBox4.getSelectedItem().toString(),
jComboBox5.getSelectedItem().toString(),
jComboBox6.getSelectedItem().toString());
jTextArea1.setText(st1.report());
}
17 NU : Spring 2024, ECEN324 - Computer System Software
Get function
• in Java, a "getter" function (often called a get method) is used to retrieve or access
the value of a private instance variable of a class.
• Getter methods are a key part of the encapsulation principle, which is fundamental
in object-oriented design.
• They provide a controlled way to access the values of private variables from outside
the class.

18 NU : Spring 2024, ECEN324 - Computer System Software


Student getStudent()
{
return this; Example 06 get
}
private void
jButton5ActionPerformed(java.awt.event.ActionEvent evt)
{
Student tempSt=new Student();
tempSt = st1.getStudent();
jTextField1.setText(Integer.toString(st1.id));
jTextField2.setText(tempSt.name);
jCheckBox1.setSelected(tempSt.irregular);
jCheckBox2.setSelected(tempSt.transferred);
jComboBox7.setSelectedItem(tempSt.level);
jComboBox1.setSelectedItem(tempSt.cc1);
jComboBox2.setSelectedItem(tempSt.cc2);
jComboBox3.setSelectedItem(tempSt.cc3);
jComboBox4.setSelectedItem(tempSt.cc4);
jComboBox5.setSelectedItem(tempSt.cc5);
jComboBox6.setSelectedItem(tempSt.cc6);
}

19 NU : Spring 2024, ECEN324 - Computer System Software


The Static Members of a Class
“Static Fields”
• A variable you have declared of a class is also called an instance of the class.
• In the same way, you can declare various instances of the same class as necessary:
• Each one of these instances gives you access to the members of the class but each
instance holds the particular values of the members of its instance.

NU : Spring 2024,
ECEN324 -
20
Computer System
• In your application, you can declare a class member and refer to it regardless of
which instance of an object you are using. Such a member variable is called static.
• To declare a member variable of a class as static, type the static keyword on its left.
• Whenever you have a static member, in order to refer to it, you must "qualify" it in
the class in which you want to call it.
• Qualifying a member means you must specify its class.

NU : Spring 2024,
ECEN324 -
21
Computer System
The Static Members of a Class
Static Methods
• Like a member variable, a method of a class can be defined as static.
• Consequently, this particular method can access any member of the class regardless
of the instance if there are many instances of the class declared.

NU : Spring 2024,
ECEN324 -
22
Computer System
ass Student implements Serializable

static int totalStudents=5;


static int getTotalStudents()
{
return totalStudents;
Example 09
}
static void setTotalStudents( int x)
{
totalStudents=x;
}

ivate void jButton7ActionPerformed(java.awt.event.ActionEvent evt)

jTextField3.setText(Integer.toString(Student.getTotalStudents())); // TODO add


ur handling code here:
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt)

Student.setTotalStudents(Integer.parseInt(jTextField3.getText()));
}
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt)

jTextField4.setText(Integer.toString(st1.getTotalStudents())); // TODO add your


ndling code here:
// TODO add your handling code here:
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt)
NU : Spring 2024,
st1.setTotalStudents(Integer.parseInt(jTextField4.getText()));
ECEN324 -
} Computer System 23
Characteristics of Members of a Class
Constants
• You can create a constant variable in a class. To create a constant variable, type the
final keyword to the left of the variable.
• Once again, when declaring a constant, you must initialize it with an appropriate
constant value.

NU : Spring 2024,
ECEN324 -
24
Computer System
Thanks,..
C U next week, isA

25 NU : Spring 2025, ECEN324 - Computer System Software

You might also like