MCSL025 Java Lab Records 1

Session 1 : Data Types, Variables & Operators

Ex 1: 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. 

 

Code:
class rectangle
{
int h,w;
rectangle(int x,int y)
{
h=x; w=y;
}
int area()
{
return(h*w);
}
}
class s01_01
{
public static void main(String args[])
{
rectangle r=new rectangle(10,20);
int area=r.area();
System.out.println("Area of Rectangle="+area);
}

 

 

Ex 2: Write a program in Java to find the result of following expression (Assume a = 10, b = 5)
i) (a < < 2) + (b > > 2)
ii) (a) | | (b > 0)
iii) (a + b ∗100) / 10
iv) a & b

 

 Code:
class s01_02
{
public static void main(String args[])
{
int a=10,b=5;
System.out.println(" i) (a<<2)+(b>>2): "+(a<<2)+(b>>2) );
System.out.println(" ii) (a)||(b>0) : "+(a)||(b>0) );
System.out.println("iii) (a+b*100)/10 : "+(a+b*100)/10 );
System.out.println(" iv) (a&b) : "+(a&b) );
}
} 

 

 

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

 

Code:
class s01_03
{
public static void main(String args[])
{
int i = 0;
int j = 0;
for(int k=0; ;k++)
{
if(k>=args.length) break;
int l;
try
{
l = Integer.parseInt(args[k]);
}
catch(NumberFormatException e)
{
j++;
System.out.println("INVALID ARGUMENT :" + args[k]);
continue;
}
i++;
System.out.println("VALID ARGUMENT :" + args[k]);
}
System.out.println("NUMBER OF VALID ARGUMENTS :" + i);
System.out.println("NUMBER OF INVALID ARGUMENT:" + j);
}
} 

 

 

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

 

Code:
class s01_04
{
public static void main(String args[])
{
int reg,m1,m2,m3;
String name;
reg=Integer.parseInt(args[0]);
name=args[1];
m1=Integer.parseInt(args[2]);
m2=Integer.parseInt(args[3]);
m3=Integer.parseInt(args[4]);
System.out.println("------------------------");
System.out.println(" MARK LIST ");
System.out.println("------------------------");
System.out.println("Register No : "+reg);
System.out.println("Name : "+name);
System.out.println("Mark1 : "+m1);
System.out.println("Mark2 : "+m2);
System.out.println("Mark3 : "+m3);
System.out.println("Total : "+(m1+m2+m3));
System.out.println("Average : "+(float)(m1+m2+m3)/3);
System.out.println("------------------------");
}
}

 

 


Session 2 : Statements and Array

 

 Ex 5: Write a program in Java to find A×B where A is a matrix of 3×3 and B is a matrix of 3×4. Take the
values in matrixes A and B from the user.

 

 Code:
class s02_01
{
public static void main(String arg[])
{
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int c[][]=new int[3][3];
int n=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
a[i][j]=Integer.parseInt(arg[n++]);
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
b[i][j]=Integer.parseInt(arg[n++]);
//multipying the two matrix
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]);
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
System.out.print(c[i][j]+" ");
System.out.println();
}
}
} 

 

 

 

Ex 6: 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. 

 

Code:
class s02_02
{
public static void main(String arg[])
{
int sum=0;
long n=Long.parseLong(arg[0]);
while(n>0)
{
sum+=n%10;
n/=10;
}
System.out.println("Sum="+sum);
}
}

Session 3 : Class And Objects

 

 Ex 7: Write a program in Java with class Rectangle with the data fields width, length, area and colour. The
length, width and area are of double type and colour is of string type .The methods are set_ length () ,
set_width (), set_ colour(), and find_ area (). 

 

Code:
import java.io.*;
class rect
{
int width,length;
String color;
void set_length(int a)
{ length=a; }
void set_width(int a)
{ width=a; }
void set_color(String a)
{ color=a; }
int area()
{ return(width*length); }
String getcolor()
{ return(color); }
}
class s03_01
{
public static void main(String arg[])throws Exception
{
String s=null;
DataInputStream in=new DataInputStream(System.in);
rect a=new rect();
System.out.println("Enter the length for first rectangle");
s=in.readLine();
a.set_length(Integer.parseInt(s));
System.out.println("Enter the width for first rectangle");
s=in.readLine();
a.set_width(Integer.parseInt(s));
System.out.println("Enter the Color for first rectangle");
a.set_color(in.readLine());
rect b=new rect();
System.out.println("Enter the length for second rectangle");
s=in.readLine();
b.set_length(Integer.parseInt(s));
System.out.println("Enter the width for second rectangle");
s=in.readLine();
b.set_width(Integer.parseInt(s));
System.out.println("Enter the Color for second rectangle");
b.set_color(in.readLine());
if(a.area()==b.area() && a.getcolor().equals(b.getcolor()))
System.out.println("Matching Rectangle ");
else
System.out.println("Non Matching Rectangle ");
}
} 

 

Ex 8: Create a class Account with two overloaded constructors. The first constructor is used for initializing,
the name of account holder, the account number and the initial amount in the account. The second
constructor is used for initializing the name of the account holder, the account number, the addresses, the
type of account and the current balance. The Account class is having methods Deposit (), Withdraw (), and
Get_Balance(). Make the necessary assumption for data members and return types of the methods. Create
objects of Account class and use them.

 

 Code:
class account
{
String name,address,type;
int accno,bal;
account(String n,int no,int b)
{ name=n; accno=no; bal=b; }
account(String n,int no,String addr,String t,int b)
{
name=n; accno=no;
address=addr;
type=t; bal=b;
}
void deposite(int a) { bal+=a; }
void withdraw(int a) { bal-=a; }
int getbalance() { return(bal); }
void show()
{
System.out.println("________________________");
System.out.println(" ACCOUNT DETAILS");
System.out.println("------------------------");
System.out.println("Name : "+name);
System.out.println("Account No : "+accno);
System.out.println("Address : "+address);
System.out.println("Type : "+type);
System.out.println("Balance : "+bal);
System.out.println("------------------------");
}
}
class s03_02
{
public static void main(String arg[])throws Exception
{
account a1=new account("Anil",555,5000);
account a2=new account("Anil",666,"Tirur","Current account",1000);
a1.address="Calicut";
a1.type="fixed deposite";
a1.deposite(5000);
a2.withdraw(350);
a2.deposite(a2.getbalance());
a1.show();
a2.show();
}
} 

 

 

Ex 9: Write a program in Java to create a stack class of variable size with push() and pop () methods. Create
two objects of stack with 10 data items in both. Compare the top elements of both stack and print the
comparison result. 

 

Code:
import java.io.*;
class stack
{
int data[]=new int[50];
int sp=0;
int pop()
{
if(sp<=0)
{
System.out.println("Stack is empty");
return(0);
}
else
return(data[sp--]);
}
void push(int a)
{
if(sp>=50)
System.out.println("Stack overflow");
else
data[sp++]=a;
}
}
class s03_03
{
public static void main(String arg[])
{
DataInputStream in=null;
String s;
int d;
stack s1=new stack();
stack s2=new stack();
try
{
in=new DataInputStream(System.in);
for(int i=0;i<10;i++)
{
System.out.println(1+i+") Enter data for the first stack");
s=in.readLine();
d=Integer.parseInt(s);
s1.push(d);
}
for(int i=0;i<10;i++)
{
System.out.println(1+i+") Enter data for the second stack");
s=in.readLine();
d=Integer.parseInt(s);
s2.push(d);
}
if(s1.pop()==s2.pop())
System.out.println("The top of the stacks are same");
else
System.out.println("The top of the stacks are same");
}
catch(Exception e) { System.out.println(e); }
}
}

 

No comments

Powered by Blogger.