[go: up one dir, main page]

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

String Operations

The document discusses three Java programs: 1) A StringOperations program that takes user input for two strings and performs operations like length, concatenation, uppercase/lowercase conversion. 2) An array program that takes user input to populate an array, then sorts it in ascending and descending order. 3) A Rectangle class program that defines a rectangle with width, length, and color and includes methods to set/get these values and calculate area. It takes user input to create two rectangles and checks if they match.
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)
59 views6 pages

String Operations

The document discusses three Java programs: 1) A StringOperations program that takes user input for two strings and performs operations like length, concatenation, uppercase/lowercase conversion. 2) An array program that takes user input to populate an array, then sorts it in ascending and descending order. 3) A Rectangle class program that defines a rectangle with width, length, and color and includes methods to set/get these values and calculate area. It takes user input to create two rectangles and checks if they match.
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

STRING OPERATIONS

package stringoperations;

import java.util.Scanner;

public class STRINGOPERATIONS {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

String first = "", second = "";

Scanner sc = new Scanner(System.in);

System.out.println("String Operation");

System.out.println();

System.out.print("Enter the first Sting: ");

first = sc.nextLine();

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

second = sc.nextLine();

System.out.println("The strings are: " + first + " , " + second);

System.out.println("The length of the first string is :" + first.length());

System.out.println("The length of the second string is :" + second.length());

System.out.println("The concatenation of first and second string is :" + first.concat(" " + second));

System.out.println("The first character of " + first + " is: " + first.charAt(0));

System.out.println("The uppercase of " + first + " is: " + first.toUpperCase());

System.out.println("The lowercase of " + first + " is: " + first.toLowerCase());


if (first.length() != second.length()) {

System.out.println(first + " and " + second + " are not same.");

} else {

System.out.println(first + " and " + second + " are same.");

ASCENDING AND DESCENDING ORDER

package q2ass;

import java.util.Scanner;

public class Q2ass {

public static void main(String[] args) {


//Create Object for Scanner class to get input from keyword
Scanner scn = new Scanner(System.in);

System.out.print("Enter the size of an array: ");


//Set array size
int size = scn.nextInt(), temp;

//Get input from keyword


System.out.print("Enter array values\n");
int[] arr = new int[size];
//Get all input from keyword
for(int i =0; i<size;i++){
arr[i]=scn.nextInt();
}

//Validate the element for ascending sort order


for(int m=0;m<arr.length;m++){
for(int n=m+1;n<arr.length;n++){
if(arr[m]>arr[n]){
temp = arr[m];
arr[m]=arr[n];
arr[n]=temp;
}
}
}

//Display the ascending order of given elememts


System.out.print("ascending order are: \n");
for(int t: arr){
System.out.println(t);
}
//Validate the element for descending sort order
for(int m=0;m<arr.length;m++){
for(int n=m+1;n<arr.length;n++){
if(arr[m]<arr[n]){
temp = arr[m];
arr[m]=arr[n];
arr[n]=temp;
}
}
}

//Display the descending order


System.out.print("descending order are: \n");
for(int t: arr){
System.out.println(t);
}
}
}

RECTANGLE

import java.io.*;

public class rectangle{


int width,length;
String color;
void get_length(int a)
{
length=a;
}
void get_width(int a)
{
width=a;
}
String get_color(String a)
{
color=a;
return(color);
}
int find_area()
{
return(width*length);
}
String getcolor()
{
return(color);
}

public static void main(String arg[])throws Exception


{
String s=null;
DataInputStream in=new DataInputStream(System.in);
rectangle a=new rectangle();
System.out.println("Enter the length for first rectangle");
s=in.readLine();
a.get_length(Integer.parseInt(s));
System.out.println("Enter the width for first rectangle");
s=in.readLine();
a.get_width(Integer.parseInt(s));
System.out.println("Enter the Color for first rectangle");
a.get_color(in.readLine());
rectangle b=new rectangle();
System.out.println("Enter the length for second rectangle");
s=in.readLine();
b.get_length(Integer.parseInt(s));
System.out.println("Enter the width for second rectangle");
s=in.readLine();
b.get_width(Integer.parseInt(s));
System.out.println("Enter the Color for second rectangle");
b.get_color(in.readLine());
if(a.find_area()==b.find_area() && a.getcolor().equals(b.getcolor()))
System.out.println("Matching Rectangle ");
else
System.out.println("Non Matching Rectangle ");
}
}

You might also like