JAVA Experiment No 2 - String Handling and Operators in Java
JAVA Experiment No 2 - String Handling and Operators in Java
if(s1.equals(s2)){
System.out.println("s1 and s2 have same values");
}else
{
System.out.println("s1 and s2 have not same values");
}
String s3=s2;
if(s3==s2){
System.out.println("s3 and s2 has same address");
}
else{
System.out.println("s3 and s2 has not same address");
}
}
}
Aim 1: Write a program to check whether two strings are equal or not.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter 1st string.");
String str1= in.nextLine();
System.out.println("Enter 2nd string.");
String str2= in.nextLine();
if(str1.equals(str2)) {
System.out.println("Strings are equal.");
}
else {
System.out.println("Strings are not equal.");
}
}
}
OUTPUT:
Enter 1st string.
abc
Enter 2nd string.
ABC
Strings are not equal.
AIM 2: Write a Java program that reads a string from the keyboard, and
outputs the string twice in a row, first all uppercase and next all lowercase. If,
for instance, the string “Hello" is given, the output will be “HELLOhello"
class stringorder
{
static String name[]={"Adventure", "Life", "With", “BTech(CSE)”};
public static void main(String args[])
{
int n=name.length;
String temp=null;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(name[j].compareTo(name[i])>0)
{
temp=name[i];
name[i]=name[j];
name[j]=temp;
}
}
}
System.out.println("After Sorting String is: ");
for(int i=0; i<n; i++)
{
System.out.println(name[i]);
}
}
}
/*
Output:
After Sorting String is:
Adventure
BTech(CSE)
Life
With
*/
Aim 4: Write an application that asks the user to enter two integers
obtains them from the user and prints their sum, product, difference and
quotient (division).
// File: NumberCalc1.java
import java.util.Scanner; // include scanner utility for accepting keyboard
input
System.out.printf("Enter First
Number:\t ");
num1=input.nextInt(); // store
next integer in num1
System.out.printf("Enter Second
Number:\t ");
num2=input.nextInt(); // store
next integer in num2
AIM 7: Write a program to find greater number out of two using ternary
operator.
PROGRAM:
import java.util.Scanner;
public class TerOp {
public static void main(String[] args) {
int g;
Scanner in= new Scanner(System.in);
System.out.println("Enter two numbers.");
int x=in.nextInt();
int y=in.nextInt();
System.out.println((x > y) ? x : y + " is greater.");
}
}
OUTPUT:
Enter two numbers.
32
532
532 is greater.
void add()
{
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
System.out.println("Addition of matrix is:");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.print(c[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
void mul()
{
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
{
c[i][j]=0;
for(int k=0;k<2;k++)
c[i][j]+=a[i][k]*b[k][j];
}
System.out.println("Multiplication of matrix is:");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.print(c[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
}
class matrix
{
public static void main(String args[])
{
demo ob=new demo();
ob.add();
ob.mul();
}
}
OUTPUT:
D:\DOCUME~1\GUEST>CD\
D:\>cd jdk1.3\bin
D:\JDK1.3\BIN>javac matrix.java
D:\JDK1.3\BIN>java matrix
Addition of matrix is:
3 9
10 9
Multiplication of matrix is:
17 34
23 46
AIM 10: Write an application that inputs three integers from the user and
displays the sum, average, product, smallest and largest of the numbers.