SlideShare a Scribd company logo
1 of 19
CS : 302
Getters & Setters
GETTERS & SETTERS
 Getters and Setters are used to effectively protect your
data, particularly when creating classes. For each
variable, the get method returns its value, while
the set method sets the value.
Getters start with get, followed by the variable name, with
the first letter of the variable name capitalized.

Setters start with set, followed by the variable name, with
the first letter of the variable name capitalized.
EXAMPLE
 public class Vehicle {
private String color;
// Getter
public String getColor() {
return color;
}
// Setter
public void setColor(String c) {
this.color = c;
}
}
The getter method returns the value of the attribute.
The setter method takes a parameter and assigns it to the
attribute.
The keyword this is used to refer to the current object.
Basically, this.color is the color attribute of the current object.
FILL IN THE BLANKS :
 class A {
 private int x;
 public______ getX() {
 return ________ ;
 }
 Public______ setX(int x) {
 this.x = x;
 }
 }
 ANS : int, x, void
CONSTRUCTORS
 Constructors are special methods invoked when an object is
created and are used to initialize them.
A constructor can be used to provide initial values for object
attributes.
- A constructor name must be same as its class name.
- A constructor must have no explicit return type.
Example of a constructors:
 public class Vehicle {
private String color;
Vehicle() {
color = "Red";
}
}
 The Vehicle() method is the constructor of our class, so
whenever an object of that class is created, the color attribute
will be set to "Red".
CONSTRUCTORS
 A constructor can also take parameters to initialize
attributes.
 public class Vehicle {
private String color;
Vehicle(String c) {
color = c;
}
}

You can think of constructors as methods that
will set up your class by default, so you don’t
need to repeat the same code every time.
FILL IN THE BLANKS : CONSTRUCTORS
 class Person {
 private int age;
 public __________ ( _____________ myage) {
 age = myage;
 }
 }
 Private int constructor person
 ANS : person , int
USING CONSTRUCTORS
 The constructor is called when you create an object using
the new keyword.

Example:public class MyClass {
public static void main(String[ ] args) {
Vehicle v = new Vehicle("Blue");
}
}

This will call the constructor, which will set
the color attribute to "Blue".
 Q1 : True or false: The constructor must have the same
name as the class.
 ANS : TRUE
CONSTRUCTORS
 A single class can have multiple constructors with different numbers of
parameters.
The setter methods inside the constructors can be used to set the
attribute values.
Example:
 public class Vehicle {
 private String color;
 Vehicle() {
 this.setColor("Red");
 }
 Vehicle(String c) {
 this.setColor(c);
 }
 // Setter
 public void setColor(String c) {
 this.color = c;
 }
 }
CONSTRUCTORS
 The class above has two constructors, one without any
parameters setting the color attribute to a default value
of "Red", and another constructor that accepts a
parameter and assigns it to the attribute.
 Now, we can use the constructors to create objects of
our class.
 (Java automatically provides a default constructor, so all
classes have a constructor, whether one is specifically
defined or not.)

 LAB TIME we will run this program
FILL IN THE BLANKS : CONSTRUCTORS
 Fill in the blanks.
 ________ A
 {
 private int x;
 public A( _____ val) {
 x = val;
 }
 }
 ANS : class , int
VALUE TYPES
 Value types are the basic types, and include byte, short, int,
long, float, double, boolean, and char.
These data types store the values assigned to them in the
corresponding memory locations.
So, when you pass them to a method, you basically operate
on the variable's value, rather than on the variable itself.
Example:
 public class MyClass {
 public static void main(String[ ] args) {
 int x = 5;
 addOneTo(x);
 System.out.println(x);
 }
 static void addOneTo(int num) {
 num = num + 1;
 }
 }
 // Outputs "5"
VALUE TYPES
 What is the output of this code?
 public static void main(String[ ] args) {
 int x = 4;
 square(x);
 System.out.println(x);
 }
 static void square(int x) {
 x = x*x;
 }
 ANS : 4
REFERENCE TYPES
 A reference type stores a reference (or address) to the memory location where
the corresponding data is stored.
When you create an object using the constructor, you create a reference variable.
For example, consider having a Person class defined:
 public class MyClass {
 public static void main(String[ ] args) {
 Person j;
 j = new Person("John");
 j.setAge(20);
 celebrateBirthday(j);
 System.out.println(j.getAge());
 }
 static void celebrateBirthday(Person p) {
 p.setAge(p.getAge() + 1);
 }
 }
 //Outputs "21"
REFERENCE TYPES
 The method celebrateBirthday takes a Person
object as its parameter, and increments its
attribute.
Because j is a reference type, the method affects
the object itself, and is able to change the actual
value of its attribute.

Arrays and Strings are also reference data types.

REFERENCE TYPES
 What is the output of this code?
 public static void main(String[ ] args) {
 Person p = new Person();
 p.setAge(25);
 change(p);
 System.out.println(p.getAge());
 }
 static void change(Person p) {
 p.setAge(10);
 }
 ANS : 10
THE MATH CLASS
 The JDK defines a number of useful classes, one
of them being the Math class, which provides
predefined methods for mathematical operations.
You do not need to create an object of
the Math class to use it. To access it, just type
in Math.and the corresponding method.
Math.abs() returns the absolute value of its
parameter.
 int a = Math.abs(10); // 10
int b = Math.abs(-20); // 20
THE MATH CLASS
Math.ceil() rounds a floating point value up to the nearest integer value.
The rounded value is returned as a double.
double c = Math.ceil(7.342); // 8.0
 Math.floor() rounds a floating point value down to the
nearest integer value.double f = Math.floor(7.343); // 7.0
 Math.max() returns the largest of its parameters.
 int m = Math.max(10, 20); // 20
 Conversely, Math.min() returns the smallest parameter.
 int m = Math.min(10, 20); // 10
 Math.pow() takes two parameters and returns the first parameter raised
to the power of the second parameter.
 double p = Math.pow(2, 3); // 8.0
 There are a number of other methods available in the Math class,
including:
sqrt() for square root, sin() for sine, cos() for cosine, and others.
THE MATH CLASS
 What is the value of the following expression?
 Math.abs(Math.min(-6, 3));
 3 -6 6
 ANS : 6

More Related Content

What's hot (20)

Functions in c
Functions in cFunctions in c
Functions in c
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Function C++
Function C++ Function C++
Function C++
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Java constructors
Java constructorsJava constructors
Java constructors
 
This pointer
This pointerThis pointer
This pointer
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Java- Nested Classes
Java- Nested ClassesJava- Nested Classes
Java- Nested Classes
 

Similar to Getters_And_Setters.pptx

Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6sotlsoc
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Palak Sanghani
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in javaMahmoud Ali
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Abou Bakr Ashraf
 
Advanced c#
Advanced c#Advanced c#
Advanced c#saranuru
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptxarjun431527
 
Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Andrew Petryk
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4Vince Vo
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IEduardo Bergavera
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined MethodPRN USM
 
Jdk1.5 Features
Jdk1.5 FeaturesJdk1.5 Features
Jdk1.5 Featuresindia_mani
 
3 functions and class
3   functions and class3   functions and class
3 functions and classtrixiacruz
 

Similar to Getters_And_Setters.pptx (20)

Notes(1).pptx
Notes(1).pptxNotes(1).pptx
Notes(1).pptx
 
Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
 
Csharp generics
Csharp genericsCsharp generics
Csharp generics
 
Built-in Classes in JAVA
Built-in Classes in JAVABuilt-in Classes in JAVA
Built-in Classes in JAVA
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
Synapseindia dot net development
Synapseindia dot net developmentSynapseindia dot net development
Synapseindia dot net development
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in java
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
Jeop game-final-review
Jeop game-final-reviewJeop game-final-review
Jeop game-final-review
 
Advanced c#
Advanced c#Advanced c#
Advanced c#
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
 
Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)Generic Types in Java (for ArtClub @ArtBrains Software)
Generic Types in Java (for ArtClub @ArtBrains Software)
 
Java execise
Java execiseJava execise
Java execise
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
Class & Object - User Defined Method
Class & Object - User Defined MethodClass & Object - User Defined Method
Class & Object - User Defined Method
 
Jdk1.5 Features
Jdk1.5 FeaturesJdk1.5 Features
Jdk1.5 Features
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
 

Recently uploaded

GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort servicejennyeacort
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998YohFuh
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAbdelrhman abooda
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 

Recently uploaded (20)

GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998RA-11058_IRR-COMPRESS Do 198 series of 1998
RA-11058_IRR-COMPRESS Do 198 series of 1998
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 

Getters_And_Setters.pptx

  • 1. CS : 302 Getters & Setters
  • 2. GETTERS & SETTERS  Getters and Setters are used to effectively protect your data, particularly when creating classes. For each variable, the get method returns its value, while the set method sets the value. Getters start with get, followed by the variable name, with the first letter of the variable name capitalized.  Setters start with set, followed by the variable name, with the first letter of the variable name capitalized.
  • 3. EXAMPLE  public class Vehicle { private String color; // Getter public String getColor() { return color; } // Setter public void setColor(String c) { this.color = c; } } The getter method returns the value of the attribute. The setter method takes a parameter and assigns it to the attribute. The keyword this is used to refer to the current object. Basically, this.color is the color attribute of the current object.
  • 4. FILL IN THE BLANKS :  class A {  private int x;  public______ getX() {  return ________ ;  }  Public______ setX(int x) {  this.x = x;  }  }  ANS : int, x, void
  • 5. CONSTRUCTORS  Constructors are special methods invoked when an object is created and are used to initialize them. A constructor can be used to provide initial values for object attributes. - A constructor name must be same as its class name. - A constructor must have no explicit return type. Example of a constructors:  public class Vehicle { private String color; Vehicle() { color = "Red"; } }  The Vehicle() method is the constructor of our class, so whenever an object of that class is created, the color attribute will be set to "Red".
  • 6. CONSTRUCTORS  A constructor can also take parameters to initialize attributes.  public class Vehicle { private String color; Vehicle(String c) { color = c; } }  You can think of constructors as methods that will set up your class by default, so you don’t need to repeat the same code every time.
  • 7. FILL IN THE BLANKS : CONSTRUCTORS  class Person {  private int age;  public __________ ( _____________ myage) {  age = myage;  }  }  Private int constructor person  ANS : person , int
  • 8. USING CONSTRUCTORS  The constructor is called when you create an object using the new keyword.  Example:public class MyClass { public static void main(String[ ] args) { Vehicle v = new Vehicle("Blue"); } }  This will call the constructor, which will set the color attribute to "Blue".  Q1 : True or false: The constructor must have the same name as the class.  ANS : TRUE
  • 9. CONSTRUCTORS  A single class can have multiple constructors with different numbers of parameters. The setter methods inside the constructors can be used to set the attribute values. Example:  public class Vehicle {  private String color;  Vehicle() {  this.setColor("Red");  }  Vehicle(String c) {  this.setColor(c);  }  // Setter  public void setColor(String c) {  this.color = c;  }  }
  • 10. CONSTRUCTORS  The class above has two constructors, one without any parameters setting the color attribute to a default value of "Red", and another constructor that accepts a parameter and assigns it to the attribute.  Now, we can use the constructors to create objects of our class.  (Java automatically provides a default constructor, so all classes have a constructor, whether one is specifically defined or not.)   LAB TIME we will run this program
  • 11. FILL IN THE BLANKS : CONSTRUCTORS  Fill in the blanks.  ________ A  {  private int x;  public A( _____ val) {  x = val;  }  }  ANS : class , int
  • 12. VALUE TYPES  Value types are the basic types, and include byte, short, int, long, float, double, boolean, and char. These data types store the values assigned to them in the corresponding memory locations. So, when you pass them to a method, you basically operate on the variable's value, rather than on the variable itself. Example:  public class MyClass {  public static void main(String[ ] args) {  int x = 5;  addOneTo(x);  System.out.println(x);  }  static void addOneTo(int num) {  num = num + 1;  }  }  // Outputs "5"
  • 13. VALUE TYPES  What is the output of this code?  public static void main(String[ ] args) {  int x = 4;  square(x);  System.out.println(x);  }  static void square(int x) {  x = x*x;  }  ANS : 4
  • 14. REFERENCE TYPES  A reference type stores a reference (or address) to the memory location where the corresponding data is stored. When you create an object using the constructor, you create a reference variable. For example, consider having a Person class defined:  public class MyClass {  public static void main(String[ ] args) {  Person j;  j = new Person("John");  j.setAge(20);  celebrateBirthday(j);  System.out.println(j.getAge());  }  static void celebrateBirthday(Person p) {  p.setAge(p.getAge() + 1);  }  }  //Outputs "21"
  • 15. REFERENCE TYPES  The method celebrateBirthday takes a Person object as its parameter, and increments its attribute. Because j is a reference type, the method affects the object itself, and is able to change the actual value of its attribute.  Arrays and Strings are also reference data types. 
  • 16. REFERENCE TYPES  What is the output of this code?  public static void main(String[ ] args) {  Person p = new Person();  p.setAge(25);  change(p);  System.out.println(p.getAge());  }  static void change(Person p) {  p.setAge(10);  }  ANS : 10
  • 17. THE MATH CLASS  The JDK defines a number of useful classes, one of them being the Math class, which provides predefined methods for mathematical operations. You do not need to create an object of the Math class to use it. To access it, just type in Math.and the corresponding method. Math.abs() returns the absolute value of its parameter.  int a = Math.abs(10); // 10 int b = Math.abs(-20); // 20
  • 18. THE MATH CLASS Math.ceil() rounds a floating point value up to the nearest integer value. The rounded value is returned as a double. double c = Math.ceil(7.342); // 8.0  Math.floor() rounds a floating point value down to the nearest integer value.double f = Math.floor(7.343); // 7.0  Math.max() returns the largest of its parameters.  int m = Math.max(10, 20); // 20  Conversely, Math.min() returns the smallest parameter.  int m = Math.min(10, 20); // 10  Math.pow() takes two parameters and returns the first parameter raised to the power of the second parameter.  double p = Math.pow(2, 3); // 8.0  There are a number of other methods available in the Math class, including: sqrt() for square root, sin() for sine, cos() for cosine, and others.
  • 19. THE MATH CLASS  What is the value of the following expression?  Math.abs(Math.min(-6, 3));  3 -6 6  ANS : 6