Program to Explain Getter and Setter Functions

Topics: Field, Subroutine, Object-oriented programming Pages: 5 (436 words) Published: February 3, 2013
1369-6-15E AID:000 | 27/012013 -------------------------------------------------

Program Plan:
* Create a class Pizza
* Define the variables
* Provide methods to get and set these variables
-------------------------------------------------

/*********************************************************************** The program Pizza.java creates a class Pizza and defines methods to get and set variables in the class ***********************************************************************/ Program:

//creating class Pizza
class Pizza
{
//declaring the fields in the class
String Toppings;
int Diameter;
double Price;
-------------------------------------------------

//defining methods to set and get the field Toppings respectively void setToppings(String z)
{
//setting the filed Toppings
Toppings=z;
}

String getToppings()
{
return Toppings;
}
-------------------------------------------------

//defining methods to set and get the field Diameter respectively void setDiameter(int x)
{
//setting the filed Diameter
Diameter=x;
}

int getDiameter()
{
return Diameter;
}
-------------------------------------------------

//defining methods to set and get the field Price respectively void setPrice(double y)
{
//setting the field Price
Price=y;
}

double getPrice()
{
return Price;
}
}
-------------------------------------------------

Program Plan:
* Create a class Pizza
* Define the variables
* Provide methods to get and set these variables
* Create another class TestPizza
* Initialize an instance of the class Pizza
* Set the fields of the instance with a value
* Output those fields with the use of getter functions
-------------------------------------------------

/*********************************************************************** The program TestPizza.java creates a class Pizza and defines methods to get and set variables in the class, creates another class TestPizza to demonstrate use of get and set methods of class Pizza ***********************************************************************/ Program:

//creating class Pizza
class Pizza
{
//declaring the fields in the class
String Toppings;
int Diameter;
double Price;
-------------------------------------------------

//defining methods to set and get the field Toppings respectively void setToppings(String z)
{
//setting the filed Toppings
Toppings=z;
}

String getToppings()
{
return Toppings;
}
-------------------------------------------------

//defining methods to set and get the field Diameter respectively void setDiameter(int x)
{
//setting the filed Diameter
Diameter=x;
}

int getDiameter()
{
return Diameter;
}
-------------------------------------------------

//defining methods to set and get the field Price respectively void setPrice(double y)
{
//setting the field Price
Price=y;
}

double getPrice()
{
return Price;
}
}
-------------------------------------------------

//Creating class TestPizza
class TestPizza
{
public static void main (String args[]) //main function {
//declaring the variables
int diameter;
double price;
String topping;
-------------------------------------------------

//creating an instance of class Pizza, Here order1 is an object of type class Pizza Pizza order1=new Pizza();
-------------------------------------------------

//setting the fields of the instance with values
//the inputs are...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Explain the functions of assessment in learning and development Essay
  • Explain the Functions of Assessment in Learning and Development Essay
  • Explain the Function of Assessemnt and Learning and Development Essay
  • Program Essay
  • Program Essay
  • Essay about production function
  • Essay on Program
  • Programs Essay

Become a StudyMode Member

Sign Up - It's Free