Assignment-1 1. Print 'Hello World' in Java.: Sybca-Div-2-Java Avani Joshi ROLL NO:-102
Assignment-1 1. Print 'Hello World' in Java.: Sybca-Div-2-Java Avani Joshi ROLL NO:-102
ASSIGNMENT-1
class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
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);
}
}
import java.util.*;
public class pa_1
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
1
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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;
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.
2
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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.
6. Calculate area of square, rectangle and triangle using the three same name methods
'area';
3
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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)
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));
System.out.println("A / B is "+ (a/b));
System.out.println("A * B is "+ (a*b));
}
}
import java.util.Scanner;
class Arr
{
public static void main(String[] args)
{
int[] myarr = new int[5];
4
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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("");
}
}
}
5
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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("");
}
}
}
import java.util.Scanner;
public class pattern2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows to print the pattern ");
int rows = sc.nextInt();
6
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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.
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.
7
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
import java.util.Scanner;
class AverageCalc
{
double avg=0;
AverageCal(int a[])
{
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);
}
}
import java.util.*;
public class stck
8
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
{
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);
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);
}
}
9
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
row1 = a.length;
col1 = a[0].length;
row2 = b.length;
col2 = b[0].length;
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: ");
10
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
16. Create one string array of size 5. Insert 5 names through user and sort the array name
wise and display it.
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:");
11
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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)
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.
12
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
import java.util.*;
class stu
{
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 'id, 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)
13
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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=");
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;
14
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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;
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
{
15
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
20. Write a program in Java with class Rectangle with the data fields width, length, area
and colour. Thelength, 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
16
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
the objects then display "Matching Rectangles", otherwise display "Non matching
Rectangle".
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();
17
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
r.set_colour();
r.find_area();
System.out.println("\n"+"Enter r1 object detail"+"\n");
r1.set_width();
r1.set_length();
r1.set_colour();
r1.find_area();
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.
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;
18
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
}
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;
}
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
{
19
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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();
do
{
20
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
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");
}
break;
case 2 :
System.out.println("0.exit");
System.out.println("1.Deposit");
System.out.println("2.Withdraw");
System.out.println("3.Get Balance");
21
SYBCA-DIV-2-JAVA AVANI JOSHI ROLL NO:-102
22