Experiment 02 PS EDITED
Experiment 02 PS EDITED
Learning Objectives: To understand the method and usage of arithmetic operator in program.
Theory:
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);
}
}
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);
// 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);
scanner.close();
}
}
Output:
Problem Statement 3: Write a Java program to understand various string operations (use
strln, strcpy, strcat, strtrim functions).
public class string {
// Substring operation
String substring = str.substring(7, 12); // Extracting "World"
System.out.println("Substring: " + substring);
// 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:
Course Outcome:
CO1: Understand about Various operators and String.
Viva Questions:
What are the primitive and non-primitive data types?
What are Ternary operators?
What are strings?