Professional Documents
Culture Documents
Arithmetic Operator
Syntax :
op1 + op2
op1 - op2
op1 * op2
op1 / op2
op1 % op2
class add {
Assignment Operator
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
a. Simple if
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
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
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
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 :
class as10 {
int rollNo = 19;
String name = "abhishek";
String Class = "bca";
String Address = "satara";
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
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; )
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
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 :
obj1 = null;
obj2 = null;
System.gc();
Program :
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 :
.... methods
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();
}
}