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...
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...
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...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
5. In the following code snippet, which member function of the class Car is called first?...
5. In the following code snippet, which member function of the class Car is called first? class Car { public: void start(); void accelerate(double acc_speed); void stop(); double get_speed() const; Car(); private: double speed; }; Car::Car() { speed = 0; } void Car::start() { accelerate(get_speed() + 10); } void Car::stop() { speed = 0; } void Car::accelerate(double acc_speed) { speed = speed + acc_speed; } double Car::get_speed() const { return speed; } int main() { Car c1; c1.start(); c1.accelerate(10); c1.get_speed();...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max   // 2.) A constructor which takes initial values for // min and max // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is greater than...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables,...
public class OperationsBetween { // You must define the following: // 1.) Two private instance variables, min and max // // 2.) A constructor which takes initial values for // min and max // // 3.) An instance method named sum, which sums the // values between min and max and returns the // result. For example, if min = 3 and max = 5, // then this should return 12 (3 + 4 + 5). If // min is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT