Skip to content

Commit

Permalink
011 added sources for arrays, continuing with chapter 5
Browse files Browse the repository at this point in the history
  • Loading branch information
iuliana committed Sep 20, 2018
1 parent 108440a commit 34f82b7
Show file tree
Hide file tree
Showing 98 changed files with 186 additions and 8 deletions.
Empty file modified .gitignore 100644 → 100755
Empty file.
Empty file modified Contributing.adoc 100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion LICENSE.txt 100644 → 100755
Expand Up @@ -22,4 +22,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
Empty file modified README.adoc 100644 → 100755
Empty file.
Empty file modified build.gradle 100644 → 100755
Empty file.
Empty file modified chapter00/chapter00.gradle 100644 → 100755
Empty file.
Empty file modified chapter00/src/main/java/com/apress/bgn/ch0/Base.java 100644 → 100755
Empty file.
Empty file modified chapter00/src/main/java/com/apress/bgn/ch0/HelloWorld.java 100644 → 100755
Empty file.
Empty file.
Empty file modified chapter00/src/main/java/module-info.java 100644 → 100755
Empty file.
Empty file modified chapter00/src/main/resources/log4j2.xml 100644 → 100755
Empty file.
Empty file modified chapter01/chapter01.gradle 100644 → 100755
Empty file.
Empty file modified chapter01/src/main/java/com/apress/bgn/ch1/Main.java 100644 → 100755
Empty file.
Empty file.
Empty file modified chapter01/src/main/java/module-info.java 100644 → 100755
Empty file.
Empty file modified chapter03/chapter03.gradle 100644 → 100755
Empty file.
Empty file modified chapter03/src/main/java/com/apress/bgn/ch3/Main.java 100644 → 100755
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file modified chapter03/src/main/java/module-info.java 100644 → 100755
Empty file.
Empty file modified chapter04/chapter04.gradle 100644 → 100755
Empty file.
Empty file modified chapter04/src/main/java/com/apress/bgn/ch4/FirstSample.java 100644 → 100755
Empty file.
Empty file modified chapter04/src/main/java/com/apress/bgn/ch4/basic/Actor.java 100644 → 100755
Empty file.
Empty file.
Empty file modified chapter04/src/main/java/com/apress/bgn/ch4/basic/Gender.java 100644 → 100755
Empty file.
Empty file modified chapter04/src/main/java/com/apress/bgn/ch4/basic/Human.java 100644 → 100755
Empty file.
Empty file modified chapter04/src/main/java/com/apress/bgn/ch4/basic/Musician.java 100644 → 100755
Empty file.
Empty file modified chapter04/src/main/java/com/apress/bgn/ch4/basic/Sample.java 100644 → 100755
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file modified chapter04/src/main/java/com/apress/bgn/ch4/gen/Pair.java 100644 → 100755
Empty file.
Empty file modified chapter04/src/main/java/com/apress/bgn/ch4/hierarchy/Actor.java 100644 → 100755
Empty file.
Empty file.
Empty file.
Empty file.
Empty file modified chapter04/src/main/java/com/apress/bgn/ch4/hierarchy/Human.java 100644 → 100755
Empty file.
Empty file.
Empty file.
Empty file.
Empty file modified chapter04/src/main/java/com/apress/bgn/ch4/lambda/Addition.java 100644 → 100755
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions chapter04/src/main/java/module-info.java 100644 → 100755
Expand Up @@ -4,4 +4,5 @@
module chapter.four {
requires chapter.zero;
requires transitive java.logging;
exports com.apress.bgn.ch4.hierarchy;
}
18 changes: 18 additions & 0 deletions chapter05/arrays/build.gradle
@@ -0,0 +1,18 @@
plugins {
id 'java-library'
}

/* module with primitive types examples */
ext.moduleName = 'chapter.five.arrays'

jar {
manifest {
attributes(
"Created-By": System.getProperty('java.version'),
"Specification-Title": "Java for Absolute Beginners",
"Main-Class": "com.apress.bgn.ch5.CollectionsDemo",
"Implementation-Version": version,
"Class-Path": configurations.compile.collect { it.getName() }.join(' ')
)
}
}
82 changes: 82 additions & 0 deletions chapter05/arrays/src/main/java/com/apress/bgn/ch5/ArraysDemo.java
@@ -0,0 +1,82 @@
/*
Freeware License, some rights reserved
Copyright (c) 2018 Iuliana Cosmina
Permission is hereby granted, free of charge, to anyone obtaining a copy
of this software and associated documentation files (the "Software"),
to work with the Software within the limits of freeware distribution and fair use.
This includes the rights to use, copy, and modify the Software for personal use.
Users are also allowed and encouraged to submit corrections and modifications
to the Software for the benefit of other users.
It is not allowed to reuse, modify, or redistribute the Software for
commercial use in any way, or for a user's educational materials such as books
or blog articles without prior permission from the copyright holder.
The above copyright notice and this permission notice need to be included
in all copies or substantial portions of the software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.apress.bgn.ch5;

import java.util.Arrays;

/**
* @author Iuliana Cosmina
* since 1.0
*/
public class ArraysDemo {

int array[];

public static void main(String... args) {
ArraysDemo ad = new ArraysDemo();
if (ad.array == null) {
System.out.println("array unusable");
}

// after proper initialization
ad.array = new int[2];
for (int i = 0; i < ad.array.length; ++i) {
System.out.println("array["+ i +"]= " + ad.array[i]);
}
// setting elements explicitly

ad.array[0] = 5;
for (int i = 0; i < ad.array.length; ++i) {
System.out.println("array["+ i +"]= " + ad.array[i]);
}

// define array with direct initialization
int another[] = {1,4,3,2};
for (int i = 0; i < another.length; ++i) {
System.out.println("array["+ i +"]= " + another[i]);
}

//sort array
Arrays.sort(another);
for (int i = 0; i < another.length; ++i) {
System.out.println("array["+ i +"]= " + another[i]);
}

//Arrays.stream(another).forEach(element -> System.out.println(element));
Arrays.stream(another).forEach(System.out::println);

// multidimensional array
int[][] intMatrix = {{1,0},{0,1}};
for (int i = 0; i < intMatrix.length; ++i) {
for (int j = 0; j < intMatrix[i].length ; ++j) {
System.out.print( intMatrix[i][j] + " ");
}
System.out.println();
}
}
}
5 changes: 5 additions & 0 deletions chapter05/arrays/src/main/java/module-info.java
@@ -0,0 +1,5 @@
/**
* Created by iuliana.cosmina on 3/5/18.
*/
module chapter.five.collections {
}
Empty file modified chapter05/chapter05.gradle 100644 → 100755
Empty file.
Empty file modified chapter05/collections/build.gradle 100644 → 100755
Empty file.
Empty file.
Empty file modified chapter05/collections/src/main/java/module-info.java 100644 → 100755
Empty file.
Empty file modified chapter05/data-structures/build.gradle 100644 → 100755
Empty file.
Empty file.
Empty file modified chapter05/data-structures/src/main/java/module-info.java 100644 → 100755
Empty file.
Empty file modified chapter05/data-types/build.gradle 100644 → 100755
Empty file.
Empty file.
Empty file modified chapter05/data-types/src/main/java/module-info.java 100644 → 100755
Empty file.
5 changes: 5 additions & 0 deletions chapter05/primitives/build.gradle 100644 → 100755
Expand Up @@ -5,6 +5,11 @@ plugins {
/* module with primitive types examples */
ext.moduleName = 'chapter.five.primitives'

dependencies {
compile project(':chapter04')
testImplementation testing.junit
}

jar {
manifest {
attributes(
Expand Down
4 changes: 2 additions & 2 deletions chapter05/primitives/src/main/java/com/apress/bgn/ch5/CharLister.java 100644 → 100755
Expand Up @@ -2,9 +2,9 @@

public class CharLister {
public static void main(String... args) {
for (int i = 0; i < 65536; ++i ) {
for (int i = 0; i < 65536; ++i) {
char c = (char) i;
if(i%10 ==0) {
if (i % 10 == 0) {
System.out.println();
}
System.out.print("c[" + i + "]=" + c + " ");
Expand Down
Empty file.
Empty file.
53 changes: 48 additions & 5 deletions chapter05/primitives/src/main/java/com/apress/bgn/ch5/NumericDemo.java 100644 → 100755
Expand Up @@ -10,8 +10,46 @@ public class NumericDemo {

public static void main(String... args) {
NumericDemo nd = new NumericDemo();

// testing equality of zeroes
System.out.println("Int zero: " + nd.i);
System.out.println("Float zero: " + nd.f);
if (nd.i == nd.f) {
System.out.println("Both are zero.");
}

System.out.println(nd);

//initializations
nd.b = 0b1100;
System.out.println("Byte binary value: " + nd.b);

nd.i = 42 ; // decimal case
nd.i = 045 ; // octal case - base 8
System.out.println("Int octal value: " + nd.i);
nd.i = 0xcafe ; // hexadecimal case - base 16
System.out.println("Int hexadecimal value: " + nd.i);

nd.i = 0b10101010101010101010101010101011;
System.out.println("Int binary value: " + nd.i);

//Java 7 syntax
nd.i = 0b1010_1010_1010_1010_1010_1010_1010_1011;
System.out.println("Int binary value: " + nd.i);

nd.l = 1000_000l; // equivalent to 1000_000L
System.out.println("Long value: " + nd.l);

nd.f = 5;
System.out.println("Integer value assigned to a float variable: " + nd.f);

nd.f = 2.5f; // equivalent to nd.f = 2.5F;
System.out.println("Decimal value assigned to a float variable: " + nd.f);

nd.d = 2.5d; // equivalent to nd.d = 2.5D;
System.out.println("Decimal value assigned to a double variable: " + nd.f);

// implicit conversions
short sb = nd.b;
System.out.println("byte to short: " + sb);

Expand All @@ -21,30 +59,35 @@ public static void main(String... args) {
long lb = nd.b;
System.out.println("byte to long: " + lb);


byte bv = 23;
short sbv = bv;
System.out.println("byte to short: " + sbv);

int ibv = bv;
System.out.println("byte to int: " + ibv);

// explicit conversion
ibv = 130;
bv = (byte) ibv;
System.out.println("int to byte: " + bv);

// comparison and assignements between different numeric types
nd.f = nd.l;
nd.i = 2;
if (nd.i < nd.d) {
System.out.println("yeey!");
System.out.println("Int smaller than double value!");
}



float maxLongF = Float.MIN_VALUE;
System.out.println("max long= " + Float.MIN_VALUE);
float maxLongF = Long.MAX_VALUE;
System.out.println("max long= " + Long.MIN_VALUE);
System.out.println("float max long= " + maxLongF);

String s = String.format("%-24s: min = %s, max = %s", Float.class, Float.MIN_VALUE, Float.MAX_VALUE);
System.out.println(s);

s = String.format("%-24s: min = %s, max = %s", Double.class, Double.MIN_VALUE, Double.MAX_VALUE);
System.out.println(s);
}

@Override
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions chapter05/primitives/src/main/java/com/apress/bgn/ch5/ReferencesDemo.java 100644 → 100755
@@ -1,15 +1,36 @@
package com.apress.bgn.ch5;

import com.apress.bgn.ch4.hierarchy.*;

public class ReferencesDemo {


public static void main(String... args) {
Performer performer = new Performer("John", 40, 1.91f, Gender.MALE);
Human human = performer;
Actor actor = performer;
Musician musician = performer;

//performer = musician;
//or
//performer = human;
//or
performer = (Performer) actor;
}

public static void main2(String... args) {
IntContainer k = new IntContainer(42);
IntContainer q = new IntContainer(44);

swap2(k,q);

System.out.println("k = " + k.getValue());
System.out.println("q = " + q.getValue());

Performer performer = new Performer("John", 40, 1.91f, Gender.MALE);
Human human = new Performer("Jack", 40, 1.91f, Gender.MALE);
Actor actor = new Performer("Jean", 40, 1.91f, Gender.MALE);
Musician musician = new Performer("Jodie", 40, 1.71f, Gender.FEMALE);
}

static void swap(IntContainer a, IntContainer b) {
Expand Down
1 change: 1 addition & 0 deletions chapter05/primitives/src/main/java/module-info.java 100644 → 100755
Expand Up @@ -2,4 +2,5 @@
* Created by iuliana.cosmina on 3/5/18.
*/
module chapter.five.primitives {
requires chapter.four;
}
Empty file modified chapter06/chapter06.gradle 100644 → 100755
Empty file.
Empty file modified chapter06/src/main/java/com/apress/bgn/ch6/Main.java 100644 → 100755
Empty file.
Empty file modified chapter06/src/main/java/module-info.java 100644 → 100755
Empty file.
Empty file modified chapter07/chapter07.gradle 100644 → 100755
Empty file.
Empty file modified chapter07/src/main/java/com/apress/bgn/ch7/Main.java 100644 → 100755
Empty file.
Empty file modified chapter07/src/main/java/module-info.java 100644 → 100755
Empty file.
Empty file modified chapter08/chapter08.gradle 100644 → 100755
Empty file.
Empty file modified chapter08/src/main/java/com/apress/bgn/ch8/Main.java 100644 → 100755
Empty file.
Empty file modified chapter08/src/main/java/module-info.java 100644 → 100755
Empty file.
Empty file modified chapter09/chapter09.gradle 100644 → 100755
Empty file.
Empty file modified chapter09/src/main/java/com/apress/bgn/ch9/Main.java 100644 → 100755
Empty file.
Empty file modified chapter09/src/main/java/module-info.java 100644 → 100755
Empty file.
Empty file modified chapter10/chapter10.gradle 100644 → 100755
Empty file.
Empty file modified chapter10/src/main/java/com/apress/bgn/ch10/Main.java 100644 → 100755
Empty file.
Empty file modified chapter10/src/main/java/module-info.java 100644 → 100755
Empty file.
Empty file modified chapter11/chapter11.gradle 100644 → 100755
Empty file.
Empty file modified chapter11/media-handling/build.gradle 100644 → 100755
Empty file.
Empty file.
Empty file modified chapter11/media-handling/src/main/java/module-info.java 100644 → 100755
Empty file.
Empty file modified chapter11/read-write-file/build.gradle 100644 → 100755
Empty file.
Empty file.
Empty file modified chapter11/read-write-file/src/main/java/module-info.java 100644 → 100755
Empty file.
Empty file modified chapter11/serialization/build.gradle 100644 → 100755
Empty file.
Empty file.
Empty file modified chapter11/serialization/src/main/java/module-info.java 100644 → 100755
Empty file.
Empty file modified chapter12/chapter12.gradle 100644 → 100755
Empty file.
Empty file modified chapter12/src/main/java/com/apress/bgn/ch12/Main.java 100644 → 100755
Empty file.
Empty file modified chapter12/src/main/java/module-info.java 100644 → 100755
Empty file.
Empty file modified java-for-beginners_small.png 100644 → 100755
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions settings.gradle 100644 → 100755
Expand Up @@ -5,6 +5,7 @@ include 'chapter01'
include 'chapter03'
include 'chapter04'

include 'chapter05:arrays'
include 'chapter05:data-types'
include 'chapter05:data-structures'
include 'chapter05:primitives'
Expand Down Expand Up @@ -33,4 +34,5 @@ rootProject.children.each { project ->
assert project.buildFile.exists()
assert project.buildFile.isFile()
}
include 'arrays'

0 comments on commit 34f82b7

Please sign in to comment.