Assignment No.
- 7
Name : Dalvi Ritesh Rakesh Roll No. : 28
Enrollment No. : 202007255 Date: / /2023
______________________________________________________________
Q1 Write the Java programs on operators.
Arithmatic operators
Program :
class arithmatic
public static void main(String Args[])
int a = 10 , b = 10;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a/b);
System.out.println(a*b);
System.out.println(a%b);
Output :-
Shift Operators
Program :
class leftshift
public static void main(String Args[])
System.out.println(10<<2);
System.out.println(10<<3);
System.out.println(60<<2);
System.out.println(90<<3);
System.out.println(10>>2);
System.out.println(10>>4);
System.out.println(60>>2);
System.out.println(90>>4);
System.out.println(10>>>2);
System.out.println(10>>>4);
System.out.println(60>>>2);
System.out.println(90>>> 4);
Output :
Unary Operators
Program :
class increament
{
public static void main(String Args[])
int a = 10;
System.out.println(a++);
System.out.println(++a);
System.out.println(a--);
System.out.println(--a);
Output :
2) Assignment Operators
Program :
class assignmentoperators
{
public static void main(String Args[])
int a=10,b=20;
System.out.println(a+=b);
System.out.println(a-=b);
System.out.println(a*=b);
System.out.println(a/=b);
System.out.println(a%=b);
System.out.println(a=b);
Output :
Logical Operators
Program :
class logicalopr
{
public static void main(String[] args) {
System.out.println((5 > 3) && (8 > 5));
System.out.println((5 > 3) && (8 < 5));
System.out.println((5 < 3) || (8 > 5));
System.out.println((5 < 3) || (8 < 5));
System.out.println(!(5 == 3));
System.out.println(!(5 > 3));
Output :
Relational Operators
Program :
class relationalopra
public static void main(String[] args)
{
int a = 7, b = 11;
System.out.println("a is " + a + " and b is " + b);
System.out.println(a == b);
System.out.println(a != b);
System.out.println(a > b);
System.out.println(a < b);
System.out.println(a >= b);
System.out.println(a <= b);
Output :
Assignment No:8
Name: Dalvi Ritesh Rakesh Roll No:12
Enrollment No :202007389 Date: \ \2023
Write a Java Program on Control Statement
1) If
The if Statement
Use the if statement to specify a block of Java code to be executed if a condition is
true.
Syntax :-
Code :
classifstm {
public static void main(String[] args) {
int x = 20;
int y = 18;
System.out.println("Value of x is"+x);
System.out.println("Value of y is"+y);
if (x > y)
System.out.println("x is greater than y");
}
}
Output :-
importjava.util.Scanner;
public class evenodd {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number: ");
intnum = reader.nextInt();
if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
Code :
Output :
Write a java proram by using if else-if
Code :
import java.io.*;
classelseifladd {
public static void main(String[] args)
inti = 12;
System.out.println("the given number is"+i);
if (i< 10)
System.out.println("i is less than 10\n");
else if (i< 15)
System.out.println("i is less than 15\n");
else if (i< 20)
System.out.println("i is less than 20\n")
else
System.out.println("i is greater than " + "or equal to 20\n");
Switch Case
Code :
classswitcs {
public static void main(String[] args)
{
int day = 5;
String dayString;
switch (day) {
case 1:
dayString = "Monday";
break;
case 2:
dayString = "Tuesday";
break;
case 3:
dayString = "Wednesday";
break;
case 4:
dayString = "Thursday";
break;
case 5:
dayString = "Friday";
break;
case 6:
dayString = "Saturday";
break;
case 7:
dayString = "Sunday";
break;
default:
dayString = "Invalid day";
}
System.out.println(dayString);
}
}
Output:
Assignment No:9
Name: Dalvi Ritesh Rakesh Roll No:12
Enrollment No :202007389 Date: \ \2023
Write a java program on Looping Statements
1) While Loop (Display Odd Numbers)
classoddhun
public static void main(String args[])
int number=100;
System.out.print("List of odd numbers from 1 to "+number+": ");
for (inti=1; i<=number; i++)
if (i %2!=0)
System.out.print(i + " ");
Output :
Do-while loop (Display odd numbers)
Code :
importjava.util.Scanner;
classevenwlp{
public static void main (String args[]){
inti;
Scanner scan=new Scanner(System.in);
System.out.print("Print all odd number until:\n");
intnum=scan.nextInt();//Reads input from user and stored in variable num
System.out.print("odd number from i to "+num+" are: \n");
i=1;
do{
if(i%2==1){
System.out.print(i+"\n");
i++;
while(i<=num);
}
Using For loop (1 to 20 Odd number)
Code :
importjava.util.Scanner;
classfrlpoddnum
public static void main (String args[]){
inti;
Scanner scan=new Scanner(System.in);
System.out.print("Print all odd number until:\n ");
intnum=scan.nextInt();
System.out.print("odd number from 1 to "+num+" are: \n");
for(i=1; i<=num; i++){
if(i%2==1)
System.out.print(i+"\n");
Write a java program using For each loop
Code
import java.io.*;
class easy
{
public static void main(String[] args)
{
int ar[] = { 10, 50, 60, 80, 90 };
for (int element : ar)
System.out.print(element + " ");
}
}
Output :
Assignment No: 10
Name: Dalvi Ritesh Rakesh Roll No:12
Enrollment No :202007389 Date: \ \2023
Write a Java Program to display Student Roll no, student name & address using
class methods and objects
Code :-
class Student
{
int id;
String name;
String address;
classStudentassiinfo{
public static void main(String args[]){
Student s1=new Student();
s1.id=101;
s1.name="Ritesh";
s1.address="satara";
System.out.println(s1.id+" "+s1.name+""+s1.address+"");
Assignment No: 11
Name: Dalvi Ritesh Rakesh Class:BCA-III
Roll No:12 Date: \ \2023
Write a Java program on Constructor
1) Default constructor
Code
classDefaultConstructor{
publicDefaultConstructor ()
{
System.out.println ("This is a no-argument constructor");
}
public static void main (String args[])
{
newDefaultConstructor();
}
}
2) No Args Arguments
Code:
public class noargcr {
public static void greet()
{
System.out.println(
"Hey! this is Without Arguement constructor.");
}
public static void main(String args[])
{
greet();
}
}
Output :
3)Parameterized Constructor
Code :
import java.io.*;
class Student
{
String name;
int id;
Student (String name, int id)
{
this.name = name;
this.id = id;
}
}
public class constud
{
public static void main (String args[])
{
Student student1 = new Student ("Ritesh", 1);
System.out.println ("Student Name is: " + student1.name);
System.out.println ("Student Id is : " + student1.id);
}
}
Assignment No: 12
Name: Dalvi Ritesh Rakesh Class:BCA-III
Roll No:12 Date: 2 \ 1 \2023
Que ) Write a java program to accept details From employee and Display data to the
user using scanner Class
Code :
importjava.util.Scanner;
class epinfo
public static void main(String args[])
Scanner sc = new Scanner(System.in);
System.out.print("Enter Your Name:");
String name = sc.nextLine();
System.out.print("Enter Your Address:");
String address = sc.nextLine();
System.out.print("Enter Your Email Id:");
String emailid = sc.nextLine();
System.out.print("Enter Your Id:");
int id = sc.nextInt();
System.out.print("Enter Your Salary:");
double salary = sc.nextDouble();
System.out.print("Enter Your Aadhar No:");
longaadharno = sc.nextLong();
System.out.println("Enter Your Gender: 1.male 2.female 3.others");
int gender = sc.nextInt();
{ if(gender==1)
{ System.out.println("male");}
else if(gender==2)
{System.out.println("female");}
else{
System.out.println("others"); }}
System.out.println("Enter Your Marital Status: 1.married 2.unmarried ");
intch = sc.nextInt();
switch(ch)
{case 1: System.out.println("married="+(1));
break;
case 2: System.out.println("unmarried="+(2));
break;
default: System.out.println("single");
System.out.println("<<-------Employee Information------->>" );
System.out.println("Employee name is :"+name );
System.out.println("Employee Address is is :"+address );
System.out.println("Employee Email id is :"+emailid );
System.out.println("Employee Salary is :"+salary );
System.out.println("Employee Adhar card no is :"+aadharno );
System.out.println("Employee Gender is :"+gender);
System.out.println("Employee Marital status is :"+ch);
}
Assignment No: 13
Name: Dalvi Ritesh Rakesh Class:BCA-III
Roll No:12 Date: 2 \ 1 \2023
Write a program on Garbage collector
Gc collector calls “jvm” and Finallize () method called as “gc”
Code
class Employee
private int ID;
private String name;
private int age;
private static int nextId = 1;
public Employee(String name, int age)
this.name = name;
this.age = age;
this.ID = nextId++;
public void show()
System.out.println("Id=" + ID + "\nName=" + name
+ "\nAge=" + age);
public void showNextId()
System.out.println("Next employee id will be="+ nextId);
protected void finalize()
{
--nextId;
public class UseEmployee {
public static void main(String[] args)
Employee E = new Employee("GFG1", 56);
Employee F = new Employee("GFG2", 45);
Employee G = new Employee("GFG3", 25);
E.show();
F.show();
G.show();
E.showNextId();
F.showNextId();
G.showNextId();
Employee X = new Employee("GFG4", 23);
Employee Y = new Employee("GFG5", 21);
X.show();
Y.show();
X.showNextId();
Y.showNextId();
X = Y = null;
System.gc();
System.runFinalization();
}
E.showNextId();
Output :
Assignment No: 14
Name: Dalvi Ritesh Rakesh Class:BCA-III
Roll No:12 Date: 2 \ 1 \2023
Write a Java Program on Single Inheritance
Code:
class Shape {
public int calculateArea(int length,int breadth) {
return length * breadth;
class Rectangle extends Shape {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
System.out.println("Area of rectangle :: "+ rectangle.calculateArea(10,5));
Output:
Assignment No. - 15
Name : Dalvi Ritesh Rakesh Roll No. : 12
Enrollment No. : 202007389 Date: / /2023
___________________________________________________________________________
Write A Java program on Multiple inheritance
class Car
{
public Car()
{
System.out.println("Class Car");
}
public void vehicleType()
{
System.out.println("Vehicle Type: Car");
}
}
class Maruti extends Car{
public Maruti()
{
System.out.println("Class Maruti");
}
public void brand()
{
System.out.println("Brand: Maruti");
}
public void speed()
{
System.out.println("Max: 90Kmph");
}
}
public class Maruti800 extends Maruti{
public Maruti800()
{
System.out.println("Maruti Model: 800");
}
public void speed()
{
System.out.println("Max: 80Kmph");
}
public static void main(String args[])
{
Maruti800 obj=new Maruti800();
obj.vehicleType();
obj.brand();
obj.speed();
}
}
Output :
Assignment No. - 16
Name : Dalvi Ritesh Rakesh Roll No. : 12
Enrollment No. : 202007389 Date: / /2023
___________________________________________________________________________
Write a Java program on Hierarchical Inheritance
class Animal
void eat()
System.out.println("eating...");}
class Dog extends Animal{
void bark()
System.out.println("barking...");}
class Cat extends Animal
void meow()
System.out.println("meowing...");}
}
class aniin{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
Assignment No. - 17
Name : Dalvi Ritesh Rakesh Roll No. : 12
Enrollment No. : 202007389 Date: / /2023
___________________________________________________________________________
Write A Java program On Runtime Polymorphism
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and run");
}
}
public class TestDog {
public static void main(String args[]) {
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move(); // runs the method in Animal class
b.move(); // runs the method in Dog class
}
}
Assignment No. - 18
Name : Dalvi Ritesh Rakesh Roll No. : 12
Enrollment No. : 202007389 Date: / /2023
Write a Java Program on Compile time polymorphism
a) By changing number of arguments