Sybca-div-2 JAVA Roll no.
-194
Assignment :- 1
1. Print 'hello world' in java.
Ans.
class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
2. Addition of two values passing by command line argument.
Ans.
class add
{
public static void main(String args[])
{
int a,b,c;
a= Integer.parseInt(args[0]);
b= Integer.parseInt(args[6]);
for(int i=0;i<=5;i++)
{
c=i+i;
}
System.out.println("SUM= "+c);
}
}
3. Write a program to find the result of following expression.
1) a << 2 + b >> 2
2) (a<>2)
3) a & b
4) a | 4 + a >> b & 7
Ans.
import java.util.*;
public class pa_1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a,b,a1,a2,a3,a4;
System.out.print("Enter a value");
a=sc.nextInt();
System.out.print("Enter b value");
b=sc.nextInt();
a1=a<<2+b>>2;
1
Sybca-div-2 JAVA Roll no.-194
a2=(a<<2)+(b>>2);
a3=a&b;
a4=a|4+a>>b&7;
System.out.println("a<<2+b>>2="+ a1);
System.out.println("(a<<2)+(b>>2)="+a2);
System.out.println("&b="+a3);
System.out.println("a|4+a>>b&7="+a4);
}
}
4. Write a program in java to explain the use of break and continue statements.
Ans.
public class breakc
{
public static void main(String args[])
{
for(int i = 0; i < 10; i++)
{
if( i == 4)
break;
System.out.println(i);
}
}
}
/*public class breakc
{
public static void main(String args[])
{
for(int i = 0; i < 10; i++)
{
if( i == 4)
continue;
System.out.println(i);
}
}
} */
5. Create Box class with data members width, height and depth. Objects of this class initialize with zero value
or by passing single value. Create a method to calculate volume and display it.
Ans.
public class Boxs
{
double h,w,d;
Box(double width,double height,double depth)
{
h=height;
w=width;
d=depth;
}
2
Sybca-div-2 JAVA Roll no.-194
public static void main(String args[])
{
Boxs bc=new Boxs(8.5,80.3,9.6);
System.out.println(bc.volume());
}
}
6. Calculate area of square, rectangle and triangle using the three same name methods 'area';
Ans.
public class Area
{
void area(float x)
{
System.out.println("Area of the square :" + Math.pow(x, 2));
}
void area(float x, float y)
{
System.out.println("Area of the rectangle :" + x*y);
}
void area(double x)
{
double z = 3.14 * x * x;
System.out.println("Area of the circle :" + z);
}
}
class Overload
{
public static void main(String args[])
{
Area ob = new Area();
ob.area(11,12);
ob.area(2.5);
}
}
7. Make CALC class with two data members. Perform addition, subtraction, multiplication and division
operation of given two values by user. Initialize both data members with zero. (use menu-driven)
Ans.
import java.util.Scanner;
public class calu
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println("A + B is "+ (a+b));
System.out.println("A - B is "+ (a-b));
3
Sybca-div-2 JAVA Roll no.-194
System.out.println("A / B is "+ (a/b));
System.out.println("A * B is "+ (a*b));
}
}
8. Create array with values {8,7,6,5,4} and display it.
Ans.
import java.util.Scanner;
class Arr
{
public static void main(String[] args)
{
int[] myarr = new int[5];
Scanner s = new Scanner(System.in);
System.out.println("Enter 5 integers of array");
for(int i = 0; i < 5; i++)
myarr[i] = s.nextInt();
System.out.println("Elements of the array are:");
for(int i = 0; i < 5; i++)
System.out.println(myarr[i]);
}
}
9. Write a program which display following pattern.
1
12
123
1234
Ans.
import java.util.Scanner;
public class pattern
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows :");
int rows = sc.nextInt();
System.out.println("** Printing the pattern... **");
for (int i = 1; i <= rows; ++i)
{
for (int j = 1; j <= i; ++j)
{
System.out.print(" "+ j);
}
System.out.println("");
}
4
Sybca-div-2 JAVA Roll no.-194
}
}
10. Write a program which display following pattern.
1
23
456
7 8 9 10
Ans.
import java.util.Scanner;
public class patterns
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows :");
int n = sc.nextInt();
int k=1;
System.out.println("** Printing the pattern... **");
for (int i=1; i<=n; i++)
{
for(int j=i; j<=n; j++)
{
System.out.print(" ");
}
for(int j=1; j<=i; j++)
{
System.out.print(""+k);
k=k+1;
}
System.out.println("");
}
}
}
11. Write a program which display following pattern.
1
22
333
4444
Ans.
import java.util.Scanner;
public class pattern2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
5
Sybca-div-2 JAVA Roll no.-194
System.out.println("Enter the number of rows to print the pattern ");
int rows = sc.nextInt();
System.out.println("** Printing the pattern... **");
for (int i=1; i<=rows; i++)
{
for (int j=rows; j>i; j--)
{
System.out.print(" ");
}
for (int k=1; k<=i; k++)
{
System.out.print(i + " ");
}
System.out.println("");
}
}
}
12. Create integer array of size 5. Insert 5 values through user and sort the array and display it. Also calculate
total of all 5 values.
Ans.
import java.util.Scanner;
class sums
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
int[] array = new int[10];
System.out.println("Enter the elements:");
for (int i=0; i<5; i++)
{
array[i] = scanner.nextInt();
}
for( int num : array)
{
sum = sum+num;
}
System.out.println("Sum of array elements is:"+sum);
}
}
13. Write a program which have a class AVERAGE contain a single dimensional array MARKS (size 5) as data
member. Take input from parameterized constructor. Take methods to calculate average marks and display it.
Create only one object of class.
Ans.
import java.util.Scanner;
class AverageCalc
{
double avg=0;
AverageCal(int a[])
6
Sybca-div-2 JAVA Roll no.-194
{
for(int i=0;i<a.length;i++)
{
avg=avg+a[i];
}
}
}
class AverageMarks
{
public static void main(String args[])
{
int i;
System.out.println("Enter number of subjects");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
System.out.println("Enter marks");
for( i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
AverageCalc c=new AverageCalc(a);
System.out.print("Average of (");
for(i=0;i<n-1;i++)
{
System.out.print(a[i]+",");
}
System.out.println(a[i]+") ="+c.avg/n);
}
}
14. Create STACK class with push, pop fun.
Ans.
import java.util.*;
public class stck
{
public static void main(String args[])
{
Stck <Integer> stk = new Stck<>();
System.out.println("stack: " + stk);
pushelmnt(stk, 20);
pushelmnt(stk, 13);
pushelmnt(stk, 89);
pushelmnt(stk, 90);
pushelmnt(stk, 11);
pushelmnt(stk, 45);
pushelmnt(stk, 18);
popelmnt(stk);
7
Sybca-div-2 JAVA Roll no.-194
popelmnt(stk);
try
{
popelmnt(stk);
}
catch (EmptyStackException e)
{
System.out.println("empty stack");
}
}
static void pushelmnt(Stak stk, int x)
{
stk.push(new Integer(x));
System.out.println("push -> " + x);
System.out.println("stack: " + stk);
}
static void popelmnt(Stck stk)
{
System.out.print("pop -> ");
Integer x = (Integxr) stk.pop();
System.out.println(x);
System.out.println("stck: " + stk);
}
}
15. Write a program in java to find A x B where A is a matrix of 3 x 3 and B is a matrix of 3 x 4. Take the
values in matrixes A and B form the user.
Ans.
public class matx
{
public static void main(String[] args)
{
int row1, col1, row2, col2;
int a[][] = {
{1, 3, 2},
{3, 1, 1},
{1, 2, 2}
};
int b[][] = {
{2, 1, 1, 1},
{1, 0, 1, 2},
{1, 3, 1, 1}
};
row1 = a.length;
col1 = a[0].length;
row2 = b.length;
col2 = b[0].length;
8
Sybca-div-2 JAVA Roll no.-194
if(col1 != row2)
{
System.out.println("Matrices cannot be multiplied");
}
else
{
int prod[][] = new int[row1][col2];
for(int i = 0; i < row1; i++)
{
for(int j = 0; j < col2; j++)
{
for(int k = 0; k < row2; k++)
{
prod[k][j] = prod[i][j] + a[i][k] * b[k][j];
}
}
}
System.out.println("Product of two matrices: ");
for(int i = 0; i < row1; i++)
{
for(int j = 0; j < col2; j++)
{
System.out.print(prod[i][j] + " ");
}
System.out.println();
}
}
}
}
16. Create one string array of size 5. Insert 5 names through user and sort the array name wise and display it.
Ans.
import java.util.Scanner;
public class string
{
public static void main(String[] args)
{
int count;
String temp;
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of strings you would like to enter:");
count = scan.nextInt();
String str[] = new String[count];
Scanner scan2 = new Scanner(System.in);
System.out.println("Enter the Strings one by one:");
for(int i = 0; i < count; i++)
{
str[i] = scan2.nextLine();
}
scan.close();
scan2.close();
9
Sybca-div-2 JAVA Roll no.-194
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
if (str[i].compareTo(str[j])>0)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
System.out.print("Strings in Sorted Order:");
for (int i = 0; i <= count - 1; i++)
{
System.out.print(str[i] + ", ");
}
}
}
17. 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 2+3+4+5+1 = 15).
Ans.
import java.util.Scanner;
class sum1
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
int[] array = new int[10];
int sum = 0;
System.out.println("Enter the elements:");
for (int i=0; i<5; i++)
{
array[i] = scannar.nextInt();
}
for( int num : array)
{
sum = sum+num;
}
System.out.println("Sum of array elements is:"+sum);
}
}
18. Create class STUDENT with rno and name data members. Create 5 objects using array (array of objects).
Enter data through user and display it.
Ans.
import java.util.*;
class stu
10
Sybca-div-2 JAVA Roll no.-194
{
Scanner sc=new Scanner(System.in);
int rno;
String name;
void getdata()
{
System.out.print("Enter Roll Number=");
rno=sc.nextInt();
sc.nextLine();
System.out.print("Enter Name=");
name=sc.nextLine();
}
void display()
{
System.out.println(rno+" "+name);
}
}
class prac0
{
public static void main(String args[])
{
std s[]=new std[5];
int i;
for(i=0;i<5;i++)
{
s[i]=new STUDENT();
s[i].getdata();
}
for(i=0;i<5;i++)
{
s[i].display();
}
}
}
19. Create class EMPLOYEE with i͚ d, name and salary͛ private data-members. Create 5 objects dynamically
through user input. Create two methods which display data salary wise and name wise. (make menu-driven by
switch case).
Ans.
import java.util.*;
class EMP
{
Scanner sc=new Scanner(System.in);
Private int id;
String name;
int salary;
public int I,S;
String n;
void getdata()
{
System.out.print("Enter Id=");
11
Sybca-div-2 JAVA Roll no.-194
id=sc.nextInt();
I=id;
System.out.print("Enter name=");
sc.nextLine();
name=sc.nextLine();
n=name;
System.out.print("Enter salary=");
salary=sc.nextInt();
S=salary;
}
void display_name(EMP e[])
{
int i,j,id,salary;
String name;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(e[i].n.compareTo(e[j].n)>0)
{
id=e[i].I;
e[i].I=e[j].I;
e[i].I=id;
salary=e[i].S;
e[i].S=e[j].S;
e[j].S=Salary;
name=e[i].n;
e[i].n=e[j].n;
e[j].n=name;
}
}
}
System.out.println("id"+"\t"+" name"+"\t"+" salary");
for(i=0;i<5;i++)
{
System.out.println(e[i].I+"\t"+e[i].n+"\t"+e[i].S);
}
}
void display_salary(EMP e[])
{
int i,j,id,salary;
String name;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(e[i].S>e[j].S)
{
id=e[i].I;
e[i].I=e[j].I;
e[j].I=id;
12
Sybca-div-2 JAVA Roll no.-194
salary=e[i].S;
e[i].S=e[j].S;
e[j].S=salary;
name=e[i].n;
e[i].n=e[j].n;
e[j].n=name;
}
}
}
System.out.println("id"+"\t"+" name"+"\t"+" salary");
for(i=0;i<5;i++)
{
System.out.println(e[i].I+"\t"+e[j].n+"\t"+e[i].S);
}
}
}
class prac1 extends EMP
{
public static void main(String args[])
{
EMPLOYEE []e=new EMPLOYEE[5];
EMPLOYEE d=new EMPLOYEE();
Scanner sc=new Scanner(System.in);
int i,ch;
for(i=0;i<5;i++)
{
e[i]=new EMP();
e[i].getdata();
}
do
{
System.out.println("0.exit");
System.out.println("1.name wise display");
System.out.println("2.salary wise display");
System.out.println("Select number");
ch=sc.nextInt();
switch(ch)
{
case 0:
System.exit(0);
break;
case 1:
d.display_name(e);
break;
case 2:
d.display_salary(e);
break;
default:
System.out.println("Select Proper Number");
}
13
Sybca-div-2 JAVA Roll no.-194
}while(ch>0);
}
}
20. 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(). Create two object of Rectangle and compare their area and colour. If area
and color both are the same for the objects then display M ͞ atching Rectangles͟, otherwise display ͞Non matching
Rectangle͟.
Ans.
import java.util.*;
class Rectangle
{
Scanner sc=new Scanner(System.in);
double width,length,area;
String colour;
void set_width()
{
System.out.print("Enter width=");
width=sc.nextDouble();
}
void set_length()
{
System.out.print("Enter length=");
length=sc.nextDouble();
}
void set_colour()
{
System.out.print("Enter colour=");
sc.nextLine();
colour=sc.nextLine();
}
void find_area()
{
area=(width*length);
System.out.println("Area="+area);
}
}
class prac2
{
public static void main(String args[])
{
Rectangle r=new Rectangle();
Rectangle r1=new Rectangle();
System.out.println("Enter r object detail"+"\n");
r.set_width();
r.set_length();
r.set_colour();
r.find_area();
System.out.println("\n"+"Enter r1 object detail"+"\n");
r1.set_width();
14
Sybca-div-2 JAVA Roll no.-194
r1.set_length();
r1.set_colour();
r1.find_area();
if(r.area==r1.area && r.colour.equals(r1.colour)==true)
{
System.out.println("match");
}
else
{
System.out.println("not match");
}
}
}
21. 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.
Ans.
import java.util.*;
class Acc
{
Scanner sc=new Scanner(System.in);
String name;
double ac_no;
int amount;
String address;
String ac_type;
Acc(String na,double acno,int am)
{
name=na;
ac_no=acno;
amount=am;
}
Acc(String na,double acno,String add,String actype,int cbalance)
{
name=na;
ac_no=acno;
address=add;
ac_type=actype;
amount=cbalance;
}
void detail(String a,double acno,int am,String add,String actype)
{
name=na;
ac_no=acno;
amount=am;
address=add;
ac_type=actype;
15
Sybca-div-2 JAVA Roll no.-194
}
void Deposit()
{
System.out.print("Enter Amount=");
int am=sc.nextInt();
amount=amount+am;
}
void Withdraw()
{
System.out.print("Enter withdraw amount=");
int am=sc.nextInt();
amount=amount-am;
if(amount>2000)
{
System.out.println("Balance Less then 2000");
amount=amount+am;
}
}
void Get_Balance()
{
System.out.println("Current Balance="+amount);
}
}
class prac3
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String na,add;
String actype;
int am,ch,ch1;
double acno;
System.out.println("Enter First Account Holder Detail"+"\n\n");
System.out.print("Enter Account Holder Name=");
na=sc.nextLine();
System.out.print("Enter Address=");
add=sc.nextLine();
System.out.print("Enter Account Type=");
actype=sc.nextLine();
System.out.print("Enter Account Number=");
acno=sc.nextDouble();
System.out.print("Enter Amount=");
am=sc.nextInt();
Acc a1=new Account(na,acno,am);
a1.detail(na,acno,am,add,actype);
System.out.println("\n\n"+"Enter Second Account Holder Detail"+"\n\n");
16
Sybca-div-2 JAVA Roll no.-194
System.out.print("Enter Account Holder Name=");
na=sc.nextLin();
sc.nextLine();
System.out.print("Enter Address=");
add=sc.nextLin();
System.out.print("Enter Account Type=");
actype=sc.nextLine();
System.out.print("Enter Account Number=");
acno=sc.nextDouble();
System.out.print("Enter Amount=");
am=sc.nextInt();
Acc a2=new Account(na,acno,add,actype,am);
a2.detail(na,acno,am,add,actype);
do
{
System.out.println("\n\n"+"0.exit");
System.out.println("1.First Account Holder");
System.out.println("2.Second Account Holder");
System.out.println("Enter Number You perfome a task");
ch=sc.nextInt();
switch(ch)
{
case 0:
System.exit(0);
break;
case 1:
System.out.println("0.exit");
System.out.println("1.Deposit");
System.out.println("2.Withdraw");
System.out.println("3.Get Balance");
System.out.println("Select your choich");
ch1=sc.nextInt();
switch(ch1)
{
case 0:
System.exit(0);
break;
case 1:
a1.Deposit();
break;
case 2:
a1.Withdraw();
break;
case 3:
a1.Get_Balance();
break;
default:
System.out.println("Select proper number");
17
Sybca-div-2 JAVA Roll no.-194
}
break;
case 2 :
System.out.println("0.exit");
System.out.println("1.Deposit");
System.out.println("2.Withdraw");
System.out.println("3.Get Balance");
System.out.println("Select your choich");
ch1=sc.nextInt();
switch(ch1)
{
case 0:
System.exit(0);
break;
case 1:
a2.Deposit();
break;
case 2:
a2.Withdraw();
break;
case 3:
a2.Get_Balance();
break;
default:
System.out.println("Select proper number");
}
break;
default:
System.out.println("Select proper number");
}
}while(ch>0);
}
}
18