0% found this document useful (0 votes)
79 views15 pages

JAVA Experiment No 2 - String Handling and Operators in Java

The document contains 9 coding aims related to Java strings, operators, and math functions. The aims cover topics like string comparison, uppercase/lowercase conversion, sorting strings, number calculations, remainder operator, increment/decrement operators, ternary operator, matrix addition/multiplication, and calculating powers. For each aim, the document provides the full code for a program to demonstrate the concept, and includes sample input/output where applicable.

Uploaded by

Akanksha Thakur
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
79 views15 pages

JAVA Experiment No 2 - String Handling and Operators in Java

The document contains 9 coding aims related to Java strings, operators, and math functions. The aims cover topics like string comparison, uppercase/lowercase conversion, sorting strings, number calculations, remainder operator, increment/decrement operators, ternary operator, matrix addition/multiplication, and calculating powers. For each aim, the document provides the full code for a program to demonstrate the concept, and includes sample input/output where applicable.

Uploaded by

Akanksha Thakur
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 15

Experiment no 2:

Java Strings and Operators

// A demo Java String program


class StringDemo{
public static void main(String args[]){
String s1="Hello";
String s2=new String("Hello");
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
if(s1==s2){
System.out.println("s1 and s2 has same address");
}
else{
System.out.println("s1 and s2 has not same address");
}

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"

import java.util.Scanner; // include scanner utility for accepting input

public class Question2


{ // begin class
public static void main(String[] args)
{// begin the main method
Scanner input=new Scanner(System.in); //create a new Scanner
object to use
String str; //declaring a string variable str
System.out.printf("Enter String: ");
str=input.nextLine();// store next line in str
// display the same string in both uppercase and lowercase
System.out.printf("%s%s",str.toUpperCase(),str.toLowerCase());
}
}

AIM 3: Program to manipulate sorting of strings in ascending order.

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

public class NumberCalc1 { // begin class

public static void main(String[] args) { // begin the main method


Scanner input=new Scanner (System.in); //create a new Scanner
object to use
int num1=0, num2=0; // initialize variables
System.out.printf("NUMBER CALCULATIONS\n\n");

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

// display the sum, product, difference and quotient of the two


numbers
System.out.printf("-----------------------------------\n");
System.out.printf("\tSum =\t\t %d\n",
num1+num2); System.out.printf("\
tProduct =\t %d\n", num1*num2);
System.out.printf("\tDifference =\t %d\n",
num1-num2); System.out.printf("\
tQuotient =\t %d\n", num1/num2);
}
}

Aim 5: Write an application that reads two integers, determines whether


the first is a multiple of the second and print the result. [Hint Use the
remainder operator.]

import java.util.Scanner; // include scanner utility for accepting keyboard


input

public class Multiple2 { // begin class


public static void main(String[] args) { // begin the main method
Scanner input=new Scanner (System.in); //create a new Scanner
object to use
int num1=0, num2=0, k;// initialize variables

System.out.printf("Enter First number: ");


num1=input.nextInt(); // store next integer in
num1

System.out.printf("Enter Second Number: ");


num2=input.nextInt(); // store next integer in num2

k=num2%num1; // assign the remainder ofnum2 divided by num1 to


the integer k

if (k==0){ // check if k is 0. remainder k will be 0 if num1 is a


multiple of num2,
System.out.printf("%d is a multiple of %d", num1, num2);
}
else {
System.out.printf("%d is not a multiple of %d", num1, num2);
}
}
}

AIM 6: Write a program to display numbers using increment and decrement


operators.
PROGRAM:
import java.util.Scanner;
public class IncDec {
static void inc (int n) {
System.out.println("Using i++");
int i=0;
while(i < n) {
System.out.print(i++ + " ");
}
i=0;
System.out.println("\nUsing ++i");
while(i < n) {
System.out.print(++i + " ");
}
}
static void dec (int n) {
System.out.println("Using i--");
int i = n;
while(i > 0) {
System.out.print(i-- + " ");
}
System.out.println("\nUsing --i");
i = n;
while (i > 0) {
System.out.print(--i + " ");
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter limit.");
int n = in.nextInt();
System.out.println("Increment:");
inc(n);
System.out.println("\nDecrement:");
dec(n);
}
}
OUTPUT:
Enter limit.
10
Increment:
Using i++
0123456789
Using ++i
1 2 3 4 5 6 7 8 9 10
Decrement:
Using i--
10 9 8 7 6 5 4 3 2 1
Using --i
9876543210

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.

AIM 8: Program for addition and multiplication of matrices /


class demo
{
int a[][]={{1,5},{7,3}};
int b[][]={{2,4},{3,6}};
int c[][]=new int[2][2];

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:

Microsoft(R) Windows DOS


(C)Copyright Microsoft Corp 1990-2001.

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 9: Write a program to calculate power of a number.


import java.util.Scanner;
public class Expo {
public static void main(String[] args) {
System.out.println("Enter the base.");
Scanner in = new Scanner(System.in);
int base = in.nextInt();
System.out.println("Enter the index.");
int index = in.nextInt();
long result = 1;
int i = index;
while (i != 0) {
result *= base;
--i;
}
System.out.println(base + "^" + index + " = " + result);
}
}
OUTPUT:
Enter the base.
6
Enter the index.
5
6^6 = 7776

AIM 10: Write an application that inputs three integers from the user and
displays the sum, average, product, smallest and largest of the numbers.

import java.util.Scanner; // include scanner utility for accepting keyboard input

public class Question2


{
public static void main(String[] args)
{
Scanner input=new Scanner (System.in);
int num1=0, num2=0, num3, bigger=0, smaller=0;
System.out.printf("NUMBER CALCULATIONS\n\n");
System.out.printf("Enter First Number:\t\t ");
num1=input.nextInt(); // store next integer in num1
System.out.printf("Enter Second Number:\t ");
num2=input.nextInt(); // store next integer in num2
System.out.printf("Enter Third Number:\t ");
num3=input.nextInt(); // store next integer in num3
; // checks the biggest number in and assigns it to bigger variable
bigger=num1>num2?num1:num2
bigger=bigger>num3?bigger:num3;
// checks the smallest number in and assigns it to smaller variable
smaller=num1<num2?num1:num2;
smaller=smaller<num3?smaller:num3;
// display the sum, average, product, smallest and the biggest of all three numbers
System.out.printf("\t-------------------------\n");
System.out.printf("\t\t\tSum =\t\t %d\n", num1+num2+num3);
System.out.printf("\t\t\tAverage =\t %d\n", (num1+num2+num3)/3);
System.out.printf("\t\t\tProduct =\t %d\n", num1*num2*num3);
System.out.printf("\t\t\tBiggest =\t %d\n", bigger);
System.out.printf("\t\t\tSmallest =\t %d\n", smaller);
}
}

You might also like