ProgrammingChurch-JavaEdition

Assignments and Discussions for our Sunday Java Coding Sessions

View project on GitHub

Programming Church - 2021 JavaEdition

Overall Goals

The main goal is to expand our ability to build stuf in Java using modern practices for 2021 and have some fun along the way. We’ve keeping the irreverant ‘Programming Church’ moniker because we meet on Sunday morning for and there’s a whole lot of preaching and teaching going on. He’re is a list of concepts we’ll take on:

  1. Java language basics
  2. OO Design - making the leap to Objects rather than funtions
  3. Dependancy Management (package mangement) with Maven and Gradle
  4. Spring and Spring boot
  5. Enterprise Patterns for Java
    • Source Control
    • CI/CD patterns
  6. Enterprise Architecture
    • Databases / schema change management
    • Config management
    • Security
    • End Point Management
    • Messaging Systems
    • Caching patterns
  7. Various Cloud Deployments
    • Docker and K8s
    • Azure
    • GCP native
    • Amazon native

01/17/2021 First Assignment

01/31/2021

Goals for Today:

Notes from 1/31/21 live discussion

  • type casting
    • The example below calculates the slope using int math, then casts it to a double
      • To properly complete the math, we could either cast the scanner ints to doubles, or cast each side of the division to double
    • When doing math, it’s a good idea to avoid int data types becuase division can cause hard to find bugs.
  int x1 = scanner.nextInt();
  int y1 = scanner.nextInt();
  int x2 = scanner.nextInt();
  int y2 = scanner.nextInt();

  double slope = Math.abs(y1 - y2) / Math.abs(x1 - x2);
  • Object Oriented Programming
    • Passing messages between objects for it to handle the message - don’t care how outside the object
    • Diagrams - describe code, use as a plan for writing code, UML (Unified Modeling Language)
      • Sequence diagram
      • Package diagram
      • State diagram
    • Principles of Object Oriented Design - SOLID
      • Single Responsibility - each class should have a single responsibility
      • Open-Closed - open for extension, closed for modification
      • Liskov Substitution Principle - coding to the interface not the base class
      • Interface Segregation - create an Object that’s a composite of other Objects
      • Dependency Injection

Assignments for next week

2/7/2021

Goals for the Week

2/14/2021

Goals for the Week

2/7/2021

Goals for the Week

2/14/2021

Goals for the Week

Discussion Notes

We reviewed the Sudoku problem from the JetBrains Academy Mine Sweeper project. Screen Shot 2021-02-28 at 12 53 00 PM

We discussed the code that was in many of the solutions. The algorithm that seems to be predominant in the solutions on JetBrains Academy is to simply look for any duplicate number in the rows, column, and each mini-square. There was another solution that used an additive checksum algorithm to determine if a row or column matched the business rules. Below is a sample of the second solution.

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int nsq = n * n; 
        boolean solved = true;
//        The sum of each row, column, and nxn square will be the same in a solved sudoku
        int sum = 0;
        for (int i = nsq; i > 0; i--) {
            sum += i;
        }

        int[][] arr = new int[nsq][nsq];

        // Check row validity while adding data
        for (int col = 0; col < nsq; col++) {
            int rowSum = 0;
            for (int row = 0; row < nsq; row++) {
                arr[col][row] = scanner.nextInt();
                rowSum += arr[col][row];
            }
            if (rowSum != sum) {
                solved = false;
                break;
            }
        }
        
        // flip the row/column loops to calculate columns
        for (int row = 0; row < nsq; row++) {
            int columnSum = 0;
            for (int col = 0; col < nsq; col++) {
                columnSum += arr[col][row];
            }
            if (columnSum != sum) {
                solved = false;
                break;
            }
        }

        // check the nxn squares - use same loops, but incease start place by n
        for (int i = 0; i < nsq; i += n) {
            int squareSum = 0;
            for (int col = i; col < n + i; col++) {
                for (int row = i; row < n + i; row++) {
                    squareSum += arr[col][row];
                }
            }
            if (squareSum != sum) {
                solved = false;
                break;
            }
        }
        
        System.out.println(solved ? "YES" : "NO");
    }
}

Both algorithms are incomplete and fail in-depth verification, setting off alarm bells because neither approach is verifiability correct. They rely on the fact that if both the rows and the columns passed the check everything was good, although independantly either check is incomplete. They might generate the needed output for this simple exercise, but in either case it is easy to develop a test case that makes the code fail.
Even worse, refering to the additive checksum solution above, there is an implicit dependency between code blocks. Columns and rows and mini-squares must add up for the code to be correct. That means checkRows() can return true, but it’s really only true if checkColumns() and checkSquares is also true, that introduces a really dangerous logic dependancy between these two code modules. It’s easy enough to write an if statement that says “if( checkRows() && checkColumns) the model is correct”. However if in the future someone tries to re-use checkRows() independently they will find it can’t work on it’s own. In big systems those bugs are very difficult to find and fix.
As an alternative, we decided it would be better to use TDD and develop several test cases to exercise the code before we wrote a solution. Those test cases included rows and columns that didn’t have duplicates, but failed the sudoku business rules, and rows and columns that passed an additive checksum but failed the sudoku roles due the the communitive property of addition. In the end our solution was to keep track of the digits we saw in a separate array. If we saw all the digits we expect know the row or column is correct.

Assignments for next week

  • Continue to work through the coding examples in stage 3 and 4 of JetBrains Academy Mine Sweeper project).
  • Read the Java Coding Conventions at: https://www.oracle.com/java/technologies/javase/codeconventions-introduction.html
  • Listen to: Security Now Podcast 807 Dependancy Coversion story at 1:28:30
  • Listen to the Security Now Podcast 807 story on Microsoft’s final “Solorigate” update at 50:00. It emphasized the importance of keeping secrets separate from your code. Security Now Podcast 807

2/14/2021

Goals for the Week

  • Discuss coding style
  • Discuss recursion vs loops for problem solving
  • Discuss the Dependancy Confusion sectin of Security Now 807. It demonstrate that the industry puts too much trust on external dependancies, and also shows how one creative bounty hunter put dependancy management strategies of today’s dynamic languages to work to earn significant payouts from several bug bounty programs.
  • Discuss the story on Microsoft’s final “Solorigate” update at 50:00 of Security Now 807. It emphasized the importance of keeping secrets separate from your code.
  • Review Interesting coding issues that came up in this weeks exercises from JetBrains Academy Mine Sweeper project.

    Discussion Notes

    Random Topics:

  • Lithium Ion Car Jumpstarters are a great way to jumpstart cars in winter. They also make great general use portable power packs for all sorts of purposes.
  • Gaming Headphones for Remote Pairing - The Sennheiser Gaming Headset is the most popular among John’s collegues.
  • Work From Home Desk Enhancements - John’s latest addition to his WFH office is an adjustable vesa arm mounting system to hold his laptop, monitor, and other accessories. Viozon vesa arm mount system

Assignments for next week