[Question] #1
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
Code Explaination:
- It demonstrates basic arithmetic operations in Java to swap and manipulate integer values.
- The program uses a Scanner to collect user input for var1 and var2.
- It swaps the values of var1 and var2 without a temporary variable using arithmetic operations
- After swapping, both var1 and var2 are scaled down by multiplying them by 0.1 and 0.2, respectively.
- The program displays the swapped and scaled values as "Swapped values: var1 = [var1], var2 = [var2]."
Source Code(Java):
import java.util.Scanner;
public class Question05_01_Venkata {
public static void main(String[]
args) {
// Step 1: Input
Scanner input = new Scanner(System.in);
System.out.print("Enter
the value of var1: ");
int var1 = input.nextInt();
System.out.print("Enter
the value of var2: ");
int var2 = input.nextInt();
// Step 2: Swap using arithmetic
operations
// Swap var1 and var2 without
using a temporary variable
var1 = var1 + var2;
var2 = var1 - var2;
var1 = var1 - var2;
// Step 3: Apply a scaling factor
// Scale down the swapped values
var1 = (int) (0.1 * var1);
var2 = (int) (0.2 * var2);
// Step 4: Output
System.out.println("Swapped
values: var1 = " + var1 + ", var2 = " + var2);
}
}
[Question] #2
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.
In Second line, Print the Area of the Rectangle
- Code Explaination:
- Class
Structure:
- Two
classes: Rectangle for storing dimensions and RectangleArea
for calculation.
- Inheritance:
RectangleArea extends Rectangle for added functionality.
- User
Input:
- The
program collects user input for rectangle width and height.
- Input
uses the Scanner class, and clear prompts are provided.
- Display
Method Override:
- RectangleArea class overrides display method to show dimensions and area.
- Area
Calculation:
- The program calculates the rectangle area by multiplying width and height.
- Main
Method:
- The
entry point creates an instance of RectangleArea.
- Displays
a user-friendly interface with a title and prompts.
- Collects
user input and displays rectangle dimensions and area.
- User-Friendly:
- The code offers a clear and user-friendly interface.
- Purpose:
- Allows users to calculate the area of a rectangle from its dimensions. It follows best coding practices for clarity and usability.
Source Code(Java):
import java.util.Scanner;
class Rectangle {
int width;
int height;
public void display() {
System.out.println("Rectangle
dimensions: " + width + " x " + height);
}
}
class RectangleArea extends Rectangle {
public void readInput() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter
the width of the rectangle: "+"\n");
width = scanner.nextInt();
System.out.print("Enter
the height of the rectangle: "+"\n");
height = scanner.nextInt();
scanner.close();
}
@Override
public void display() {
super.display(); // Display
dimensions from the parent class
int area = width * height;
System.out.println("Area
of the rectangle: " + area);
}
}
public class Codeathon05_02_Venkata {
public static void main(String[]
args) {
RectangleArea rectangleArea = new
RectangleArea();
System.out.println("Rectangle
Area Calculator");
System.out.println("--------------------------"+"\n");
rectangleArea.readInput(); // Prompt user to
enter width and height
rectangleArea.display(); // Display rectangle dimensions and area
}
}
Thank you
Venkata kishore T(Intern)
Shield Warriors,
Data Shield Team,
Enterprise Minds.
No comments:
Post a Comment