Monday, October 9, 2023

Codeathon05_Haritha


[Question]

 Java Inheritance / Simple oops

a)  Create Two Classes:

BaseClass

The Rectangle class should have two data fields-width and height of Int types. The class should have display() method, to print the width and height of the rectangle separated by space.

DerivedClass

The RectangleArea class is Derived from Rectangle class, I.e., It is the Sub-Class of Rectangle class. The class should have read_Input() method, to Read the Values of width and height of the Rectangle. The RectangleAreadass should also Overload the display() Method to Print the Area (width"height) of the Rectangle.

[Sample Input]

The First and Only Line of Input contains two space separated Integers denoting the width and height of the Rectangle.

Constraints

1 <= width, height <= 10^3

[Sample Output]

The Output Should Consist of Exactly Two Lines. In the First e, Print the Width and Height of the Rectangle Separated by Space.

In the Second Line, Print the Area of the Rectangle.

Explanation:

BaseClass contains two integer data fields for the width and height of a rectangle and a display() method to print these values separated by a space.

RectangleArea is a subclass of BaseClass and extends it. It includes a read_Input() method to read width and height values (you can use a Scanner object for input). It also overrides the display() method to print the area of the rectangle (width * height) instead of its dimensions.

The input consists of a single line with two space-separated integers representing the width and height of the rectangle. The output includes two lines: the first line prints the width and height, and the second line prints the area of the rectangle.


Program

package src;

import java.util.Scanner;

class Rectangle{

    int width;

    int height;

    void display() {

        System.out.println(width + " " + height);

    }

}

class RectangleArea extends Rectangle {

         void read_input() {

        Scanner scanner = new Scanner(System.in);

        width = scanner.nextInt();

        height = scanner.nextInt();

        scanner.close();

    }

    @Override

    void display() {              // Method to display the area

        super.display();

        int area = width * height;

        System.out.println(area);     // prints the area of a rectangle

    }

}

public class Codeathon05_Haritha {

    public static void main(String[] args) {

        RectangleArea rectangle = new RectangleArea();

        rectangle.read_input();

        rectangle.display();          // To display the area

    }

}

[Explanation of Solution]

Rectangle Class:

This class represents a basic rectangle and has two instance variables:

width to store the width of the rectangle.

height to store the height of the rectangle.

It also has a method called display(), which is used to display the width and height of the rectangle when called.

Rectangle Area Class:

This class extends the Rectangle class, which means it inherits the width and height fields and the display() method from the Rectangle class.

It adds its own method called read_input(), which is used to read the width and height of the rectangle from the user through the standard input (keyboard).

Inside the read_input() method, it creates a Scanner object to read input from the user. It reads two integers (the width and height) and assigns them to the width and height fields of the RectangleArea object.

This class also overrides the display() method inherited from the Rectangle class. In the overridden display() method:

It first calls the super.display() method to display the width and height inherited from the Rectangle class.

Then, it calculates the area of the rectangle (width * height) and stores it in a local variable called area.

Finally, it prints the area of the rectangle to the standard output.

Codeathon05_Haritha Class (Main):

This class contains the main() method, which is the entry point of the program.

Inside the main() method:

It creates an instance of the RectangleArea class called rectangle.

It then calls the read_input() method on the rectangle object, which reads the width and height of the rectangle from the user.

Finally, it calls the display() method on the rectangle object to display both the width and height of the rectangle and its calculated area.

Codeathon05b_Haritha

b) Write a Java Program that Swap the Values of 02 Variables without Using a 03rd Variable. The Swapped Value Should be In Such a Way that the 1st Variable will hold only 10% (rounded down) of the 2nd value and the 02nd Variable will hold only the 20% (rounded down) of the 1st value.

Example Input

 var1=250, var2=400

Example Output

var1=40, var2=50

Explanation:

This Java program swaps the values of two variables, `var1` and `var2`, without using a third variable. It ensures that after swapping, `var1` holds 10% (rounded down) of the original `var2` value, and `var2` holds 20% (rounded down) of the original `var1` value.

For example, if `var1` initially has a value of 250 and `var2` has a value of 400, after swapping, `var1` will be updated to hold 10% of 400 (40), and `var2` will hold 20% of 250 (50). The program then prints the swapped values, resulting in `var1=40` and `var2=50`.

Program:

import java.util.Scanner;

public class Codeathon05b_Haritha {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int var1 = sc.nextInt();

        int var2 = sc.nextInt();

        var1 = var1 + var2;        // var1 now holds the sum

        var2 = (var1 -  var2);     // var2 now holds 20% of var1

        var1 = (var1 - var2);

        System.out.println("var1 = " + ((int)(0.10*var1)));

        System.out.println("var2 = " + ((int)(0.20*var2)));

    }

}

Explanation:

1. The program starts by importing the Scanner class to read input from the user.

2. It initializes two integer variables, var1 and var2, by reading two integer values from the user using the Scanner.

3. The code then performs the following operations on these variables:

4. var1 = var1 + var2;: This line adds the values of var1 and var2 and stores the result back in var1. Essentially, it calculates the sum of the two input values and assigns it to var1.

5. var2 = (var1 - var2);: This line calculates the difference between the new value of var1 and the original value of var2. It then assigns this difference to var2. This operation seems to be intended to calculate 20% of the original value of var1 and store it in var2.

6. var1 = (var1 - var2);: This line calculates the difference between the new value of var1 (which already includes the result of subtracting 20% of the original var1) and the current value of var2. It then assigns this difference back to var1. This operation seems to be intended to calculate 80% of the original value of var1 and store it in var1.

7. Finally, the program prints the values of var1 and var2 after these operations, but it multiplies them by 0.10 and 0.20, respectively, and converts the results to integers using (int). So, the printed values represent 10% and 20% of the original var1 and var2, respectively.





Haritha .P(Intern),
Guard Ninjas,
Data Shield Team,
Enterprise Minds.   






0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home