0% found this document useful (0 votes)
261 views

MCS024 Session 1 and 2

The document describes 4 exercises in a Java programming lab session. 1) The first exercise involves writing a program to calculate the area of a rectangle given user input for length and width. 2) The second exercise involves writing a program to evaluate 4 mathematical expressions. 3) The third exercise describes using break and continue statements in a for loop. 4) The fourth exercise involves writing a program to calculate the average of marks obtained in class 10+2 by taking user input for 6 subjects.

Uploaded by

nisha02t
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
261 views

MCS024 Session 1 and 2

The document describes 4 exercises in a Java programming lab session. 1) The first exercise involves writing a program to calculate the area of a rectangle given user input for length and width. 2) The second exercise involves writing a program to evaluate 4 mathematical expressions. 3) The third exercise describes using break and continue statements in a for loop. 4) The fourth exercise involves writing a program to calculate the average of marks obtained in class 10+2 by taking user input for 6 subjects.

Uploaded by

nisha02t
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

Session 4: JAVA PROGRAMMING LAB.

Session 1: Data types, variables and operators.

Exercise1: Write a program in Java to implement the formula (Area= Height*Width) to find the area of
a rectangle, where height and width are the rectangle’s height and width.

import java.io.*;

class S1e1

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

//variable declaration

int length,width,area;

String s = new String();

//take user input

DataInputStream d = new DataInputStream(System.in);

//Read length of rectangle

System.out.println("Enter length of the rectangle: ");

s=d.readLine();

length = Integer.parseInt(s);

//Read width of rectangle

System.out.println("Enter width of the rectangle: ");

s=d.readLine();

width = Integer.parseInt(s);

area = length * width;

System.out.println("Length of rectangle = "+length+"\n" );

System.out.println("Width of rectangle = "+width+"\n" );

System.out.println("Area of rectangle = "+area+"\n" );


}

Exercise2: Write a program in Java to find the result of following expression (Assume a=10, b=5)

i. (a<<2) + (b>>2)
ii. (a_bol) || (b>0)
iii. (a + b * 100) / 10
iv. a&b

import java.io.*;
class S1e2
{
public static void main(String args[]) throws IOException
{
//variable declaration
int a=10,b=5;
int res1,res3,res4;
boolean a_bol,res2;

//expression 1
res1 = (a<<2) + (b>>2) ;
System.out.println("\n1. (a<<2) + (b>>2) = "+res1);

//expression 2
if(a>0)
a_bol = true;
else
a_bol = false;

res2 = (a_bol) || (b>0);


System.out.println("\n2. (a_bol) || (b>0) = "+res2);

//expression 3
res3 = (a + b * 100) / 10 ;
System.out.println("\n3. ((a + b * 100) / 10) = "+res3);

//expression 4
res4 = a & b;
System.out.println("\n4. a & b = "+res4);

}
}

Exercise3: Write a program in Java to explain the use of break and continue statements.

//use of break and continue

import java.io.*;

class S1e3

public static void main(String args[])

for (int i=1; i<100; i++)

if(i%5 == 0)

continue;

if( i>20 )

break;

System.out.println(i);

}
Execise4: Write a program in Java to find the average of marks you obtained in your 10+2 class.

//program to find the average of marks obtained in 10+2

import java.io.*;

class S1e4

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

//variable declaration

int mat,phy,chem,elec,lang1,lang2,total;

float avg;

String S=new String();

DataInputStream d= new DataInputStream(System.in);

//input marks

System.out.println("Enter marks obtained in Maths:");

S=d.readLine();

mat=Integer.parseInt(S);

System.out.println("Enter marks obtained in Physics:");

S=d.readLine();
phy=Integer.parseInt(S);

System.out.println("Enter marks obtained in Chemistry:");

S=d.readLine();

chem=Integer.parseInt(S);

System.out.println("Enter marks obtained in Electronics:");

S=d.readLine();

elec=Integer.parseInt(S);

System.out.println("Enter marks obtained in Lang 1:");

S=d.readLine();

lang1=Integer.parseInt(S);

System.out.println("Enter marks obtained in Lang 2:");

S=d.readLine();

lang2=Integer.parseInt(S);

//calculate total and average

total = mat+phy+chem+elec+lang1+lang2;

avg = total/6 ;

//display result

System.out.println("Total marks obtained:"+total);

System.out.println("Average of marks in 10+2:"+avg);

}
Session 2: Statements and array

Exercise1: Write a program in Java to find AxB where A is a 3x3 and B is a matrix of 3x4. Take the
values in matrixes A and B from user.

//multiplication of 2 matrices

import java.io.*;

class S2e1

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

int mat1[][]= new int[5][5];

int mat2[][]= new int[5][5];

int res[][] = new int[5][5];

int r1,c1,r2,c2;

String S= new String();

DataInputStream d= new DataInputStream(System.in);

System.out.println("Enter no of rows and columns in matrix A:");

S=d.readLine();

r1=Integer.parseInt(S);

S=d.readLine();

c1=Integer.parseInt(S);
System.out.println("Enter no of rows and columns in matrix B:");

S=d.readLine();

r2=Integer.parseInt(S);

S=d.readLine();

c2=Integer.parseInt(S);

if(c1!=r2)

System.out.println("No of cols of MatA is not equal to number of rows of MatB,hence it


cannot be multiplied");

else

System.out.println("Enter matrix A row-wise:");

for(int i=1;i<=r1;i++)

for(int j=1;j<=c1;j++)

S=d.readLine();

mat1[i][j]=Integer.parseInt(S);

System.out.println("Enter matrix B row-wise:");

for(int i=1;i<=r2;i++)

{
for(int j=1;j<=c2;j++)

S=d.readLine();

mat2[i][j]=Integer.parseInt(S);

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

for(int i=1;i<=r1;i++)

for(int j=1;j<=c1;j++)

System.out.print("\t");

System.out.print(mat1[i][j]);

System.out.println();

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

for(int i=1;i<=r2;i++)

for(int j=1;j<=c2;j++)

System.out.print("\t");

System.out.print(mat2[i][j]);

System.out.println();

}
//multiplication:

for(int i=1;i<=r1;i++)

for(int j=1;j<=c2;j++)

res[i][j]=0;

for(int k=1;k<=c1;k++)

res[i][j]=res[i][j]+mat1[i][k]*mat2[k][j];

//Display Output

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

for(int i=1;i<=r1;i++)

for(int j=1;j<=c2;j++)

System.out.print("\t");

System.out.print(res[i][j]);

System.out.println();

}
}

Exercise2: Write a program in Java to compute the sum of the digits of a given integer. Remember,
your integer should not be less than the five digits. (e.g., if input is 23451 then sum of the digits of
23451 will be 15)

import java.io.*;

class S2e2

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

{
int num,sum=0,len,i;

DataInputStream d=new DataInputStream(System.in);

String s;

System.out.println("Please enter the number > 9999");

s=d.readLine();

len=s.length();

num=Integer.parseInt(s);

if(num<=9999)

System.out.println("Invalid");

else

for(i=1;i<=len;i++)

sum=sum+num % 10;

num= num / 10;

System.out.println("The sum of all num is : " +len+ " digit is " + sum );

You might also like