Java Exercise 5

Mannan Ul Haq
0

 Objective:

  • Classes and Objects in Java
  • Constructors

Activity:

Task 1:

1- Create a class named Rectangle.
2- Define its private variables length, width, variable area.
3- Create a default constructor to initialize these variables.
4- Create a parametrized constructor.
5- Create these functions:
a. Define set functions for length and width. These should check that the values are greater than zero, else this function should print “length should be greater than zero” “width should be greater than zero”. Name of functions should be like “setLength()”
b. Define calculateArea() function, it should set the area =length*width.
c. Define a get functions for all 3 member variables. Function names should like “getLength()”
d. Define a function setValues() that takes both length and width as input and sets those values and also sets the area.
e. Create a function isSquare() it should return True if length=width of rectangle else false.
f. Define a function to display() rectangle. It should print a rectangle using 0, for example if length =3 and width = 9, display should print

000000000
000000000
000000000

Task 2:

1- Create a class named Cuboid.
2- Define its private variables length, width, height,
3- Create a default and parametrized constructors to initialize these variables.
4- Create a copy constructor.
5- Inside this class make these functions:
a. Define set functions of length width and height. These values cannot be zero
b. Define get functions for all the member variables.
c. Define a function to calculate the area of cube.
d. Define a function to calculate volume of cube. This should calculate and return volume= W*H*L
e. Define a display() function to display length, width, height and volume.
f. Define a function isCube() which will return true if W=L=H.


Task 3:

- Create the main.

A. Create an object of type rectangle in main using default constructor.
B. Create an object of type rectangle in main using parameterized constructor.
C. Try to set the length and width to 0, -1, it should print message.
D. Set its length and width to 3 and 9 resp.
E. Call calculateArea() function
F. Use get functions to print the length width and area of rectangle.
G. Call display rectangle function to print it using *s.
H. Call isSquare() function for this rectangle.
I. Create an object obj1 of type cuboid in main using default constructor without passing values.
J. Create an object obj2 of type cuboid in main using parameterized constructor by passing values in the constructor.
K. Copy Values of obj1 in obj2 using your copy constructor function.
L. Try to set the L, W, H to 0, -1, 0 it should print message.
M. Set its height, length and width to 10, 10 and 10 resp.
N. Call getVolume() and print the volume in main
O. Use get functions to print the L, W, H and volume of cuboid.
P. Call display function to print the values of L, W, H and volume of cuboid.
Q. Call isCube() function for this cuboid and print the returned value.


Solution:

import java.util.Scanner;

class Rectangle {
    private int length;
    private int width;
    private int area;

    public Rectangle() {
        this.length = 0;
        this.width = 0;
        this.area = 0;
    }

    public Rectangle(int length, int width) {
        this.length = length;
        this.width = width;
        this.area = 0;
    }

    public void setLength() {
        if (length < 1)
            System.out.println("Length should be Greater than Zero!");
    }

    public void setWidth() {
        if (width < 1)
            System.out.println("Width should be Greater than Zero!");
    }

    public void calculateArea() {
        area = length * width;
    }

    public int getLength() {
        return this.length;
    }

    public int getWidth() {
        return this.width;
    }

    public int getArea() {
        return this.area;
    }

    public void setValues() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter Length:");
        length = scanner.nextInt();
        System.out.println("Enter Width:");
        width = scanner.nextInt();
        area = length * width;
    }

    public boolean isSquare() {
        return (length == width);
    }

    public void display() {
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < width; j++) {
                System.out.print("0");
            }
            System.out.println();
        }
    }

    public void display2() {
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < width; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

class Cuboid {
    private int length;
    private int width;
    private int height;
    Scanner scanner = new Scanner(System.in);

    public Cuboid() {
        this.length = 5;
        this.width = 5;
        this.height = 4;
    }

    public Cuboid(int length, int width, int height) {
        this.length = length;
        this.width = width;
        this.height = height;
    }

    public Cuboid(Cuboid original) {
        System.out.println("Copy Constructor Called!!!");
        this.length = original.length;
        this.width = original.width;
        this.height = original.height;
    }

    public void setLength() {
        do {
            System.out.println("Enter Length greater than Zero: ");
            length = scanner.nextInt();
            if (length <= 0) {
                System.out.println("Error!");
            }
        } while (length <= 0);
    }

    public void setWidth() {
        do {
            System.out.println("Enter Width greater than Zero: ");
            width = scanner.nextInt();
            if (width <= 0) {
                System.out.println("Error!");
            }
        } while (width <= 0);
    }

    public void setHeight() {
        do {
            System.out.println("Enter Height greater than Zero: ");
            height = scanner.nextInt();
            if (height <= 0) {
                System.out.println("Error!");
            }
        } while (height <= 0);
    }

    public int getLength() {
        return this.length;
    }

    public int getWidth() {
        return this.width;
    }

    public int getHeight() {
        return this.height;
    }

    public void calculateArea() {
        int area = length * width;
        System.out.println("Area: " + area);
    }

    public int calculateVolume() {
        return length * width * height;
    }

    public boolean isCube() {
        return (length == width && length == height);
    }

    public void display() {
        int volume = calculateVolume();
        System.out.println("Length: " + length);
        System.out.println("Width: " + width);
        System.out.println("Height: " + height);
        System.out.println("Volume: " + volume);
    }
}

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        char choice1 = 'n';
        do {
            System.out.println("Press 1: For Task 1.");
            System.out.println("Press 2: For Task 2.");
            System.out.println("Press 3: For Task 3.");
            int choice2 = scanner.nextInt();

            if (choice2 == 1) {
                Rectangle area1 = new Rectangle();
                Rectangle area2 = new Rectangle(10, 5);

                System.out.println("For Default: length = 0 and width = 0.");
                area1.setLength();
                area1.setWidth();
                area1.calculateArea();
                System.out.println("Area = " + area1.getArea());

                System.out.println("For Parametrized: length = 10 and width = 5.");
                area2.setLength();
                area2.setWidth();
                area2.calculateArea();
                System.out.println("Area = " + area2.getArea());

                area1.setValues();

                boolean square;
                square = area1.isSquare();
                if (square) {
                    System.out.println("Length is equal to Width.");
                }
                else {
                    System.out.println("Length is not equal to Width.");
                }

                area1.display();
            }
            else if (choice2 == 2) {
                Cuboid cube1 = new Cuboid();
                Cuboid cube2 = new Cuboid(6, 6, 3);

                System.out.println("For Default: length = 5 and width = 5 and height = 4.");
                System.out.println("For Parametrized: length = 6 and width = 6 and height = 3.");

                cube1.setLength();
                cube1.setWidth();
                cube1.setHeight();

                cube1.calculateArea();

                int volume;
                volume = cube1.calculateVolume();

                cube1.display();

                boolean cube;
                cube = cube1.isCube();
                if (cube) {
                    System.out.println("Length is equal to Width and Height.");
                }
                else {
                    System.out.println("Length is not equal to Width and Height.");
                }
            }

            else if (choice2 == 3) {
                Rectangle area3 = new Rectangle();
                Rectangle area4 = new Rectangle(3, 9);

                System.out.println("For Default: length = 0 and width = 0.");
                area3.setLength();
                area3.setWidth();

                System.out.println("For Parametrized: length = 3 and width = 9.");
                area4.setLength();
                area4.setWidth();
                area4.calculateArea();
                System.out.println("Area = " + area4.getArea());

                area3.setValues();

                System.out.println("Length: " + area3.getLength());
                System.out.println("Width: " + area3.getWidth());
                System.out.println("Area: " + area3.getArea());

                boolean square;
                square = area3.isSquare();
                if (square) {
                    System.out.println("Length is equal to Width.");
                }
                else {
                    System.out.println("Length is not equal to Width.");
                }

                area3.display2();

                Cuboid cube3 = new Cuboid();
                Cuboid cube4 = new Cuboid(6, 6, 3);

                System.out.println("For Default: length = 5, width = 5, and height = 4.");
                System.out.println("For Parametrized: length = 6, width = 6, and height = 3.");

                Cuboid cube6 = new Cuboid(cube4);

                cube3.setLength();
                cube3.setWidth();
                cube3.setHeight();

                cube3.calculateArea();

                int volume;
                volume = cube3.calculateVolume();
                System.out.println("Volume: " + volume);

                System.out.println("Length: " + cube3.getLength());
                System.out.println("Width: " + cube3.getWidth());
                System.out.println("Height: " + cube3.getHeight());

                cube3.display();

                boolean cube;
                cube = cube3.isCube();
                if (cube) {
                    System.out.println("Length is equal to Width and Height.");
                }
                else {
                    System.out.println("Length is not equal to Width and Height.");
                }
            }

            System.out.println("Press y: To Repeat Program!");
            choice1 = scanner.next().charAt(0);

        } while (choice1 == 'y');
    }
}


Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !