Question

In: Computer Science

Using ONE of the following themes to create a class and demonstrate the idea. Use this...

Using ONE of the following themes to create a class and demonstrate the idea. Use this to create a solution (two ADTs and a driver program) to demonstrate the following concepts. Create a one page summary to describe where you are using each of these concepts.

Themes:(PICK ONE)

  • a house has a door
  • a semester has a holiday break
  • a cell phone has a shatterproof case
  • a chair has a cushion

Concepts to include:

  1. composition
  2. separating interface from implementation
  3. an include guard
  4. utilizing a driver program
  5. constructor with default arguments
  6. destructor
  7. utility function
  8. toString function
  9. the this Pointer
  10. a static data member
  11. default memberwise assignment
  12. constant object
  13. constant function
  14. arrow operator
  15. cascaded function calls

Solutions

Expert Solution

Using the theme: a house has a door (Using C++)

One of the most effective advantage of the object-oriented programming(OOPS) languages is code reuse. And the concepts of inheritance and composition are used to achieve the same.

Here we will look upon the concept of object composition to implement has- a relation in classes.

Composition(HAS-A) is achieved by the use of instance variables that are references to other objects. For example: a house has a door.

Let us define two abstract data types separately for house and door using the classes as ADT in C++.

#include <utility>      //Utility class
#include <iostream>  
#ifndef HOUSE_H        //let there be a header class House.h
#define HOUSE_H         //Include guards avoiding double inclusion of the header file
using namespace std; 
public class House {   //classname
    private String location;    //data members
        private static int doors;          //static data member
    public House()
    {
    } 
    public House(String location,int doors ){  //constructor with default arguments
      this.location=location;
      this.doors=doors;                       
     }
    public String getLocation() {      //member functions
        return location;
    }
    public void setLocation(String location) {
        this->location = location;              //this pointer using arrow operator
    }
    public static int getNoOfDoors() const {  //constant function
        return doors;                            //cannot modify any data member of this object
    }
    public static void setNoOfDoors(int doors) {
        this.doors = abs(doors);                  //abs is a utility function for absolute value
    }
    public ~House(){               //Default destructor that does nothing
    }
    
    
}


public class Door {

    //composition has-a relationship
    private House house;
   
    public Door(){                     //Constructor
        this.house=new House();
        house.setNoofDoors(10);
    }
    public int getNoOfDoors() {
        return house.getNoOfDoors();
    }
    public getA()
                {
                        cout <<"\nA"<<endl;

                        return *this;
                }
                
    public getB()
                {
                        cout <<"\nB"<<endl;

                        return *this;
                }

}

//Here is a driver program for composition that uses door object and calculate the total no. of doors in a house.


public class CheckForDoors {

    int main() {
        Door door();                
        int noofdoors = door.getNoOfDoors();
        cout <<noofdoors<<endl;
        door.getA().getB();
    }

}

Output:
 10
 A
 B

An ADT comprises of two separate sections:

  1. Interface Design
  2. The code of the class that implements the interface

The classes we have made are separated from its implementation. The classes can be defined as a way of representation for similar kind of objects or instances. Separating the interfaces from its implementation is a good practice for software engineering. We can say a class represents:

  • Set of values an object may have
  • Operations that can be performed on those values

toString Method in Java:

The toString() method comes in handy if we have to return the string representation of the object.

toString() method on the object is internally invoked whenever we print any object. We need to override the toString() method according to our use.


Related Solutions

Code in C++. Using ONE of the following themes to create a class and demonstrate the...
Code in C++. Using ONE of the following themes to create a class and demonstrate the idea. Use this to create a solution (two ADTs and a driver program) to demonstrate the following concepts. Create a one page summary to describe where you are using each of these concepts. Themes:(PICK ONE) a house has a door a semester has a holiday break a cell phone has a shatterproof case a chair has a cushion Concepts to include: composition separating interface...
write the program in java. Demonstrate that you understand how to use create a class and...
write the program in java. Demonstrate that you understand how to use create a class and test it using JUnit Let’s create a new Project HoursWorked Under your src folder, create package edu.cincinnatistate.pay Now, create a new class HoursWorked in package edu.cincinnatistate.pay This class needs to do the following: Have a constructor that receives intHours which will be stored in totalHrs Have a method addHours to add hours to totalHrs Have a method subHours to subtract hours from totalHrs Have...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string...
In C++ Demonstrate inheritance. Create an Airplane class with the following attributes: • manufacturer : string • speed : float Create a FigherPlane class that inherits from the Airplane class and adds the following attributes: • numberOfMissiles : short
Please analyze and create a starter code in Python using spaCy, that can demonstrate practical use...
Please analyze and create a starter code in Python using spaCy, that can demonstrate practical use of the package. Also give a brief explanation of what the purpose of the spaCy package is. Please use 3 different methods such as a loop statement, if statement, tuples, lists etc. Please provide detailed comments on each line of code. Please explain the purpose of the method used. Please write and run your code example.
"Gambling Greg" Assignment Outcomes: Demonstrate the ability to create and use structs Demonstrate the ability to...
"Gambling Greg" Assignment Outcomes: Demonstrate the ability to create and use structs Demonstrate the ability to create and use menus Demonstrate the ability to create and use an array of structs Demonstrate the ability to generate and use random numbers Program Specifications: Assume that gambling Greg often goes to the Dog Racing Track. Greg loves to bet on the puppies. In each race Greg will place a wager and pick a dog. The dog information will be stored in a...
Inheritance - Polymorphism One advantage of using subclasses is the ability to use polymorphism. The idea...
Inheritance - Polymorphism One advantage of using subclasses is the ability to use polymorphism. The idea behind polymorphism is that several different types of objects can have the same methods, and be treated in the same way. For example, have a look at the code we’ve included for this problem. We’ve defined Shape as an abstract base class. It doesn’t provide any functionality by itself, but it does supply an interface (in the form of .area() and .vertices() methods) which...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to create an array of structs Program Specifications: DESIGN and IMPLEMENT a program that will CREATE and use three different variables of type PERSON. Create a struct using the typedef command for a DATE. Create a struct for a PERSON with the following fields. name [this will be a string] birthdate [this will be a DATE] gender [this will be a char] annualIncome [this will...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to create an array of structs Program Specifications: DESIGN and IMPLEMENT a program that will CREATE and use three different variables of type PERSON. Create a struct using the typedef command for a DATE. Create a struct for a PERSON with the following fields. name [this will be a string] birthdate [this will be a DATE] gender [this will be a char] annualIncome [this will...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to create an array of structs Program Specifications: DESIGN and IMPLEMENT a program that will CREATE and use three different variables of type PERSON. Create a struct using the typedef command for a DATE. Create a struct for a PERSON with the following fields. name [this will be a string] birthdate [this will be a DATE] gender [this will be a char] annualIncome [this will...
Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive...
Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive a Square Class from the Rectangle Class 3. Derive a Right Triangle Class from the Rectangle Class 4. Create a Circle Class 5. Create a Sphere Class 6. Create a Prism Class 7. Define any other classes necessary to implement a solution 8. Define a Program Driver Class for Demonstration (Create a Container to hold objects of the types described above Create and Add...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT