In: Computer Science
string getStyle() ;
int getNumOfBedRooms() ;
int getNumOfBathRooms();
int getNumOfCarsGarage();
int getYearBuilt();
int getFinishedSquareFootage() ;
double getPrice(double p) ;
double getTax(double t) ;
void print() ;
houseType();
houseType(string s, int numOfBeds, int numOfBaths, int
numOfCars,
int yBuilt, int squareFootage, double p, double t);
private:
string style;
int numOfBedrooms;
int numOfBathrooms;
int numOfCarsGarage;
int yearBuilt;
int finishedSquareFootage;
double price;
double tax;
};
Questions
a.Write the definition of the member function set so that private
members are set accordingy to the parameters.
b. Write the definition of the member function print that prints
the values of the data members.
c. Write the definition of the constructor of the class houseType
so that the private member variables are initialized according to
the parameters.
d. Write a Java statement that prints the value of the object
newHouse.
e. Write a Java statement that declares an object house of type
newHouse and initializes the member variables of house to
"Ranch",3,2,2,2005,1300,185000 and 3600.0 respectively.
f. Which function members are accessors and which are mutators
?
Solved in Java please
a.
The definition of member function set that set the private members are as follows :
//function set with all parameters that is to be set for private memebrs
void set(string s, int numOfBeds, int numOfBaths, int numOfCars, int yBuilt, int squareFootage, double p, double t){
//all private members
//set the style
//this keyword is used to reference the current object
this.style = s;
//set the number of bed
rooms
this.numOfBedrooms = numOfBeds;
//set the number of bath
rooms
this.numOfBathrooms = numOfBaths;
//set the number of cars
this.numOfCarsGarage = numOfCars;
//set the built year
this.yearBuilt = yBuilt;
//set the finished square
footage
this.finishedSquareFootage = squareFootage;
//set the price
this.price = p;
//set the tax
this.tax=t;
}
b. The definition of member function print that prints the value of data members
void print(){
//print the value of data members
System.out.println("Style : "+this.style);
System.out.println("Number of bedrooms : "+this.numOfBedrooms);
System.out.println("Number of bathrooms : "+this.numOfBathrooms);
System.out.println("Number of cars : "+this.numOfCarsGarage);
System.out.println("Built year : "+this.yearBuilt);
System.out.println("Finished square footage : "+this.finishedSquareFootage);
System.out.println("Price : "+this.price);
System.out.println("Tax : "+this.tax);
}
c. Constructor to initialize the private members, a constructor should not have a return type.
houseType(string s, int numOfBeds, int numOfBaths, int numOfCars, int yBuilt, int squareFootage, double p, double t){
//set the style
//this keyword is used to reference the current object
this.style = s;
//set the number of bed
rooms
this.numOfBedrooms = numOfBeds;
//set the number of bath
rooms
this.numOfBathrooms = numOfBaths;
//set the number of cars
this.numOfCarsGarage = numOfCars;
//set the built year
this.yearBuilt = yBuilt;
//set the finished square
footage
this.finishedSquareFootage = squareFootage;
//set the price
this.price = p;
//set the tax
this.tax=t;
}
d. Java statement to print the values of an object of class houseType. Consider obj is an object of class houseType , then the below statement can be used to print the values
//print() is the method in class houseType
//it display all the values
obj.print();
e. The statement to create an object house of class houseType , which also initializes the values is given below
houseType house = new houseType("Ranch",3,2,2,2005,1300,185000,3600.0);
//this creates an object house by calling the parameterized constructor declared in the class definition
//which will initialize all the member variables with the passed parameters
f. Accessors are used to access or get the value of private member variables. Mutators are used to set the value of the private member variables. Here,
Accessors are :
getStyle() ;
getNumOfBedRooms() ;
getNumOfBathRooms();
getNumOfCarsGarage();
getYearBuilt();
getFinishedSquareFootage() ;
getPrice() ;
getTax() ;