Skip to content

Commit 5fd28b0

Browse files
Ranga Rao KaranamRanga Rao Karanam
authored andcommitted
First Update of All Code Examples
1 parent f283175 commit 5fd28b0

File tree

191 files changed

+10145
-0
lines changed

Some content is hidden

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

191 files changed

+10145
-0
lines changed

pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.in28minutes.java.beginners</groupId>
5+
<artifactId>JavaTutorialForBeginners</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<dependencies>
8+
<dependency>
9+
<groupId>junit</groupId>
10+
<artifactId>junit</artifactId>
11+
<version>4.12</version>
12+
<scope>test</scope>
13+
</dependency>
14+
</dependencies>
15+
<build>
16+
<finalName>in28minutes</finalName>
17+
<pluginManagement>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-compiler-plugin</artifactId>
22+
<version>3.2</version>
23+
<configuration>
24+
<verbose>true</verbose>
25+
<source>1.8</source>
26+
<target>1.8</target>
27+
<showWarnings>true</showWarnings>
28+
</configuration>
29+
</plugin>
30+
</plugins>
31+
</pluginManagement>
32+
</build>
33+
</project>
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package com.in28minutes.java.beginners.concept.examples.arrays;
2+
3+
import java.util.Arrays;
4+
5+
public class ArrayExamples {
6+
public static void main(String[] args) {
7+
// Declare an Array. All below ways are legal.
8+
int marks[]; // Not Readable
9+
int[] runs; // Not Readable
10+
int[] temperatures;// Recommended
11+
12+
// Declaration of an Array should not include size.
13+
// int values[5];//Compilation Error!
14+
15+
// Declaring 2D ArrayExamples
16+
int[][] matrix1; // Recommended
17+
int[] matrix2[]; // Legal but not readable. Avoid.
18+
19+
// Creating an array
20+
marks = new int[5]; // 5 is size of array
21+
22+
// Size of an array is mandatory to create an array
23+
// marks = new int[];//COMPILER ERROR
24+
25+
// Once An Array is created, its size cannot be changed.
26+
27+
// Declaring and creating an array in same line
28+
int marks2[] = new int[5];
29+
30+
// new Arrays are alway initialized with default values
31+
System.out.println(marks2[0]);// 0
32+
33+
// Default Values
34+
// byte,short,int,long-0
35+
// float,double-0.0
36+
// boolean false
37+
// object-null
38+
39+
// Assigning values to array
40+
marks[0] = 25;
41+
marks[1] = 30;
42+
marks[2] = 50;
43+
marks[3] = 10;
44+
marks[4] = 5;
45+
46+
// ArrayOnHeap.xls
47+
48+
// Note : Index of an array runs from 0 to length - 1
49+
50+
// Declare, Create and Initialize Array on same line
51+
int marks3[] = { 25, 30, 50, 10, 5 };
52+
53+
// Leaving additional comma is not a problem
54+
int marks4[] = { 25, 30, 50, 10, 5, };
55+
56+
// Default Values in Array
57+
// numbers - 0 floating point - 0.0 Objects - null
58+
59+
// Length of an array : Property length
60+
int length = marks.length;
61+
62+
// Printing a value from array
63+
System.out.println(marks[2]);
64+
65+
// Looping around an array - Enhanced for loop
66+
for (int mark : marks) {
67+
System.out.println(mark);
68+
}
69+
70+
// Fill array with same default value
71+
Arrays.fill(marks, 100); // All array values will be 100
72+
73+
// Access 10th element when array has only length 5
74+
// Runtime Exception : ArrayIndexOutOfBoundsException
75+
// System.out.println(marks[10]);
76+
77+
// String Array: similar to int array.
78+
String[] daysOfWeek = { "Sunday", "Monday", "Tuesday", "Wednesday",
79+
"Thursday", "Friday", "Saturday" };
80+
81+
// Array can contain only values of same type.
82+
// COMPILE ERROR!!
83+
// int marks4[] = {10,15.0}; //10 is int 15.0 is float
84+
85+
// Cross assigment of primitive arrays is ILLEGAL
86+
int[] ints = new int[5];
87+
short[] shorts = new short[5];
88+
// ints = shorts;//COMPILER ERROR
89+
// ints = (int[])shorts;//COMPILER ERROR
90+
91+
// 2D Arrays
92+
int[][] matrix = { { 1, 2, 3 }, { 4, 5, 6 } };
93+
94+
int[][] matrixA = new int[5][6];
95+
96+
// First dimension is necessary to create a 2D Array
97+
// Best way to visualize a 2D array is as an array of arrays
98+
// ArrayOnHeap.xls
99+
matrixA = new int[3][];// FINE
100+
// matrixA = new int[][5];//COMPILER ERROR
101+
// matrixA = new int[][];//COMPILER ERROR
102+
103+
// We can create a ragged 2D Array
104+
matrixA[0] = new int[3];
105+
matrixA[0] = new int[4];
106+
matrixA[0] = new int[5];
107+
108+
// Above matrix has 2 rows 3 columns.
109+
110+
// Accessing an element from 2D array:
111+
System.out.println(matrix[0][0]); // 1
112+
System.out.println(matrix[1][2]); // 6
113+
114+
// Looping a 2D array:
115+
for (int[] array : matrix) {
116+
for (int number : array) {
117+
System.out.println(number);
118+
}
119+
}
120+
121+
// Printing a 1D Array
122+
int marks5[] = { 25, 30, 50, 10, 5 };
123+
System.out.println(marks5); // [I@6db3f829
124+
System.out.println(Arrays.toString(marks5));// [25, 30, 50, 10, 5]
125+
126+
// Printing a 2D Array
127+
int[][] matrix3 = { { 1, 2, 3 }, { 4, 5, 6 } };
128+
System.out.println(matrix3); // [[I@1d5a0305
129+
System.out.println(Arrays.toString(matrix3));
130+
// [[I@6db3f829, [I@42698403]
131+
System.out.println(Arrays.deepToString(matrix3));
132+
// [[1, 2, 3], [4, 5, 6]]
133+
134+
// matrix3[0] is a 1D Array
135+
System.out.println(matrix3[0]);// [I@86c347
136+
System.out.println(Arrays.toString(matrix3[0]));// [1, 2, 3]
137+
138+
// Comparing Arrays
139+
int[] numbers1 = { 1, 2, 3 };
140+
int[] numbers2 = { 4, 5, 6 };
141+
System.out.println(Arrays.equals(numbers1, numbers2)); // false
142+
int[] numbers3 = { 1, 2, 3 };
143+
System.out.println(Arrays.equals(numbers1, numbers3)); // true
144+
145+
// Sorting An Array
146+
int rollNos[] = { 12, 5, 7, 9 };
147+
Arrays.sort(rollNos);
148+
System.out.println(Arrays.toString(rollNos));// [5, 7, 9, 12]
149+
150+
// Array of Objects(ArrayOnHeap.xls)
151+
Person[] persons = new Person[3];
152+
153+
// Creating an array of Persons creates
154+
// 4 Reference Variables to Person
155+
// It does not create the Person Objects
156+
// ArrayOnHeap.xls
157+
System.out.println(persons[0]);// null
158+
159+
// to assign objects we would need to create them
160+
persons[0] = new Person();
161+
persons[1] = new Person();
162+
persons[2] = new Person();
163+
164+
// Other way
165+
// How may objects are created?
166+
Person[] personsAgain = { new Person(), new Person(), new Person() };
167+
168+
// How may objects are created?
169+
Person[][] persons2D = { { new Person(), new Person(), new Person() },
170+
{ new Person(), new Person() } };
171+
172+
}
173+
}
174+
175+
class Person {
176+
long ssn;
177+
String name;
178+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.in28minutes.java.beginners.concept.examples.basics;
2+
3+
public class Actor {
4+
5+
// name is a Member Variable
6+
private String name;
7+
8+
public String getName() {
9+
return name;
10+
}
11+
12+
public void setName(String name) {
13+
// This is called shadowing
14+
// Local variable or parameter with
15+
// same name as a member variable
16+
// this.name refers to member variable
17+
// name refers to local variable
18+
this.name = name;
19+
}
20+
21+
public static void main(String[] args) {
22+
// bradPitt & tomCruise are objects or instances
23+
// of Class Actor
24+
// Each instance has separate value for the
25+
// member variable name
26+
Actor bradPitt = new Actor();
27+
bradPitt.setName("Brad Pitt");
28+
29+
Actor tomCruise = new Actor();
30+
tomCruise.setName("Tom Cruise");
31+
}
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.in28minutes.java.beginners.concept.examples.basics;
2+
3+
public class CricketScorer {
4+
// Instance Variables - constitute the state of an object
5+
private int score;
6+
7+
// Behavior - all the methods that are part of the class
8+
// An object of this type has behavior based on the
9+
// methods four, six and getScore
10+
public void four() {
11+
score = score + 4;
12+
}
13+
14+
public void six() {
15+
score = score + 6;
16+
}
17+
18+
public int getScore() {
19+
return score;
20+
}
21+
22+
public static void main(String[] args) {
23+
CricketScorer scorer = new CricketScorer();
24+
scorer.six();
25+
// State of scorer is (score => 6)
26+
scorer.four();
27+
// State of scorer is (score => 10)
28+
System.out.println(scorer.getScore());
29+
}
30+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.in28minutes.java.beginners.concept.examples.basics;
2+
3+
public class Cricketer {
4+
String name;
5+
int odiRuns;
6+
int testRuns;
7+
int t20Runs;
8+
9+
public int totalRuns() {
10+
int totalRuns = odiRuns + testRuns + t20Runs;
11+
return totalRuns;
12+
}
13+
}

0 commit comments

Comments
 (0)