Question

In: Computer Science

white a class having two private variables and one member function which will return the area...

white a class having two private variables and one member function which will return the area of rectangle

Solutions

Expert Solution

This code is written in Java language.

There are following Components present in Code.

1. A class named Rectangle.

2. Two private variables i.e length and breadth

3.A constructor with two parameters to set the values of length and breadth.

4. member function getArea() which will return area of rectangle.

5. A main class to test the code.

Following is the code which is saved in Rectangle.java

Comments are added to give an idea about what code is doing.

// A class with name Rectangle
public class Rectangle {

        // Two private variables
        private int length;

        private int breadth;

        // Constructor to set the values
        public Rectangle(int x, int y) {
                length = x;
                breadth = y;
        }

        // function which will return Area of rectangle i.e length*breadth
        public int getArea() {
                return length * breadth;
        }

        public static void main(String[] args) {
        // Creating a Rectangle with Length = 10 and Breadth = 5
                Rectangle rec = new Rectangle(10, 5);
                System.out.println("Area of rectangle is " + rec.getArea());
        }

}

When you will run above code, output will be like following :

Please let us know in comments if you have any doubt in above solution.


Related Solutions

# List the two private member variables (including name and functionality) in the node class. #Write...
# List the two private member variables (including name and functionality) in the node class. #Write a general pattern for a loop statement that traverses all the nodes of a linked list
C++ make a rational class that includes these members for rational -private member variables to hold...
C++ make a rational class that includes these members for rational -private member variables to hold the numerator and denominator values -a default constructor -an overloaded constructor that accepts 2 values for an initial fraction -member fractions add(), sub(), mul(), div(), less(), eq(), and neq() (less should not return true if the object is less than argument) -a member function that accepts an argument of type of ostream that writes the fraction to that open output stream do not let...
Divide by classes. Parameters and return types of each function and class member function should be...
Divide by classes. Parameters and return types of each function and class member function should be decided in advance. Write a program that computes a patient's bill for a hospital stay. The different components are: PatientAccount Class Surgery Class Pharmacy Class main program Patient Account will keep total of the patient's charges. Keep track of the number of days spent in the hospital. Group must decide on hospital's daily rate. Surgery class will have stored within it the charges for...
C++ language You will create a Hangman class. Possible private member variables: int numOfCharacters; //for the...
C++ language You will create a Hangman class. Possible private member variables: int numOfCharacters; //for the secret word char * secretWord; char *guessedWord; public: //please create related constructors, getters, setters,constructor() constructor() You will need to initialize all member variables including the two dynamic variables. destructor() Please deallocate/freeup(delete) the two dynamic arrays memories. guessALetter(char letter) 1.the function will check if the letter guessed is included in the word. 2. display the guessed word with related field(s) filled if the letter guessed...
myLinkedList.java>>>>>>>> public class MyLinkedList implements MiniList<Integer>{ /* Private member variables that you need to declare: **...
myLinkedList.java>>>>>>>> public class MyLinkedList implements MiniList<Integer>{ /* Private member variables that you need to declare: ** The head pointer ** The tail pointer */ public class Node { // declare member variables (data and next) // finish these constructors public Node(int data, Node next) {} public Node(int data) {} // HINT: use this() with next = null } // Initialize the linked list (set head and tail pointers) public MyLinkedList() {} @Override public boolean add(Integer item) { return false; }...
(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function...
(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function that returns r value of property variable -Put(int d): A function that stores d in attribute variable r *Declare a one-dimensional array of type Circle and in each array element Read and store integers from standard input device *Declare the swap() function to swap two elements with each other *Write a program that sorts the elements of a one-dimensional array of circle type in...
1.For Python Which statement(s) are true regarding private class variables? private variables can be accessible inside...
1.For Python Which statement(s) are true regarding private class variables? private variables can be accessible inside the class. private variables can be accessible outside the class. private variable values can be changed directly outside of the class private variables can be changed outside the class through set methods. 2. In the following code, def A: def __init__(self):      __a = 1      self.__b = 1      self.c = 1      __d__ = 1 # Other methods omitted Which of the following is a private...
java programming Create a class named Money. It should have member variables for Member Variables Store...
java programming Create a class named Money. It should have member variables for Member Variables Store dollars and cents as members (both should be int). They should be accessible from only inside the class. Methods  Write a default constructor that sets members to 0.  Write a two-parameter constructor that sets members to the parameter values.  Write get/set methods for the member variables.  Write an override method for toString. The returned string should be formatted as a...
PLEASE WRITE THIS IN C++ Write a ComplexNumber class that has two member variables realNum and...
PLEASE WRITE THIS IN C++ Write a ComplexNumber class that has two member variables realNum and complexNum of type double. Your class shall have following member functions Complex(); Complex(double, double); Complex add(Complex num1, Complex num2); Complex subtract(Complex num1, Complex num2); string printComplexNumber(); Write a driver program to test your code. Please make sure your code is separated into Complex.h Containing definition of the class Complex.cpp Containing the implementation ComplexTestProgram.cpp Containing main program Use the following directive in class definition to...
write a member function in C++ , that takes two lists and return list that contain...
write a member function in C++ , that takes two lists and return list that contain the merge of the two lists in the returned list: first insert the first list and then the second list  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT