You are on page 1of 25

KALYANI GOVERNMENT ENGINEERING

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.*;

public class a13 {

public static void main(String args[])throws IOException

Scanner br = new Scanner (System.in);

System.out.println("Enter the no. of elements");

int n = br.nextInt();

int arr[]=new int[n];

System.out.println("Enter the elements");

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:

Enter the no. of elements

Enter the elements

53

22

sorted array

22

53

11. Write a program for matrix multiplication


import java.util.Scanner;
public class MatixMultiplication {

public static void main(String[] args) {


int rowM1, colM1;
int rowM2, colM2;

Scanner in = new Scanner(System.in);


System.out.print("Enter Number of Rows and
Columns of First Matrix : ");
rowM1 = in.nextInt();
colM1 = in.nextInt();

System.out.print("Enter elements of First Matrix :


");
int M1[][] = new int[rowM1][colM1];
for(int i = 0; i < rowM1; i++){
for(int j = 0; j < colM1; j++){
M1[i][j] = in.nextInt();
}
}
System.out.println("First Matrix : " );
for(int i = 0; i < rowM1; i++){
for(int j = 0; j < colM1; j++){
System.out.print(" " +M1[i][j]+"\t");
}
System.out.println();
}

System.out.print("Enter Number of Rows and


Columns of Second Matrix : ");
rowM2 = in.nextInt();
colM2 = in.nextInt();
if(colM1 != rowM2){
throw new IllegalArgumentException("The
number of columns of the first matrix should equal the
number of rows of the second matrix.");
}
System.out.print("Enter elements of Second
Matrix : ");
int M2[][] = new int[rowM2][colM2];
for(int i = 0; i < rowM2; i++){
for(int j = 0; j < colM2; j++){
M2[i][j] = in.nextInt();
}
}
System.out.println("Second Matrix : " );
for(int i = 0; i < rowM2; i++){
for(int j = 0; j < colM2; j++){
System.out.print(" " +M2[i][j] + "\t");
}
System.out.println();
}
//same number of rows as in the first matrix and
//same number of columns as in the second matrix
int resMatrix[][] = new int[rowM1][colM2];
int sum = 0;
int row = 0;
for(int i = 0; i < rowM1; i++){
for(int j = 0; j < colM2; j++){
sum = 0;
for(int k = 0; k < colM1; k++){
sum = sum + M1[i][k] * M2[k][j];
}
resMatrix[i][j] = sum;
}
}

System.out.println("Result Matrix : " );


for(int i = 0; i < resMatrix.length; i++){
for(int j = 0; j < colM2; j++){
System.out.print(" " +resMatrix[i][j]+"\t");
}
System.out.println();
}
}
}
Output

Enter Number of Rows and Columns of First Matrix : 2


3
Enter elements of First Matrix : 1
2
3
4
5
6
First Matrix :
1 2 3
4 5 6
Enter Number of Rows and Columns of Second Matrix :
3
2
Enter elements of Second Matrix : 7
8
9
10
11
12
Second Matrix :
7 8
9 10
11 12
Result Matrix :
58 64
139 154
13. Consider the following string:
String hannah=”Did Hannah see bees?Hannah
did.”;
(a) What is the value displayed by the expression
hannah.length()?
(b) What is the value returned by the method call
hannah.charAt(12)?
Question: What is the value displayed by the
expression hannah.length()?

Answer: 32.

Question: What is the value returned by the method


call hannah.charAt(12)?

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:

Length of string GeeksforGeeks=13


Capacity of string GeeksforGeeks=29
19. Write a java program using try/catch to generate
Arithmetic Exception to
catch division-by-zero.
class Excep
{
public static void main(String[] args)
{
try
{
int arr[]={1,2};
arr[2]=3/0;
}
catch(ArithmeticException ae)
{
System.out.println("divide by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("array index out of bound
exception");
}
}
}
OUTPUT-
divide by zero
20. Write a program to handle “divide by zero
exception” with the following
message in finally block “Exception occurred” if the
quotient part is -1 otherwise print the result in finally
block.
public class Exceptions {

public static void main(String[] args) {


try {

int a = 20;
int b = 0;
// java.lang.ArithmeticException: / by
zero
// Occurs here
int div = a / b;

System.out.println("Div= " + div);

} catch (ArithmeticException ae) {


// catch arithmetic exception
System.out.println(ae);
}

}
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;

public ArrayE(int zero)


{
this.zero=zero;
}

public int value() throws ArithmeticException


{
int take=5/this.zero;
return take;
}

public static void main(String[] args)


{
ArrayE b=new ArrayE(0);

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);
}
}

public class Example1


{
void productCheck(int weight) throws
InvalidProductException{
if(weight<100){
throw new InvalidProductException("Product
Invalid");
}
}

public static void main(String args[])


{
Example1 obj = new Example1();
try
{
obj.productCheck(60);
}
catch (InvalidProductException ex)
{
System.out.println("Caught the exception");
System.out.println(ex.getMessage());
}
}
}
Output:

Caught the exception


Product Invalid
23. Define an Exception called “NoMatchException”
that is thrown when a string
is not equal to “India”. Write a java program that
uses this Exception.
import java.io.DataInputStream;
import java.io.IOException;

public class NoMatchException extends Exception {

public static void main(String args[]){


String s = new String();
DataInputStream in = new
DataInputStream(System.in);
try{
System.out.println("Enter the String:");
s = in.readLine();
}
catch(IOException nme)
{}
try{

if(s.equals("India"))
{
throw new NoMatchException();
}
}
catch(NoMatchException nme){
System.out.println("No Match Exception Caught: "
+nme);
}
}
}

You might also like