0% found this document useful (0 votes)
10 views3 pages

Java Programs for Arithmetic and Geometry

The document contains three JAVA programs: one for performing basic arithmetic operations, another for checking if a number is even or odd, and a third for calculating the perimeter and area of various geometrical figures. Each program includes user input prompts and sample output to demonstrate functionality. The programs utilize the Scanner class for input and perform calculations based on the provided values.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Java Programs for Arithmetic and Geometry

The document contains three JAVA programs: one for performing basic arithmetic operations, another for checking if a number is even or odd, and a third for calculating the perimeter and area of various geometrical figures. Each program includes user input prompts and sample output to demonstrate functionality. The programs utilize the Scanner class for input and perform calculations based on the provided values.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment 1

1. Write a JAVA program to perform arithmetic operations.

import [Link];

public class ArithmeticOperations {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter first number: ");

int a = [Link]();

[Link]("Enter second number: ");

int b = [Link]();

[Link]("Addition: " + (a + b));

[Link]("Subtraction: " + (a - b));

[Link]("Multiplication: " + (a * b));

[Link]("Division: " + (a / b));

// Sample Output:

// Enter first number: 10

// Enter second number: 5

// Addition: 15

// Subtraction: 5

// Multiplication: 50

// Division: 2

2. Write a JAVA program to check whether a number is even or odd.

import [Link];

public class EvenOdd {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter a number: ");

int num = [Link]();

if (num % 2 == 0)

[Link](num + " is Even.");

else

[Link](num + " is Odd.");

// Sample Output:

// Enter a number: 7

// 7 is Odd.

3. Write a JAVA program to find the perimeter and area of different geometrical figures.

import [Link];

public class Geometry {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

// Rectangle

[Link]("Enter length and breadth of rectangle: ");

double l = [Link]();

double b = [Link]();

[Link]("Rectangle Area: " + (l * b));

[Link]("Rectangle Perimeter: " + (2 * (l + b)));

// Circle

[Link]("Enter radius of circle: ");

double r = [Link]();

[Link]("Circle Area: " + ([Link] * r * r));

[Link]("Circle Perimeter: " + (2 * [Link] * r));

// Triangle
[Link]("Enter base and height of triangle: ");

double base = [Link]();

double height = [Link]();

[Link]("Triangle Area: " + (0.5 * base * height));

[Link]("Enter three sides of triangle: ");

double a = [Link]();

double b1 = [Link]();

double c = [Link]();

[Link]("Triangle Perimeter: " + (a + b1 + c));

// Sample Output:

// Enter length and breadth of rectangle: 4 5

// Rectangle Area: 20.0

// Rectangle Perimeter: 18.0

// Enter radius of circle: 3

// Circle Area: 28.27

// Circle Perimeter: 18.85

// Enter base and height of triangle: 4 5

// Triangle Area: 10.0

// Enter three sides of triangle: 3 4 5

// Triangle Perimeter: 12.0

Common questions

Powered by AI

Input validation is critical to prevent errors and ensure the robustness of the programs. Without validation, the arithmetic operations program might encounter division by zero errors, leading to runtime crashes, while non-integer input could trigger `InputMismatchException`. The geometrical calculations could yield incorrect or runtime errors if negative or non-numeric values are entered. Ignoring input validation compromises program reliability and could lead to unexpected behavior, security issues, and poor user experience .

The program ensures accuracy by using the precise mathematical formulas for each geometric figure. It utilizes the `Scanner` class for user input, ensuring correct data capture. For the area and perimeter calculations, it directly applies these formulas: for rectangles (Area = length * breadth, Perimeter = 2*(length + breadth)), circles (Area = π*r^2, Circumference = 2*π*r), and triangles (Area = 0.5*base*height, Perimeter = sum of all sides). Furthermore, it employs `Math.PI` for accurate π values, minimizing floating-point errors .

The `Scanner` class is pivotal for accepting user input from the console, parsing it into various types as needed, like `int` or `double`. It simplifies capturing inputs in a user-friendly manner. Alternatives in modern Java applications include using `BufferedReader` for faster input on larger data sets, though it requires manual parsing. `Console` class provides another alternative for more complex input handling, which can handle passwords securely .

The program for geometric calculations, while straightforward, does not inherently incorporate modularity or code reuse as each calculation (area and perimeter for different shapes) is performed sequentially within the `main` method. Code redundancy exists because similar logic for displaying outputs is repeated. Refactoring into separate methods for each shape's calculations and potentially invoking these methods could improve modularity and reusability of the code, allowing for cleaner and more maintainable programming structures .

The EvenOdd program showcases simplicity by applying basic modulus arithmetic (num % 2 == 0) to determine evenness. The logic is encapsulated in a minimalistic if-else statement responding directly to user input processed through the `Scanner` class. This approach is efficient and directly focuses on solving the problem without additional complex logic or structures. This straightforward design is a perfect use case for demonstrating basic conditionals in programming .

Enhancing user interactivity could involve employing graphical interfaces using JavaFX or Swing for visual feedback and intuitive interactions. Additionally, adding validation for user inputs, error messages for invalid entries, and context-sensitive help can improve user experience. Interactive prompts and additional user options, like repeating operations without restarting, storing previous results, or picking operations dynamically using menus could further refine the program's interactivity and flexibility .

The first program focuses on arithmetic operations using scanner inputs to perform addition, subtraction, multiplication, and division. The second checks the even or odd nature of an integer using a simple modulus operation, employing a conditional statement to determine the result. The third employs formulas to calculate area and perimeter for rectangles, circles, and triangles using user inputs, leveraging mathematics for geometric properties. Each program emphasizes user input handling and output display via the `Scanner` class and `System.out.println` statements .

Potential edge cases in the arithmetic program include division by zero, which will throw an `ArithmeticException`, and input mismatch errors if non-integer input is provided, causing a `InputMismatchException`. Mitigation strategies include using try-catch blocks to handle such exceptions gracefully. Moreover, input validation can be implemented to ensure data type conformity, and conditions to check if the denominator is zero before performing division .

Floating-point operations within geometrical calculations (e.g., area of a circle using π) necessitate precise floating-point arithmetic provided by Java's double-precision `double` type to maintain accuracy in calculations, avoiding typical pitfalls associated with floating-point arithmetic, such as rounding errors. Conversely, basic arithmetic operations in the first program use `int` types as they're aimed at integer arithmetic, bringing simplicity but lacking decimal precision, leading to truncation in division results if not handled .

In the arithmetic operations program, the logic involves directly performing and displaying basic arithmetic calculations (addition, subtraction, multiplication, division) for two input integers. Conversely, the even or odd number program evaluates whether a single integer is divisible by 2 using the modulus operator, then branches with a simple if-else structure to ascertain and print the number's parity .

You might also like