Professional Documents
Culture Documents
COLLEGE
NAME-A.Vijay Kalyan
ROLL-10201615054
Object oriented programming lab
assignment
ASSIGNMENT-2
10. Write a program to sort any list of given
numbers.
import java.io.*;
import java.util.*;
int n = br.nextInt();
for(int i=0;i<n;i++)
arr[i] = br.nextInt();
for(int i=0;i<n;i++)
for(int j=0;j<n-1;j++)
if(arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}System.out.println("\nsorted array");
System.out.println("\n");
for(int i=0;i<n;i++)
System.out.println(arr[i]);
OUTPUT:
53
22
sorted array
22
53
Answer: 32.
Answer: e.
14. Show two ways to concatenate the following two
strings together to get the
string “Hi, Mom”:
String hi=”Hi”;
String mom=”mom”;
hi.concat(mom) and hi + mom.
15. Write a program that implements the following two
string methods:
a. equals() and
b. equalsIgnoreCase()
Example 1: equals()
public class EqualsExample1{
public static void main(String args[]){
String str1= new String("Hello");
String str2= new String("Hi");
String str3= new String("Hello");
System.out.println("str1 equals to
str2:"+str1.equals(str2));
System.out.println("str1 equals to
str3:"+str1.equals(str3));
System.out.println("str1 equals to
Welcome:"+str1.equals("Welcome"));
System.out.println("str1 equals to
Hello:"+str1.equals("Hello"));
System.out.println("str1 equals to
hello:"+str1.equals("hello"));
}
}
Output:
str1 equals to str2:false
str1 equals to str3:true
str1 equals to Welcome:false
str1 equals to Hello:true
str1 equals to hello:false
Example2: equalsIgnoreCase()
public class EqualsExample2{
public static void main(String args[]){
String str1= new String("Apple");
String str2= new String("MANGO");
String str3= new String("APPLE");
System.out.println("str1 equals to
str2:"+str1.equalsIgnoreCase(str2));
System.out.println("str1 equals to
str3:"+str1.equalsIgnoreCase(str3));
System.out.println("str1 equals to
Welcome:"+str1.equalsIgnoreCase("Welcome"));
System.out.println("str1 equals to
Apple:"+str1.equalsIgnoreCase("Apple"));
System.out.println("str2 equals to
mango:"+str2.equalsIgnoreCase("mango"));
}
}
Output:
str1 equals to str2:false
str1 equals to str3:true
str1 equals to Welcome:false
str1 equals to Apple:true
str2 equals to mango:true
16. Given a string, return a new string where the last 3
chars are now in upper
case. If the string has less than 3 chars, uppercase
whatever is there.
public String endUp(String str) {
if(str.length() < 3)
return str.toUpperCase();
return str.substring(0, str.length() - 3) +
str.substring(str.length() - 3).toUpperCase();
}
17. Write a java program, which will read a string and
rewrite it in the
alphabetical order. For example, the word STRING
should be written as
GINRST.
import java.io.*;
import java.util.*;
class Alphabatic
{
String alphaOrder(String str)
{
char[] charArray=str.toCharArray();
Arrays.sort(charArray);
String aString=new String(charArray);
return aString;
}
public static void main(String[] args)throws
IOException
{
System.out.println("Enter the String->");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String inputString=br.readLine();
Alphabatic obj=new Alphabatic();
String alphaString=obj.alphaOrder(inputString);
System.out.println("String in the Alphabetic Order :"
+alphaString);
}
}
OUTPUT-
String-Ginrst
18. Write a java program that implements String-
Buffer class concept.
import java.io.*;
class GFG
{
public static void main (String[] args)
{
StringBuffer s=new StringBuffer("GeeksforGeeks");
int p=s.length();
int q=s.capacity();
System.out.println("Length of string
GeeksforGeeks="+p);
System.out.println("Capacity of string
GeeksforGeeks="+q);
}
}
Run on IDE
Output:
int a = 20;
int b = 0;
// java.lang.ArithmeticException: / by
zero
// Occurs here
int div = a / b;
}
Copy
Output:
java.lang.ArithmeticException: / by zero
21. Using the concept of throws and thow statement
implement the divide by zero exception
USING THROW-
public class ArrayE
{
private int what;
try{
System.out.println("value in what = "+b.value());
}catch(DivideByZeroException
e){System.out.println("caught the little tinker"+e);}
}
}
22. Write a java program where one function will
throw an user defined
Exception (suppose no integer value should not be
greater than 1000) to a
function from where it was called and there it will
be catch.
class InvalidProductException extends Exception
{
public InvalidProductException(String s)
{
// Call constructor of parent Exception
super(s);
}
}
if(s.equals("India"))
{
throw new NoMatchException();
}
}
catch(NoMatchException nme){
System.out.println("No Match Exception Caught: "
+nme);
}
}
}