[Question]
Java Inheritance / Simple oops [www.techgig.com]
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 of Solution]
This program demonstrates the concept of inheritance in object-oriented programming, where a subclass (RectangleArea) inherits attributes and methods from its superclass (BaseClass) and extends them to perform additional tasks, such as calculating and displaying the area of a rectangle.
Github Repo for Code: https://tinyurl.com/3avkxnz2
[Solution (Java Code)]
/*
* Core Java Problem Solving - Codeathon.
* A.R. Kishore Kumar ~ (c) 2023 ~ -- ~ Tirupati, Andhra Pradesh, India ~
*
*/
package codeathon;
import java.util.Scanner;
//Rectangle class (Super Class) with Setter/Getter and display() methods
class Rectangle
{
private int width;
private int height;
public void display()
{
System.out.println(width + " " + height);
}
public int getWidth()
{
return width;
}
public void setWidth(int width)
{
this.width=width;
}
public int getHeight()
{
return height;
}
public void setHeight(int height)
{
this.height=height;
}
}
//RectangleArea Sub Class with overrided display() method
class RectangleArea extends Rectangle
{
public void read_input()
{
Scanner sc = new Scanner (System.in);
setWidth(sc.nextInt());
setHeight(sc.nextInt());
}
public void display()
{
super.display();
System.out.println(getWidth()*getHeight());
}
}
public class Codeathon05A_Kishore
{
public static void main(String args[] )
{
RectangleArea rectangleArea = new RectangleArea();
rectangleArea.read_input();
rectangleArea.display();
}
}
[Question]
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.
[Sample Input]
var1=250, var2=400
[Sample
Output]
var1=40, var2=50
[Explanation of Solution]
This Java program efficiently swaps the values of two variables, var1 and var2, without utilizing a third variable. It guarantees that, after swapping, var1 holds 10% (rounded down) of the original var2 value, and var2 contains 20% (rounded down) of the initial var1 value.
Github Repo for Code: https://tinyurl.com/mrczn6hh
[Solution (Java Code)]
/*
* Core Java Problem Solving - Codeathon.
* A.R. Kishore Kumar ~ (c) 2023 ~ -- ~ Tirupati, Andhra Pradesh, India ~
*
*/
package codeathon;
import java.util.Scanner;
public class Codeathon05B_Kishore
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("var1 = ");
int var1 = sc.nextInt();
System.out.print("var2 = ");
int var2 = sc.nextInt();
// logic to swap two numbers without using thrid variable in
// a single line
var1 = (var1+var2) - (var2 = var1);
var1 = (int) Math.round (var1* 0.10 ) ;
var2 = (int) Math.round (var2* 0.20 ) ;
System.out.println("var1=" + var1 + ", var2=" + var2);
}
}
Java Inheritance / Simple oops [www.techgig.com]
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
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 of Solution]
This program demonstrates the concept of inheritance in object-oriented programming, where a subclass (RectangleArea) inherits attributes and methods from its superclass (BaseClass) and extends them to perform additional tasks, such as calculating and displaying the area of a rectangle.
Github Repo for Code: https://tinyurl.com/3avkxnz2
[Solution (Java Code)]
/*
* Core Java Problem Solving - Codeathon.
* A.R. Kishore Kumar ~ (c) 2023 ~ -- ~ Tirupati, Andhra Pradesh, India ~
*
*/
package codeathon;
import java.util.Scanner;
//Rectangle class (Super Class) with Setter/Getter and display() methods
class Rectangle
{
private int width;
private int height;
public void display()
{
System.out.println(width + " " + height);
}
public int getWidth()
{
return width;
}
public void setWidth(int width)
{
this.width=width;
}
public int getHeight()
{
return height;
}
public void setHeight(int height)
{
this.height=height;
}
}
//RectangleArea Sub Class with overrided display() method
class RectangleArea extends Rectangle
{
public void read_input()
{
Scanner sc = new Scanner (System.in);
setWidth(sc.nextInt());
setHeight(sc.nextInt());
}
public void display()
{
super.display();
System.out.println(getWidth()*getHeight());
}
}
public class Codeathon05A_Kishore
{
public static void main(String args[] )
{
RectangleArea rectangleArea = new RectangleArea();
rectangleArea.read_input();
rectangleArea.display();
}
}
[Question]
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.
[Sample Input]
var1=250, var2=400
var1=40, var2=50
[Explanation of Solution]
This Java program efficiently swaps the values of two variables, var1 and var2, without utilizing a third variable. It guarantees that, after swapping, var1 holds 10% (rounded down) of the original var2 value, and var2 contains 20% (rounded down) of the initial var1 value.
Github Repo for Code: https://tinyurl.com/mrczn6hh
[Solution (Java Code)]
/*
* Core Java Problem Solving - Codeathon.
* A.R. Kishore Kumar ~ (c) 2023 ~ -- ~ Tirupati, Andhra Pradesh, India ~
*
*/
package codeathon;
import java.util.Scanner;
public class Codeathon05B_Kishore
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("var1 = ");
int var1 = sc.nextInt();
System.out.print("var2 = ");
int var2 = sc.nextInt();
// logic to swap two numbers without using thrid variable in
// a single line
var1 = (var1+var2) - (var2 = var1);
var1 = (int) Math.round (var1* 0.10 ) ;
var2 = (int) Math.round (var2* 0.20 ) ;
System.out.println("var1=" + var1 + ", var2=" + var2);
}
}
No comments:
Post a Comment