JAVA COnstructos Conditons Arrays
JAVA COnstructos Conditons Arrays
A constructor initializes an object when it is created. It has the same name as its class and
is syntactically similar to a method. However, constructors have no explicit return type.
Typically, you will use a constructor to give initial values to the instance variables defined
by the class, or to perform any other start-up procedures required to create a fully formed
object.
All classes have constructors, whether you define one or not, because Java automatically
provides a default constructor that initializes all member variables to zero. However, once
you define your own constructor, the default constructor is no longer used.
Syntax
Following is the syntax of a constructor −
class ClassName {
ClassName() {
}
}
Java allows two types of constructors namely −
• No argument Constructors
• Parameterized Constructors
AD
No argument Constructors
As the name specifies the no argument constructors of Java does not accept any parameters
instead, using these constructors the instance variables of a method will be initialized with
fixed values for all objects.
Example
Public class MyClass {
Int num;
MyClass() {
num = 100;
}
}
You would call constructor to initialize objects as follows
public class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass();
MyClass t2 = new MyClass();
System.out.println(t1.num + " " + t2.num);
}
}
This would produce the following result
100 100
Task: create a class called `circle` that represents a circle. The class should have the
following features:
1. Private instance variables: `radius` (double) and `pi` (final double, with a value of
3.14159).
2. A no-argument constructor that sets the default value of `radius` to 1.0.
3. A method called `getarea()` that calculates and returns the area of the circle (pi *
radius * radius).
4. A method called `getcircumference()` that calculates and returns the
circumference of the circle (2 * pi * radius).
Public class circle {
private double radius;
private final double pi = 3.14159;
public circle() {
// no-argument constructor
radius = 1.0;
}
In this example, the `circle` class has a no-argument constructor that sets the default
value of the `radius` field to 1.0. The `getarea()` method calculates the area of the circle
using the formula `pi * radius * radius`, and the `getcircumference()` method calculates
the circumference using the formula `2 * pi * radius`.
In the `main()` method, we create an instance of the `circle` class using the no-argument
constructor and then print the radius, area, and circumference of the circle. Since we
haven't provided any arguments to the constructor, it will use the default value of 1.0 for
the radius.
Radius: 1.0
Area: 3.14159
Circumference: 6.28318
Parameterized Constructors
Most often, you will need a constructor that accepts one or more parameters. Parameters
are added to a constructor in the same way that they are added to a method, just declare
them inside the parentheses after the constructor's name.
Example
Here is a simple example that uses a constructor −
// A simple constructor.
class MyClass {
int x;
Task: Create a class called `Rectangle` that represents a rectangle. The class should have the
following features:
In this example, the `Rectangle` class has a parameterized constructor that accepts
the length and width of the rectangle as arguments. The constructor sets the
corresponding instance variables using the `this` keyword.
The `getArea()` method calculates the area of the rectangle by multiplying the length
and width, and the `getPerimeter()` method calculates the perimeter by adding twice
the length and twice the width.
In the `main()` method, we create an instance of the `Rectangle` class using the
parameterized constructor by passing the length as 4.5 and the width as 3.2. Then,
we print the length, width, area, and perimeter of the rectangle.
Java programming language provides the following types of loop to handle looping
requirements.
Sr.No. Loop & Description
1 while loop
Repeats a statement or group of statements while a given condition is true. It tests
the condition before executing the loop body.
2 for loop
Execute a sequence of statements multiple times and abbreviates the code that
manages the loop variable.
3 do...while loop
Like a while statement, except that it tests the condition at the end of the loop
body.
1 break statement
Terminates the loop or switch statement and transfers execution to the statement
immediately following the loop or switch.
2 continue statement
Causes the loop to skip the remainder of its body and immediately retest its
condition prior to reiterating.
AD
Enhanced for loop in Java
As of Java 5, the enhanced for loop was introduced. This is mainly used to traverse
collection of elements including arrays.
Syntax
Following is the syntax of enhanced for loop −
for(declaration : expression) {
// Statements
}
• Declaration − The newly declared block variable, 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.
Example
public class Test {
for(int x : numbers ) {
System.out.print( x );
System.out.print(",");
}
System.out.print("\n");
String [] names = {"James", "Larry", "Tom", "Lacy"};
Processing Arrays
When processing array elements, we often use either for loop or foreach loop because all
of the elements in an array are of the same type and the size of the array is known.
Example
Here is a complete example showing how to create, initialize, and process arrays −
public class TestArray {
D
The foreach Loops
JDK 1.5 introduced a new for loop known as foreach loop or enhanced for loop, which
enables you to traverse the complete array sequentially without using an index variable.
Example
The following code displays all the elements in the array myList −
Live Demo