Array in java using scanner to accept integer

//using scanner class
import java.util.*;
class ex_7
{
    public static void main(String[]args)
    {
        Scanner sc=new Scanner(System.in);
        int i;
        int n[]=new int[10];
       
        for(i=0;i<10;i++)
        {
            System.out.println("Enter the number:"+(i+1)+" :");
            n[i]=sc.nextInt();
        }
        //now displaying the number
        System.out.println("the number are:");
        for(i=0;i<10;i++)
        System.out.print(n[i]+"\t");
    }
}

Array in java using argument for integer, character and string

class ex_4
{
    public static void main(int m[])
    {
        int i,sum;
        sum=0;
        int l=m.length;
        for(i=0;i<l;i++)
        sum+=m[i]; 
        System.out.println("Length of the array: "+l);
        System.out.println("the sum of the array elements ="+sum);
    }
}

output

Length of the array: 5
the sum of the array elements =15


class ex_5
{
    public static void main(char ch[])
    {
        int i;
        int l=ch.length;
        for(i=0;i<l;i++)
        System.out.println(ch[i]);
    }
}


class ex_6
{
    public static void main(String n[ ])
    {
        int i;
        int l=n.length;
        System.out.println("The names are:");
        for(i=0;i<l;i++)
        System.out.println(n[i]);
    }
}

Array in java using assignment for String

//assignment for string
class ex_3
{
    public static void main(String[]args)
    {
        int i;
        String n[]={"Sid","Shivam","Anay","Rahul","Trisha"};
        for(i=0;i<5;i++)
        System.out.println(n[i]);
    }
}

output

Sid
Shivam
Anay
Rahul
Trisha

Array in java using assignment for characters

//using assignment for character
class ex_2
{
    public static void main(String[]args)
    {
        int i;
        char ch[]={'I','N','D','I','A'};
        for(i=0;i<5;i++)
        System.out.println(ch[i]);
    }
}

output

I
N
D
I
A

Array in java use of length command

//by using assignment statement for integer
// use of length command
class ex_1
{
    public static void main(String[]args)
    {
        int i,sum;
        sum=0;
        int m[]={2,3,4,5,6}; //0 index 2  1 index 3  2 index 4
        int l=m.length;
        for(i=0;i<5;i++)
        sum+=m[i];  //sum=2+3+4
        System.out.println("Length of the array: "+l);
        System.out.println("the sum of the array elements ="+sum);
    }
}

output

Length of the array: 5
the sum of the array elements =20

Wap to accept three different single digit numbers between 1 and 9 (both inclusive). Display the greatest the greatest and smallest three digit number.

//Wap to accept three different single digit numbers between 1 and 9 (both inclusive).
//Display the greatest the greatest and smallest three digit number.
/* input = 2,7,4
 * output = Greatest three digit number =742
 * Smallest three digit number = 247
 */
import java.util.*;
class number
{
    public static void main(String[]args)
    {
        Scanner sc=new Scanner(System.in);
        int a,b,c,gn=0,sn=0;
        System.out.println("enter the first number between 1 and 9:");
        a=sc.nextInt();
        System.out.println("enter the second number between 1 and 9:");
        b=sc.nextInt();
        System.out.println("enter the third number between 1 and 9:");
        c=sc.nextInt();
        if((a>b)&&(a>c))
        {
            if(b>c)
            {
                gn=100*a+10*b+c;
                sn=100*c+10*b+a;
            }
            else
            {
                gn=100*a+10*c+b;
                sn=100*b+10*c+a;
            }
        }
        if((b>a)&&(b>c))
        {
            if(a>c)
            {
                gn=100*b+10*a+c;
                sn=100*c+10*a+b;
            }
            else
            {
                gn=100*b+10*c+a;
                sn=100*a+10*c+b;
            }
        }
        if((c>a)&&(c>b))
        {
            if(a>b)
            {
                gn=100*c+10*a+b;
                sn=100*b+10*a+c;
            }
            else
            {
                gn=100*c+10*b+a;
                sn=100*a+10*b+c;
            }
        }
        System.out.println("Greatest three digit number:"+gn);
        System.out.println("Smallest three digit number:"+sn);
    }
}
               

OUTPUT

enter the first number between 1 and 9:
5
enter the second number between 1 and 9:
7
enter the third number between 1 and 9:
4
Greatest three digit number:754
Smallest three digit number:457

ESCAPE SEQUENCE IN JAVA

EXAMPLE - 1

/*created by hra
in bluej platform
class 9
roll no. 7*/
class escape_ex
{
    public static void main(String[]args)
    {
        System.out.println("different ways of using backslash n :");
        System.out.println("1. the use of backslash n along with the message");
        System.out.println("Name: Sanjay Kumar \n\n" + "Age: 16 yrs \n" + "Class: X");
        int a = 14, b=26; // here a is used to store value 14 and b is storing 26
        System.out.println("2.The use of backslash n seperated by '+' ");
        System.out.println("First number ="+a+"\n"+"Second number ="+b+"\n"+"Sum="+(a+b));
    }
}

OUTPUT-

different ways of using backslash n :
1. the use of backslash n along with the message
Name: Sanjay Kumar

Age: 16 yrs
Class: X
2.The use of backslash n seperated by '+'
First number =14
Second number =26
Sum=40


EXAMPLE - 2

class escape_ex1
{
    public static void main(String[]args)
    {
        System.out.println("Different ways of using backslash t :");
        System.out.println("1. The use of backslash t along with the message");
        System.out.println("Name :Sanjay Mittal \t\t"+"Age:\t\t"+"16\t"+"years");
        System.out.println("2. The use of backslash t seperated by '+' and within double quotes");
        System.out.println("Name : Sammy Sarkar"+"\t\t"+"Age:"+"\t\t"+"16\t"+"years");
    }
}

OUTPUT

Different ways of using backslash t :
1. The use of backslash t along with the message
Name :Sanjay Mittal Age: 16 years
2. The use of backslash t seperated by '+' and within double quotes
Name : Sammy Sarkar Age: 16 years


Rabindra Jayanti Celebration 2020

https://youtu.be/je1fQENX0yk

https://youtu.be/3RUDVTN8L5g

https://youtu.be/bKAGPe3Wqgc

https://youtu.be/CpC9ROIU_DU

https://youtu.be/Hg_uoUTVgSI

https://youtu.be/z1dHXZ1YZC4

https://youtu.be/qekTuwHudAQ

https://youtu.be/B8Z4EC1vFpo

https://youtu.be/kqf4R8zcNHM

https://youtu.be/QVb_DyFGzsY

https://youtu.be/09TFqt0850g

https://youtu.be/BCyNHJ3QrwU

https://youtu.be/paTU0bnt_i0

https://youtu.be/E2_hsDSGntk

https://youtu.be/I7A0Yrgq7k8

https://youtu.be/JpZMuPRB7l0

https://youtu.be/95ZaAuzy8V0

https://youtu.be/hmHG1kTBT_I

https://youtu.be/0rr15moCiYo

https://youtu.be/O1jpi6wYyc0

https://youtu.be/cq9imJbEDbU

https://youtu.be/mk9ud2M2DdI

https://youtu.be/saDbZlbT2DA

https://youtu.be/t2xEl5U5J8I

https://youtu.be/IvqkH8EU5ws

https://youtu.be/l20vYFXNG7s

https://youtu.be/AB5SQT5m7fo

https://youtu.be/2_xlQFDD80g

https://youtu.be/VjTCVYqbNIc

https://youtu.be/aX9a9yLqkkc

https://youtu.be/D6FNIBpRJnI

https://youtu.be/jDeJN2wr17s

https://youtu.be/WoPpk9pYE1A

Chapter -1 CONCEPT OF OBJECTS

Chapter 1: Concept of Objects
Practice Questions
------------------------------>Objective-Type Questions<-----------------------------
A. Choose the correct answer:
1. An object is represented by two attributes, out of which one is characteristics and the other
one is ___________.
a) Behaviour b) Situation c) Abstraction d) Encapsulation
Ans. a) Behaviour
2. Name the programming technique that implements programs as an organized collection of
interactive objects.
a) Procedural Programming b) Modular Programming
c) Object-Oriented Programming d) None of these
Ans. c)Object-Oriented Programming
3. Name the characteristics of Object-Oriented Programming that hides the complexity and
provides a simple interface.
a) Encapsulation b) Polymorphism c) Abstraction d) Inheritance
Ans. c) Abstraction
4. What is the behaviour aspect of an object represented by?
a) Functions b) Data Members c) Both a and b d) None of these
Ans. a)Functions
5. What is the ability of an object to have many methods which functions differently but have the
same name?
a) Polymorphism b) Encapsulation c) Abstraction d) Inheritance
Ans. a)Polymorphism
6. Name the object-oriented technique that encompasses the parent class state and behaviours
into its child.
a) Polymorphism b) Encapsulation c) Abstraction d) Inheritance
Ans. d)Inheritance
7. Name the art of implementing encapsulation in Object-Oriented Programming.
a) Polymorphism b) Encapsulation c) Abstraction d) Class
Ans. d)class
8. What is meant by state of an object?
a) Functions of the object b) Data members of the object
c) Content of an object d) All of these
Ans. c) Content of an object
9. Name the term used to express the ability of objects to interact with each other.
a) Message Passing b) Instantiation c) Logical construct d) All of these
Ans. a)Message Passing
B. Fill in the blanks:
1. An object is an identity with certain characteristics and behaviour.
2. The values/attributes of the characteristics of an object are called the state of an object.
3. All the complexities of a program should be hidden in such a way that abstraction is obtained.
4. Class is a mechanism to implement encapsulation.
5. Objects interact with each other with the help of Message Passing.
6. A huge program divided into several individual segments is called functions/methods.
7. The objects methods/functions are said to access data.
8. A class is a logical construct and an object is a physical reality.
9. All objects have identity and are distinguishable even if the constituent components are same.
----------------------------->Subjective-Type Questions<-----------------------------
Answer the following questions:
1. Give two examples of real-world objects. Also specify their characteristics and
behaviour.
Ans. Real Life Objects
Example1:
Object Name: Pen
Characteristics: Company made, Colour, Body Shape and Nip.
Behaviour:
1) Used for writing.
2) Used for correction
3) Used for drawing or sketching
Example 2:
Object Name: Dog
Characteristics: Name, Colour and Breed.
Behaviour:
1) Barking
2) Wagging its tail
3) Running speed
2. What do you understand by state of an object? Explain with an example.
Ans. State of an object refers to the condition in which an object is in. Thus the values/attributes of its
characteristics is represented For example, a book is an object, which may be either in “open” or
“closed” state, similarly a bulb may be in switched on or switched off state.
3. How are objects implemented in Software?
Ans. Every object in real life has certain characteristics and behavior. It is the characteristic that is
represented by data variables and behaviour by methods or functions encapsulated in a class using a
software.
4. What is abstraction? How is encapsulation related to it?
Ans. Abstraction is the process of hiding the complexity and giving a simple interface and encapsulation
is the process of implementing abstraction.
Using encapsulation, the data and the code is wrapped round inside a class.

5. Define encapsulation. [ICSE 2006]
Ans. Encapsulation is the process of wrapping/combining the characteristics and behavior into a single
unit of an object to form a unique identit.
6. Explain the term object using an example. [ICSE 2006]
Ans. An object is an entity with certain characteristics and behaviour making it having its own
individuality and distinguishable. For example a pen has certain characteristics, for example its colour,
shape, size, etc. and behaviour which is used to write, sketch or draw, etc. Even though two pens are
identical with the same colour, texture, shape and size, yet they are two distinguishable pens.
7. What is a class?
Ans. A class is a method to implement encapsulation is programming. It is the class keyword in Java
that is used to wrap around the data members and member methods into a single unit. It acts as a
template that allows instances of it to be created, which are called objects.
8. What is Message Passing?
Ans. When the objects need to interact with one another, they pass or request information to or from
one another. This interaction is knows as message passing.
9. What is Object-Oriented Programming?
Ans. Object Oriented Programming is a technique of implementing programs which are organized as a
co-interactive collection of objects, each of which represents an instance of a class.
10. State the 4 characteristics/principles of Object-Oriented Programming. [ICSE 2005]
Ans. The 4 characteristics/principles of Object Oriented Programming are:
i) Abstraction
ii) Encapsulation
iii) Polymorphism
iv) Inheritance
11. Explain briefly:
a) Abstraction b) Inheritance c) Polymorphism
Ans. a) Abstraction: Abstraction refers to the art of hiding the complexities and giving a simple
interface. For example in a car, for a layman, it is good enough to know driving. Hardly will he
know the intricacies of movement of the engine, the working of the electrical and electronic
components; what he will know or will be interested with is the steering for turning, the accelerator
for speed and the switches. This is because he has been given a simple interface and the complexity
of the car engine movement has been kept completely hidden from him. Thus he doesn’t bother
about the intricacies of the car’s engine movement, but concentrates only on driving.
b) Inheritance: Inheritance is the ability to inherit characteristics from another object. In more
concrete terms, an object is able to pass on its state and behaviours to its children. For inheritance
to work, the objects need to have characteristics in common with each other. It is basically a term
that is used to represent hierarchical relationship of generalization.
c) Polymorphism: The term Polymorphism is actually a combination of two terms
Poly and Morphism. Here the term Poly means many and Morphism means forms. It is the ability of
objects to have many methods of the same name, but each one responds to different types of specific
behaviour as they have different forms.
12. How does class acts as a blueprint for an object?
Ans. A class acts as a blueprint or template that defines certain characteristics and behaviour and an
object is an instance or copy of a class.
13. How does encapsulation acts as a protective layer for an object?
Ans. Encapsulation is a technique that binds together function and the data into a single unit. You can
imagine it to be as a protective wrapper that prevents the code and data from being accessed by other
codes defined outside the wrapper.
14. How does modularity helps in reducing the complexity of a program?
Ans. Modularity helps in breaking a huge task into smaller units called functions for easy
maintainability and reusability thus reducing the complexity of the task at large.
15. What do you understand by data abstraction? Explain with an example. [ICSE 2006 & 2010]
Ans. Data abstraction refers to, providing only essential information to the outside world and hiding
their background details, i.e., to represent the needed information in program without presenting the
data.
Let's take one real life example of a TV, which you can turn on and off, change the channel, adjust the
volume, and add external components such as speakers, VCRs, and DVD players, BUT you do not know
its internal details, that is, you do not know how it receives signals over the air or through a cable, how it
translates them, and finally displays them on the screen.
16. Give a real world explanation about abstraction.
Ans. A switch board is an example of abstraction. Imagine if the concept of switchboard did not exist.
You would find a great number of wires hanging round your room. To start with an appliance, you
needed to join two wires. But essentially you need to know which group of wires should be joined to
start an appliance. Moreover, more the number of wires, more will it be confusing to identify the wires
to start an appliance. Thus joining of wires is not only confusing but may also prove fatal as far as safety
is concerned. Thus, the electrician installs a switchboard that connects each of the wires to a switch.
Hence, it is just enough to know for the user to switch on that switch that starts an appliance.
17. Why is abstraction often referred to be as relative?
Ans. Abstraction is often referred to as relative because abstraction is the selective examination of
certain aspects of a problem. The goal of abstraction is to isolate those aspects that are important for
some purpose and suppress or hide those aspects that are unimportant. Abstraction must always be for
some purpose, because the purpose determines what is important and what is not. Many different
abstractions of the same thing are possible, depending on the purpose for which they are made.

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...