Report Java Programming Languages
Report Java Programming Languages
A
Report
on
JAVA PROGRAMMING FOR SOFTWARE DEVELOPMENT
Submitted in Partial Fulfilment of the Requirement for
the Award of the Degree of
BACHELOR OF TECHNOLOGY
IN
Subhash Kumar
(18BTCSE009)
VAUGH INSTITUTE OF AGRICULTURAL ENGINEERING AND T
ECHNOLOGY
FACULTY OF ENGINEERING AND TECHNOLOGY
SAM HIGINBOTTOM UNIVERSITY OF AGRICULTURE,
TECHNOLOGY AND SCIENCES
(FORMELY ALLAHABAD AGRICULTURAL INSTITUTE)
NAINI, PRAYAGRAJ-211007
2020
2
DECLARATION
ABSTRACT
Writing applications in Java using Swing can be quite a daunting experience which requires
understanding of some large libraries, and fairly advanced aspects of Java. In a graphical
system, a windowing toolkit is usually responsible for providing a framework to make it
relatively painless for a graphical user interface (GUI) to render the right bits to the screen
at the right time.
4
ACKNOWLEGEMENT
Table of content
TITLE PAGE NO.
DECLARATION
ABSTRACT i
ACKNOWLEDGEMENT ii
LIST OF TABLES iv
LIST OF FIGURES v
LIST OF NOTATION/SYMBOLS vi
CHAPTER 1 INTRODUCTION
1.1 Introduction 1
1.2 Basic 3
2.2 Constructors 5
2.3 Inheritance 6
3.1 Composition 7
3.2 Encapsulation 8
3.3 Polymorphism 9
5.2 Summary
5.3 Conclusion
APPENDIX
A.Certificate
7
CHAPTER 1 INTRODUCTION
1.1 Introduction
Java provides three ways for executing the loops. While all the ways provide
similar basic functionality, they differ in their syntax and condition checking time.
1. while loop: A while loop is a control flow statement that allows code
to be executed repeatedly based on a given Boolean condition. The
while loop can be thought of as a repeating if statement.
Syntax :
Flowchart:
10
Value of x:4
2. for loop: for loop provides a concise way of writing the loop
structure. Unlike a while loop, a for statement consumes the
initialization, condition and increment/decrement in one line thereby
providing a shorter, easy to debug structure of looping.
Syntax:
Flowchart:
{
public static void main(String args[])
{
String array[] = {"Ron", "Harry", "Hermoine"};
//enhanced for loop
for (String x:array)
{
System.out.println(x);
}
/* for loop for same function
for (int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}
*/
}
}
Output:
Ron
Harry
Hermoine
2. do while: do while loop is similar to while loop with only difference
that it checks for condition after executing the statements, and
therefore is an example of Exit Control Loop.
Syntax:
do
{
statements..
}
while (condition);
14
Flowchart:
Output:
Value of x: 21
Pitfalls of Loops
Output:
Exception in thread "main" java.lang.OutOfMemoryError:
Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.util.Arrays.copyOf(Unknown Source)
at java.util.ArrayList.grow(Unknown Source)
at java.util.ArrayList.ensureCapacityInternal(Unknown
Source)
at java.util.ArrayList.add(Unknown Source)
at article.Integer1.main(Integer1.java:9)
17
A class is a user defined blueprint or prototype from which objects are created.
It represents the set of properties or methods that are common to all objects of
one type. In general, class declarations can include these components, in
order:
1. Modifiers: A class can be public or has default access.
2. class keyword: class keyword is used to create a class.
3. Class name: The name should begin with an initial letter
(capitalized by convention).
4. Superclass (if any): The name of the class’s parent (superclass), if
any, preceded by the keyword extends. A class can only extend
(subclass) one parent.
5. Interfaces (if any): A comma-separated list of interfaces
implemented by the class, if any, preceded by the keyword
implements. A class can implement more than one interface.
6. Body: The class body surrounded by braces { }.
Objects correspond to things found in the real world. For example, a graphics
program may have objects such as “circle”, “square”, “menu”. An online
shopping system might have objects such as “shopping cart”, “customer”, and
“product”.
2.2 Constructors
Constructors are used to initialize the object’s state. Like methods, a constructor also
contains collection of statements(i.e. instructions) that are executed at time of
Object creation.
// A Constructor
new Geek() {}
.......
}
Types of constructor
2.3 Inheritance
3.1 Encapsulation
Advantages of Encapsulation:
● Data Hiding: The user will have no idea about the inner
implementation of the class. It will not be visible to the user that how
the class is storing values in the variables. He only knows that we
are passing the values to a setter method and variables are getting
initialized with that value.
● Increased Flexibility: We can make the variables of the class as
read-only or write-only depending on our requirement. If we wish to
make the variables as read-only then we have to omit the setter
methods like setName(), setAge() etc. from the above program or if
we wish to make the variables as write-only then we have to omit
the get methods like getName(), getAge() etc. from the above
program
● Reusability: Encapsulation also improves the re-usability and easy
to change with new requirements.
● Testing code is easy: Encapsulated code is easy to test for unit
testing.
There are situations in which we will want to define a superclass that declares
the structure of a given abstraction without providing a complete implementation
of every method. That is, sometimes we will want to create a superclass that
only defines a generalization form that will be shared by all of its subclasses,
leaving it to each subclass to fill in the details.
Consider a classic “shape” example, perhaps used in a computer-aided design
system or game simulation. The base type is “shape” and each shape has a
colour, size and so on. From this, specific types of shapes are
derived(inherited)-circle, square, triangle and so on – each of which may have
additional characteristics and behaviours. For example, certain shapes can be
flipped. Some behaviours may be different, such as when you want to calculate
the area of a shape. The type hierarchy embodies both the similarities and
differences between the shapes.
24
Advantages of Abstraction
Object is the superclass of all other classes and Object reference can refer to
any type object. These features lack type safety. Generics adds that type safety
feature. We will discuss that type safety feature in later examples.
Generics in Java is similar to templates in C++. For example, classes like
HashSet, Array List, HashMap, etc use generics very well. There are some
fundamental differences between the two approaches to generic types.
Generic Class
Like C++, we use <> to specify parameter types in generic class creation. To
create objects of generic class, we use following syntax.
// To create an instance of generic class
BaseType <Type> obj = new BaseType <Type>()
Advantages of Generics:
Programs that uses Generics has got many benefits over non-generic code.
1. Code Reuse: We can write a method/class/interface once and use
for any type we want.
.
2. Type Safety : Generics make errors to appear compile time than at
run time (It’s always better to know problems in your code at
compile time rather than making your code fail at run time).
Suppose you want to create an ArrayList that store name of
students and if by mistake programmer adds an integer object
instead of string, compiler allows it. But, when we retrieve this data
from ArrayList, it causes problems at runtime.
27