Skip to content

Commit add9157

Browse files
Ranga Rao KaranamRanga Rao Karanam
authored andcommitted
First Version
1 parent 2b64023 commit add9157

127 files changed

Lines changed: 9881 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

abstract-class.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
### /com/in28minutes/java/classmodifiers/nonaccess/abstractclass/AbstractClassExample.java
2+
```
3+
package com.in28minutes.java.classmodifiers.nonaccess.abstractclass;
4+
5+
public abstract class AbstractClassExample {
6+
7+
// Abstract class can contain instance and static variables
8+
public int publicVariable;
9+
private int privateVariable;
10+
static int staticVariable;
11+
12+
// Abstract Class can contain 0 or more abstract methods
13+
// Abstract method does not have a body
14+
abstract void abstractMethod1();
15+
16+
abstract void abstractMethod2();
17+
18+
// Abstract Class can contain 0 or more non-abstract methods
19+
public void nonAbstractMethod() {
20+
System.out.println("Non Abstract Method");
21+
}
22+
23+
public static void main(String[] args) {
24+
// An abstract class cannot be instantiated
25+
// Below line gives compilation error if uncommented
26+
// AbstractClassExample ex = new AbstractClassExample();
27+
}
28+
}
29+
30+
// A non-abstract sub class of an abstract class should
31+
// implement all the abstract methods
32+
// Below class gives compilation error if uncommented
33+
/*
34+
* class SubClass extends AbstractClassExample {
35+
*
36+
* }
37+
*/
38+
39+
// This class implements both abstractMethod1 and abstractMethod2
40+
class SubClass2 extends AbstractClassExample {
41+
void abstractMethod1() {
42+
System.out.println("Abstract Method1");
43+
}
44+
45+
void abstractMethod2() {
46+
System.out.println("Abstract Method2");
47+
}
48+
}
49+
50+
// We can create a subclass of an abstract class which is abstract
51+
// It doesn't need to implement all the abstract methods
52+
abstract class AbstractSubClass extends AbstractClassExample {
53+
void abstractMethod1() {
54+
System.out.println("Abstract Method1");
55+
}
56+
// abstractMethod2 is not defined at all.
57+
}
58+
```

arrays.md

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
## Examples
2+
### /com/in28minutes/java/arrays/ArrayExamples.java
3+
```
4+
package com.in28minutes.java.arrays;
5+
6+
import java.util.Arrays;
7+
8+
public class ArrayExamples {
9+
public static void main(String[] args) {
10+
// Declare an Array. All below ways are legal.
11+
int marks[]; // Not Readable
12+
int[] runs; // Not Readable
13+
int[] temperatures;// Recommended
14+
15+
// Declaration of an Array should not include size.
16+
// int values[5];//Compilation Error!
17+
18+
// Declaring 2D ArrayExamples
19+
int[][] matrix1; // Recommended
20+
int[] matrix2[]; // Legal but not readable. Avoid.
21+
22+
// Creating an array
23+
marks = new int[5]; // 5 is size of array
24+
25+
// Size of an array is mandatory to create an array
26+
// marks = new int[];//COMPILER ERROR
27+
28+
// Once An Array is created, its size cannot be changed.
29+
30+
// Declaring and creating an array in same line
31+
int marks2[] = new int[5];
32+
33+
// new Arrays are alway initialized with default values
34+
System.out.println(marks2[0]);// 0
35+
36+
// Default Values
37+
// byte,short,int,long-0
38+
// float,double-0.0
39+
// boolean false
40+
// object-null
41+
42+
// Assigning values to array
43+
marks[0] = 25;
44+
marks[1] = 30;
45+
marks[2] = 50;
46+
marks[3] = 10;
47+
marks[4] = 5;
48+
49+
// ArrayOnHeap.xls
50+
51+
// Note : Index of an array runs from 0 to length - 1
52+
53+
// Declare, Create and Initialize Array on same line
54+
int marks3[] = { 25, 30, 50, 10, 5 };
55+
56+
// Leaving additional comma is not a problem
57+
int marks4[] = { 25, 30, 50, 10, 5, };
58+
59+
// Default Values in Array
60+
// numbers - 0 floating point - 0.0 Objects - null
61+
62+
// Length of an array : Property length
63+
int length = marks.length;
64+
65+
// Printing a value from array
66+
System.out.println(marks[2]);
67+
68+
// Looping around an array - Enhanced for loop
69+
for (int mark : marks) {
70+
System.out.println(mark);
71+
}
72+
73+
// Fill array with same default value
74+
Arrays.fill(marks, 100); // All array values will be 100
75+
76+
// Access 10th element when array has only length 5
77+
// Runtime Exception : ArrayIndexOutOfBoundsException
78+
// System.out.println(marks[10]);
79+
80+
// String Array: similar to int array.
81+
String[] daysOfWeek = { "Sunday", "Monday", "Tuesday", "Wednesday",
82+
"Thursday", "Friday", "Saturday" };
83+
84+
// Array can contain only values of same type.
85+
// COMPILE ERROR!!
86+
// int marks4[] = {10,15.0}; //10 is int 15.0 is float
87+
88+
// Cross assigment of primitive arrays is ILLEGAL
89+
int[] ints = new int[5];
90+
short[] shorts = new short[5];
91+
// ints = shorts;//COMPILER ERROR
92+
// ints = (int[])shorts;//COMPILER ERROR
93+
94+
// 2D Arrays
95+
int[][] matrix = { { 1, 2, 3 }, { 4, 5, 6 } };
96+
97+
int[][] matrixA = new int[5][6];
98+
99+
// First dimension is necessary to create a 2D Array
100+
// Best way to visualize a 2D array is as an array of arrays
101+
// ArrayOnHeap.xls
102+
matrixA = new int[3][];// FINE
103+
// matrixA = new int[][5];//COMPILER ERROR
104+
// matrixA = new int[][];//COMPILER ERROR
105+
106+
// We can create a ragged 2D Array
107+
matrixA[0] = new int[3];
108+
matrixA[0] = new int[4];
109+
matrixA[0] = new int[5];
110+
111+
// Above matrix has 2 rows 3 columns.
112+
113+
// Accessing an element from 2D array:
114+
System.out.println(matrix[0][0]); // 1
115+
System.out.println(matrix[1][2]); // 6
116+
117+
// Looping a 2D array:
118+
for (int[] array : matrix) {
119+
for (int number : array) {
120+
System.out.println(number);
121+
}
122+
}
123+
124+
// Printing a 1D Array
125+
int marks5[] = { 25, 30, 50, 10, 5 };
126+
System.out.println(marks5); // [I@6db3f829
127+
System.out.println(Arrays.toString(marks5));// [25, 30, 50, 10, 5]
128+
129+
// Printing a 2D Array
130+
int[][] matrix3 = { { 1, 2, 3 }, { 4, 5, 6 } };
131+
System.out.println(matrix3); // [[I@1d5a0305
132+
System.out.println(Arrays.toString(matrix3));
133+
// [[I@6db3f829, [I@42698403]
134+
System.out.println(Arrays.deepToString(matrix3));
135+
// [[1, 2, 3], [4, 5, 6]]
136+
137+
// matrix3[0] is a 1D Array
138+
System.out.println(matrix3[0]);// [I@86c347
139+
System.out.println(Arrays.toString(matrix3[0]));// [1, 2, 3]
140+
141+
// Comparing Arrays
142+
int[] numbers1 = { 1, 2, 3 };
143+
int[] numbers2 = { 4, 5, 6 };
144+
System.out.println(Arrays.equals(numbers1, numbers2)); // false
145+
int[] numbers3 = { 1, 2, 3 };
146+
System.out.println(Arrays.equals(numbers1, numbers3)); // true
147+
148+
// Sorting An Array
149+
int rollNos[] = { 12, 5, 7, 9 };
150+
Arrays.sort(rollNos);
151+
System.out.println(Arrays.toString(rollNos));// [5, 7, 9, 12]
152+
153+
// Array of Objects(ArrayOnHeap.xls)
154+
Person[] persons = new Person[3];
155+
156+
// Creating an array of Persons creates
157+
// 4 Reference Variables to Person
158+
// It does not create the Person Objects
159+
// ArrayOnHeap.xls
160+
System.out.println(persons[0]);// null
161+
162+
// to assign objects we would need to create them
163+
persons[0] = new Person();
164+
persons[1] = new Person();
165+
persons[2] = new Person();
166+
167+
// Other way
168+
// How may objects are created?
169+
Person[] personsAgain = { new Person(), new Person(), new Person() };
170+
171+
// How may objects are created?
172+
Person[][] persons2D = { { new Person(), new Person(), new Person() },
173+
{ new Person(), new Person() } };
174+
175+
}
176+
}
177+
178+
class Person {
179+
long ssn;
180+
String name;
181+
}
182+
```

basics-class-object.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
### /com/in28minutes/java/basics/Actor.java
2+
```
3+
package com.in28minutes.java.basics;
4+
5+
public class Actor {
6+
7+
// name is a Member Variable
8+
private String name;
9+
10+
public String getName() {
11+
return name;
12+
}
13+
14+
public void setName(String name) {
15+
// This is called shadowing
16+
// Local variable or parameter with
17+
// same name as a member variable
18+
// this.name refers to member variable
19+
// name refers to local variable
20+
this.name = name;
21+
}
22+
23+
public static void main(String[] args) {
24+
// bradPitt & tomCruise are objects or instances
25+
// of Class Actor
26+
// Each instance has separate value for the
27+
// member variable name
28+
Actor bradPitt = new Actor();
29+
bradPitt.setName("Brad Pitt");
30+
31+
Actor tomCruise = new Actor();
32+
tomCruise.setName("Tom Cruise");
33+
}
34+
}
35+
```
36+
### /com/in28minutes/java/basics/Cricketer.java
37+
```
38+
package com.in28minutes.java.basics;
39+
40+
public class Cricketer {
41+
String name;
42+
int odiRuns;
43+
int testRuns;
44+
int t20Runs;
45+
46+
public int totalRuns() {
47+
int totalRuns = odiRuns + testRuns + t20Runs;
48+
return totalRuns;
49+
}
50+
}
51+
```
52+
### /com/in28minutes/java/basics/CricketScorer.java
53+
```
54+
package com.in28minutes.java.basics;
55+
56+
public class CricketScorer {
57+
// Instance Variables - constitute the state of an object
58+
private int score;
59+
60+
// Behavior - all the methods that are part of the class
61+
// An object of this type has behavior based on the
62+
// methods four, six and getScore
63+
public void four() {
64+
score = score + 4;
65+
}
66+
67+
public void six() {
68+
score = score + 6;
69+
}
70+
71+
public int getScore() {
72+
return score;
73+
}
74+
75+
public static void main(String[] args) {
76+
CricketScorer scorer = new CricketScorer();
77+
scorer.six();
78+
// State of scorer is (score => 6)
79+
scorer.four();
80+
// State of scorer is (score => 10)
81+
System.out.println(scorer.getScore());
82+
}
83+
}
84+
```

0 commit comments

Comments
 (0)