Program to Explain Getter and Setter Functions

Only available on StudyMode
  • Download(s) : 71
  • Published : February 3, 2013
Open Document
Text Preview
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...
tracking img