Java Tech Lounge
Java Tech Lounge
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
1.1
1.2
1.3
https://campuscommune.tcs.com/communities/java-lounge/content/it-application-overview-and-features-0
1 / 293
Campus Commune
7/31/2014
1.4
1.5
https://campuscommune.tcs.com/communities/java-lounge/content/it-application-overview-and-features-0
2 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
Figure 1
Or if you are reading any posts with your tablet,
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-to-application-understanding-0
3 / 293
Campus Commune
7/31/2014
Figure 2
You are using Applications/App.
So what exactly does an Application mean?
An application is a type of software that allows you to perform specific tasks which otherwise is very difficult to perform manually.
Before we go any further think about few computer applications you are familiar with.
Sticky Notes
You can use Sticky Notes to write a to-do list, jot down a phone number, or to do anything else that you would you use a pad of paper for.
Figure 3
Windows Notepad
Notepad is a basic text editor that you can use to create simple documents.
Figure 4
Whatsapp
WhatsApp Messenger is a proprietary, cross-platform instant messaging subscription service for smart phones.
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-to-application-understanding-0
4 / 293
Campus Commune
7/31/2014
Figure 5
Facebook Application
A social utility that connects people with friends and others who work, study and live around them
Figure 6
IRCTC Portal
Online Passenger Reservation System provides booking facility of Railway tickets online
Figure 7
By giving a closer look to these applications you will find that few of the applications are specially designed for Desktop/Personal computer while
few others only work on mobile devices. Similarly you will find that in order to run certain other applications you need a web browser like Internet
Explorer, Chrome etc.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
PRIYANKA
I have just submitted my quiz and on the last question ie. 20th question it was showing 19 out of 20 and when i have
submitted the quiz my score was 70 instead of 78. I cannot understand what actually has happened. Suggestions are highly
helpful.
about 2 months ago
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-to-application-understanding-0
5 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/different-types-of-applications-with-examples-0
6 / 293
Campus Commune
7/31/2014
Figure 8
Figure 9
Mobile Applications
Desktop and laptop computers aren't the only devices that can run applications. You can also download applications for mobile devices like smart
phones and tablet computers, which opens up a lot of new possibilities. Here are a few examples of mobile applications:
Google Wallet : is a mobile payment application
Figure 10
ICICI Mobile Banking Application
Figure 11
Web Applications - are applications that are accessed over a network such as the Internet or an Intranet.
Eg : ICICI Net Banking application
https://campuscommune.tcs.com/communities/java-lounge/content/different-types-of-applications-with-examples-0
7 / 293
Campus Commune
7/31/2014
Figure 12
Passport Seva Portal to deliver passport services to citizens in a timely transparent manner.
Figure 13
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SUMANTA SAHA
According to above study material, "Web applications are applications that are accessed over a network such as the Internet
or an Intranet". Does that make every website a web application?
about 23 days ago
HARIPRASAD R
Not necessarily. Only a site that performs a particular work. A simple static site cant be taken as an application,
ABDUL HAQ
Some of the apps on mobile run based on internet such as Whatsapp, Facebook messanger etc.,. Are such apps called as
Mobile Applications or Web Applications?
about 2 months ago
https://campuscommune.tcs.com/communities/java-lounge/content/different-types-of-applications-with-examples-0
8 / 293
Campus Commune
7/31/2014
KALYAN PILLA
Web applications are applications with a web interface. Think of it this way- Thunderbird is a Desktop
application, gmail(web-mail) that you access through a browser is a Web application and the gmail app on
your phone is a Mobile application. All the three perform the same task of delivering your mail from a remote
server to you.
The above classification is NOT based on what an app does or how it works, rather on the platform on which an
application works.
ABDUL HAQ
What is the difference between internet and intranet? If there is any difference between them, please elaborate them.
about 2 months ago
APURV KUMBHARE
http://compnetworking.about.com/cs/intranets/g/bldef_intranet.htm
Refer this link..
VEERABHADRASWAMY BODA
internet world wide collection of inter collected network whereas intranet is group of computers connected to use
internet . Both use TCP/IP protocol . But the range varies intranet is limited to those group of computers .
Intranet provides security .
ALURI REDDY
VINDHYA BODDUPALLI
facebook is a web application
SAI YERIGERI
facebook messenger and whatsapp are mobile based applications, because they work only on mobile.
SREE SETTURU
facebook is a mobile application. nw we have a seperate facebook app like whatsapp
UDAYADITYA BORUAH
Facebook, in the general falls into both the categories of mobile as well as web applications. If it is used as a
https://campuscommune.tcs.com/communities/java-lounge/content/different-types-of-applications-with-examples-0
9 / 293
Campus Commune
7/31/2014
mobile app on the smartphones,it is a mobile application. However when accessed with the desktop or laptop it is
a web application.
KALYAN PILLA
Facebook is an online service that allows you to socialize and connect with people. You could access this service
using
A Mobile application (facebook app)
The Web application (via https://www.facebook.com or https://m.facebook.com on you desktop, laptop,
mobile or any other Web connected Device)
Some Desktop application (eg. facebook XMPP on pidgin)
ROHAN DUTTA
FACEBOOK is a web application because we are using an web browser where we have to provide an url.
But if you are using FACEBOOK MESSENGER or FACEBOOK APP from your smartphone or tablet then it is
a mobile application.
JITENDRA CHOUDHARY
As MOBILE APP : If one has installed Facebook App in his mobile phone/handset/tablets and accessing the
facebook from there, then it is an MOBILE APP. Mobile app is developed in the native programming languages
like Java, C++. Also these days, mobile apps can be developed with HTML5 ans its sets of API provided by the
mobile OS. Mobile apps use REST or SOAP APIs to communicate with the server.
As Web App : When we are accessing the facebook from web browsers like Internet Explorer, Google Chrome,
then it is an web app. We are typing in broser's address bar http://www.facebook.com to access and use it. In
this case at its front-end, UI(user-interface) is built using HTML,CSS, and JavaScript.
In mobile app case, we will not see the domain facebook.com directly anywhere, we directly interact with app
after login, all domain bases internet access is being done by the mobile app internally. All facebook does, is
providing data in each version of app.
https://campuscommune.tcs.com/communities/java-lounge/content/different-types-of-applications-with-examples-0
10 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/basic-application-architecture-0
11 / 293
Campus Commune
7/31/2014
Figure 14
Presentation Layer / User Interface layer
The top most layer of the application through which end-user interacts with the system. The main function of this layer is to translate the tasks and
results to something which the end user can understand.
Business layer
This layer implements the core functionality of the system, and encapsulates the relevant business logic.
Data/Resource layer
This layer stores and retrieves information from the storage systems (databases/file systems). The information is then passed to the Business layer
for processing and then eventually back to the user.
Layers and Tiers of an Application
Layer
Irrespective of the type of application that you are designing, and whether it has a user interface or it is a services application that only exposes
services (not to be confused with services layer of an application), you can decompose the design into logical groupings of software components.
These logical groupings are called layers. Layers help to differentiate between the different kinds of tasks performed by the components, making it
easier to create a design that supports re-usability of components.
Benefits of layers:
Simplicity: easy to design once layers and their interaction are defined clearly
Flexibility: easy to modify and develop networks by separate layers modifications
Incremental changes: add new layers, add new functions to a layer
Tier
When the logical groups of components are physically separated in different machines, it is known as Tier. The communication between the
different tiers of an application is through network.
Single/One Tier Architecture All layers (Presentation, Business and Resource) are at same place. The application does not require network
connection to work. It is easy to install in a system and use it.
Figure 15
Eg: MS Access, MS Excel
2- Tier Architecture - In 2 tier architecture there can be 2 possibilities
1. Presentation Layer in one machine and Business & Resource Layer is available in different machine
2. Presentation & Business layer in one machine and Resource layer in another machine.
https://campuscommune.tcs.com/communities/java-lounge/content/basic-application-architecture-0
12 / 293
Campus Commune
7/31/2014
Figure 16
3 Tier Architecture In this all three layers of an application are physically separated in different machines.
Eg : Most of the web applications which involves data storage are 3 -tier architecture applications.
Figure 17
The below figure will help you in visualizing the communication in a web application.
In this example a 3-tier web application system is represented where all three layers of the application is distributed in three different machines.
The Presentation layer is at the client tier and from there request is sent to the Business layer and after processing the request its again sent to
Storage/Resource tier for storing.
Figure 18
Multi-tier/ N-tier Architecture: All the 3 -tiered applications can be again physically separated by breaking it into different tiers and establishing
a communication between all those tiers.
The terms tier and layer are often used interchangeably. When I use tier, I am referencing a physical layer. When I use layer, I am referencing a
distinct software layer. Layers must exist on a tier, and thus tiers contain layers.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SUMANTA SAHA
The above study material states, "Layers must exist on a tier". Is it possible for a tier to contain more than one layer?
https://campuscommune.tcs.com/communities/java-lounge/content/basic-application-architecture-0
13 / 293
Campus Commune
7/31/2014
VELANTISH V.
Moderator
Layers are a logical separation and tiers are a physical separation. Which means in a layers architecture the
presentation, business logic and data layer are on the same machine where as in tiers they are all located on
different physical machines.
HEMANT MAHANT
What are the examples of system having 2-tier architecture.?
about 25 days ago
ALURI REDDY
The UNIX print spooler is an example of a two-tier client-server architecture.
The client reads a file to be printed and passes the file's contents to the server. The server performs a service by
printing the file.
PALAK PARHI
logical grouping of components? what does components actually refer to?
about 2 months ago
GAMPA SOWMYA
Component actually refers to a reusable part of the software.
ABHISHEK SAH
As stated these are "software" components which are blocks of of instructions(code) each for a different purpose.
Similarly their are hardware components such as mouse,CPU , Display etc.
ANUJ PODDAR
Elements that encapsulate processing and data in a systems architecture are referred to as software
components.
A software component is an architectural entity that
encapsulates a subset of the systems functionality and/or data
restricts access to that subset via an explicitly defined interface
has explicitly defined dependencies on its required execution context
Components typically provide application-specific services.
Put another way, a software component is a locus of computation and state in a system.
https://campuscommune.tcs.com/communities/java-lounge/content/basic-application-architecture-0
14 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
Create
https://campuscommune.tcs.com/communities/java-lounge/content/basic-application-functions-crud-0
15 / 293
Campus Commune
7/31/2014
Lets take example of well-known website Google. When you first time register or create an account in this website, you have to fill certain details
and click on register or sign up button. On clicking on the button, information entered by you will be verified and added into the database and your
account will be created. This is one of the example for CREATE operation.
Similarly, adding a contact in gmail application of your Google account is also an example of create operation.
You can find these kind of options in many other websites. Creating your Facebook, twitter, IRCTC account, registering in naukary.com, even
creating an event in your Facebook account are all examples of create operation.
Whenever a new set of information is added into the database of the system, then it is a create operation.
It results in creation of new entry into the database of that web application.
Read
Lets take the example of Google again. When you log in to your Google account, a home page appears. It is a read operation. In this, the web
application retrieves the information from the application's database, corresponding to the log in credentials which you have provided and displays
it.
Viewing your profile by clicking on view profile is also an example of read operation.
When you are searching a person by their name, it is also a read operation. In this the web application will search for various user profiles with that
particular name and displays the list.
Even viewing your contact list of gmail is an example of read.
https://campuscommune.tcs.com/communities/java-lounge/content/basic-application-functions-crud-0
16 / 293
Campus Commune
7/31/2014
You can relate these examples with options being provided by many other social networks and web applications.
It shows that, read is basically, retrieving data from the database and displaying it to the user.
Update
Think, you want to change some of your basic information in your Google account, then, you will click on the edit button, which will take you to the
edit profile page. It will display all the information which you have already added. You can enter the new information and by clicking on save, the
information will get updated. Old information gets overwritten by the new information in the database.
Editing the contact details in gmail account is another example of update operation.
First time while filling the information, you might have left few non-mandatory fields blank. Filling those information later is also an example of
update operation. Even removing few information (not all) like, phone number from your profile is also an example of update. Here the particular
information will be set to blank.
Basically, update is, first a read operation, which displays the information which is already present in the database of the web application and then
overwrites it with a new information.
Delete
Let us take the example of contacts in gmail. If you want to delete a contact from the contacts list, you can select the particular contact and click on
the delete button. It will delete the contact from the contacts list.
https://campuscommune.tcs.com/communities/java-lounge/content/basic-application-functions-crud-0
17 / 293
Campus Commune
7/31/2014
If in future, you do not wish to use the account which you have created, then you can go for deactivation of the account. This is also an example of
the delete operation.
Now a day most of the websites do not provide direct deletion, they allow you to just deactivate the account. Few websites even have facility to
delete the account.
Delete is basically, deleting the information from the database of the website or making the information inaccessible.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
GANGISETTY VS CHANDANA
Ithink its update oeration performed Whenever a new set of information is added to the database of the sysytem ,as we are
updating the data base with new features but its given as create what is the right answer?
about 26 days ago
YUKTI
Best Answer
Whenever we make any changes to already existing information that is referred to as an UPDATE,while adding
some new information is CREATE.
SUMANTA SAHA
UPDATE is used to modify a single record in a database not the whole database. UPDATE is not applicable for the
whole database. In order to insert a new record(which you meant update) CREATE operation must be
performed.
SUMAN SINGH
When you are creating an entry, you are simply adding a new information or a new entry to the database. Where
as updating refers to changing the values of existing entries.
RIYANKA DIWAKAR
How many courses are there in java lounge?
about 1 month ago
KARTHIK ND
Why most of the websites are not providing direct deletion?
about 2 months ago
ALAMELU N
https://campuscommune.tcs.com/communities/java-lounge/content/basic-application-functions-crud-0
18 / 293
Campus Commune
7/31/2014
Without reading(READ) the information that you want to delete,you cant delete(DELETE) the information.So
delete cannot be performed directly !
PAVAN K
If you perform the direct deletion that content in your account for suppose is a redundant one then it will cause a
user dilemma in selection of content so a read and the next step of deletion makes the best output and its a
reliable method of deletion
UDAYADITYA BORUAH
The point of not providing direct deletion makes the account created by the user to be again accessible in the
nearby future if he/she wishes to. This prevents repeated space allocation to a single user in the database, thereby
preventing complications, if any.
SUMANTA SAHA
PAVAN K, could you please explain about the redundat data you mentioned?
https://campuscommune.tcs.com/communities/java-lounge/content/basic-application-functions-crud-0
19 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
2.1
https://campuscommune.tcs.com/communities/java-lounge/content/need-of-programming-and-introduction-to-oop-approa
20 / 293
Campus Commune
7/31/2014
2.2
2.3
2.4
2.5
Elements of Programming
2.6
Characteristics of an object
2.7
2.8
https://campuscommune.tcs.com/communities/java-lounge/content/need-of-programming-and-introduction-to-oop-approa
21 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-to-problem-solving-approach-0
22 / 293
Campus Commune
7/31/2014
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-to-problem-solving-approach-0
23 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/step-by-step-approach-to-problem-solving-0
24 / 293
Campus Commune
7/31/2014
iii) Implementation
Implementation means acting on the chosen solution .This phase deals with the actual process of implementation of our design. In terms of
programming, the code would follow the design and then write code to translate the human-understandable design to a machine understandable
language. During implementation more problems may arise especially if identification of the original problem was not carried out fully.
Now once we have the inputs, we need to take the following steps
1.
2.
3.
4.
Boil water
Add tea-leaves
Add sugar
Add milk
We will instruct the machine to perform these steps sequentially. The machine understands this and using the inputs that were provided to it, will
solve the problem.
Finally the outcome of these steps will be tea and in the machines terms, this is called as the output.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
ANKITA SHEKHAR
What is design or a plan? Code, document, blueprint or a pictorial outline?
about 23 days ago
MOHIT SRIVASTAVA
Design is basically the outlay that how we are going to tackle the problem, let me quote an example if a bowler is
bowling a bouncer then the batsman have some shots to tackle this kind of delivery, these so called "some
shots" here is a plan of facing a bouncer ball.
https://campuscommune.tcs.com/communities/java-lounge/content/step-by-step-approach-to-problem-solving-0
25 / 293
Campus Commune
7/31/2014
now coming to your question it can be document, pictorial outline(flow chart) or a code(module functionality will
be defined)
VELANTISH V.
Moderator
Design is a roadmap or a strategic approach for someone to achieve a unique expectation. It defines the
specifications, plans, parameters, activities and processes.
Let us consider an anology of constructing a building, before even starting with the actual construction we have a
blue print (called the plan i.e nothing but the design) which would state whether the contructed house is 2BHK or
3BHK. Where is the entrance of the room and passage. Because if there are any modifications to be done at that
point in time it can be done easily with less cost and time. Where as just imagine the to be done after/during
construction there there would be lot of cost, time and effort incurred. Similar is the case with software
development and the importance of design.
Design consists of components, classes and sequence in which it has to be implemented to arrive at the results.
This is represent by UML (Unified Modelling Language).
PRIYANKA SAWANT
What is the right ans???
MOHAMMED RAHIMAN
question:: what is done in 4th step of problem solving??
I answered TESTING......is it wrong answer????
similarly ,what is done in 3rd step of problem solving???
I answerd CODING...is it really wrng answer?????
about 2 months ago
BHAVYA PULIPATI
I also faced the same problem
PRABHAKARAN
It seems that in the question answers, they have included a step in between Identifying possible solutions and
Making Decisions and implementation namely testing the design. One of the reference links also has it like that.
PRIYANKA SAWANT
faced the same problm ..fourth step is implementation..is also wrong
SUVIGYA MITTAL
so whats the correct ans???
SAI KANUPARTHI
Same problem for me too.. This question repeated for me twice and second time I have not attempted it so to
escape from negative marking.
SOURAV CHAKRABORTY
in one of aspire modules, they listed seven steps of problem solving...its confusing
MADDI
Third step is implementing the solution.... not just coding.
https://campuscommune.tcs.com/communities/java-lounge/content/step-by-step-approach-to-problem-solving-0
26 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
27 / 293
Campus Commune
7/31/2014
iii) Output
Make reasonable predictions about the extent of rain next year.
This is an example of using analytical skills.
Declarative and Imperative Knowledge
i) Declarative knowledge is factual knowledge or the knowledge of the solution. For example knowing that "A pen is used for writing" is a
declarative knowledge. Declarative knowledge is knowledge or the possession of information that is either true or false. It describes objects and
events by specifying the properties which characterize them; it does not pay attention to the actions needed to obtain a result
Take an example of a jigsaw puzzle
28 / 293
Campus Commune
7/31/2014
A representation or model that includes the important, essential or distinguishing aspects of something while suppressing or ignoring less
important, immaterial or diversionary details is called as Abstraction. Hence abstraction is a way of managing problem complexity by focusing on
the essential details and ignoring lower level details.
An example can be taken of a refrigerator
As a user, we just need to know that a fridge can be used to preserve food materials by freezing them. Hence the user just needs to open the fridge
and put the items inside.
User does not need to know the internal functioning of the fridge, hence the wiring,electronics and other details are hidden behind the body of the
fridge. This process of hiding the irrelevant data is called as Abstraction.
Abstraction types
i) Functional Abstraction
Let see the above refrigerator example, all refrigerator contains a freezer box which can preserves the food material below zero degree
Celsius temperature. As a user we need to get the food material to be preserved but we don't need how it will be get preserved , this way of
hiding
irrelevant details or process and getting the functionality of preservation to be done is an example of functional abstraction.
ii) Data Abstraction
Let see the above refrigerator example,the inner temperature of the refrigerator is an essential data which is sensed by the sensor and refrigerator
monitors and controls the inner temperature. Here inner temperature is the internal data which is used by the refrigerator to maintain the
temperature of the fridge, which is not controlled or altered by external mechanism and is used by the functionalities of the refrigerator is an
example of data abstraction.
Knowledge in programming language
https://campuscommune.tcs.com/communities/java-lounge/content/key-elements-required-for-problem-solving-0
29 / 293
Campus Commune
7/31/2014
A programming language is a computer language programmers use to develop applications, scripts, or other set of instructions for a computer to
execute.
Programming languages can be used to create programs that control the behaviour of a machine and/or to express algorithms precisely.
Thousands of different programming languages have been created, mainly in the computer field, with many more being created every year.
Elements of a programming language are i) Syntax: structural elements of the language. Programs must be syntactically correct.
Eg: English as a language has got a certain syntax. We have predefined syllables and words like 'a' or 'cup' or 'welcome'. These words convey a
meaning to the language.
ii) Grammar: defines how syntactical elements need to be combined to form programs
Eg: Continuing the same example, English language has got a certain grammar. The grammar has to be followed to construct a proper
sentence. 'I is a boy' is grammatically wrong although syntactically correct.'I am a boy' is syntactically and grammatically correct.
iii) Semantics: defines meaning of the code
Eg: Continuing the same example, English language has got a certain semantics. 'An elephant is white and has got two legs' is syntactically ,
grammatically correct. But it doesnt make any sense. Hence it is semantically wrong.
https://campuscommune.tcs.com/communities/java-lounge/content/key-elements-required-for-problem-solving-0
30 / 293
Campus Commune
7/31/2014
Superior coding techniques and programming practices are hallmarks of a professional programmer. The coding techniques are primarily those that
improve the readability and maintainability of code,whereas the programming practices are mostly performance enhancements.
ii)
The readability of source code has a direct impact on how well a developer comprehends a software system.
iii) Maintainability
Code maintainability refers to how easily that software system can be changed to add new features, modify existing features, fix bugs, or improve
performance
Although readability and maintainability are the result of many factors, one particular facet of software development upon which all
developers have an influence is coding technique. The easiest method to ensure that a team of developers will yield quality code is to establish a
coding standard, which is then enforced at routine code reviews.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/key-elements-required-for-problem-solving-0
31 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/evolution-of-programming-approaches-1
32 / 293
Campus Commune
7/31/2014
Non-maintainable
Reduced reusability
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/evolution-of-programming-approaches-1
33 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
34 / 293
Campus Commune
7/31/2014
possible.
Object Oriented Programming Approach
We write programs that make up software to solve a problem in real world. In real world problem domains we do not find any functions that work
on data. We find objects that are described by some state (data) and having some behaviour (functions or routines).
Object Oriented (OO) Approach to programming is about implementing a system as collection of objects that have state and behaviours,
interacting with each other to achieve expected functionalities.
Example: C++ & Java
In OO programming, the program basic entity is object. An object is any real world entity that has a well-defined structure and behaviour.
Forget programming for a while. Think about the Real World and the things that are in it. What things are objects? What things are not objects?
Look around you. You may try to list five objects and list five non-objects.
It is easier to list things that are objects than to list things that are not objects. Just to talk about something seems to make it an object, somehow.
When we observe, the humans view the world in object-oriented terms. The human brain wants to think about objects, and our thoughts and
memories are organized into objects and their relationships. Perhaps non-human brains work differently.
One of the ideas of object-oriented software is to organize software in a way that matches the thinking style of our object-oriented brains.
Look at my list of objects and your own list and try to describe what all objects have in common. What makes an object?
An object is made of tangible material (the pen is made of plastic, metal, ink).
An object holds together as a single whole (the whole pen, not a fog).
An object has properties (the color of the pen, where it is, how thick it writes.).
An object can do things and can have things done to it.
The first item in this list is too restrictive. For example, you can think of your bank account as an object, but it is not made of material. (Although
you and the bank may use paper and other material in keeping track of your account, your account exists independently of this material.) Although
it is not material, your account has properties (a balance, an interest rate, an owner) and you can do things to it (deposit money, cancel it) and it
can do things (charge for transactions, accumulate interest).
The last three items on the list seem clear enough. In fact, they have names:
i. An object has identity (each object is a distinct individual).
ii. An object has state (it has various properties, which might change).
iii. An object has behavior (it can do things and can have things done to it).
iv. This is a somewhat ordinary description of what an object is like. (This list comes from the book Object-oriented Analysis and Design, by Grady
Booch, Addison-Wesley, 1994.) Do not be surprised if other notes and books have a different list. When you start writing object-oriented software
you will find that this list will help you decide what your objects should be.
Every object has its own characteristics. Every object has 5 sets of characteristics Viz. State, Behaviour, Responsibility, Communication and Identity.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/elements-of-programming-0
35 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
36 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/characteristics-of-an-object-1
37 / 293
Campus Commune
7/31/2014
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
MOUNIKA
In to how many phases can a checked algorithm can be programmed?
SAI KANUPARTHI
I think it depends on the number of steps in the algorithm.
PARIMALA PITCHIKA
Can I have examples of software objects?? Is the entire purpose of a program revolve around object?
about 2 months ago
HEMANKITA
For suppose if you have a class employee and you access it using an object e1 then e1 is a software object here.
HEMANKITA
For suppose if you have a class employee and you access it using an object e1 then e1 is a software object here.
PARIMALA PITCHIKA
Thank u all of u
https://campuscommune.tcs.com/communities/java-lounge/content/characteristics-of-an-object-1
38 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/classes-and-objects-0
39 / 293
Campus Commune
7/31/2014
Here Car is the template having the generic states and behaviours. The states and behaviour of the template are grouped in the following manner.
class Car {
int speed = 0; //state
int gear = 1; //state
// Behaviour
void changeGear(int newValue) {
gear = newValue;
}
// Behaviour
void speedUp(int increment) {
speed = speed + increment;
}
void applyBrakes(int decrement) {
speed = speed - decrement;
}
void printStates() {
System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear);
}
}
All various types of Cars which follow the generalization become Objects. Example: Maruti, Volkswagen, Audi, etc.,.
Basic difference between Programming Paradigms
Functional/procedural programming:
Program is a list of instructions to the computer
Object-oriented programming:
Program is composed of a collection objects that communicate with each other
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
RAJARAJESHWARI S
what is cadence in this program ? It is undefined , right??
about 19 days ago
https://campuscommune.tcs.com/communities/java-lounge/content/classes-and-objects-0
40 / 293
Campus Commune
7/31/2014
ABHISHEK SRIVASTAVA
System.out.print() is defined in java.io package but why we can use System.out.print() without importing java.io package
about 28 days ago
PREETHI PATTABIRAMAN
The System object has references to java.io.PrintStream objects embedded in it. So you don't need
to explicitly import these - the runtime can derive this information unambiguously since it was embedded at
compile-time. Note also (in case there's any confusion), java.lang is implicitly imported, hence you don't require an
import statement for System .
PALAK PARHI
what does the last function do?
about 2 months ago
MAHADEEP RAY
printStates function displays the values.
VELANTISH V.
Moderator
It displays in the console the current state of the car such as its speed and the gear(1st, 2nd or 3rd etc.) its
running with.
https://campuscommune.tcs.com/communities/java-lounge/content/classes-and-objects-0
41 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
Q
2.8. Reference Links & videos
Reference links
http://en.wikipedia.org/wiki/Problem_solving
http://www.mindtools.com/pages/main/newMN_TMC.ht
http://www.wikihow.com/Improve-Analytical-Skills
To know more on programming approach, visit http://www.maths.udsm.ac.tz/mujuni/mt512/Programming%20Approaches.pdf
Related Videos
https://campuscommune.tcs.com/communities/java-lounge/content/reference-links-and-videos
42 / 293
Campus Commune
7/31/2014
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SRIKAVYA PINGALI
Please help me out.....Marks are getting deducted even if I mark the correct option....!!!
End user requirements are understood in requirements analysis stage....I marked it as true....Marks got deducted.
In designing phase involves preparing document blueprint and pictorial design but not coding as per this material....In this
also I have my marks.....!!!
There are 2 other questions in the way where I have lost marks....!!!
Please suggest me where should I report about this as same happened with me in aspire also...!!!!
about 1 month ago
https://campuscommune.tcs.com/communities/java-lounge/content/reference-links-and-videos
43 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
3.Introduction to Java,JVM,JDK
Objective
Introduction to Java
History of Java
Brief to Java Platform, JVM, JDK, JRE
Installation of JDK and its configurations
Run a sample program
3.1
What is java
3.2
History of Java
3.3
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-to-javajvmjdk
44 / 293
Campus Commune
7/31/2014
3.4
3.5
3.6
3.7
Eclipse
3.8
Compileonline.com
3.9
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-to-javajvmjdk
45 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
46 / 293
Campus Commune
7/31/2014
Summary:
Java is platform Independent and Portable.
Java is Robust and secured.
Java is pure object oriented language.
https://campuscommune.tcs.com/communities/java-lounge/content/what-is-java-0
47 / 293
Campus Commune
7/31/2014
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SIVAKUMAR R
Is java really a pure object oriented language? If it is pure object oriented then its primitive data type should be objects,
which is not. Am i right? Correct me if i am wrong.
about 9 days ago
HARIPRASAD R
I think Java is not pure object oriented language.
about 11 days ago
ABHISHEK SAH
I think there is a typo in Step 2: Compile the Source Code: Compile the source code "Hello.java" into portable bytecode
"HelloWorld.class" .
It should be Step 2: Compile the Source Code: Compile the source code "HelloWorld.java" into portable bytecode
"HelloWorld.class"
SAIF PATEL
Its correct,
because the mane of the program is "hello". So it will be compile by the name of the class..
on compiling it will generate the class file with the name of the class specified in the program.
MAYANK
it is incorrect because the main method is in HelloWorld class. so the name of file should be HelloWorld.java
SUBRATA MAITI
It is write because the class is not public, so you can name the file anything. If the class is public class then name
should be as the class name. And always after compiling the .class should be same name as the classes present in
the .java file.
SATYANARAYAN K
I tried to execute the given code in my PC.I have java (jre7) installed. I am unable to execute as shown in the above
picture.Please help me out.Or do i need to install some other version of java and try it out?
about 29 days ago
THANGARAJ
install JDK7 and try it
KALYAN PILLA
Java Runtime Environment (JRE) is required to execute a java object file. In order to compile a java Source code
you will need Java Development Kit (JDK).
KARMANJOT
name of the file "hello.java" is given wrong. try saving the file as "HelloWorld.java".
https://campuscommune.tcs.com/communities/java-lounge/content/what-is-java-0
48 / 293
Campus Commune
7/31/2014
SATYANARAYANA MURTHY
can a compiler checks or detect run time errors?
about 1 month ago
SWATI SINGH
what is JRK? it also needs to be installed ?? plz reply
about 2 months ago
VEERABHADRASWAMY BODA
I think it's JRE not JRK . jre means java runtime enviroment which contains jvm,class libraries and other
supporting files.It doesn't contain any development tools like compiler etc. so inorder to run your program which
requires jvm and other class libraries jre must be installed in your computer
AMEYA TEMBHEKAR
What is a classpath and can you elaborate the declaration in the above program?
about 2 months ago
ABUL M
I cant understand about passive data, dynamic, active data clearly. Can anyone explain it with some other example
about 2 months ago
PARIMALA PITCHIKA
Passive data is you can only read pages .i.e.,static.
Active/dynamic having dynamic data ,filling forms and having hyperlinks which makes you turn to another pages.
https://campuscommune.tcs.com/communities/java-lounge/content/what-is-java-0
49 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/history-of-java-0
50 / 293
Campus Commune
7/31/2014
J2SE 1.4
J2SE 5.0
Java SE 6
Year of Release
Description
6-Feb-2002
30-Sept-2004
11-Dec-2006
Java Platform
JRE and JDK : See the JDK Section in detail
Java Programming Language:
Java Virtual Machines See the JVM Section in detail
Base Libraries
Integration Libraries
User Interface Libraries
Deployment
Java Deployment
Tool Specifications
JDK Tools & Utilities
Platforms
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
ALURI REDDY
What is the purpose of diamond box in java?
about 18 days ago
VELANTISH V.
Moderator
Diamond symbol in java is used for generics (<>). Generally collections in java can hold any type of object such
as employee, department, suppliers. But practically we would need to narrow down collection to hold strictly
objects of just a class. That is where generics comes into picture.
You can read more details while learning collections in java.
https://campuscommune.tcs.com/communities/java-lounge/content/history-of-java-0
51 / 293
Campus Commune
7/31/2014
PILLI
what is meant by type inference for generic instance creation?????
and also about java nio package??
M REDDY
what is JDBC 4.0 ??
about 2 months ago
PARIMALA PITCHIKA
What is Type inference for generic instance creation?
about 2 months ago
In general if we want to create generic type collection we can use below syntax
ArrayList<String> list = new ArrayList<String>();
but in java7 there is a future called Type inference in this we no need to specify the generic type String inside the
dymond brakets in the right side of ArryList object creation, we can just specify in the left portion,This is also
known as the diamond operator i.e
ArrayList<String> list = new ArrayList<>();
Exactly only the left side of the statement must specify the type arguments <type>, while the right side will be
inferred from the left side.
S KIRTHIKA
what is nio packagev means????
about 2 months ago
52 / 293
Campus Commune
7/31/2014
read, or the data is fully written. The thread can do nothing else in the meantime.
Non-blocking mode enables a thread to request reading data from a channel, and only get what is currently
available,if no data is currently available. Rather than remain blocked until data becomes available for reading,the
thread can then go on and do something else in the mean time..
ALURI REDDY
Java NIO (New IO) is an alternative IO API for Java.
The differences between Java NIO and IO
1) IO is stream oriented, where NIO is buffer oriented.
2) NIO allows you to manage multiple channels using only a single (or fewer) threads, but the cost is that parsing
the data might be somewhat more complicated than when reading data from a blocking stream using standard
IO.
https://campuscommune.tcs.com/communities/java-lounge/content/history-of-java-0
53 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/jvm-java-virtual-machine-0
54 / 293
Campus Commune
7/31/2014
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
PERABATHINI KIRTI
Can anyone explain what "Both JDK and JRE contains the JVM" means?
about 19 days ago
SUMANTA SAHA
JRE contains JVM & JDK contains JRE. Hence both JRE JDK contains JVM.
ANURAG MODI
Is JVM a sort of document where only specification is given?
ABHISHEK SAH
No, JVM is the Java Virtual Machine where the compiled bytecode is run/interpreted. JVM makes the java programs
platform independent. Sun has 5.5billion JVM enabled device. So a java program can be run in 5.5billion devices.
JVMs are platform specific, and they provide same environment to the bytecode for all the devices.
SREE SETTURU
what is API?
about 2 months ago
MUTHU S
https://campuscommune.tcs.com/communities/java-lounge/content/jvm-java-virtual-machine-0
55 / 293
Campus Commune
7/31/2014
ALURI REDDY
An application programming interface (API) is a particular set of rules ('code') and specifications that software
programs can follow to communicate with each other. It serves as an interface between different software
programs and facilitates their interaction, similar to the way the user interface facilitates interaction between
humans and computers.
https://campuscommune.tcs.com/communities/java-lounge/content/jvm-java-virtual-machine-0
56 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/jre-java-runtime-environment-0
57 / 293
Campus Commune
7/31/2014
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SASIVINEETHA GATTUPALLE
Where can i find vedio 6?? There are no vediods in this submodule.Someone do respond please. Thank you.
about 25 days ago
SWATI PARIDA
what are core classes?
about 2 months ago
https://campuscommune.tcs.com/communities/java-lounge/content/jre-java-runtime-environment-0
58 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/know-more-on-command-promt-0
59 / 293
Campus Commune
7/31/2014
class TestClass {
public static void main(String args[])
{
System.out.println("TestClass is running fine.");
}
}
After execution of TestClass, you should get the message on the command prompt as "TestClass is running fine". Have you got the output on
command prompt / console.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
GOKULKUMAR V
Where can i find the link for video 6 mentioned above?
Thanks in advance
about 1 month ago
MAHIMA
Someone please provide the link to video 6.
https://campuscommune.tcs.com/communities/java-lounge/content/know-more-on-command-promt-0
60 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/different-tools-to-create-java-program-0
61 / 293
Campus Commune
7/31/2014
After the download is complete, double click the installer and follow the installation process. Follow the recommended settings during installation.
Depending on the location that was chosen to install the JDK, you would see the following contents.
The outer most folder that has all the installed contents is generally referred to as Java Home.
So, as the per the below installation, the Java Home directory is C:\jdk1.5.0
The developer frequently needs an access to the java compiler (javac) and java interpreter (java). These tools come along with JDK and you will find
them under the bin folder in Java installation folder.
To enable to developer to use these tools from any directory in the command prompt, we set these values in the operating system environment
variable.
Configure JDK:
Environment variables are key and value pair. PATH is one of the environment variable (key). It's value is a collection of more than 1 directory path.
Operating system uses the PATH variable to scan all the directory when a user want to execute a tool without mentioning the installation path.
eg. PATH = C:\jdk1.5.0\bin; C:\Program Files\Intel\iCLS Client\; C:\Program Files\IBM\Trace Facility\
As you can see, the delimeter (;) is used to separate multiple directory mentioned as a value to the PATH environment variable.
To set and environment variable, right click on Computer icon, and select Properties.
https://campuscommune.tcs.com/communities/java-lounge/content/different-tools-to-create-java-program-0
62 / 293
Campus Commune
7/31/2014
Right click on the Advanced system setting, it will open System Properties as shown below.
Go to the Advanced tab, and click on the Environment Variables button available at the bottom. This is would open a small window that will list all
the existing environment variables. It has 3 button New (to add a new environment variable), Edit (to edit the value of existing environment
variable) and Delete (to delete and existing environment variable)
Click on the button New. It will open a new window with 2 empty text boxes. Enter the variable name as PATH. In variable value, enter the path
to the bin folder in java installation directory.
Example:
Create a folder javaproject in your D: drive. Create a file MyFirstJavaProgram.java with following contents.
https://campuscommune.tcs.com/communities/java-lounge/content/different-tools-to-create-java-program-0
63 / 293
Campus Commune
7/31/2014
If the compilation is successful, a file MyFirstJavaProgram.class is generated. The generated class file can be run with help of an interpreter java.
The syntax to execute a class file is java FileName
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
There are no doubts yet
Closed Doubts
https://campuscommune.tcs.com/communities/java-lounge/content/different-tools-to-create-java-program-0
64 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
3.7. Eclipse
Good as of now we have learnt how to install JDK on our systems. We also learnt how to write a java program, compile it and run the java
program. Let us now learn another way of writing Java program. That is using an IDE. IDE means Integrated Development Environment. There
are many IDEs available in the market but we will be using Eclipse. Eclipse is an Integrated Environment which provides programmers perform a
lot of activities required during development at one place. Some of the activities that can be done using Eclipse are writing code, making code more
readable, like indentation, providing spacing, compiling, running, debugging etc. There are many tools available in the market which helps in doing
each of the activity mentioned above in separate windows, but IDE integrates all the above windows into one environment. Using Eclipse we can
write programs in many languages like C, CPP and Java, html, Perl etc. But we will only be focused on learning how to use Eclipse to develop Java
programs. First of all let us learn how to get eclipse installed in our system.
Install Eclipse:
Eclipse has been evolving and there are many versions available and you may choose any of the existing versions for java development but we
would recommend use of Galileo. You can download the same using the following url http://www.eclipse.org/downloads/packages/eclipse-ide-javaee-developers/galileor . You may download the appropriate version depending upon your Operating System. Please refer the below diagram for
the same.
https://campuscommune.tcs.com/communities/java-lounge/content/eclipse-0
65 / 293
Campus Commune
7/31/2014
Make sure you download the right installation file based on your OS (winodws, linux or Mac) and architecture (32 or 64 bit). You may download
and place the archive (zip, tar.gz) at any location of the hard disk of your system. Then the archive needs to be extracted using any of the archiving
tools provided by your OS. The installation of eclipse is done just by unzipping the downloaded zip file.
Configure Eclipse:
Before starting eclipse, make sure that you have downloaded Java Development Kit(JDK). We recommend you to download version 1.5. Refer to
the download, install & configure JDK section present in this document for details.
Start eclipse from the eclipse installation directory. The eclipse installation directory will have an eclipse application as highlighted below.
1. Start Eclipse
When eclipse starts up for the first time it prompts you for the location of the workspace folder. All your data will be stored in the workspace folder.
You can accept the default or choose a new location.
If you check "Use this as the default and do not ask again", this dialog box will not come up again.
After selecting workspace, you will see a welcome screen. Close the welcome screen.
https://campuscommune.tcs.com/communities/java-lounge/content/eclipse-0
66 / 293
Campus Commune
7/31/2014
Setting up eclipse
Now we need to make sure of certain things before proceeding with the creation of Java Project. We should set up environment for running
programs in eclipse.
2.1 Compiler compliance level
Right click Window and select Preferences.
Compiler compliance level can be changed here. Select the appropriate version from drop down and click OK.
https://campuscommune.tcs.com/communities/java-lounge/content/eclipse-0
67 / 293
Campus Commune
7/31/2014
In the above scenario previous version was 1.6 which is now changed to 1.5
2.2 Installed JREs
You can also add/remove JRE definitions using Installed JREs from Java of Preferences tab.
Now the default JRE is already checked in the Installed JREs, you can add new JRE using Add button and to remove, select the existing JRE and
click Remove button.
On clicking Add a window is opened where you need to select JRE Type
Select the type of JRE and click Next. Now you need to add JRE definition, for this you need to click on Directory.
Choose the appropriate folder from the respective location and click OK
Now the library files related to the new definition are added. Click Finish to add it.
https://campuscommune.tcs.com/communities/java-lounge/content/eclipse-0
68 / 293
Campus Commune
7/31/2014
A wizard to create new project will appear. Select Java Project from the list of options available. Click Next.
https://campuscommune.tcs.com/communities/java-lounge/content/eclipse-0
69 / 293
Campus Commune
7/31/2014
It opens a wizard to configure few setting for the new project. Enter the project name of your choice eg. FirstJavaProject. Make sure your have
select the below highlighted options. Click Finish.
Eclipse may ask you to open the associate perspective. Click Yes.
4. Project Directory.
https://campuscommune.tcs.com/communities/java-lounge/content/eclipse-0
70 / 293
Campus Commune
7/31/2014
Reconfirm the location where you want the class to be created its is in the src folder.
Enter the package name tcs.ilp
Enter the class name - MyFirstJavaProgram
Eclipse created a package tcs.ilp under the src folder. It also creates a java file MyFristJavaProgram.java. Double click on the
MyFristJavaProgram.java and eclipse opens the contents of the file in a window.
https://campuscommune.tcs.com/communities/java-lounge/content/eclipse-0
71 / 293
Campus Commune
7/31/2014
Here you do not need to compile the java file as eclipse does it for us automatically. We just need to run the program. Eclipse provides a console
window where you can see the output of the program that you executed as Java Application.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SASIVINEETHA GATTUPALLE
What does the following statements mean?
1.Use an execution environment JRE
2.Use a project specific JRE
about 7 days ago
SASIVINEETHA GATTUPALLE
I have installed jdk 1.8.0_11. Now which eclipse package should I install inorder to run java programs? Please help me. I'm
confused.... Thanks in advance.
about 7 days ago
https://campuscommune.tcs.com/communities/java-lounge/content/eclipse-0
72 / 293
Campus Commune
7/31/2014
SAILESH SEERA
windows 64bit it was not opening
about 8 days ago
MEGHA GOYAL
I am unable to find Eclipse Galileo for Windows 64 bit... plz provide link for it...
about 15 days ago
VISHWA DEEPAK
I think Eclipse Galileo is not available for windows 64 bit. is it ? if yes please provide the link for download .
about 18 days ago
ATUL VISWANATH
Download Eclipse Luna ver 4.4 64-bit instead!
ABHISHEK SAH
What is the difference in using Eclipse Galileo, eclipse juno or myeclipse ?
about 20 days ago
SOURAV GHOSH
Eclipse Galileo and Eclipse Juno represent codenames for two different release versions of Eclipse. Instead of using
the traditional version numbers directly for branding, each simultaneous release versions are given code names.
Presently, the versions are named using words whose first letter increments by one with each successive release
version. For example, Eclipse Galileo is followed by Eclipse Helios, Indigo, Juno, Kepler, and the latest version
to-date, Eclipse Luna.
SRIJAN GUPTA
in the given link eclipse galileo for 32 bit windows system can be downloaded.. will it run in 64 bit system also??
about 21 days ago
RAJENDRA LAMROR
no
download for 64 bit
SOURAV GHOSH
Yes, it will run. A 64 bit OS can run both 64 bit software as well as 32 bit software. However, 32 bit OS can only
run 32 bit software. So, if you have 64 bit OS running on your system, you can download either 32 bit eclipse or
64 bit eclipse. Both will run perfectly on your system. However, I'd recommend that you install the same bit
length JDK on your machine, i.e. if you install 32-bit version of eclipse you should link it with a 32-bit version of
JDK, and vice versa.
RIZWANA AHMAD
hey...
i downloaded java from the link provided but i somehow ended up at jdr 1.7 n eclipse dont support that, can anyone plz give
me the correct link of jdk n jre for windows 8.1 64bit...!! im stucked at this point from 2 days..plz help :(
MAYUR GUPTA
install jdk 5
MAYUR GUPTA
https://campuscommune.tcs.com/communities/java-lounge/content/eclipse-0
73 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/eclipse-0
74 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
3.8. Compileonline.com
Compileonline.com is a web application which is used to compile and execute programming languages online. You can compile java programs as
well. Here is the quick guide to compile and execute your program.
Step1:
Open browser and type www.compileonline.com in the address bar.
https://campuscommune.tcs.com/communities/java-lounge/content/compileonline-0
75 / 293
Campus Commune
7/31/2014
Step 2:
Click on Java in the Programming Languages section.
https://campuscommune.tcs.com/communities/java-lounge/content/compileonline-0
76 / 293
Campus Commune
7/31/2014
The left hand area is where the source code is written and the right side Result tab is where the output is displayed. Steps to compile and execute
the programs are given on the right hand area. We will now execute a sample code.
Step 3:
Write a program on the left hand area in Main class file tab
Step 4: Finally click the following button [ Ctrl+E on Windows & Command+E on Mac ] to see the result.
Step 2: Click Support class file and write the program in the area provided
In the above program, we have created a Dog class which has a method bark(). Now to execute the above program, we need a Test class which
contains main() method and a call to bark() method.
https://campuscommune.tcs.com/communities/java-lounge/content/compileonline-0
77 / 293
Campus Commune
7/31/2014
Step 3: Click Main class file tab and create a class with main() method
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/compileonline-0
78 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
Q
3.9. Reference Links & videos
Reference links
http://www.devmanuals.com/tutorials/java/corejava/JavaHistory.html
http://www.freejavaguide.com/history.html
http://www.devmanuals.com/tutorials/java/corejava/IntroductionJava.html
http://www.w3resource.com/java-tutorial/download-and-Install-JDK-Eclipse-IDE.php
http://docs.oracle.com/javase/tutorial/getStarted/application/index.html
http://www.devmanuals.com/tutorials/java/corejava/JavaEclipseHelloWorld.html
http://www.devmanuals.com/tutorials/java/corejava/JavaVirtualMachine.html
http://viralpatel.net/blogs/java-virtual-machine-an-inside-story/
http://www.javatpoint.com/internal-details-of-jvm#jvminternalarch
https://campuscommune.tcs.com/communities/java-lounge/content/reference-links-and-videos-0
79 / 293
Campus Commune
7/31/2014
http://docs.oracle.com/javase/tutorial/getStarted/intro/definition.html
http://en.wikipedia.org/wiki/Java_Development_Kit
http://www.javatpoint.com/difference-between-jdk-jre-and-jvm
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
ANUGRAH TIWARI
the quiz is not opening for this module.
kindly respond quickly
thanking you in advance
https://campuscommune.tcs.com/communities/java-lounge/content/reference-links-and-videos-0
80 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
Java Variables
4.2
4.3
https://campuscommune.tcs.com/communities/java-lounge/content/operators-and-variables-in-java-0
81 / 293
Campus Commune
7/31/2014
4.4
Operators
4.5
String
4.6
4.7
Practice Problems
https://campuscommune.tcs.com/communities/java-lounge/content/operators-and-variables-in-java-0
82 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
Variable declaration
https://campuscommune.tcs.com/communities/java-lounge/content/java-variables-0
83 / 293
Campus Commune
7/31/2014
Note:Product.java example which is been given that can not be executed as main method is not there.
Types of Variables
Member / Instance variables:
Instance variables are declared in a class, but outside a method, constructor or any block.
Access modifiers such as private, public, protected can be specified for instance variables.
They are visible for all methods, constructors and blocks in the class.
Values can be assigned during the declaration or within the constructor.
Example 1:
In this example we can see how to declare and assign initial value to the variables. We can also see the access modifiers used with the variable
declaration. Here we have used private access modifier.
Note:Product.java example which is been given that can not be executed as main method is not there.
Example 2:
This example demonstrates the default values of instance variables.
https://campuscommune.tcs.com/communities/java-lounge/content/java-variables-0
84 / 293
Campus Commune
7/31/2014
Note:Employee.java example which is been given that can not be executed as main method is not there.
The default initial values of the data members of Employee class are as shown below:
name is null (as it is a String object reference)
id is 0(as it is primitive type : integer)
basicSalary is 0.0 (as it is primitive type : float)
Local variables
Local variables are declared inside methods, constructors or blocks.
Access modifiers cannot be used for local variables.
They are visible only within the declared method, constructor or block.
There is no default value for local variables. So local variables should be declared and an initialized before its first use.
Example:
This example demonstrates how to declare and use local variables.
Note:Calculator.java example which is been given that can not be executed as main method is not there.
Here "d" is the local variable created inside the calculation method.
Parameters
Parameters are the list of arguments that come into the function. Parameters are used to pass values to functions and to receive values in a
function.
Example: This example demonstrates to how to use parameters to receive values.
https://campuscommune.tcs.com/communities/java-lounge/content/java-variables-0
85 / 293
Campus Commune
7/31/2014
Here we are using int a, int b as parameters to receive values sent to add method.
Note:Calculator.java example which is been given that can not be executed as main method is not there.
Example 2:
This example demonstrates to how to use parameters to send values to functions.
In c.add(a,b) a and b act as parameters, which are used to send values to add method.
Note: Until, you will not include import statement or if both programs (Tester.java, Calculator.java)
are not in same package that program will not run.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
ORUGUNTA MEGHANA
i am not able to see figure examples for this topic only,,is this happening to anyone else also??
about 1 month ago
VISHNU PRAKASH
Try changing the security settings in your browser.
KRISHNA DHARMARAJ
ya I am also facing the same problem
POORNIMA MURUGESAN
Try in chrome. I had the same problem in mozilla.
NANDHINI R
I too had the same problem..open the image in a new window and you can view it..It worked for me..
https://campuscommune.tcs.com/communities/java-lounge/content/java-variables-0
86 / 293
Campus Commune
7/31/2014
BALAJI PATRO
wat settings do we hav to change?
KALYAN PILLA
The CA certificate provided by campus commune is not identified by most browsers.
If you ever had to "Add Exception" to SSL certificate provided by nextstep, this can be your problem.
Solution:
1. Right click on the place holder for the image
2. Select Open Image in a new tab
3. Add Exception when a Security Warning appears ( Now the images, probably present on a different server,
are allowed)
4. Continue browsing the course in a usual manner. Now, the images will load.
This worked for me with Chrome on Trisquel.
PRIYANKA MUKHERJEE
Open the image in a new window.
PAYAL DASH
i hv nt yet cmpleted my task but it shows dat u r failed n my 2nd chance also faited,....how can it be????
about 1 month ago
ABDUL HAQ
Can any one please tell me what are the access modifiers??
VIDHI MITTAL
Access modifiers basically mean the way in which each variable or method can be accessed. java supports public,
protected and private access modifiers.
ABDUL HAQ
Thanks Mittal ji
https://campuscommune.tcs.com/communities/java-lounge/content/java-variables-0
87 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
88 / 293
Campus Commune
7/31/2014
ix. Static variables in a class can be accessed from a second class by calling with the class name of the first. ClassName.variableName. Thus we can
see that the static variables are not called by objects. It is directly called with class name.
Example 1:
Following example demonstrates how to declare and use static variables.
Note: StaticExample.java example which is been given that can not be executed as main method is not there.
https://campuscommune.tcs.com/communities/java-lounge/content/static-variables-and-functions-0
89 / 293
Campus Commune
7/31/2014
Note: Calculator.java example which is been given that can not be executed as main method is not there.
Here add is a static method written in calculator class. We are accessing that in TestCalculator class.
Since it is static method we can access it without creating object of that class, directly with the classname.
Points to remember
1. Static methods can't use non-static, instance variables directly.
2. Static methods can't use non-static, methods directly.
That means, if a static method need to access a non-static variable or method, then first create an object and using that object you need to access
the method or variable.
This is because; static members which are related directly to class cannot see instance variable state and behaviour.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/static-variables-and-functions-0
90 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/numeric-data-types-0
91 / 293
Campus Commune
7/31/2014
Each of these types uses a fixed number of bits. i.e. same number of bits will be used no matter what value is represented.
For example, all values represented using the short data type use 16 bits. The value zero (as a short) uses 16 bits and the value thirty thousand
also uses 16 bits.
1] byte:
byte data type is an 8-bit signed two's complement integer.
Minimum value is -128 (-2^7)
Maximum value is 127 (inclusive)(2^7 -1)
Default value is 0
byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int.
Example: byte a = 100 , byte b = -50
2] short:
short data type is a 16-bit signed two's complement integer.
Minimum value is -32,768 (-2^15)
Maximum value is 32,767 (inclusive) (2^15 -1)
short data type can also be used to save memory as byte data type.
A short is 2 times smaller than an int.
Default value is 0.
Example: short s = 10000, short r = -20000
3] int:
int data type is a 32-bit signed two's complement integer.
Minimum value is - 2,147,483,648.(-2^31)
Maximum value is 2,147,483,647(inclusive).(2^31 -1)
int is generally used as the default data type for integral values unless there is a concern about memory.
The default value is 0.
Example: int a = 100000, int b = -200000
4] long:
long data type is a 64 bit signed two's complement integer.
Minimum value is -9,223,372,036,854,775,808.(-2^63)
Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
This type is used when a wider range than int is needed.
Default value is 0L.
Example: long a = 100000L, int b = -200000L
5] float:
float data type is a single-precision 32-bit IEEE 754 floating point.
float is mainly used to save memory in large arrays of floating point numbers.
Default value is 0.0
float data type is never used for precise values such as currency.
Example: float f1 = 234.5f
6] double:
double data type is a double-precision 64-bit IEEE 754 floating point.
https://campuscommune.tcs.com/communities/java-lounge/content/numeric-data-types-0
92 / 293
Campus Commune
7/31/2014
The result has fractional value, so we are using float variable to store it
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SAYANTAN GANGOPADHYAY
how we initialize bytes????...as we know byte a=20;...but they declared it as a wrong answer!!!!!!!!!!!
about 13 days ago
SAYANTAN GANGOPADHYAY
stream of data is fetched from network or file by using which data type????
about 13 days ago
https://campuscommune.tcs.com/communities/java-lounge/content/numeric-data-types-0
93 / 293
Campus Commune
7/31/2014
LAKSHMI D
char
RIZWANA AHMAD
which data type should be used to store currency?? if not double..?is there anything like long double in java? or any toher
data type that is specially used for this purpose
about 22 days ago
ABHISHEK SAH
Currency is a highly precise value so Java provides a class named "Currency" itself for it. The data type suitable is
BigDecimal.
For more refer: http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
SIDDHARTH GOSALIA
In the example program given above, instead of storing the answer as 'float', can I store it as 'double'. What difference will it
make to the program as both of them can store decimal values?
about 26 days ago
SHWETA NAYAK
yes you can store it as double. but the memory required for storing a value is more, if double is used since its size is
64 bits
https://campuscommune.tcs.com/communities/java-lounge/content/numeric-data-types-0
94 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
4.4. Operators
Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Misc Operators
1] The Arithmetic Operators:
Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic
operators:
Assume integer variable A holds 10 and variable B holds 20, then:
https://campuscommune.tcs.com/communities/java-lounge/content/operators-0
95 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/operators-0
96 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/operators-0
97 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/operators-0
98 / 293
Campus Commune
7/31/2014
The table below shows all Java operators from highest to lowest precedence, along with their associativity. Here, operators with the highest
precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be
evaluated first.
For more information on precedence and associativity you can visit the following link.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/operators-0
99 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
4.5. String
Strings are an incredibly common type of data in computers. Strings, which are widely used in Java programming, are a sequence of characters. In
the Java programming language, strings are objects.
Java string is a series of characters gathered together, like the word "Hello", or the phrase "practice makes perfect". Create a string in the code by
writing its chars out between double quotes.
Following are some of the ways to instantiate a java String
String str1 = "Hello";
String str2 = new String("Apple");
String str3 = new String(char []);
Methods:
There are methods for:
- concatenating two strings - concat() ,+
https://campuscommune.tcs.com/communities/java-lounge/content/string
100 / 293
Campus Commune
7/31/2014
Result:
- getting the character at a given position within the string -- charAt() .By using indexOf() method you get get the position of the sepcified string
or char from the given string. You can also get the index strting from a specified position of the string.
ex:
charAt( int index )
@Returns the char value at the specified index
will result as
Combining charAt() with another String method, we can get the final character of any String, regardless of length:
will result as
https://campuscommune.tcs.com/communities/java-lounge/content/string
101 / 293
Campus Commune
7/31/2014
or char from the given string. We can also get the index strting
Output is
https://campuscommune.tcs.com/communities/java-lounge/content/string
102 / 293
Campus Commune
Ask a doubt
7/31/2014
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/string
103 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
Related Videos
https://campuscommune.tcs.com/communities/java-lounge/content/reference-links-and-videos-1
104 / 293
Campus Commune
Ask a doubt
7/31/2014
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/reference-links-and-videos-1
105 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
Q
4.7. Practice Problems
1. Write a program to find the difference between sum of the squares and the square of the sums of 2 numbers?
2. Develop a program that accepts the area of a square and will calculate its perimeter.
3. Utopias tax accountants always use programs that compute income taxes even though the tax rate is a solid, never-changing 15%. Write a
program that calculates net pay of an employee based on number of hours the employee has worked. Assume an hourly rate of $12.
4. An old-style movie theater has a simple profit program. Each customer pays $5 per ticket. Every performance costs the theater $20, plus $.50
per attendee. Develop the program that accepts the number of attendees (of a show) and calculates how much income the show earns.
5. Develop the program which computes the height that a rocket reaches in a given amount of time. If the rocket accelerates at a constant rate g,
it reaches a speed of g t in t time units and a height of 1/2 * v * t where v is the speed at t.
6. Develop a program that accepts the length and width of a rectangular floor and the edge length of a square tile and will compute the whole
number of tiles needed to cover the floor completely.
Click here to download the Solutions
https://campuscommune.tcs.com/communities/java-lounge/content/practice-problems
106 / 293
Campus Commune
7/31/2014
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SASIVINEETHA GATTUPALLE
In the solution to the first problem the numbers taken are integers and the result would surely be an integer.Then why is the
result stored in a variable of double data type instead of int data type? Thank You.
about 24 days ago
https://campuscommune.tcs.com/communities/java-lounge/content/practice-problems
107 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
Introduction to Classes
5.2
Objects or Instances
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-to-class-in-javaaccess-specifiers-thi
108 / 293
Campus Commune
7/31/2014
5.3
5.4
5.5
Static Keyword
5.6
Practice Problems
5.7
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-to-class-in-javaaccess-specifiers-thi
109 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-to-classes-0
110 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-to-classes-0
111 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-to-classes-0
112 / 293
Campus Commune
7/31/2014
Declaration of Methods
A method is a program module that contains a series of statements that carry out a task. To execute a method, you invoke or call it from another
method; the calling method makes a method call, which invokes the called method. Any class can contain an unlimited number of methods, and
each method can be called an unlimited number of times.
The syntax for method declaration is as given below. Java main method is given as an example.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SHYAM VASANI
What is the last date for completion of Tech Lounge section?
about 17 days ago
SHABU K V
Moderator
Day before you join TCS. In fact ILP will be a continuation of tech Lounge and better you prepare Tech Lounge
easier it is to do ILP
SIDDHARTH GOSALIA
Should a java class name necessarily begin with an upper case letter? Is it a rule?
about 26 days ago
KALYAN PILLA
All PUBLIC class names begin with an upper case letter by "convention".
SAI KANUPARTHI
It is not a rule but we follow to understand conventions easily.
ABHISHEK SAH
It is not a rule,i.e you will not get an error if your class name does not start with upper case letter but Java has some
naming "convention"/recommendations which is followed mostly for the easy of developer too.
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-to-classes-0
113 / 293
Campus Commune
7/31/2014
SHANKAR PAUL
Why string is given as arguments to the main function in java?
about 2 months ago
PREETHI PATTABIRAMAN
Well it is how the method signature is defined for main in jvm. You try as int args. ... it will compile but won't run.
Because main doesn't match the signature as defined in jvm. String is the most common object in java.
PREETHI PATTABIRAMAN
https://www.google.co.in/url?
sa=t&source=web&rct=j&ei=ZdaFU7uxJMSkkQXTuYGACQ&url=http://stackoverflow.com/questions/10183611/usingint-instead-of-string-public-static-void-main-intargs&cd=1&ved=0CCcQFjAA&usg=AFQjCNFXSz7LpKgUNoCiI2Dux7dcKO1rVA&sig2=kAyqyVw8w1AY7UwW1cyJYw
ABHISHEK SAH
The String []args is an array of string which is passed to main. args is the array name here and basically it is helpful
in taking "command line arguments". Command line arguments are values passed when you type the run
command for the program. Eg: c:/> java Test 23 45
Here 23 and 45 are stored in this String array and can be used in the program.(Test here being the class name.)
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-to-classes-0
114 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/objects-or-instances
115 / 293
Campus Commune
7/31/2014
Objects are made up of attributes(properties) and methods(behaviours). Attributes are the characteristics that define an object; the
values contained in attributes differentiate the objects of the same class from one another.
To understand this in a better way, let's take an example of Mobile as an object. Mobile has characteristics like model, manufacturer, cost,
operating system etc. So if we create Samsung mobile object and iPhone mobile object, we can distinguish them from their characteristics. The
values of the attributes of an object are also referred to as the object's state.
Objects need not be physical objects. It can be any entity. It can be account,an organization etc.
We can say --- Object is an instance of a class.
https://campuscommune.tcs.com/communities/java-lounge/content/objects-or-instances
116 / 293
Campus Commune
7/31/2014
Consider an object Car. What we know about a car defines its attributes. What a car can do defines its behaviours/methods.
https://campuscommune.tcs.com/communities/java-lounge/content/objects-or-instances
117 / 293
Campus Commune
7/31/2014
Creating an Object:
As mentioned previously, a class provides the blueprints for objects. So basically an object is created from a class. In Java, the new key word is used
to create new objects.
There are three steps when creating an object from a class:
Declaration: A variable declaration with a variable name with an object type.
Instantiation: The 'new' key word is used to create the object.
Initialization: The 'new' keyword is followed by a call to a constructor. This call initializes the new object.
Example of creating an object is given below:
ClassName ObjectReferenceVariableName = new ConstructorName();
Vehicle
https://campuscommune.tcs.com/communities/java-lounge/content/objects-or-instances
118 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/objects-or-instances
119 / 293
Campus Commune
7/31/2014
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SHRUTI SINHA
What is meant by 'make' attribute of car class?? What does it actually supposed to store?
about 22 days ago
SASIVINEETHA GATTUPALLE
What does the phrase "Methods of an object" exactly mean? Thank You.
about 24 days ago
LAKSHMI SHANKHWALKER
In a class you define variables and functions. Like class Car above, model is a variable and start() is a function.
these functions are called as methods. when you create an object of the class say Alto, Alto has a model and a
start() method. method is some function an object can do, or can have done to it.
SRIKAVYA PINGALI
Class is a keyword....I marked it as true but marks got deducted.....Please tell me if I m wrong...!!!
about 1 month ago
ASHUTOSH JHA
Its "class"..I guess you did not notice the caps C
SARANYA P
Can we create instance variables without access specifiers? If yes, what is the major difference between creating instance
variables with and without access specifiers??
ie. Instance variable without public or private.
about 1 month ago
AMARDEEP BURNWAL
instance variable without access specifier is considered as 'default' access specified.
ANAND NARLA
Can we create a class without a constructor?
https://campuscommune.tcs.com/communities/java-lounge/content/objects-or-instances
120 / 293
Campus Commune
7/31/2014
HARISH VALUROUTHU
yes anand we can create a class without a constructor
ALURI REDDY
YES
If you don't specify a constructor, a default constructor will be generated by the compiler.
SHANKAR PAUL
what is the use of this keyword?
about 2 months ago
PREETHI PATTABIRAMAN
This keyword is used as a default object reference which refers the current object. This is used in constructor to
initialize variables or in methods to indicate the data with which the method should be executed. Default object
reference to current object is this.
PREETHI PATTABIRAMAN
This keyword is used as a default object reference which refers the current object. This is used in constructor to
initialize variables or in methods to indicate the data with which the method should be executed. Default object
reference to current object is this.
ALURI REDDY
In java, this is a reference variable that refers to the current object.
Usage of this keyword
1)this keyword can be used to refer current class instance variable.
2)this() can be used to invoke current class constructor.
3)this keyword can be used to invoke current class method (implicitly)
https://campuscommune.tcs.com/communities/java-lounge/content/objects-or-instances
121 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/objects-or-instances
122 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/constructors-and-access-specifiers
123 / 293
Campus Commune
7/31/2014
Access Specifiers
Each object has members (members can be variables or methods) which can be declared to have specific access.
https://campuscommune.tcs.com/communities/java-lounge/content/constructors-and-access-specifiers
124 / 293
Campus Commune
7/31/2014
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
ABHISHEK SAH
Which access specifier is most suitable for constant values? (Asked in the quiz.)
https://campuscommune.tcs.com/communities/java-lounge/content/constructors-and-access-specifiers
125 / 293
Campus Commune
7/31/2014
MADDI
static is the most suitable one because its value is shared by all the objects.
SIDDHARTH GOSALIA
"Variables that are declared private can be accessed outside the class if public getter methods are present in the class." What
does this statement mean?
about 26 days ago
VAIBHAV CHAUDHARY
As u can see in above example Satellite class can access "number of passengers" variable which is declared private in
Flight class with the help of "getNumberOfPassengers" method in Flight class which is declared public in Flight class.
As per the rule variables declared with private access specifiers can be accessed only with in the class so if some
other class wants to access a private variable they must access that variable using a public method of that class (i.e
in case of above problem using getNumberOfPassengers method ).That means you can not access the private
variable directly but you can access it with the help of public method in the same class.
MOHAMMED RAHIMAN
questn::Which of these is used as default specifier for a member of class if no access specifier is used for it ???
a) private
b) public
c) public, within its own class
d) protected
I answered (a)....since default access specifier for class members is private(strictly speaking package-private)...though it
deduced my marks..
similarly,, questn::Which of these is used to access member of class before object of that class is created?
a) public
b) private
c) static
d) protected
here..i went with (c),,since for static members can be accesed without any object creation and
instantiation,,where we can call the static members prefixed with its classname.....finally it too turned out be
the wrng answer......
are they really wrong answers..plzzz let me kno abt your ideas on those questns.....
THANXin advance...
ANBU SELVAM
I also suffered with the same problem...
https://campuscommune.tcs.com/communities/java-lounge/content/constructors-and-access-specifiers
126 / 293
Campus Commune
7/31/2014
PRIYANKA SAWANT
the ans to the first q is public ,within its own class.
bcz default access specifier allows a class to be accessed by other classes within the same package.
DIVYA PANICKER
Answer for the first question is "public within its own package". When default specifier is used for the member of a
class it can be accessed from any class within the same package.
Answer for the second question is "static"
SHANKAR PAUL
As mentioned earlier there are four access specifiers in java.What is the fourth access specifier and what are its properties?
about 2 months ago
PREETHI PATTABIRAMAN
The fourth is simply referred as default. Public private protected and default. But while I was attending a class my
teacher called the default specifier as "friendly"
PREETHI PATTABIRAMAN
http://javapapers.com/core-java/access-modifiers-in-java-explain/
https://campuscommune.tcs.com/communities/java-lounge/content/constructors-and-access-specifiers
127 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/use-of-this-keyword
128 / 293
Campus Commune
7/31/2014
Let's understand the problem if we don't use this keyword by the example given below:
In the above example, parameter (formal arguments) and instance variables are same that is why we are using this keyword to distinguish between
local variable and instance variable.
Solution of the above problem by this keyword
129 / 293
Campus Commune
7/31/2014
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
AMALA VANKAYALA
How the problem got solved without using this keyword. what is the function of the value
about 2 months ago
PREETHI PATTABIRAMAN
It's just a variable name. You can even name it x y z etc. And it'll work fine.
TEJASWINI
This is because the instance variables used in the class Vehicle are not same as that of the parameter names in
the constructor Vehicle.
https://campuscommune.tcs.com/communities/java-lounge/content/use-of-this-keyword
130 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
Consider the below class Vehicle, here manufacturteName is declared as static and getManufactureName() method also declared as static.
https://campuscommune.tcs.com/communities/java-lounge/content/static-keyword
131 / 293
Campus Commune
7/31/2014
OUTPUT is:
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SASIVINEETHA GATTUPALLE
The class Vehicle is closed only after main method then the private variables are available to the main method also. Now what
is the need of the method "getManufactureName" in the program? Thank you.
about 24 days ago
KALYAN PILLA
The method getManufactureName() is not of much importance in the given example. However, if you were to
access the variable 'manufactureName' from another class (eg., if your main was in another class) the method
getManufactureName() would be necessary.
https://campuscommune.tcs.com/communities/java-lounge/content/static-keyword
132 / 293
Campus Commune
7/31/2014
BSANJAYA
anyone completed programs and uploaded??
about 1 month ago
https://campuscommune.tcs.com/communities/java-lounge/content/static-keyword
133 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
134 / 293
Campus Commune
7/31/2014
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/reference-links-and-videos-2
135 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/reference-links-and-videos-2
136 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
Packages
6.2
Import Statement
6.3
https://campuscommune.tcs.com/communities/java-lounge/content/java-library-packages-use-of-import
137 / 293
Campus Commune
6.4
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/java-library-packages-use-of-import
138 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
6.1. Packages
A package is a namespace that organizes a set of related classes and interfaces, providing access protection and name space management.
Conceptually packages can be thought of as similar to different folders on the computer. Packages are used in Java to prevent naming conflicts, to
control access, and to make searching/locating and usage of classes easier.
Java platform provides a very large library of classes and interfaces organized into different packages known as Application Programming Interface
or API. Some of the packages in Java API Library are:
java.lang - bundles the fundamental classes
java.io - classes for input, output functions are bundled in this package
Because software written in Java can be composed of hundreds of individual classes, programmers can define their own packages to group related
classes together. It is a good practice to organize by grouping related classes and interfaces into packages so that a programmer can easily locate
and find the classes he needs.
Since the package creates a new namespace there won't be any class name conflicts with other class names in other packages.
https://campuscommune.tcs.com/communities/java-lounge/content/packages
139 / 293
Campus Commune
7/31/2014
Eg: Both java.awt and java.util packages have a class called List. There is no naming conflict because the two List classes are part of two
different packages.
Using packages, it is easier to provide access control and it is also easier to locate the related classes.
Eg: Consider a scenario where a bank has many employees. New employees are added, some may resign from bank, so those employees need to
be removed from bank. Many customers come to the bank to get services. Customers open account, deposit money to account, withdraw money
from account etc.
Here we have different objects and so different classes need to be created. If it is going to be a large application, it will be difficult to manage all the
files without using packages. Also if we want to set different visibility, like Customer should not see the details of Employee, packages can be used
to organize and set visibility using access modifiers.
Implementation of Packages in Java
The package in Java is represented using the keyword 'package'. The package statement is written with the keyword 'package' followed by the
name of package and ends with a semi colon.
Syntax: package packagename ;
Eg:
package employee ;
package com.tcs.employees ;
The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all
types in the file.
Creating packages in Eclipse IDE
Step1: Create a package in eclipse and provide a name for the package as shown below. Package name can be a one word name or multiple words
separated by a dot.
Eg: com.tcs.employees (or) employee
https://campuscommune.tcs.com/communities/java-lounge/content/packages
140 / 293
Campus Commune
7/31/2014
Step 2: Create classe(s) inside the employee package. The java class file should have package statement as the first line. In the screenshot below
you can see the employee package. The classes Employee.java and TestEmployee.java are created under this package. You can see that the first
line in Employee.java is the package statement.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
DIVJYOT MAKKAR
Are static members visible outside the package?
What is static import?
What are wrapper classes?Is java.lang.void a primitive wrapper class?
about 16 days ago
SWARNADEEP SAHA
Hi,
Visibility and access does not depend on static keyword. To use the static member outside of the package it must
be public.
like. public static <whatever>
https://campuscommune.tcs.com/communities/java-lounge/content/packages
141 / 293
Campus Commune
7/31/2014
SWARNADEEP SAHA
The normal import declaration imports classes from packages, so that they can be used without package
reference. Similarly the static import declaration imports static members from classes and allowing them to be
used without class reference. (source : http://javapapers.com/core-java/what-is-a-static-import-in-java/)
A wrapper class wraps primitive data type and give them an object appearence.
I think there are only 8 of that type. Byte, Short, Integer, Long, Float, Double. Charecter and Boolean. If you find
that void is also one of them please let me know.
https://campuscommune.tcs.com/communities/java-lounge/content/packages
142 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/import-statement
143 / 293
Campus Commune
7/31/2014
You can see in the above example, the TestEmployee class is under the test package. You can also see the import statement to import Employee in
TestEmployee class. This is there because TestEmployee is using Employee class, which is part of a different package employee. However even after
the Employee class is imported into TestEmployee there are still errors in Testemployee class. This is because the Employee class is not visible to
classes outside its package. Earlier when the TestEmployee class was inside employee package, it knew the Employee class. But when it was moved
into a different package, and even after Employee class was imported the Employee class and its constructor are not visible. This is because only
public members of a package/class are visible to classes from other packages. So specify public access specifier to the class, constructor and method
which need to be accessed from outside.
In the below screen, you can see the changes made to access specifiers of Employee class and its constructor and methods. They are all declared
with public access modifier.
To use a public package member from outside its package any one of the following can be used
a) Refer to the class/member by its fully qualified name
b) Import specific class/member in a package
c) Import all members/classes in a package
https://campuscommune.tcs.com/communities/java-lounge/content/import-statement
144 / 293
Campus Commune
7/31/2014
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SURAJ V
Is it possible to import static classes from a package to another package?
about 11 days ago
SOURAV CHAKRABORTY
There isn't any setBasicpay method in Employee class. Is bob.setBasicpay(2500) a correct statement?
about 15 days ago
HALIMA SHAHUL
What is meant by fully qualified name.
about 2 months ago
ABUL M
If you import identically-named classes from different packages, you must use the fully qualified names when
declaring the classes. For example, the Java core libraries contain the classes java.sql.Date and java.util.Date. In
this case, you must write the fully qualified names of java.sql.Date and java.util.Date to use them.
Example : java.lang.* is used any class. but if we have two identical class name in two different
package then we should use java.lang.Classname
https://campuscommune.tcs.com/communities/java-lounge/content/import-statement
145 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/more-on-access-modifiers
146 / 293
Campus Commune
7/31/2014
class Employee {
private String name;
private String empNo;
private int age;
private double basicPay;
// Constructor
Employee(String name, String empNo, int age) {
this.name = name;
this.empNo = empNo;
this.age = age;
}
}
Protected Access Modifier protected
Protected is related to inheritance (a parent child relation). Protected members in a class can be accessed only by its child classes from
same/different package as well as by any other classes in the same package.
The child class can be anywhere, in other package or within the same package of the parent class.
The protected access modifier can be applied only to methods or fields, not to class. We can see more on it when we go through inheritance.
Java Libraries
Java differs from most other languages in that the number of classes and interfaces in its standard libraries is very large. Many common tasks have
already been implemented by these libraries.
They are generally of high quality and are widely used. Implementing something which already exists in the libraries is probably wasted effort.
Significant changes and additions to the standard libraries occur in each major release of Java, and it pays to keep current.
The most widely used packages are java.lang and java.util. There are other packages used for working with data such as java.sql, javax.sql, java.io
etc.
For graphical applications, see the Swing classes (javax.swing, and so on).
You should go through JDK documentation just to get an idea of what is available. Later, when a specific need arises, you will often know which
packages might be helpful.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SOLMANRAJU MAGANTI
What are Public Getter Methods ?
about 1 month ago
TEJASWINI
Public Getter Methods are the methods that take 0 parameters and returns a value. The returned value can be
private member present in the class.
https://campuscommune.tcs.com/communities/java-lounge/content/more-on-access-modifiers
147 / 293
Campus Commune
7/31/2014
i.e we can access the private members present in a class through another class.
https://campuscommune.tcs.com/communities/java-lounge/content/more-on-access-modifiers
148 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
7.Conditional operations
Objective
Understand conditional constructs, boolean operations and expressions, logical operators and precedence
Perform simple, nested and complex conditional operations
Ability to choose right conditional constructs
7.1
Conditional Operations
7.2
7.3
Practice Problems
https://campuscommune.tcs.com/communities/java-lounge/content/conditional-operations
149 / 293
Campus Commune
7.4
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/conditional-operations
150 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
if(condition)
{
/* statement(s) will execute if the condition is true.
For example, a=5
https://campuscommune.tcs.com/communities/java-lounge/content/conditional-operations-0
151 / 293
Campus Commune
7/31/2014
if(condition)
{
/* statement(s) will execute if the condition is true.*/
}
else
{
/* statement(s) will execute if the boolean condition is false */
}
In the above example, if a=5 and condition is to check if a>6 then it will go to else block.
Flow Diagram:
3. switch A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being
given as input is checked for each case.
This statement helps to select one out of the many choices based on an integer or String value. The conditions will accept only integer, character or
String. The syntax is different from if else statements. Here we are comparing the input value with different cases. We can set a default case for
inputs which does not satisfy any case.
The syntax of the switch statement is as follows.
switch (expression) {
case value_1 :
statement(s);
break;
case value_2 :
statement(s);
break;
https://campuscommune.tcs.com/communities/java-lounge/content/conditional-operations-0
152 / 293
Campus Commune
7/31/2014
.
.
.
case value_n :
statement(s);
break;
default:
statement(s);
}
Failure to add a break statement after a case will not generate a compile error but may have more serious consequences because the statements on
the next case will be executed.
The following rules apply to a switch statement:
1. The variable used in a switch statement can only be a byte, short, int, char or String(It is allowed from jdk 1.7 version.).
2. You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
3. The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal.
4. When the variable is found equal to a case, the statements following that case will execute until a break statement is reached. When a break
statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. Not every case needs to
contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a
task when none of the cases is true. No break is needed in the default case.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
KARMANJOT
There are lot of topics which are being asked in quizzes but are not covered in the study material. So please tell how to
prepare for them.
about 8 days ago
MOHINI GUPTA
The statements before the if block gets executed only if the condition is false
a) TRUE
b) FALSE
What is the answer?
about 22 days ago
SURBHI AGARWAL
false
PRIYANKA SAWANT
The statements above the if block have nothing to do with if condition and so ans is FALSE.
PRIYANKA SAWANT
only statemnts inside if are affected by condition
AMARDEEP BURNWAL
none of these
https://campuscommune.tcs.com/communities/java-lounge/content/conditional-operations-0
153 / 293
Campus Commune
7/31/2014
SUMAN SINGH
it will always be executed. it has got nothing to do with the if statement
https://campuscommune.tcs.com/communities/java-lounge/content/conditional-operations-0
154 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
The "?:" operator evaluates an expression which may also be an operand and returns operand1 if the expression is true; otherwise returns
operand2, if the expression is false. We can understand this thing with the help of a diagram shown as:
https://campuscommune.tcs.com/communities/java-lounge/content/ternary-operator-and-if-else-statement
155 / 293
Campus Commune
7/31/2014
If we analyze this diagram then we find that, operand1 is returned, if the expression is true; otherwise operand2 is returned in case of false
expression.
Lets have an example implementing some Logical operators:
If we analyze the table we find that result is always true only in case of first condition where both operands(or expressions) are true. On the other
hand result is always false in other conditions. Note that this table works alike for & and && operators.
In case of "&" operator,if both operands or expressions are true,the result is always true, otherwise it is false if either left-hand or right-hand
operand is false.
In case of "&&" operator, the result is also true, if both operands or expressions are true.
But this operator evaluates only the left-hand operand. It doesn't evaluate the right-hand operand if the left-hand operand is false then it will not
jump to the right-hand operand to evaluate it, and will come out from that statement and read the next statement. That's why this mechanism is
known as short-circuiting. Consider the following statements where the second expression returns false after evaluating only the left-hand operand.
https://campuscommune.tcs.com/communities/java-lounge/content/ternary-operator-and-if-else-statement
156 / 293
Campus Commune
7/31/2014
But the "&" operator always evaluates both of its operands whether the first operand is true or false. Consider the following statements where the
second expression returns false after evaluating the right-hand operand.
If we analyze the table then we find that, result is always false only if both operands or expression are false. On the other hand, result is always true
in rest of the other conditions.
Still these exists a major difference in their mode of use:
The "|" operator always evaluates both of its operands and returns true if one or other of its operand is true. Otherwise false if both the conditions
are false. Consider the following statements where the second expression returns alse after evaluating the right-hand operand.
157 / 293
Campus Commune
7/31/2014
false; but if the value of an operand or expression is false then it reverses as true.
Consider the following example:
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/ternary-operator-and-if-else-statement
158 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/reference-links-and-videos-4
159 / 293
Campus Commune
7/31/2014
Rs. 5.75, which is 1.25 = 0.25 1/100 500 for the first Rs. 500 and 0.50 1/100 900 = 4.50 for the next Rs. 900. Determine by hand the
pay backs amount for a customer who charged Rs. 2000 and one who charged Rs. 2600.
Define the program, which accepts a charge amount and computes the corresponding pay back amount.
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/reference-links-and-videos-4
160 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
8.1
Arrays
8.2
https://campuscommune.tcs.com/communities/java-lounge/content/basic-iterations-and-arrays
161 / 293
Campus Commune
7/31/2014
8.3
8.4
8.5
8.6
https://campuscommune.tcs.com/communities/java-lounge/content/basic-iterations-and-arrays
162 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
8.1. Arrays
We have seen how to declare variable in the previous section. int x; Here x is a variable of type int, so x will be capable of storing an integer value.
At any point of time a variable can hold only a single value.
Few more eg.
int age=20;
int score=70;
Say, I need to store scores of last 5 matches. What kind of data type do I take? May be I will take 5 variables of type int.
int scoreMatch1;
int scoreMatch2;
int scoreMatch3;
int scoreMatch4;
int scoreMatch5;
https://campuscommune.tcs.com/communities/java-lounge/content/arrays-2
163 / 293
Campus Commune
7/31/2014
Similarly, I need to record no. of kms I drove each day in last 5 days.
int kmsDay1;
int kmsDay2;
int kmsDay3;
int kmsDay4;
int kmsDay5;
Similarly, I need to record the amount spent for each day in last 5 days.
double expenseDay1;
double expenseDay2;
double expenseDay3;
double expenseDay4;
double expenseDay5;
The above approach will not be efficient if the requirement(of recording data for a specific period) changes from just last 5 days to last 30
days.
Array:
It is a single variable, capable of storing multiple values (of same data type). But an array does not store the multiple values directly. It is a
collection of more than one variable, of same data type.
Declaring arrays:
The general syntax to declare an array is : data type variable name[]; The square brackets indicate that the variable is an array of the data
type declared.
Example:
int scores [];
int kmsTravelled [];
double expenses [];
char grades[];
Also, all the above arrays are just declared, and have not yet been initialized(not defined the size).
Here scores is not just an int type variable. scores is an array of type int i.e. It is capable of storing more than one int value.
int score = 70; Here the variable score holds a value 70.
Arrays
Here the array scores do not hold the values directly. It is just a reference (pointing to) to the 5 variables score1 score5. Hence arrays are
called references or objects in Java.
If we do not initialize an int variable, its default value is 0. In case of array (as it does not store value directly), if we do not initialize(do not say
to which list of variables it refers to) it, the default value is null. i.e. it does not point to any list of variables.
Similarly, expenses is not just a double type variable. expenses is an array of type double i.e. it is capable of storing more than one double
value.
Similarly, grades is not just a char type variable. grades is an array of type char i.e. it is capable of storing more than one char value.
Once an array is declared, you can think of multiple variables being generated automatically in a sequential order i.e. if an array double[] expense is
https://campuscommune.tcs.com/communities/java-lounge/content/arrays-2
164 / 293
Campus Commune
7/31/2014
declared, the variable that stores the first value will be expense[0], the variable that stores the second value will be expense[1] and so on.
Note: While talking about arrays, always remember two key things. One, about the array itself, whether it is initialized or not. Two, about the
element inside the array whether it is initialized or not.
Initializing arrays using {} initializer:
Arrays have a specific length(or size). The length of the array cannot be changed after it has been initialized. The elements in the array are put
at index of the array. The first element in the array is at index 0, and the last element is at the index length-1.
To declare and initialize an array at the same time, refer to the below syntax:
Example 1:
int[] scores = {55,15, 92,107,67}
{} is called an initializer block. The length (or size) of an array is equal to the no. of values declared in the initializer block.
scores is an int array. Its size is 5. The value of first element is 55, value of second element is 15, value of third element is 92, value of fourth
element is 107 and value of fifth element is 67.
Example 2:
int[] expenses = {1000,1500,50,200,0}
expenses is an int array. It's size is 5. The value of first element is 1000, value of second element is 1500, value of third element is 50, value of
fourth element is 200 and value of fifth element is 0.
Example 3:
String[] softDrinks = {"Coca-Cola", "Pepsi", "Sprite", "Mountain Dew"};
softDrinks is a String array. It's size is 4. The value of first element is Coca-Cola, value of second element is Pepsi, value of third element is
Sprite, value of fourth element is Mountain Dew.
All the above examples, declare an array, initialize the array and also insert elements into the array.
You don't need to mention the length(or size) of an array in [] if you are using the initializer block.
int scores[] = new int[]{55,15, 92,107,67};
The above statement declares an array, initializes an array (of size 5) and also puts elements into the array. The value of first element is 55,
the value of second element is 15, the value of third element is 92, the value of fourth element is 107 and the value of fifth element is 67.
https://campuscommune.tcs.com/communities/java-lounge/content/arrays-2
165 / 293
Campus Commune
7/31/2014
Once the array is initialized, the length(or size) of an array can be known by the length property. It is used after the dot operator on the array
variable. Refer the syntax below:
array.length;
eg.
int scores[] = new int[]{55,15, 92,107,67};
System.out.println(scores.length);
You could either store it in an int variable eg. int size = scores.length;
Putting elements into array:
Step 1:
int scores[] = new int[5];
The above statement will declare an int array of length 5. The values in the array will be initialized to the default values as per their data type.
Step 2:
The first value in the array is accessed by the code : scores[0]. To assign values to the first element, use the assignment operator (=). The
below statement assigns a value 55 to the first element in the array. The remaining elements still have the default value.
scores[0] = 55;
Assuming that the above array is initialized with the given values. The elements of the array are accessed by their index. The first element of the
array is stored at index 0 and the last element is stored at index which is length-1.
To retrieve the value of the elements at index 0, use scores[0] which will return the value 55.
Similarly,
scores[1] will return the value 15
scores[2] will return the value 92
scores[3] will return the value 107
scores[4] will return the value 67
We can use a variable to store the value we retrieve from the array,
https://campuscommune.tcs.com/communities/java-lounge/content/arrays-2
166 / 293
Campus Commune
7/31/2014
The library class has an array of Book, i.e. the array will have multiple objects of Book representing the real books in the library.
> Creating book objects:
Assuming the Book class has parameterized constructor.
Book b1 = new Book(101, Head First Java, Author1);
Book b2 = new Book(102, Head First JSP, Author2);
Book b3 = new Book(103, Java Complete Reference, Author3);
> Creating book array:
Book[] listOfBooks;
The above statement just declares the array variable. It is not initialized so the listOfBooks is still null. Let us initialize the array.
listOfBooks = new Book[3];
The above statement just initialized the array itself, the elements have not yet been initialized. Let us initialize the elements of the array by
assigning the book object value.
> Putting book objects into the book array
listOfBooks[0] = b1;
listOfBooks[1] = b2;
listOfBooks[2] = b3;
The above 3 statement, initializes all the 3 elements to the value of objects b1, b2 and b3 respectively.
> Retrieving objects from the book array
listOfBooks[0] will return the objects b1
listOfBooks[1] will return the objects b2
listOfBooks[2] will return the objects b3
We can use a variable to store the value we retrieve from the array.
Book book = listOfBooks[0]; as listOfBooks is a Book array, listOfBooks[0] is going to return a Book object.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
https://campuscommune.tcs.com/communities/java-lounge/content/arrays-2
167 / 293
Campus Commune
7/31/2014
ATUL VISWANATH
taking the example, int[ ] x, y?
Is y an array or not?
about 1 day ago
KARTHIK SAMPATHKUMAR
What are the other ways to sort the elements of array of objects based on its variables without using Comparator and
Comparable ??
about 17 days ago
NITISH HANDA
Elements cannot deleted from an array???
about 17 days ago
LALITA KUMARI
When I gave the test,there was one qstn whether arrays can be dynamically created?? I marked it as true and i got the
answer wrong.
can anybody help how is it wrong becoz we can create it dynamically and it is known as vector.
about 20 days ago
BHAWNA
hey Lalita
I guess java doesn't support dynamic array.You can go for list and add or remove items from it. So arrays can be
dynamically created would be false.
HARIPRASAD R
I guess u may be logically correct as Vector implements array .Even then i guess the class Array can't be
instantiated dynamically .
PERABATHINI KIRTI
Java does not support the concept of pointers. That could be the reason for not being able to create them
dynamically
SASIVINEETHA GATTUPALLE
Can we initialize arrays like int scores[5]; as in C language? Thank You.
about 22 days ago
PRIYANKA SAWANT
NO,u cannot assign size of array along with its reference.
Bcz the statement int score[]; jst declares an array , array object is not crated until we use new operator.
So we cannot assign a size to areference
PRIYANKA SAWANT
NO,u cannot assign size of array along with its reference.
Bcz the statement int score[]; jst declares an array , array object is not crated until we use new operator.
So we cannot assign a size to areference
PRIYANKA SAWANT
NO,u cannot assign size of array along with its reference.
Bcz the statement int score[]; jst declares an array , array object is not crated until we use new operator.
So we cannot assign a size to areference
https://campuscommune.tcs.com/communities/java-lounge/content/arrays-2
168 / 293
Campus Commune
7/31/2014
PRIYANKA SAWANT
NO,u cannot assign size of array along with its reference.
Bcz the statement int score[]; jst declares an array , array object is not crated until we use new operator.
So we cannot assign a size to areference
PRIYANKA SAWANT
NO,u cannot assign size of array along with its reference.
Bcz the statement int score[]; jst declares an array , array object is not crated until we use new operator.
So we cannot assign a size to areference
PRIYANKA SAWANT
NO,u cannot assign size of array along with its reference.
Bcz the statement int score[]; jst declares an array , array object is not crated until we use new operator.
So we cannot assign a size to areference
PRIYANKA SAWANT
NO,u cannot assign size of array along with its reference.
Bcz the statement int score[]; jst declares an array , array object is not crated until we use new operator.
So we cannot assign a size to areference
PRIYANKA SAWANT
smthng wrng with my browser...multiple ans pasted
SHWETA NAYAK
Is sorting an array easy or difficult?
about 23 days ago
ABUL M
In the above example they had declared as int[] score & char[] grade .Is this a acceptable form to declare a array?
about 2 months ago
DAGGU SAROJA
Yes,this is an acceptable form to declare an array in Java.
This type of declaration is particularly useful in cases when we need to declare multiple arrays of same datatype.
for example,if we want to declare two different arrays: months and days ,both of integer datatype,then instead of
declaring them as:
int months[],days[];
we can declare them as follows:
int[] months,days;
SYED MEHNDI
Or (less preferred)
int num[] = new int[5]
https://campuscommune.tcs.com/communities/java-lounge/content/arrays-2
169 / 293
Campus Commune
7/31/2014
Or (less preferred)
int num[] = {1, 2, 3, 4, 5};
Note: For convenience int[] num is preferable because it clearly tells that you are talking here about array. Otherwise
no difference. Not at all.
Multidimensional array
Declaration
int[][] num = new int[5][2];
Or
int num[][] = new int[5][2];
Or
int[] num[] = new int[5][2];
https://campuscommune.tcs.com/communities/java-lounge/content/arrays-2
170 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
One of the things computers are often used for is the automation of repetitive tasks. Repeating identical or similar tasks without making errors
is something that computers do well and humans do poorly. In this chapter, we shall study in detail how the repeated tasks can be automated
using Java programming.
Let us see a simple java program to print first 10 natural numbers.
Program:
https://campuscommune.tcs.com/communities/java-lounge/content/loops-and-iterations
171 / 293
Campus Commune
7/31/2014
Output:
12345678910
In the above program, we wrote the same line of code 10 times to obtain output. This is nothing but code redundancy(repetition) which
makes our program an inefficient one. To eliminate this we use loops and iterations in Java.
Loop is a block of code executed repeatedly until some condition is satisfied and one pass through(execution of) the loop is called iteration.
Here condition is a boolean expression. A boolean expression is an expression written using conditional operators(<, >, ==, <=,>= etc) and
the result of it will always be either true or false.
Syntax:
while(condition)
{
//Statements
}
Here, while is a keyword and condition is a boolean expression and as long as this condition yields true, the statements within the
braces({}) are executed. You can almost read a while statement as if it were English- while condition is true, continue executing the Statements.
Let us now write the same program(print first 10 natural numbers) using while loop.
Program:
Output:
12345678910
In the above code snippet, we eliminated those 9 repeated lines, clubbed them into a single line and obtained the output using while. Here n
https://campuscommune.tcs.com/communities/java-lounge/content/loops-and-iterations
172 / 293
Campus Commune
7/31/2014
is also called counter variable and it is used to iterate through the loop.
Output:
Roll no 1: Grade is B
Roll no 2: Grade is B
Roll no 3: Grade is C
Roll no 4: Grade is A
Roll no 5: Grade is A
Flow of the above program:
1. Initialize an integer array of percentage.
2. Initialize an integer variable rollNo=0; which is used to iterate through the array.
3. Since we need to generate grades for only those students whose percentage is given, we are specifying the condition in while as
https://campuscommune.tcs.com/communities/java-lounge/content/loops-and-iterations
173 / 293
Campus Commune
7/31/2014
rollNo<percentage.length. We have learned in the previous topic that .length property gives the size of array which is 4 in the above program.
rollNo points to the index of the array. So before entering while loop, the value of rollNo is checked.
4. If the condition is satisfied(if rollNo<percentage.length) go to step 5 else go to step 6.
5. Now control is sent to the body of loop
In the body of while loop, if-else conditions are specified to check the percentage of student marks. Based on those conditions respective grade
is printed.
Increment rollNo.
Go to Step 4.
6. End.
do
{
//statements
}while(condition);
Here, while and do are keywords and condition is a boolean expression. Each iteration of the do-while loop first executes the statements
and then evaluates the condition. If the result of this condition is true, the loop will repeat. Otherwise, the loop terminates. Notice, there is
semicolon at the end of while(); in do... while loop.
Let us now write the same program(print first 10 natural numbers) using do... while loop.
Program:
Output:
12345678910
Now you may get a question as why do.. while when we already have while and when both are giving the same output? We shall study
about it in detail.
Why do... while and how it differs from while?
If the condition controlling a while loop is initially false, then the body of the loop will not be executed at all. However, sometimes it is
desirable to execute the body of a while loop at least once, even if the conditional expression is false to begin with. In other words, there are times
when you would like to test the condition at the end of the loop rather than at the beginning.
Fortunately, Java supplies a loop that does just that: the do-while. The do-while loop always executes its body at least once, because its
condition is at the bottom of the loop. Therefore, while is called entry-called loop whereas do...while is called exit-controlled loop.
The difference is best illustrated using the programs below:
/*Program to demonstrate difference between while and do..while*/
Do-while example:
https://campuscommune.tcs.com/communities/java-lounge/content/loops-and-iterations
174 / 293
Campus Commune
7/31/2014
Output:
do-while: 11
While example:
Output:(no output)
Flow of control(do...while loop):
https://campuscommune.tcs.com/communities/java-lounge/content/loops-and-iterations
175 / 293
Campus Commune
7/31/2014
Output:
Roll no 1: Grade is B
Roll no 2: Grade is B
Roll no 3: Grade is C
Roll no 4: Grade is A
Roll no 5: Grade is A
Flow of the above program:
1. Initialize an integer array of percentage.
2. Initialize an integer variable rollNo=0; which is used to iterate through the array
3. Execute step 4.
4. Now control is sent to the body of loop
i. In the body of do... while loop, if-else conditions are specified to check the percentage of student marks. Based on those conditions respective
grade is printed.
ii. Increment rollNo.
5. Since we need to generate grades for only those students whose percentage is given, we are specifying the condition in while as
rollNo<percentage.length. We have learned in the previous topic that .length property gives the size of array which is 4 in the above program. So
before proceeding with the next iteration, the value of rollNo is checked.
6. If the condition is satisfied(if rollNo<percentage.length) go to step 3 else go to step 7.
7. End
For loop:
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. A for loop
is useful when you know how many times a task is to be repeated.
Syntax:
https://campuscommune.tcs.com/communities/java-lounge/content/loops-and-iterations
176 / 293
Campus Commune
7/31/2014
Increment/decrement is an update statement which gets executed after the execution of body of loop.
Let us now write the same program(print first 10 natural numbers) using for loop.
Program:
Output:
12345678910
Flow of control(for loop):
https://campuscommune.tcs.com/communities/java-lounge/content/loops-and-iterations
177 / 293
Campus Commune
7/31/2014
Output:
Roll no 1: Grade is B
Roll no 2: Grade is B
Roll no 3: Grade is C
Roll no 4: Grade is A
Roll no 5: Grade is A
Flow of the above program:
1. Initialize an integer array of percentage.
2. Declare an integer variable rollNo; which is used to iterate through the array
3. Initial value of rollNo is 0 and this statement is executed only once in the entire cycle of for loop.
4. Since we need to generate grades for only those students whose percentage is given, we are specifying the condition as
rollNo<percentage.length. We have learned in the previous topic that .length property gives the size of array which is 4 in the above program. So
before entering into the for loop, the value of rollNo is checked.
5. If the condition is satisfied(if rollNo<percentage.length) go to step 6 else go to step 7.
6. Now control is sent to the body of loop
i. In the body of while loop, if-else conditions are specified to check the percentage of numbers. Based on those conditions respective grade is
printed.
ii. Increment rollNo.
iii. Go to Step 5.
7. End.
Advanced for loop:
This is an enhanced for loop which mainly used for arrays. Advanced for loop is also called 'for each' loop.
Syntax:
for(declaration : expression)
{
//Statements
}
Declaration: The newly declared block variable, which is of a type compatible with the elements of the array you are accessing. The variable will be
available within the for block and its value would be the same as the current array element.
Expression: This evaluates to the array you need to loop through. The expression can be an array variable or method call that returns an array.
https://campuscommune.tcs.com/communities/java-lounge/content/loops-and-iterations
178 / 293
Campus Commune
7/31/2014
Let us take the same example of grade generation and see how it works in advanced for loop.
Example:
/*Program to generate the grades of 5 students based on their percentage using advanced for loop */
Output:
Percentage is 50: Grade is B
Percentage is 46: Grade is B
Percentage is 32: Grade is C
Percentage is 98: Grade is A
Percentage is 75: Grade is A
In the above program, eachPercentage is a variable that is of the same data type as array of percentage. In the previous program rollNo takes
the index value whereas here eachPercentage takes value at index. Therefore, initially the value of eachPercentage is percentage[0] which is 50 and
in the second iteration eachPercentage will take the value percentage[1] which is 46 and so on.
Nested loops:
We have seen the advantages of using various methods of iteration, or looping. Now let's take a look at what happens when we combine
looping procedures. The placing of one loop inside the body of another loop is called nesting.
When you "nest" two loops, the outer loop takes control of the number of complete repetitions of the inner loop. While all types of loops
may be nested, the most commonly nested loops are for loops.
Let us look at an example on nested loops:
/*Program to print the following pattern
1
12
123
1234
12345
*/
https://campuscommune.tcs.com/communities/java-lounge/content/loops-and-iterations
179 / 293
Campus Commune
7/31/2014
Output:
1
12
123
1234
12345
In the above program, outer loop is used to determine the no.of digits to be printed in a line whereas the inner loop is used to print
the digits. We have used print() within the body of inner loop because we want the digits to be displayed in the same line and after the inner loop is
executed, we go to the next line using println() statement.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SHANKAR PAUL
The program is correct because n is incremented(n++)
about 1 month ago
SHANKAR PAUL
In the example program for do while loop there is no incrementation of the do while loop but still it is printing the list of
numbers as given.How is it possible?
about 2 months ago
SRAVYA SHIVAPURAM
The output given is wrong! The program produces infinite loop as the value of n is not incremented. The output
is infinite number of 0's as the condition is always true (i.e 0<=10).
ANAND GUPTA
I guess they forgot to add an incremental statement while editing from another source
SAGAR JAIN
No its right, Check out the last statement of "do" loop.
Its the increment roll no++ working there.
https://campuscommune.tcs.com/communities/java-lounge/content/loops-and-iterations
180 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/finite-and-infinite-loops
181 / 293
Campus Commune
7/31/2014
Output:
Tata Consultancy Services
Tata Consultancy Services
Tata Consultancy Services
.
.
.
.
This goes on printing until we forcefully stop the execution. The above program is an example of implementing infinite loop using for loop.
Similarly, you can implement an infinite loop using while and do... while as follows.
Infinite loop using while:
while(true)
{
// your code goes here
}
Infinite loop using do... while:
do{
// your code goes here
} while(true);
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/finite-and-infinite-loops
182 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/break-keyword
183 / 293
Campus Commune
7/31/2014
Output:
x is 1
x is 2
Break the loop
Exit
In the above example if the value of x is 3 then the control enters into the loop and when break statement is encountered, it comes out of the
for loop and executes the first statement after the for loop. The break statement can be used in terminating all three loops for, while and do...while.
Working of break statement:
The continue keyword can be used in any of the loop control structures. Continue statement skips the current iteration of a for, while or do
while loop. It causes the loop to immediately jump to the next iteration of the loop.
In a for loop, the continue keyword causes flow of control to immediately jump to the increment/decrement statement.
In a while loop or do/while loop, flow of control immediately jumps to the condition.
Syntax:
continue;
Example: Write a program to print numbers between 1 to 5 except 3.
Output:
x is 1
x is 2
Continue the loop
x is 4
x is 5
https://campuscommune.tcs.com/communities/java-lounge/content/break-keyword
184 / 293
Campus Commune
7/31/2014
Exit
In the above example if the value of x is 3 then the control enters into the loop and when continue statement is encountered, it jumps to the
next iteration without executing the rest of the statements within the loop.
Working of continue statement:
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
GAURAV GUPTA
If continue; statement is before increment/decrement statement in while or do-while loop then loop will become infinite or it
will terminate...?
about 29 days ago
PRIYANKA SAWANT
infinite
DAGGU SAROJA
Whenever continue is encountered in do-while loop,the control should jump to the test expression.But in the figure shown
above for the working of continue statement in do-while loop,it is shown that the control jumps to the starting of the loop.Is
that right?
about 2 months ago
https://campuscommune.tcs.com/communities/java-lounge/content/break-keyword
185 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/practice-problems-on-arrays-using-iterations
186 / 293
Campus Commune
7/31/2014
Output:
https://campuscommune.tcs.com/communities/java-lounge/content/practice-problems-on-arrays-using-iterations
187 / 293
Campus Commune
7/31/2014
Output:Costliest Book in the Library is: Head First JSP with cost of rupees 980.0
Points to remember:
Arrays
Arrays are objects(not primitive) in Java.
You create an object of array either by using the initializer block {} or by using the new operator.
Arrays have a constant length, i.e. the length cannot increase dynamically at run time.
The first index of an array is 0 and the last index is length-1. eg. if the array declared is of size 4, the values are stored at index 0 to 3.
The property length is used to find the length(or size) of an array.
If the elements in an array are not assigned any value, the value will be the default value depending on the type of array. eg. int[] will have
value 0 at index where the value is not initialized, String[] will have value null at index where the value is not initialized.
Iterations
Iterations are used to execute repetitive tasks.
Condition in the while,do... while and for should give result as false after several iterations to terminate the loop.
A loop control variable used to count the iterations is called counter variable.
https://campuscommune.tcs.com/communities/java-lounge/content/practice-problems-on-arrays-using-iterations
188 / 293
Campus Commune
7/31/2014
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/practice-problems-on-arrays-using-iterations
189 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
9.1
HTML Elements
9.2
9.3
HTML Table
https://campuscommune.tcs.com/communities/java-lounge/content/designing-web-pages-part-1-html-and-css
190 / 293
Campus Commune
7/31/2014
9.4
HTML Forms
9.5
CSS
9.6
9.7
9.8
Practice Problems
9.9
https://campuscommune.tcs.com/communities/java-lounge/content/designing-web-pages-part-1-html-and-css
191 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/html
192 / 293
Campus Commune
7/31/2014
The blue text shown in the example is the Markups to indicate the content enclosed in it.
HTML or HyperText Markup Language is the main markup language for creating web pages and other information that can be displayed in a web
browser. It is a markup language that web browsers use to interpret and compose text, images and other material into visual or audible web pages.
Now it is simple. Isn't it? We may summarize the knowledge of HTML as below
A HTML document when composed of the markup elements is saved as a file with a .html or .htm extension. The rendering of the document is
done on any web browser such as Internet Explorer, Google Chrome, etc.,.
HTML Elements
HTML is written in the form of HTML elements consisting of tags enclosed in angle brackets (like <html>), within the web page content. HTML tags
most commonly come in pairs like <h1> and </h1>, although some tags represent empty elements and so are unpaired, for example <img>. The
first tag in a pair is the start tag, and the second tag is the end tag (they are also called opening tags and closing tags). In between these tags web
designers can add text, further tags, comments and other types of text-based content. Most HTML elements have attributes.
The browser does not display the HTML tags, but uses the tags to interpret the content of the page. The purpose of a web browser is to read HTML
documents and compose them into visible web pages.
Let us see each of the HTML element in detail:
DOCTYPE
The <!DOCTYPE> declaration helps the browser to display a web page correctly.
There are many different documents on the web, and a browser can only display an HTML page 100% correctly if it knows the HTML type and
version used
Below is a HTML document showing the <!DOCTYPE> declaration in the first line.
HTML
The <html> element defines the whole HTML document. The element has a start tag <html> and an end tag </html>.
BODY
The <body> element defines the body of the HTML document. The element has a start tag <body> and an end tag </body>.
https://campuscommune.tcs.com/communities/java-lounge/content/html
193 / 293
Campus Commune
7/31/2014
Headings
HTML headings are defined with the <h1> to <h6> tags
Output of the above code may be viewed using a web browser as below:
https://campuscommune.tcs.com/communities/java-lounge/content/html
194 / 293
Campus Commune
7/31/2014
The sample HTML document consisting of these head elements is shown here with associated output screen
https://campuscommune.tcs.com/communities/java-lounge/content/html
195 / 293
Campus Commune
7/31/2014
Sample code for some of the formatting tags are given below. The output has also been displayed along with.
HTML Attributes
Attributes in HTML element provide additional information about the element.
https://campuscommune.tcs.com/communities/java-lounge/content/html
196 / 293
Campus Commune
7/31/2014
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
TANAY DAS
need help plz:
1.adding a border to an image help to identify link??frame?? or beauty of the image??? {frame-wrong}
2.<td>...</td> for inserting steps?? column?? row?? table??? {row-wrong}
3.Identify the invalid tag? a) <fontface='verdana'> b) <fontface='verdana,arial'> c) <font_face='verdana'> d)
<font='verdana'> (d-wrong)
4.adding alternative text to images will help user to save image in this name// or to get idea before it loads//or help user
when they wishes to insrt another picture (dnt knw)
about 14 days ago
SUMAN SINGH
1) just for the beauty and nothing else
2)
<td> is used to enter a data field in the tr.
for example in the last table of this page - the html attribute u would use
<tr>
<td>class</td>
<td> Specifies one or more.... </td>
</tr>
In short tr will be used to enter a row and td will be used to enter a data in that row. each td would enter a value
in a different column
3. question not clear. you have already mentioned the wrong answer
4. it will help in the SEO of the image.. it will be displayed when the image is not displayed
SAYANTAN GANGOPADHYAY
1)width of border around image
2)table
3)b
4)get idea before it loads
SAYANTAN GANGOPADHYAY
sorry the ans of ques 2 is column
SHYAM VASANI
When the IRA will be conducted? Will it be on the first day of joining or there will be gap of few days?
about 15 days ago
ATUL VISWANATH
On the second day of ILP.
https://campuscommune.tcs.com/communities/java-lounge/content/html
197 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/html-links-images-division-list
198 / 293
Campus Commune
7/31/2014
HTML Images
In HTML, images are defined with the <img> tag. The <img> tag is empty, which means that it contains attributes only, and has no closing tag.
Some of the attributes of the image tag are
src Indicates the source or the URL of the image that is to be displayed
alt alternate name or text for the image
height, width attributes describing the size
Below is an example (image will be visible if it is placed in same directory where we have html page created and image file is named as smiley.gif):
HTML Division
The <div> tag defines a division or a section in an HTML document. The <div> tag is used to group block-elements to format them with CSS. The
attributes are
class and style.
https://campuscommune.tcs.com/communities/java-lounge/content/html-links-images-division-list
199 / 293
Campus Commune
7/31/2014
Note: The <div> element is very often used together with CSS, to layout a web page.
A Simple Example to show the <div> usage:
HTML LIST
HTML provides the means for producing three types of lists: unordered (ie., unnumbered) and ordered (ie., numbered) lists, Definition (I.e define
the terms like in glossary).The UL, OL and DL elements are block elements.
Unordered Lists:
An unordered list typically is a bulleted list of items. This is probably the most common type of list on the Web. The <UL> tag opens an unordered
list while </UL> closes it. Between these tags are placed list items with an <LI> tag as follows:
type : Determines the form of bullet which precedes the list item placed within an unordered list. Values are disc,square,circle
OUTPUT
red
yellow
blue
Ordered Lists:
An ordered list is formatted exactly the same as an unordered list, except that <OL> tags are used instead of <UL>. In an ordered list, sequential
numbers are generated automatically, as shown below:
type attribute Determines the form of bullet which precedes the list item placed within an unordered list. Values are 1,a, A,i,I.
OUTPUT
1. red
2. yellow
3. blue
https://campuscommune.tcs.com/communities/java-lounge/content/html-links-images-division-list
200 / 293
Campus Commune
7/31/2014
Definition
This contains a series of terms and definitions, and would typically be used in a glossary.
OUTPUT:
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
DIVJYOT MAKKAR
In the first example my paragraph and the next line are enclosed in the<a> tag,then why is the outcome not underlined?
DIVYA PANICKER
It is because href attribute is not used. When href is used the text enclosed in the <a> tag becomes an
underlined link that can be clicked to navigate to a new page. The url of this new page is the value of the href
attribute written within " ".
https://campuscommune.tcs.com/communities/java-lounge/content/html-links-images-division-list
201 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/html-table
202 / 293
Campus Commune
7/31/2014
Table rows
Table rows are created using the <tr> tag which should go inside the <table> tag. Use an opening <tr> tag and its closing tag </tr> for every row
in the table.
for example
Table cells
Table cells are created using the <td> tag which should go inside the <tr> tag. Use an opening <td> tag and its closing tag </td> for every cell in
the table.
Output
https://campuscommune.tcs.com/communities/java-lounge/content/html-table
203 / 293
Campus Commune
7/31/2014
Also you can add headings & captions to tables, male cells span multiple rows & columns, align the content within cells, and more.
Empty cells
Sometimes you may not need to place content inside of a table cell, instead you just need the cell to be there. But a table cell may not appear
unless it has content inside of it.
You can include empty cells in a table by placing the character entity inside a table cell.
Output:
Headings
You can display headings in a table using the <th> tag. A heading is an emphasized cell in a table. Use the <th> tag just like the <td> tag.
Output:
Captions
A caption is added to a table using the <caption> tag which should be placed right below the <table> tag before the first <tr> tag.
https://campuscommune.tcs.com/communities/java-lounge/content/html-table
204 / 293
Campus Commune
7/31/2014
Output
Output
https://campuscommune.tcs.com/communities/java-lounge/content/html-table
205 / 293
Campus Commune
7/31/2014
Output
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
DIVJYOT MAKKAR
In the example of spanning a row <tr> tag has been started twice which is also underlined red.does it have a significance?
about 12 days ago
VSKCHAITANYA CHALAMCHERLA
By giving <tr> , you can observe in the output that gap between the two successive rows is increased..
for more clarity just try by giving 3 to 5 "<tr> " and verify output..
https://campuscommune.tcs.com/communities/java-lounge/content/html-table
206 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/html-table
207 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
A form will take input from the user using different form controls and form elements and then the data is passed to server for processing.
Users interact with forms through form controls.
Each control has both an initial value and a current value.
A control's initial value may be specified with the control element's value attribute.
The control's current value is first set to the initial value. Thereafter, the control's current value may be modified through user interaction and
https://campuscommune.tcs.com/communities/java-lounge/content/html-forms
208 / 293
Campus Commune
7/31/2014
The control's current value is first set to the initial value. Thereafter, the control's current value may be modified through user interaction and
scripts.
A control's initial value does not change. Thus, when a form is reset, each control's current value is reset to its initial value.
HTML defines the following control types:
Buttons
HTML provides three types of buttons:
Submit buttons: When activated, a submit button submits a form.A form may contain
Example for submit button:
Radio buttons :
Radio buttons are also on/off switches that may be toggled by the user.
They are similar to checkboxes, but only one option can be selected from the set.
e.g : <input type=radio name=color value=red><input type=radio name=color value=blue> can be used to create a radio
button control with two values as red and blue.
Example:
Menus
Menus are used when we want to choose a option from set of available options.
The select element is used to create a menu, and option element is used to define options of that menu.
e.g: <select> <option> red </option> <option> blue </option> </select> can be used to create a menu with two options as red and blue.
Text Fields & Textareas
When it comes to text controls within forms there are a few different elements available to obtain data. Text fields and textareas are specifically
used to gather text or string based data. This may include data containing passages of text content, passwords, telephone numbers, and so forth.
Text Fields
One of the primary elements used to obtain text from users is the input element. The input element uses the type attribute to determine what type
of information is to be captured within the specific control. The most popular type attribute value is text, which denotes a single line text input.
Along with setting a type attribute it is also best practice to give an input a name attribute as well. The name attribute is used as the name of the
https://campuscommune.tcs.com/communities/java-lounge/content/html-forms
209 / 293
Campus Commune
7/31/2014
control and is submitted along with the inputs data to the server.
Textarea
Another element used to capture text based data is the textarea element. The textarea element differs from the text input in that it is for larger
passages of text spanning multiple columns. The textarea also has start and end tags which can wrap plain text. Since the textarea element only
accepts one type of value the type attribute doesnt apply here, however the name attribute is still in effect.
Text input
HTML provides two types of controls that allow users to input text.
The input element creates a single-line input control and textarea element creates a multi-line input control.
Example:
File select
This control type allows the user to select files so that their contents may be submitted with a form.
The input element is used to create a file select control.
Drop Down Lists:
Drop down lists are a perfect way to provide users with a long list of options in a usable manner. Outputting every state within the country on a
page with radio buttons would create a rather cluttered and daunting list. Drop down menus provide the perfect venue for a long list of options.
To create a drop down menu the select and option elements are used. The select element will wrap all of the different menu options marked up
using the option element. Then you can apply the name attribute to the select element.
Each individual option within the menu needs to be coded using the option element. The option element will wrap the text to be included within
the menu. Additionally, the option element will include the value attribute, specific to each individual option.
As with the checked Boolean attribute with radio buttons and checkboxes, drop down menus can use the selected Boolean attribute to preselect an
option for users.
https://campuscommune.tcs.com/communities/java-lounge/content/html-forms
210 / 293
Campus Commune
7/31/2014
Multiple Selections
Using the standard drop down list and adding the Boolean attribute multiple to the select element allows a user to choose more than one option
from the menu. Additionally, using the selected Boolean attribute on more than one option element within the menu will preselect multiple options.
The height and width of the select element can be controlled using CSS and should be adjusted appropriately to allow for multiple selections. It
may also be worth mentioning to users that to choose multiple options they will need to hold down the shift key while clicking to make their
selections.
Form Buttons
After a user inputs the requested information, buttons allow them to put that information into action. Most commonly, a submit button is used to
process the data. A reset button is often used to clear data.
Submit Button
Users click the submit button to process data after filling out a form. The submit button uses the input element with a type attribute of either
submit or image. The submit attribute value is the most common as it is simple to use and provides the most control. The image attribute value is
used specifically to set an image as a submit button, however with the use of CSS the need to use an image has greatly diminished.
To determine the verbiage to be used within the button, the value attribute is used. Using CSS properties such as background, border-radius, boxshadow, and others, the button can then be styled to any specific desire.
Reset Button
To take the complete opposite action from submitting a form, users may also reset a form using the reset button. The reset button code works just
like that of the submit button, however it uses the reset value for the type attribute.
Reset buttons are becoming less and less popular because they offer a very strong action, often undesired by both websites and users. Users may
spend quite a bit of time filling out a form only to click the reset button accidentally thinking it was the submit button. Chances of a user filling out
the form again thereafter are small. Before using a reset button think of any potential consequences.
https://campuscommune.tcs.com/communities/java-lounge/content/html-forms
211 / 293
Campus Commune
7/31/2014
Other Inputs
Outside all other previously stated choices the input element does have a few other use cases. Two of which include passing hidden data and
attaching filings during form processing.
Hidden Input
Hidden inputs provide a way to pass data to the server without displaying it to users. Hidden inputs are typically used for tracking codes, keys, or
other information not pertinent to the users but helpful to the website overall. This information is not displayed on the page, however it could be
seen by viewing the source code of a page. That said, it should not be used for sensitive or secure information.
To create a hidden input you use the hidden value for the type attribute. Additionally, you pass along the appropriate name and value.
File Input
To allow users to add a file to a form, much like that of attaching a file to an email, the file value for the type attribute is used. The file input is most
commonly seen to upload pictures or files to social networks or applications.
Unfortunately, stylizing the file input is a tough task with CSS. Each browser has its own default rendering of how the input should look and doesnt
provide much control to override the default styling. JavaScript and other solutions can be built to allow for file input, but they are slightly more
difficult to construct.
When using labels with radio buttons or checkboxes the input element can be wrapped within the label element. Doing so allows omission of the
for and id attributes.
https://campuscommune.tcs.com/communities/java-lounge/content/html-forms
212 / 293
Campus Commune
7/31/2014
Fieldset
Fieldsets group form controls and labels into organized sections. Much like a div or other structural element, the fieldset is a block level element that
wraps related elements, however specifically within a form for better organization. Fieldsets by default also include a border outline that can be
modified using CSS.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
ABDUL HAQ
Can any please tell the purpose of <label>
about 1 month ago
TEJASWINI
The <label> tag defines a label for an <input> element.
The <label> element does not render as anything special for the user. However, it provides a usability
improvement for mouse users, because if the user clicks on the text within the <label> element, it toggles the
control.
ALURI REDDY
The HTML <label> tag is used to adda label to a form control like text, textarea etc.
it provides a usability improvement for mouse users, because if the user clicks on the text within the <label>
element, it toggles the control.
https://campuscommune.tcs.com/communities/java-lounge/content/html-forms
213 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
9.5. CSS
Introduction
Css stands for cascading style sheets. Styles define how html tags are displayed or formatted in the browser. Styles can be applied to html tags to
format the look and feel of the html pages. By defining CSS based styling, the styling/formatting code can be separated from html code.
Styles are normally defined in an external file called style sheets which have a .css extension. The stylesheets are then imported into the html file.
CSS works by defining rules how a selector (html tag) can be displayed.
CSS Syntax
A CSS rule set consists of a selector and a declaration block. The general syntax of a CSS rule set looks like this:
Selector {property:value; property:value; . . .}
Selector points to the html element that needs to be formatted
Property points to the specific html element's attribute that is being formatted
Value is the value given to the attribute that is used for formatting
More than one attribute/property of the html element can be specified in the CSS ruleset. The different sets of properties/value pairs are separated
https://campuscommune.tcs.com/communities/java-lounge/content/css
214 / 293
Campus Commune
7/31/2014
by a semi-colon. To improve the readability the property:value pairs can be written on separate lines.
p{color:blue; text-align:left;}
The ruleset above formats the color of the text to blue inside a html <p> tag and aligns the text to left.
p
{
color:red;
font-style:italic;
font-family:arial;
}
The ruleset above formatted the color of text to red inside a html <p> tag, and formats the text to italics and sets to Arial font.
Grouping Selectors
In style sheets there are often elements with same styling. To minimize code the selectors can be grouped. To group selectors separate each
selector with a comma.
Objective: Group selectors to apply same styling to multiple html elements
html
https://campuscommune.tcs.com/communities/java-lounge/content/css
215 / 293
Campus Commune
7/31/2014
#p1,h1
{
text-align: left;
color:red;
}
The ruleset above will find the html para and heading elements with the specified id values and apply the styling.
Class selector
The class selector uses the html class attribute of an html tag to find the html elements. Unlike id selector class is generally not given a
unique value. Thus the class attribute can be used to apply styling to more than one html element that have the same value for the class
attribute.
In order to use the class selector, first the html element is given a class name. The name of the class can be a character, string or a combination
of characters and numbers. The class name cannot start with a number. Then to find the emelemt using the class name write a period character
followed by the class name.
p.center, tr.center
{
text-align: center;
color:red;
}
The ruleset above finds all the html para and table row elements with class=center and applies the styling.
A class selector can be created free of a tag name in which case all the tags that have the same class name will be styled similarly.
.right
{
text-align:right;
color:blue;
font-family:calibri;
}
https://campuscommune.tcs.com/communities/java-lounge/content/css
216 / 293
Campus Commune
7/31/2014
The id selector can be applied to one , unique element compared to the class selector which can be applied to several different elements.
Objective: To apply style using id and class attributes.
'.' in front of each tag defines a class and corresponding class is mentioned in
We can use '#' to define style for a id and give the corresponding id for the element in HTML page. Id for each element in HTML page is always
unique. No two elements in an HTML file should have the same id.
Output:
Element selector
The element selector selects html elements based on the element name. When styling is applied based on the element name all elements that
have the same name will be applied with the same styling.
https://campuscommune.tcs.com/communities/java-lounge/content/css
217 / 293
Campus Commune
7/31/2014
p
{
text-align: center;
color: red;
}
For example the ruleset above will format all the <p> elements in the html document with the styling mentioned in the ruleset.
Objective: Use of tags to apply style in stylesheets
The styling is applied by using the html element name. In this case the style is applied using the style is applied to the html element h3.
Output
https://campuscommune.tcs.com/communities/java-lounge/content/css
218 / 293
Campus Commune
7/31/2014
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SATYANARAYAN K
Where do we need to write the CSS code?In the same notepad file as the html file or Any Special Software Required??
SUMAN SINGH
you can write it in the html code itself using this code in <head> section
<style type="text/css">
your css code
</style>
You can also write the css in another file, save it with .css extension and import this file in your html file
SUMAN SINGH
you can write it in the html code itself using this code in <head> section
<style type="text/css">
your css code
</style>
You can also write the css in another file, save it with .css extension and import this file in your html file
https://campuscommune.tcs.com/communities/java-lounge/content/css
219 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
https://campuscommune.tcs.com/communities/java-lounge/content/inserting-css-into-html
220 / 293
Campus Commune
7/31/2014
The import rule can also be used to import an external css file.
<head>
<style type="text/css">
@import url(mystyle.css);
</style>
</head>
The external style sheet can be written in any text editor. The file should contain only the styling code and should not contain any html tags. The
style sheet must be saved with a .css extension.
Objective: To apply style using an external CSS file and then include it in the HTML page.
Create a CSS file named style.css (or any other name of your choice) and write the code shown in the image below.
'.' in front of each tag defines a class and corresponding class is mentioned in the element used in HTML page.
style.css
Create a HTML file in the same folder where the CSS file is created. HTML file should contain the following code.
The style sheet is imported in the head section using the html link tag.
Output:
https://campuscommune.tcs.com/communities/java-lounge/content/inserting-css-into-html
221 / 293
Campus Commune
7/31/2014
<head>
<style>
.left
{
color:blue;
text-align:left;
font-family:"calibri";
}
</style>
</head>
Objective: To apply internal style to div tags, so that two div tags are displayed side by side. The style applied is present inside style tags in the
head section of HTML file.
Output:
https://campuscommune.tcs.com/communities/java-lounge/content/inserting-css-into-html
222 / 293
Campus Commune
7/31/2014
Inline styles
Inline styles are written as part of the html tag itself. The style attribute of the html tag is used and can contain any css property. This way of
styling should be used only sparingly and when the other two ways of inserting syles is not feasible since it is least flexible.
Output:
https://campuscommune.tcs.com/communities/java-lounge/content/inserting-css-into-html
223 / 293
Campus Commune
7/31/2014
Output:
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/inserting-css-into-html
224 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
Wait till the entire page gets loaded. Though the latest version of Notepad++ is available we recommend you to use version 5.9.4. Click
Notepad++ v5.9.4 zip package.
https://campuscommune.tcs.com/communities/java-lounge/content/different-tools-to-create-ui
225 / 293
Campus Commune
7/31/2014
You may download and place the archive (zip) at any location of the hard disk of your system. Then the archive needs to be extracted using any of
the archiving tools provided by your OS. The installation of notepad++ is done.
1. Open Notepad++
Open notepad++ from the Notepad++ installation directory. The Notepad++ installation directory will have a notepad++ application as
highlighted below.
https://campuscommune.tcs.com/communities/java-lounge/content/different-tools-to-create-ui
226 / 293
Campus Commune
7/31/2014
If you observe the code, all the HTML tags are colored in blue and comments in green. This is an advantage with Notepad++. So we can clearly
make out in case of any typing mistakes.
Course 3 - Save this file as .html/.htm type and file name can be anything of your choice. Let us name it MyFirstHTML
To execute the saved HTML file, you can follow two ways:
1. Open from saved location
Course 1 - Now your HTML file is saved and if you open the location where it is saved, the icon of the file would be your default browser icon as
following:
Course 2 - Double click the file. The file is opened with your default browser.
https://campuscommune.tcs.com/communities/java-lounge/content/different-tools-to-create-ui
227 / 293
Campus Commune
7/31/2014
Here, you can see that attributes of script tag are distinctly colored, if you try to define any other attribute which is not predefined, you will find it in
black.
Save this file as .html/.htm type and execute in the same way as we did for HTML.
https://campuscommune.tcs.com/communities/java-lounge/content/different-tools-to-create-ui
228 / 293
Campus Commune
7/31/2014
Save the program and execute it in the same way as we did for HTML and JavaScript.
Refer the below link which would help you debug your web pages opened with Internet Explorer:
http://msdn.microsoft.com/library/gg589507(VS.85).aspx
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/different-tools-to-create-ui
229 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
230 / 293
Campus Commune
7/31/2014
Design a UI page / screen to capture the following details when registering a new doctor in HMS.
First name, Middle name Last name, Registration No, Date of Birth , Gender(use radio button), Email, Specialization , Qualification, contact number,
Address, Visiting Time, Visiting Days(multiple selection should be allowed), NoOfPatientsPerDay, DepartmentNo.
Indicate the mandatory fields on the page. All fields except middle name and address are mandatory.
Use below mentioned sample values for drop down/check boxes:
Visiting Days Mon,Tues,Wed,Thurs, Fri,Sat,Sun.
Visiting Time Morning, Afternoon, Evening
Add a department
Design a UI page / screen to capture the following details when adding a new department in HMS.
Department name, Department Description(use text area), No. Of Doctors, Number of general rooms, Number of ac rooms, Number of non ac
rooms , Number of ICU , Cost of general room, Cost of ac room, Cost of non ac room, Cost of each ICU.
All fields are mandatory.
Add a patient
Design a UI page / screen to capture the following details when adding a new patient in HMS.
First Name, Last Name , Date of birth, Gender(Use radio button), Father/Spouse name, blood group(dropdown), Contact number, Visited
earlier(check box)
All fields are mandatory except contact number.
Indicate mandatory fields on the page / screen.
Use below mentioned values in the dropdown :Blood Group O+,O-,AB+,AB-,A+,A-,B+,B-,Others
Click here to download the solutions
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/reference-links-and-videos-6
231 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
Q
9.9. Reference Links & Videos
Reference Links
To know more on HTML and CSS, visit the URLs:
http://en.wikipedia.org/wiki/HTML
http://www.howtocreate.co.uk/tutorials/html/basics
http://www.htmlgoodies.com/tutorials/html_401/
http://www.w3schools.com/html/default.asp
http://html.net/tutorials/html/
http://www.tutorialspoint.com/html/what_is_new_in_html4.htm
http://www.w3resource.com/html/doctype-HTML.php
http://www.alternetwebdesign.com/htmltutorial/lesson3.htm
https://campuscommune.tcs.com/communities/java-lounge/content/reference-links-and-videos-10
232 / 293
Campus Commune
7/31/2014
http://www.w3schools.com/css/DEFAULT.asp
http://learnlayout.com/
Related Videos
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SHIVSHANKAR MURALI
While taking up the test, I got an error message stating that the webpage could not start and followed by some Raw process
output text. Unable to take up the test. What should I do now ?
I have a screenshot of the error. Unable to upload :(
about 17 days ago
DIVYA PANICKER
Contact ilp or campus commune team.
https://campuscommune.tcs.com/communities/java-lounge/content/reference-links-and-videos-10
233 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
10.1
Introduction
10.2
Conditional Operations
https://campuscommune.tcs.com/communities/java-lounge/content/designing-web-pages-part-2-javascript-and-jquery
234 / 293
Campus Commune
7/31/2014
10.3
10.4
jQuery Overview
10.5
jQuery Attributes
10.6
10.7
jQuery CSS
10.8
jQuery DOM
10.9
jQuery Events
10.10
jQuery Effects
10.11
Practice Problems
10.12
https://campuscommune.tcs.com/communities/java-lounge/content/designing-web-pages-part-2-javascript-and-jquery
235 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
10.1. Introduction
JavaScript is a scripting language that is used in most of the modern HTML pages.
It gives a dynamic touch to them.
JavaScript is used to change the content of HTML elements dynamically. It can also be used to read the content of HTML elements and validate
them and eventually show corresponding message to the user.
Just like we have tags for HTML elements, JavaScript is written inside <script></script> tags. By convention script tags are placed inside head tag,
although it is not necessary to do that.
Below is a sample of JavaScript code, in which an alert method is used which is used to display messages as pop-up.
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-18
236 / 293
Campus Commune
7/31/2014
OUTPUT:
When the browser loads the web page, the scripts are also loaded. Thus in the example above, as soon as the web page is loaded the browser ran
the script and the alert method was executed.
The JavaScript can be included into the web page in two ways.
We can either write the code in script tag or write the code in another external file, save it with .js extension and include in the web page.
For example we can write alert('hello'); in a text file and save it as myfile.js
Then we can include the js file in the web page.
<script src=myfile.js></script>
Variables
Variables are used to store data and manipulate it, just like we had in Java.
A variable in JavaScript is declared as:
var variableName=data;
To declare a variable, var keyword is used. The data type of the variables is determined by the type of data they hold.
eg:
var x=George; //string
x='a';
//char
x=5;
//int
x=4.5
//float
x=true;
//boolean
x=new Array(2,4,6,3); //array of integer
OUTPUT:
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-18
237 / 293
Campus Commune
7/31/2014
Here we can see that same variable was able to store different values at different times. So java script is type independent.
Events
An event is defined as an action performed over an HTML element.
On various actions performed by mouse or keyboard JavaScript code can be called.
We will discuss about four basic events. onclick, onsubmit, onchange, onselect
onclick event occurs when we click on any HTML element. lets see an example of clicking a button.
OUTPUT:
When we click on a button, onclick event occurrs and the alert method is called as per the code.
onsubmit event occurs when the form is being submitted. Here is a sample code.
OUTPUT:
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-18
238 / 293
Campus Commune
7/31/2014
OUTPUT:
onselect event occurs when text of an input tag is selected. Sample code:
OUTPUT:
When the text in the text box is selected onselect event occurs and according to the code the alert method was called.
Functions
All java script codes written inside script tag gets executed after the page is loaded by the browser. If we want to execute specific functionality on
various events we should put the JavaScript codes inside functions and call those function at the occurrence of events.
You must be familiar with method calling in Java. It is similar to that.
To write a function , the key word function is used, followed by the function name and the arguments.
funciton myFunction(argument1, argument2....){
var value=10;
return value;
}
A variable declared inside a function becomes a local variable and thus can be used within the function only. A variable declared outside a
function becomes a global variable and thus can be accessed by any function.
Sample Code:
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-18
239 / 293
Campus Commune
7/31/2014
OUTPUT:
In the above example, a logic for calculating area of a circle is placed. Then on the event of clicking on the button, the function was called and a
number 5 was passed as an argument. The area of the circle was shown through an alert box.
A function can call another function and return value to the calling function.
Here is an example.
OUTPUT
In this example add function was called by the calculate function and it returned the sum the numbers.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/introduction-18
240 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/conditional-operations-1
241 / 293
Campus Commune
7/31/2014
OUTPUT
Here two numbers are compared and it is determined which number is greater.
For Loop
In JavaScript there are many ways to iterate, one of them is to use a for loop.It is used in the same way as in Java.
Sample Code:
OUTPUT
Here we iterated through the array of integers and calculated the sum of those numbers.The alert method showed the result on the screen.
This can also be achieved by placing the code inside a function and then calling the function through an event on HTML element.
PopUp Boxes
In JavaScript we can create small pop-up windows to either show message or get input from user. JavaScript provides three type of popup
boxes:
Alert Box, Confirm Box and Prompt Box.
You have already seen alert box functionality, it is used to show messages as pop up.
Confirm box, as the name suggests, is used for confirmations. It returns either true or false when we click on Ok and Cancel respectively.
Syntax: Event=return confirm(Message);
https://campuscommune.tcs.com/communities/java-lounge/content/conditional-operations-1
242 / 293
Campus Commune
7/31/2014
when we write return the boolean value returned will determine whether the form will be submitted or not.
Sample Code:
OUTPUT
When you click on Cancel, false will be returned and the form will not be submitted.
When you click on OK, true will be returned and the form will proceed with the submission.
Prompt Box is used when we want an input from the user through the popup box.
Syntax: prompt(Message,default value);
The default value is not necessary to give and can be skipped. The return type is string that we enter in the box.
Sample code:
OUTPUT
https://campuscommune.tcs.com/communities/java-lounge/content/conditional-operations-1
243 / 293
Campus Commune
7/31/2014
In the code we used inbuilt function parseInt() to convert string into int.
When Cancel is pressed on a prompt box, it returns null.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SASIVINEETHA GATTUPALLE
In the sample code of the prompt box the variables are x and y. Then wat are parseInt(x) and parseInt(y)? Do they specify
anything? Thank You.
about 17 days ago
MADDI
The value that is accepted in the prompt box is a String. In order to change it to the data type needed, we use
the wrapper classes. In the code given above, we need to find sum of integers. So we have used parseInt().to
convert x and y to int.
SWARNADEEP SAHA
Hi.. Maddi,
I think there is no integer datatype in JavaScript. All arithmetic data are shared "number" variable which is a 64
bit floating point. (IEEE-754).
https://campuscommune.tcs.com/communities/java-lounge/content/conditional-operations-1
244 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/document-object-model-and-validations
245 / 293
Campus Commune
7/31/2014
In the JavaScript function we can access the elements of the page either by traversing through the elements or by using some of the methods of
these objects.
getElementById:
This method is used to access an element using the unique id we assign to it while creating the page.
Sample Code:
OUTPUT
In the above example var x=document.getElementById("name") was used to access the text field having id=name.
Here the document object's method getElementById was used.
The same could be achieved by writing
var x=document.forms["myForm"]["name"].value where we use the 2D form of document model.
or
var x=document.myForm.firstName.value where we cascade through elements using their names.
getElementsByName:
This method is used to get all the elements with the given name.
Sample code:
https://campuscommune.tcs.com/communities/java-lounge/content/document-object-model-and-validations
246 / 293
Campus Commune
7/31/2014
OUTPUT
Here all the elements by that name are stored as array of elements in variable x. After that we can we can iterate through x and get all the elements
and their value.
getElementsByTagName:
This method is used to get all the elements with the name of the tag.
Sample code:
OUTPUT
https://campuscommune.tcs.com/communities/java-lounge/content/document-object-model-and-validations
247 / 293
Campus Commune
7/31/2014
Here all the elements by the Tag name input are stored as array of elements in variable x. After that we can we can iterate through x and get all
the elements and check whether they are checked or not.
Validations
We can use JavaScript to validate the data entered by the users. For instance, we can check whether user has entered into the text field or not,
whether user has enter a string in a number field, whether user has exceeded the world limit etc.
In the example below we will be validating a form based on the criteria mentioned above.
OUTPUT
https://campuscommune.tcs.com/communities/java-lounge/content/document-object-model-and-validations
248 / 293
Campus Commune
7/31/2014
Here first validations were did to find out whether the fields were left blank or not.
If any field was left blank, proper message would be shown.
Then the mobile number was validated for being a number first. When it is confirmed that its a number only, then its length was checked. If the
length of the number is other than 10 then that means the number was not valid, so the error message was shown in the alert box.
Radio button validation
To check whether a radio button or check box is checked or not, simply first get the element by name, id or tag name and write the following
conditional code:
if(x.checked), where x is the radio element or check box element. Thus we can validate each HTML element using JavaScript.
Sample code:
https://campuscommune.tcs.com/communities/java-lounge/content/document-object-model-and-validations
249 / 293
Campus Commune
7/31/2014
OUTPUT
The above sample code that checks whether a radio button is checked or not.
As soon as JavaScript finds that the radio button is selected it return true and the form proceeds with submission otherwise false is returned and
the form is not submitted. A corresponding message is also shown through the alert box.
Select box validation
Now let us see how to validate a select box.
OUTPUT
https://campuscommune.tcs.com/communities/java-lounge/content/document-object-model-and-validations
250 / 293
Campus Commune
7/31/2014
In the above example first the select element was fetched through the document object. Then the index of the selected option was checked. The
index starts from 0. So after clicking the submit button if the index of selected option remains same, then that would mean the user did not select
any option.
If the selected index is not 0 then the code would show the value of selected option through alert box. i.e x.value here would give the selected
option value.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/document-object-model-and-validations
251 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
252 / 293
Campus Commune
7/31/2014
Cross Browser Support: jQuery has cross-browser support, and works well in IE 6.0+, FF 2.0+, Safari 3.0+, Chrome and Opera 9.0+
Latest Technology: jQuery supports CSS3 selectors and basic XPath syntax.
jQuery Installation
To start using jQuery on your web site, download the jQuery library from jQuery.com. There are two versions of jQuery available for downloading.
Both versions can be downloaded from jQuery.com.
1) Production version - this is for your live website because it has been minified and compressed
2) Development version - this is for testing and development (uncompressed and readable code)
The jQuery library is a single JavaScript file, and you reference it with the HTML <script> tag (notice that the <script> tag should be inside the
<head> section):
jQuery reads or manipulates the document object model (DOM), we need to make sure that we start adding events etc. as soon as the DOM is
ready.
If you want an event to work on your page, you should call it inside the $(document).ready() function. Everything inside it will load as soon as the
DOM is loaded and before the page contents are loaded.
To do this, we register a ready event for the document as follows:
To call upon any jQuery library function, use HTML script tags as shown below:
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-overview
253 / 293
Campus Commune
7/31/2014
When you click on the text in on the page, following result is seen:
It is better to write our custom code in the custom JavaScript file : custom.js, as follows:
jQuery Selectors
The jQuery library harnesses the power of Cascading Style Sheets (CSS) selectors to let us quickly and easily access elements or groups of elements
in the Document Object Model (DOM).
All type of selectors available in jQuery, always start with the dollar sign and parentheses: $(). A jQuery Selector is a function which makes use of
expressions to find out matching elements from a DOM based on the given criteria. The factory function $() is a synonym of jQuery() function. So
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-overview
254 / 293
Campus Commune
7/31/2014
expressions to find out matching elements from a DOM based on the given criteria. The factory function $() is a synonym of jQuery() function. So
in case you are using any other JavaScript library where $ sign is conflicting with some thing else then you can replace $ sign by jQuery name and
you can use function jQuery() instead of $().
The factory function $() makes use of following three building blocks while selecting elements in a given document:
1) Tag Name
Represents a tag name available in the DOM. For example $('p') selects all paragraphs in the document.
2) Tag ID
Represents a tag available with the given ID in the DOM. For example $('#some-id') selects the single element in the document that has an ID of
some-id.
3) Tag Class
Represents a tag available with the given class in the DOM. For example $('.some-class') selects all elements in the document that have a class of
some-class.
All the above items can be used either on their own or in combination with other selectors. All the jQuery selectors are based on the same principle
except some tweaking.
Following is a simple example which makes use of Tag Selector. This would select all the elements with a tag name p.
The result appear as show below and different alert messages are displayed for every paragraph on the page:
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-overview
255 / 293
Campus Commune
7/31/2014
Following table lists down few basic selectors and explains them with examples.
Similar to above syntax and examples, following examples would give you understanding on using different type of other useful selectors:
$('*'): This selector selects all elements in the document.
$("p > *"): This selector selects all elements that are children of a paragraph element.
$("#specialID"): This selector function gets the element with id="specialID".
$(".specialClass"): This selector gets all the elements that have the class of specialClass.
$("li:not(.myclass)"): Selects all elements matched by <li> that do not have class="myclass".
$("a#specialID.specialClass"): This selector matches links with an id of specialID and a class of specialClass.
$("p a.specialClass"): This selector matches links with a class of specialClass declared within <p> elements.
$("ul li:first"): This selector gets only the first <li> element of the <ul>.
$("#container p"): Selects all elements matched by <p> that are descendants of an element that has an id of container.
$("li > ul"): Selects all elements matched by <ul> that are children of an element matched by <li>
$("strong + em"): Selects all elements matched by <em> that immediately follow a sibling element matched by <strong>.
$("p ~ ul"): Selects all elements matched by <ul> that follow a sibling element matched by <p>.
$("code, em, strong"): Selects all elements matched by <code> or <em> or <strong>.
$("p strong, .myclass"): Selects all elements matched by <strong> that are descendants of an element matched by <p> as well as all elements that
have a class of myclass.
$(":empty"): Selects all elements that have no children.
$("p:empty"): Selects all elements matched by <p> that have no children.
$("div[p]"): Selects all elements matched by <div> that contain an element matched by <p>.
$("p[.myclass]"): Selects all elements matched by <p> that contain an element with a class of myclass.
$("a[@rel]"): Selects all elements matched by <a> that have a rel attribute.
$("input[@name=myname]"): Selects all elements matched by <input> that have a name value exactly equal to myname.
$("input[@name^=myname]"): Selects all elements matched by <input> that have a name value beginning with myname.
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-overview
256 / 293
Campus Commune
7/31/2014
$("a[@rel$=self]"): Selects all elements matched by <p> that have a class value ending with bar
$("a[@href*=domain.com]"): Selects all elements matched by <a> that have an href value containing domain.com.
$("li:even"): Selects all elements matched by <li> that have an even index value.
$("tr:odd"): Selects all elements matched by <tr> that have an odd index value.
$("li:first"): Selects the first <li> element.
$("li:last"): Selects the last <li> element.
$("li:visible"): Selects all elements matched by <li> that are visible.
$("li:hidden"): Selects all elements matched by <li> that are hidden.
$(":radio"): Selects all radio buttons in the form.
$(":checked"): Selects all checked/selected elements in the form.
$(":input"): Selects only form elements (input, select, textarea, button).
$(":text"): Selects only text elements (input[type=text]).
$("li:eq(2)"): Selects the third <li> element
$("li:eq(4)"): Selects the fifth <li> element
$("li:lt(2)"): Selects all elements matched by <li> element before the third one; in other words, the first two <li> elements.
$("p:lt(3)"): selects all elements matched by <p> elements before the fourth one; in other words the first three <p> elements.
$("li:gt(1)"): Selects all elements matched by <li> after the second one.
$("p:gt(2)"): Selects all elements matched by <p> after the third one.
$("div/p"): Selects all elements matched by <p> that are children of an element matched by <div>.
$("div//code"): Selects all elements matched by <code>that are descendants of an element matched by <div>.
$("//p//a"): Selects all elements matched by <a> that are descendants of an element matched by <p>
$("li:first-child"): Selects all elements matched by <li> that are the first child of their parent.
$("li:last-child"): Selects all elements matched by <li> that are the last child of their parent.
$(":parent"): Selects all elements that are the parent of another element, including text.
$("li:contains(second)"): Selects all elements matched by <li> that contain the text second.
You can use all the above selectors with any HTML/XML element in generic way. For example if selector $("li:first") works for <li> element then
$("p:first") would also work for <p> element.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-overview
257 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-overview
258 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
className
tagName
id
href
title
rel
src
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-attributes
259 / 293
Campus Commune
7/31/2014
In this element's mark-up, the tag name is img, and the mark-up for id, src, alt, class, and title represents the element's attributes, each of which
consists of a name and a value.
jQuery gives us the means to easily manipulate an element's attributes and gives us access to the element so that we can also change its
properties.
Get Attribute Value:
The attr() method can be used to either fetch the value of an attribute from the first element in the matched set or set attribute values onto all
matched elements.
Example:
Following is a simple example which fetches title attribute of <em> tag and sets it as the content of the div:
You see following output when the page is displayed in the browser:
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-attributes
260 / 293
Campus Commune
7/31/2014
Applying Styles:
The addClass( classes ) method can be used to apply defined style sheets onto all the matched elements. You can specify multiple classes separated
by space.
Example:
Following is a simple example which sets class attribute of a <p> and <em> tags:
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-attributes
261 / 293
Campus Commune
7/31/2014
Similar to above syntax and examples, following examples would give you understanding on using various attribute methods in different situation:
$("#myID").attr("custom") : This would return value of attribute custom for the first element matching with ID myID.
$("img").attr("alt", "Sample Image"): This sets the alt attribute of all the images to a new value "Sample Image".
$("input").attr({ value: "", title: "Please enter a value" }); : Sets the value of all <input> elements to the empty string, as well as sets the title to
the string Please enter a value.
$("a[href^=http://]").attr("target","_blank"): Selects all links with an href attribute starting with http:// and set its target attribute to _blank
$("a").removeAttr("target") : This would remove target attribute of all the links.
$("form").submit(function() {$(":submit",this).attr("disabled", "disabled");}); : This would modify the disabled attribute to the value "disabled"
while clicking Submit button.
$("p:last").hasClass("selected"): This return true if last <p> tag has associated classselected.
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-attributes
262 / 293
Campus Commune
7/31/2014
$("p").text(): Returns string that contains the combined text contents of all matched <p> elements.
$("p").text("<i>Hello World</i>"): This would set "<I>Hello World</I>" as text content of the matching <p> elements
$("p").html() : This returns the HTML content of the all matching paragraphs.
$("div").html("Hello World") : This would set the HTML content of all matching <div> to Hello World.
$("input:checkbox:checked").val() : Get the first value from a checked checkbox
$("input:radio[name=bar]:checked").val(): Get the first value from a set of radio buttons
$("button").val("Hello") : Sets the value attribute of every matched element <button>.
$("input").val("on") : This would check all the radio or check box button whose value is "on".
$("select").val("Orange") : This would select Orange option in a dropdown box with options Orange, Mango and Banana.
$("select").val("Orange", "Mango") : This would select Orange and Mango options in a dropdown box with options Orange, Mango and Banana.
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
SHADAB AZHAR
<body>
<div align="right" id="div">this div is left aligned</div>
<div id="second div">this div is unchanged</div>
</body>
</html>
Above "right " must be written in order to get required answer
about 14 days ago
PARIMALA PITCHIKA
Can a non-IT student get all these things by reading?
about 1 month ago
ARUNA K
Being a non-IT student,its really hard to understand those concepts just by reading.Hope we get any practical
training during ILP.
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-attributes
263 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-dom-traversing
264 / 293
Campus Commune
7/31/2014
Above every list has its own index, and can be located directly by using eq(index) method as below example.
Every child element starts its index from zero, thus, second list item would be accessed by using $("li").eq(1) and so on.
Example:
Following is a simple example which adds the color to the third list item.
265 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-dom-traversing
266 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-dom-traversing
267 / 293
Campus Commune
7/31/2014
Following table lists down other useful methods which you can use to locate various elements in a DOM:
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-dom-traversing
268 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
269 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-css
270 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-css
271 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-css
272 / 293
Campus Commune
Ask a doubt
7/31/2014
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-css
273 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-dom
274 / 293
Campus Commune
7/31/2014
Following is an example which makes use of .html() and .text(val) methods. Here .html() retrieves HTML content from the object and
then .text( val ) method sets value of the object using passed parameter:
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-dom
275 / 293
Campus Commune
7/31/2014
276 / 293
Campus Commune
7/31/2014
277 / 293
Campus Commune
7/31/2014
Following is an example where <div> elements are being inserted just before the clicked element:
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-dom
278 / 293
Campus Commune
7/31/2014
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
Open Doubts
Closed Doubts
KRISHNA KABI
I am having a problem while running the above codes using notepad++. On clicking on the respective webpage no further
action is being carried out as per the code. Kindly notify me in case I am wrong. Thank you.
about 1 month ago
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-dom
279 / 293
Campus Commune
7/31/2014
SUMAN SINGH
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
Insert this code too in your head
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-dom
280 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
When these events are triggered you can then use a custom function to do pretty much whatever you want with the event. These
custom functions call Event Handlers.
Binding event handlers:
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-events
281 / 293
Campus Commune
7/31/2014
Using the jQuery Event Model, we can establish event handlers on DOM elements with the bind() method as follows:
$('div').bind('click', function( event ){
alert('Hi there!');
});
This code will cause the division element to respond to the click event; when a user clicks inside this division thereafter, the alert
will be shown.
The full syntax of the bind() command is as follows:
selector.bind( eventType[, eventData], handler)
Following is the description of the parameters:
eventType: A string containing a JavaScript event type, such as click or submit. Refer to the next section for a complete list of
event types.
eventData: This is optional parameter is a map of data that will be passed to the event handler.
handler: A function to execute each time the event is triggered.
Removing event handlers:
Typically, once an event handler is established, it remains in effect for the remainder of the life of the page. There may be a need
when you would like to remove event handler.
jQuery provides the unbind() command to remove an exiting event handler. The syntax of unbind() is as follows:
selector.unbind(eventType, handler)
or
selector.unbind(eventType)
Following is the description of the parameters:
eventType: A string containing a JavaScript event type, such as click or submit. Refer to the next section for a complete list of
event types.
handler: If provided, identifies the specific listener that is to be removed.
Event Types:
The following are cross platform and recommended event types which you can bind using jQuery:
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-events
282 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-events
283 / 293
Campus Commune
7/31/2014
$("div").click( function () {
// do something here
});
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-events
284 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-events
285 / 293
Campus Commune
Ask a doubt
7/31/2014
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-events
286 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-effects
287 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-effects
288 / 293
Campus Commune
7/31/2014
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-effects
289 / 293
Campus Commune
7/31/2014
On clicking the button the content disappears and message is displayed as below:
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-effects
290 / 293
Campus Commune
7/31/2014
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/jquery-effects
291 / 293
Campus Commune
7/31/2014
Pre-ILP Home
Java Lounge
Logout
Java
Q
1.IT Application Overview And Features
Q
2.Need of Programming and Introduction to OOP approach
Q
3.Introduction to Java,JVM,JDK
Q
4.Operators and Variables in Java
Q
5.Introduction to class in java,access specifiers, this and static in Java
Q
6.Java Library, Packages, Use of import
Q
7.Conditional operations
Q
8.Basic Iterations and Arrays
Q
9.Designing Web Pages Part 1(HTML and CSS)
Q
10.Designing Web Pages Part 2 (JavaScript and JQuery)
292 / 293
Campus Commune
7/31/2014
3. Apply below validations to Add patient page created in HTML and CSS exercise:
i. Patient name,Father/spouse name cannot be greater than 30 characters, Allow only alphabets.
ii. ContactNumber sould be of 10 digits.
iii. email id should have an @ and a .)
Jquery problems:
1. Create an HTML page and on click of button select an option in a Select Box.
2. Create an HTML page and on click of button change size of div to double.
3. Create an HTML page and on click of button remove all the child elements from div.
4. Create an HTML page and on click of button show (in alert) the number of children of a div tag
5. Create an HTML page and on click of button change all the h2 tag to h3
6. Create an HTML page which contains different divs/paragraphs of text with one buttons. On click of button make the word named 'TCS' appear
as bold in the page.
7. Create an HTML page which contains a table of size 3x3. Table cells contains numbers in it. On click of button change color of every cell which has
number greater than 10. When mouse is hovered on the cell, change the color to red if the cell does not contain numeric value
Click here to download the solutions
Ask a doubt
(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)
https://campuscommune.tcs.com/communities/java-lounge/content/reference-links-and-videos-7
293 / 293