0% found this document useful (0 votes)
6 views6 pages

Experiment 02 PS EDITED

Uploaded by

miteshpurohit980
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Experiment 02 PS EDITED

Uploaded by

miteshpurohit980
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment 02(a) – Arithmetic Operators

Learning Objectives: To understand the method and usage of arithmetic operator in program.

Theory:

Operators in Java and its Types:


1. Arithmetic Operators
2. Assignment Operators
3. Logical Operators
4. Relational Operators
5. Ternary Operators

Arithmetic Operators in Java:


Arithmetic Operators are used to performing mathematical operations like addition, subtraction,
etc. Assume that A = 10 and B = 20 for the below table.

Experiment 02(b)– Logical Operator


Logical Operators in Java:
The following are the Logical operators present in Java:
Experiment 02(c)– String

Strings are used for storing text.

A String variable contains a collection of characters surrounded by double quotes:

Example:
Create a variable of type String and assign it a value:
public class Main
{
public static void main(String[] args)
{
String greeting = "Hello";
System.out.println(greeting);
}
}

Problem Statement 1: Write a Java program to understand the concept of arithmetic


operators (Simple Interest Program).
import java.util.Scanner;

public class si {
public static void main(String[] args) {
// Create a Scanner object to take input from the user
Scanner scanner = new Scanner(System.in);

// Input the principal amount


System.out.print("Enter the principal amount: ");
double principal = scanner.nextDouble();

// Input the rate of interest


System.out.print("Enter the rate of interest: ");
double rate = scanner.nextDouble();

// Input the time in years


System.out.print("Enter the time in years: ");
double time = scanner.nextDouble();

// Calculate Simple Interest


double simpleInterest = (principal * rate * time) / 100;

// Display the result


System.out.println("Simple Interest: " + simpleInterest);

// Close the scanner to avoid resource leakage


scanner.close();
}
}
Output:
Problem Statement 2: Write a Java program to understand the concept of logical operators.
(Input two numbers and show Logical AND, OR and NOT operation).
import java.util.Scanner;

public class logicaloperators {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Input two numbers


System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();

System.out.print("Enter the second number: ");


int num2 = scanner.nextInt();

// Logical AND
boolean logicalAndResult = (num1 > 0) && (num2 > 0);
System.out.println("Logical AND Result: " + logicalAndResult);

// Logical OR
boolean logicalOrResult = (num1 > 0) || (num2 > 0);
System.out.println("Logical OR Result: " + logicalOrResult);

// Logical NOT for num1


boolean logicalNotResultNum1 = !(num1 > 0);
System.out.println("Logical NOT Result for num1: " + logicalNotResultNum1);

// Logical NOT for num2


boolean logicalNotResultNum2 = !(num2 > 0);
System.out.println("Logical NOT Result for num2: " + logicalNotResultNum2);

scanner.close();
}
}
Output:
Problem Statement 3: Write a Java program to understand various string operations (use
strln, strcpy, strcat, strtrim functions).
public class string {

public static void main(String[] args) {


// Initializing a sample string
String str = " Hello, World! ";

// Length of the string


int length = str.length();
System.out.println("Length of the string: " + length);

// Accessing characters at specific positions


char firstChar = str.charAt(0);
char lastChar = str.charAt(length - 1);
System.out.println("First character: " + firstChar);
System.out.println("Last character: " + lastChar);

// Substring operation
String substring = str.substring(7, 12); // Extracting "World"
System.out.println("Substring: " + substring);

// Removing leading and trailing whitespaces


String trimmedString = str.trim();
System.out.println("Original string: \"" + str + "\"");
System.out.println("Trimmed string: \"" + trimmedString + "\"");

// String comparison
String anotherString = "Hello, World!";
boolean isEqual = str.equals(anotherString);
System.out.println("String comparison: " + isEqual);

// Case-insensitive comparison
boolean isIgnoreCaseEqual = str.equalsIgnoreCase(anotherString);
System.out.println("Case-insensitive comparison: " + isIgnoreCaseEqual);

// Lexicographical comparison
int compareResult = str.compareTo(anotherString);
System.out.println("Lexicographical comparison result: " + compareResult);
}
}

Output:

Learning Outcomes: Students will able to


LO1.1 Understand the primitive and non-primitive data types.
LO1.2 Understands the different operators.
LO1.3 Understands about Reference Object.

Course Outcome:
CO1: Understand about Various operators and String.

Conclusion:We are able to do I/O ,Operators and String functions.

Viva Questions:
What are the primitive and non-primitive data types?
What are Ternary operators?
What are strings?

For Faculty Use only:

Correction Formative Timely completion Attendance / Total


Parameters Assessment of Practical [ 40%] Learning
[40%] Attitude [20%]
Marks
Obtained

You might also like