You are on page 1of 18

Assignment No: 7

Name: Gandhale Abhishek Rajaram Roll No: 19

Enrollment No: 202008548 Date: 16/01/2023


Q. 1) Write a java program on operators:-

 Arithmetic Operator

Use : Arithmetic operators are used to do simple mathematical operations like


Addition, Subtraction, Multiplication, Division And Modulus.

Syntax :
op1 + op2
op1 - op2
op1 * op2
op1 / op2
op1 % op2

class add {

public static void main(String args[]) {


int a = 10, b = 3, add, sub, mult, div , mod;
add = a + b;
sub = a - b;
mult = a * b;
div = a / b;
mod= a%b;
System.out.println("addition is : " + add);
System.out.println("Subtraction is: " + sub);
System.out.println("multiplication is: " + mult);
System.out.println("Division is: " + div);
System.out.println("reminder is: " +mod);
}
}

 Assignment Operator

Use : Assignment operators are used in Java to assign values to variables.


Syntax :
op1 += op2
op1 -= op2
op1 *= op2
op1 /= op2
op1 %= op2
op1 = op2

class AsignOp{
public static void main(String args[]){
int a=10, b=3;
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);
}
}

 Relational Operator

Use : Relational operators are used to check the relationship between two operands.

Syntax :
op1 == op2
op1 !- op2
op1 < op2
op1 > op2
op1 >= op2
op1 <= op2

class relationalop {
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);
}
}

 Logical Operator

Use : Logical operators are used to check whether an expression is true or false .
They are used in decision making.

Syntax :
op1 && op2
op1 || op2
op1 ! op2

class logicalop{
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) || (8 < 5));
System.out.println(!(5 == 3));
System.out.println(!(5 > 3));
}
}
Assignment No: 8

Name: Gandhale Abhishek Rajaram Roll No: 19

Enrollment No: 202008548 Date: 17 /01/2023


Q.1) Write a java program on Control Statements

a. Simple if

Use : Use if to specify a block of code to be executed, if a specified condition is true

Syntax :

If (condition) {

//statement

class cal{
public static void main(String args[]){
int n=100;
for(int i=1; i<=n; i++){
if(i%2==0){
System.out.print(i+" ");
}
}
}
}

b. If else

Use : Use else to specify a block of code to be executed, if the same condition is false

Syntax :

If (condition) {

//statement

else {
//statement

class oddeven{
public static void main(String args[]){
int n=9;
if(n!=0)
System.out.println("number "+ n +" odd");
else
System.out.println("number "+ n +" even");
}
}

c. If elseif

Use : Use else if to specify a new condition to test, if the first condition is false

Syntax :

If (condition) {

//statement 1

else if (condition) {

//statement 2

else {

//statement

class IfElseIf {
public static void main(String[] args) {
int marks=65;
if(marks<50){
System.out.println("fail");
}
else if(marks>=50 && marks<60){
System.out.println("D grade");
}
else if(marks>=60 && marks<70){
System.out.println("C grade");
}
else if(marks>=70 && marks<80){
System.out.println("B grade");
}
else if(marks>=80 && marks<90){
System.out.println("A grade");
}else if(marks>=90 && marks<100){
System.out.println("A+ grade");
}else{
System.out.println("Invalid!");
}
}
}

d. Switch statement

Use : Use switch to specify many alternative blocks of code to be executed

Syntax :

Switch ( Expression ) {

Case 1 : statement 1;

Break;

Case n : statement n;

Break;

Default : Statement; }

class SwitchExample {
public static void main(String[] args) {
int number=20;
switch(number){
case 10: System.out.println("10");
break;
case 20: System.out.println("20");
break;
case 30: System.out.println("30");
break;
default:System.out.println("Not in 10, 20 or 30");
}
}
}

Assignment No: 9

Name: Gandhale Abhishek Rajaram Roll No: 19

Enrollment No: 202008548 Date: 23/01/2023

Q.1) Write a java program using


a. while-loop

Use : The Java while loop is used to iterate a part of the program repeatedly until the specified
Boolean condition is true.

Syntax :

While (condition) {

//Code

class while_loop{
public static void main(String args[]){
int n=1;
while(n<=20){
System.out.print(" "+n+" ");
n+=1;
}
}
}
b. do-while-loop

Use : Java do-while loop is used to execute a block of statements continuously until the given
condition is true.

Syntax :

Do {

//Code

While (condition)

class DoWhileExample {
public static void main(String[] args) {
int i=1;
do{
System.out.print(" "i);
i++;
}while(i<=10);
}
}

c. for loop

class forloop {
public static void main(String args[]) {
int i, j, row=6;
for(i=0; i<row; i++) {
for(j=0; j<=i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Assignment No: 10

Name: Gandhale Abhishek Rajaram Roll No: 19

Enrollment No: 202008548 Date: 24/01/2023

Q1. Write a java program to display Student rollno, Name,Address using class and
object?

Use : It is used to create an instance of the class by using the new keyword, it allocates
memory (heap) for the newly created object and also returns the reference of that object to
that memory.

Syntax :

ClassName object = new ClassName();

class as10 {
int rollNo = 19;
String name = "abhishek";
String Class = "bca";
String Address = "satara";

public void display() {


System.out.println("Roll no of student:-" + rollNo);
System.out.println("Name of Student:- " + name);
System.out.println("Student class:- " + Class);
System.out.println("Address of Student:- " + Address);
}

public static void main(String[] args) {


as10 a = new as10();
a.display();
}
}
Assignment No: 11

Name: Gandhale Abhishek Rajaram Roll No: 19

Enrollment No: 202008548 Date: 30 /01/2023


Q.1) Write a java program using all constructor:

 Default Constructor

Use : The purpose of the default constructor is to initialize the attributes of the object with
their default values.

Syntax :

classname {

method() {

// constructor body

}}

class defaultctr{
defaultctr(){System.out.println("Bike is created");}
public static void main(String args[]){
defaultctr b=new defaultctr();
}
}

 parameterized constructor

Use : A parameterized constructor used to accept parameters with which you can
initialize the instance variables.

Syntax :
Class_name (para1,para2) {
// body of the constructor
}

class paractr{
int id;
String name;
paractr(int i,String n){
id = i;
name = n;
}
void display(){
System.out.println(id+" "+name);}
public static void main(String args[]){
paractr s1= new paractr(111,"Karan");
paractr s2= new paractr(222,"Aryan");
s1.display();
s2.display();
}
}
Assignment No: 12

Name: Gandhale Abhishek Rajaram Roll No: 19

Enrollment No: 202008548 Date: 31/01/2023


Q.1) Write a java program to accept details from emp and display it.

Use : The Scanner is mostly used to receive user input and parse them into primitive data
types such as int, double or default String.

Syntax :

( import java.util.Scanner; )

Scanner obj = new Scanner(System.in);

variable = obj.nextInt();

import java.util.Scanner;
class EmpDetails {
static String check ,c;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your id - ");
int emp_id = sc.nextInt();
System.out.print("Enter your name - ");
String emp_nm = sc.next();
System.out.print("Enter your address - ");
String address = sc.next();
System.out.print("Enter your UID No- ");
double uid = sc.nextInt();
System.out.print("Enter your joining date - ");
String JoinDt = sc.next();
System.out.print("Enter your salary- ");
double salary = sc.nextDouble();
System.out.print("Enter your Email id - ");
String Email = sc.next();
System.out.print("Enter your marital status (y/n) or (Y/N):- ");
String marital = sc.next();
switch (marital) {
case "Y":
case "y":
check = "married";
break;
case "N":
case "n":
check = "unmarried";
break;
default:
break;
}
System.out.print("Enter your gender (M/M) or (F/f) :- ");
String gender = sc.next();
switch (gender) {
case "M":
case "m":
c = "male";
break;
case "F":
case "f":
c = "female";
break;
default:
break;
}
System.out.println("------------Employee Details---------");
System.out.println();
System.out.println("Employee id: " + emp_id);
System.out.println("Employee name: " + emp_nm);
System.out.println("Employee address: " + address);
System.out.println("Employee adhar no: " + uid);
System.out.println("Employee joining date: " + JoinDt);
System.out.println("Employee salary: " + salary);
System.out.println("Employee email id: " + Email);
System.out.println("Employee marital status: " + check);
System.out.println("Employee gender: " + c);
}
}
Assignment No: 13

Name: Gandhale Abhishek Rajaram Roll No: 19

Enrollment No: 202008548 Date: 31/01/2023

Q1 Write a Java program on Garbage Collection.

Use : Garbage collection is used for looking at heap memory, identifying which objects are
in use and which are not, and deleting the unused objects.

Syntax :

classname obj1 = new classname();

classname obj2 = new classname();

obj1 = null;

obj2 = null;

System.gc();

Program :

public Employee1(String name, int age) {


this.name = name;
this.age = age;
this.id = nextid++;
}
public void show() {
System.out.println("ID: " + id + " \nname: " + name + "\n age: " + age);
}
public void shownextid() {
System.out.println(nextid);
}

public static void main(String[] args)


{
Employee1 e = new Employee1("gfg", 21);
Employee1 t = new Employee1("hello", 22);
Employee1 a = new Employee1("hi",99);
e.show();
t.show();
a.show();
e.shownextid();
t.shownextid();
a.shownextid();
{
Employee1 x = new Employee1("hi",99);
Employee1 y = new Employee1("hello",77);
x.show();
y.show();
x.shownextid();
y.shownextid();
x=y = null;
System.gc();
System.runFinalization();
}
e.shownextid();
}
}
Assignment No: 14

Name: Gandhale Abhishek Rajaram Roll No: 19

Enrollment No: 202008548 Date: 31/01/2023


Q.1) Write a java program single inheritance.

Use : It allows a derived class to inherit the properties and behavior of a base class, thus enabling
code reusability as well as adding new features to the existing code.

Syntax :

class base class {

.... methods

class derivedClass extends baseClass {

methods ... along with this additional feature

Program :

class inheritance {
void salary() {
System.out.println("Salary= 200000");
}
}
class Programmer extends inheritance {
void bonus() {
System.out.println("Bonus=50000");
}
}
class single_inheritance {
public static void main(String args[]) {
Programmer p = new Programmer();
p.salary();
p.bonus();
}
}

You might also like