0% found this document useful (0 votes)
9 views10 pages

Practical PRGM - Unit I

Uploaded by

anonymous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Practical PRGM - Unit I

Uploaded by

anonymous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SATHYABMA INSTITUTE OF SCIENCE AND TECHNOLOGY SCHOOL OF COMPUTING

S12BLH31 PROGRAMMING IN JAVA


Unit – I
Basics of Java
1. A. Implementation of Matrix operation using Arrays
public class MatrixMultiplicationExample{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
//creating another matrix to store the multiplication of two matrices
int c[][]=new int[3][3]; //3 rows and 3 columns

//multiplying and printing multiplication of 2 matrices


for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}//end of k loop
System.out.print(c[i][j]+" "); //printing matrix element
}//end of j loop
System.out.println();//new line
} }}

Compile by: javac MatrixMultiplicationExample.java

Run by: java MatrixMultiplicationExample

Output:
6 6 6
12 12 12
18 18 18

1 b. Program to implement Single dimensional array using String


//Get all your friends names in the string and display the names.

class Sample
{
public static void main(String args[])
{
int i;
String m[ ]={"Anith","Ben","Rajiv","Vijay","Zafa"};
System.out.println("The names are :");
for(i=0;i<5;i++)
System.out.println(m[i]);
}
}
Output:
The names are :
Anith
Ben
Rajiv
Vijay
Zafa

1 c. Program to implement Single-dimensional array for integer values


import java.util.Scanner;
class merge
{
public static void main(String args[])
{
int i,n,m,p,pos;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of array elements arr1");
n=sc.nextInt();
System.out.println("Enter number of array elements arr2");
m=sc.nextInt();
System.out.println("Enter number of elements in merged array arr3");
p=sc.nextInt();
//Creating dimension of three arrays
int arr1[]=new int [n];
int arr2[]=new int [m];
int arr3[]=new int [p];
System.out.println("Enter elements in array arr1");
for(i=0;i<n;i++)
arr1[i]=sc.nextInt();
System.out.println("Enter elements in array aar2");
for(i=0;i<m;i++)
arr2[i]=sc.nextInt();
for(i=0;i<n;i++)
arr3[i]=arr1[i];
for(i=0;i<m;i++)
arr3[n+i]=arr2[i];
System.out.println("Array elements after merging");
for(i=0;i<p;i++)
System.out.println(arr3[i]);
}
}

Output:
Enter number of array elements arr1
3
Enter number of array elements arr2
4
Enter number of elements in merged array arr3
7
Enter elements in array arr1
1
2
3
Enter elements in array aar2
4
5
6
7
Array elements after merging
1
2
3
4
5
6
7

1 d. Program to perform String operations


class Gfg {
// main function
public static void main(String args[])
{
String s = "Welcome! to Sathyabama university Planet";
int i;
char chr;
int b=s.length();
System.out.println(b);
char ch = s.charAt(3);
System.out.println(ch);
ch = s.charAt(0);
System.out.println(ch);
for(i=0;i<b;i++)
{
chr=s.charAt(i);
{
if(chr=='e')
chr='*';
}
System.out.print(chr);//Replace a letter ‘e’ with ‘*’
}
}
}

Output:
40
c
W
W*lcom*! to Sathyabama univ*rsity Plan*t

1 e. //Sample program to illustrate various String Manipulation in Java


class Manipulation2
{
public static void main(String args[]){
StringBuffer k = new StringBuffer("SUBASH CHANDRA BOSE");
String a="understanding";
String b="computer";
String c="prime number";
String d1="COMPUTER FEST";
String d2="computer fest";
System.out.println("The substring from 5th position = "+a.substring(5));
System.out.println("The substring from 5th index and excluding 10th index="+a.substring(5,10));
System.out.println("The first position of the character m ="+b.indexOf('m'));
System.out.println("The position of 'm' from 5th character = "+c.indexOf('m' ,5));
System.out.println("The compare string = "+ d1.compareTo(d2));
System.out.println("The comparing strings after ignoring case ="+ d1.compareToIgnoreCase(d2));
System.out.println("The comparison using equals ignore case ="+ d1.equalsIgnoreCase(d2));
System.out.println("The reversed string= "+k.reverse());
}
}

Output:
The substring from 5th position = standing
The substring from 5th index and excluding 10th index=stand
The first position of the character m =2
The position of 'm' from 5th character = 8
The compare string = -32
The comparing strings after ignoring case= 0
The comparison using equals ignore case= true
The reversed string= ESOB ARDNAHC HSABUS

1 f. Program to implement Looping Control Statements


Write a program in Java to enter a three digit number and check whether the number is an
Armstrong number or not. (A three digit number is said to be Armstrong if the sum of the cubes of
its digits is equal to the original number)

Sample Input : 153


Sample Output : 153 is an Armstrong Number because 13 + 53 +33 = 153

class Armstrong
{
public static void main(String arg[])
{
int a,num,s=0,n=371;
num=n;
while(n>0)
{
a=n%10;
s=s+a*a*a;
n=n/10;
}
if(num==s)
System.out.println("The number" + num +"is an Armstrong Number");
else
System.out.println("The number" + num +"is not an Armstrong Number");
}
}
Output:
The Number 371 is an Armstrong number

1h. Program to implement Conditional control Statements


Write a program to input three different single digit numbers between 1 and 9(both inclusive).
Display the greatest and smallest three digit number.
class Number {
public static void main(String arg[]){
int a=3,b=5,c=7,gn=0,sn=0;
if((a>b)&&(a>c))
{
if(b>c)
{
gn=100*a+10*b+c;
sn=100*c+10*b+a;
}
else
{
gn=100*a+10*b+c;
sn=100*b+10*c+a;
}
}
if((b>a)&&(b>c))
{
if(a>c)
{
gn=100*b+10*a+c;
sn=100*c+10*a+b;
}
else
{
gn=100*b+10*c+a;
sn=100*a+10*c+b;
}
}
if((c>a)&&(c>b))
{
if(a>b)
{
gn=100*c+10*a+b;
sn=100*b+10*a+c;
}
else
{
gn=100*c+10*b+a;
sn=100*a+10*b+c;
}
}
System.out.println("The greatest number " + gn);
System.out.println("The smallest number " + sn);
}
}
Output:
The greatest number 753
The smallest number 357

1 I. Write a menu driven program to find the area of an Equilateral triangle.


an Isosceles triangle and a Scalene triangle as per the user's choice.

l. Equilateral triangle = A = √3a2/4 s=side of an equilateral triangle

2. Isosceles triangle = A = 1/4b*√4a2-b2

3. Scalene triangle = A = √s(s-m)(s-n)(s-p) , s = (a+b+c)/2


(where m. n and p are three sides of a scalene triangle)

import java.util.*;
class Menu
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int c,s,a,b,m=0,n=0,p=0;
double k, area=0;
System.out.println("l. Area of an Equilateral Triangle");
System.out.println("2. Area of an Isosceles Triangle");
System.out.println("3. Area of a Scalene Triangle");
System.out.println("Enter your choice");
c=in.nextInt();
switch(c)
{
case 1:
System.out.println("Enter side of an Equilateral triangle");
s=in.nextInt();
area= (Math.sqrt(3)*s*s)/4.0;
System.out.println(" Area =" +area);
break;
case 2:
System.out.println("Enter side and base of Isosceles Triangle");
a=in.nextInt();
b=in.nextInt();
area=b*(Math.sqrt(4*a*a-b*b))/4.0;
System.out.println(" Area =" +area);
break;
case 3:
System.out.println("Enter side of Scalene Triangle");
m=in.nextInt();
n=in.nextInt();
k=(m+n+p)/2.0;
area=Math.sqrt(k*(k-m)*(k-n)*(k-p));
System.out.println(" Area =" +area);
break;
default:
System.out.println("Wrong Choice!!");
}
}
}

Output:
l. Area of an Equilateral Triangle
2. Area of an Isosceles Triangle
3. Area of a Scalene Triangle

Enter your Choice


1

Enter side of an Equilateral triangle


Area =62.353829072479584

You might also like