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

Java Coding Experiments Overview

Uploaded by

pochinkisu075
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)
3 views3 pages

Java Coding Experiments Overview

Uploaded by

pochinkisu075
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

Experiment 1

public class Demo {


public static void main(String[] args) {
[Link]("Hello, Java World from Eclipse!");
[Link]("This is on line one.\nThis is on line two.");
[Link]("Column1\tColumn2\tColumn3");
[Link]("This is a backslash:\\");
[Link]("She said, \"Java is awesome!\"");
[Link]("It\'s a great day to code in Java.");
[Link]("Start of line\rReplaced");
[Link]("Utsav verma");
[Link]("2301640130136");
}
}

OUTPUT:
Hello, Java World from Eclipse!
This is on line one.
This is on line two.
Column1Column2Column3
This is a backslash:She said, "Java is awesome!"
It's a great day to code in Java.
Replaced
Utsav verma
2301640130136
Experiment 2

public class SumArguments {


public static void main(String[] args) {
if([Link] < 2) {
[Link]("Please provide two integer arguments.");
return;
}
int num1 = [Link](args[0]);
int num2 = [Link](args[1]);
int sum = num1 + num2;
[Link]("The sum of " + num1 + " and " + num2 + " is: " + sum);
[Link]("Utsav verma");
[Link]("2301640130136");
}
}

OUTPUT:
The sum of 10 and 20 is: 30
Utsav verma
2301640130136
Experiment 3

class Student {
String name;
int rollNo;
void displayStudentDetails() {
[Link]("Student Name: " + name);
[Link]("Roll Number: " + rollNo);
}
}

class Rectangle {
double length;
double width;
void displayArea() {
double area = length * width;
[Link]("Rectangle Area: " + area);
}
}

public class MainProgram {


public static void main(String[] args) {
Student student = new Student();
[Link] = "Utsav verma";
[Link] = 2301640130136;
[Link]();

Rectangle rectangle = new Rectangle();


[Link] = 10.5;
[Link] = 4.2;
[Link]();
}
}

OUTPUT:
Student Name: Utsav verma
Roll Number: 2301640130136
Rectangle Area: 44.1

Common questions

Powered by AI

Experiment 2 takes command-line arguments, requires at least two, and attempts to convert them into integers using Integer.parseInt(). If fewer than two arguments are provided, it outputs an error message and stops execution. If the arguments cannot be converted to integers, a NumberFormatException could occur, for which no explicit handling is present in the program .

The program will output the message 'Please provide two integer arguments.' because the code checks if the number of arguments provided is less than two and, if so, prints this message and terminates the program .

Using double literals for the length and width variables in the Rectangle class ensures precise representation of floating-point numbers, which is crucial in performing accurate calculations of the area. Calculating area as a product of these doubles enhances precision over using integers, preventing potentially significant rounding errors .

To extend the functionality to calculate and display the perimeter of the rectangle, I would add a method 'displayPerimeter()' in the Rectangle class. This method would compute the perimeter using the formula `(2 * (length + width))` and print it. This approach adheres to object-oriented principles by encapsulating rectangle-related functionality within the Rectangle class, maintaining a clear separation of concerns and allowing for cohesive and modular design .

'System.out.println' is used to output data to the console, automatically appending a newline character at the end. This ensures that each call to 'System.out.println' starts on a new line, which improves the readability of the program's output .

If data flow in the Student class methods is incorrectly managed, it can result in inaccuracies in output and compromise data integrity. For example, if attributes 'name' and 'rollNo' are not correctly set or accessed before displayStudentDetails() is called, it might display incorrect or default values, leading to misleading or erroneous information .

Experiment 3 uses two classes, Student and Rectangle, with attributes and methods to demonstrate encapsulation. The attributes (name, rollNo, length, width) are encapsulated within their respective classes and are accessed or modified through methods, such as displayStudentDetails() and displayArea(). This structure embodies the principle of encapsulation by hiding the internal state and requiring all interactions to be performed through well-defined interfaces .

The program uses several escape sequences: '\n' for new line, '\t' for tab, '\\' for backslash, '\"' for double quotes, and '\'' for single quotes. These escape sequences allow for formatting strings and including special characters that can't be easily inserted directly into strings without causing syntax errors .

If a larger data type is needed for the roll number in Experiment 3, the 'long' data type should be considered. This is because 'long' can store values up to 9,223,372,036,854,775,807, which is considerably larger than the maximum value 'int' can store (2,147,483,647).

Experiment 3 shows a clear separation of concerns as it defines distinct classes for different functionalities: Student and Rectangle. This separation allows each class to handle specific attributes and methods relevant to their domain. Such a design improves maintainability as changes in one class (e.g., additional student methods) have minimal impact on others (e.g., rectangle calculations), making the code easier to update and extend .

You might also like