A default constructor is one that has no methods. If we don't define a constructor then the system will create one for us.
File: "main1.java"
class Rock
{
}
public class main1
{
public static void main(String args[] )
{
Rock rockObj1 = new Rock() ;
System.out.println("Created a rock object." ) ;
}
}
A default constructor is one that does not take any arguments. In the above example we did not define a default constructor and one got created for us.
Suppose we have something that takes on a set of values such as color and we want to use that in our program. We could use something like below:
File: "enum1.java"
public class enum1
{
int someColor ;
final int BLACK = 0 ;
final int WHITE = 1 ;
final int RED = 2 ;
final int GREEN = 3 ;
final int YELLO = 4 ;
void method1()
{
someColor = GREEN ;
someColor = 20 ;
}
public static void main(String args[] )
{
}
}
We have to declare more variables ( one for each value ) and also it is not type safe. We are able to do something like:
someColor = 20 ;
Even though 20 is not a legitimate value. Java has a type defined called "Enum" that simplifies things for us.
File: enum2.java":
enum COLOR
{
BLACK , WHITE, RED, GREEN, YELLOW
};
public class enum2
{
COLOR someColor ;
void method1()
{
someColor = COLOR.GREEN ;
// Below gives compiler error
someColor = 20 ;
}
public static void main(String args[] )
{
}
}
In the above example we define a type "COLOR" and it is like a class. The syntax:
enum COLOR
{
BLACK , WHITE, RED, GREEN, YELLOW
};
Ex 1 )
Assume we have an array of positive integers ( not including 0 ) . Find the missing minimum integer.
Test cases
Input: {2, 3, 7, 6, 8, 10, 15} Output: 1
Input: { 2, 3, 7, 6, 8, 1, 10, 15 } Output: 4
Input: {2, 3, 1, 6, 4}
Output: 5
Input: { 2, 3, 7, 1 }
Output: 4
The array can be of maximum size 100 . The actual size of the array is given as N.
Solve it using 3 methods.
1)
Run a nested loop ( 2 for loops ) and start looking for numbers in the array from 1 to length of the array + 1 .
2)
Create another array of the same size and initialize with 0 . Go through the first array. If you see a number greater than the length of the array then skip it as we are bound to find a number less than that which does not exist in the array. If we have a number less than the length say 2 then mark the second array index . So for 2 make the second array :
secondArray[1] = 1 ;
Since indexes start with 0 we can subtract 1 from 2 .
3) Use the same approach as 2 but do not create another array. Instead do the marking in the same array by making the existing number and turning it into a negative.
Array1 = { 3 , 4 , 1 }
We start with 0 index . 3 is less than or equal to length so we go to ( 3-1 ) index 2 and make that number negative.
{ 3 , 4 , -1 }
We skip 4 because it is greater than length . We go to the next index 2 and see that the number is -1 . We make that positive to 1 and subtract 1 to get the index 0 and make that negative.
{ -3, , 4 , -1 }
Now we go through the array and look for the positive numbered index which is 1 so the number 2 is missing.