[Question]
Java Inheritance / Simple oops
[www.techgig.com]
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 RectangleArea dass 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³
[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.
[Explanation of the Solution]
In this problem we are using Java Inheritance / Simple oops concepts to call display method in rectangle class and area of traingle REctangleArea class.
Github Repo for Code:
[Solution (Java Code)]
package codeathon;
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() {
super.display();// get the information from parent class
int area = width * height;
System.out.println(area); // prints the area of a rectangle
}
}
public class Codeathon05A_vijay {
public static void main(String[] args) {
RectangleArea rectangleArea =new RectangleArea();
rectangleArea.read_input();
rectangleArea.display();
}
}
Swap the variables (Java code)
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.
Sample Input
var1=250, var2=400
Sample Output
var1=40, var2=50
[Explanation of the Solution]:
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`.
Github Repo for Code:
[Solution (Java Code)]
package codeathon;
import java.util.Scanner;
public class codeathon05B_vijay {
public static void main(String[] args) {
System.out.println("Enter the value of x and y");
Scanner sc = new Scanner(System.in);
int variable1 = sc.nextInt();
int variable2 = sc.nextInt();
variable1 = variable1 + variable2;
variable2 = (variable1 - variable2);
variable1 = (variable1 - variable2);
System.out.println("variable1 = " + ((int)(0.10*variable1)));//10% of variable1
System.out.println("variable2 = " + ((int)(0.20*variable2)));//20% of variable2
}
}
Thank you,
k.vijay(Intern)
vijay.keradhi@eminds.ai
Enterprise Minds.
No comments:
Post a Comment