Programming Fundamentals -- Java

ROAD MAPv1.08

Instructor(s): dave.slemon@mohawkcollege.ca, kaylin.parker@snpsteam.com, nathan.rowbottom@snpsteam.com




Software Off the Shelf
(reusable java code)
Extra

What is the JavaDoc tool and how do you use it?

more about Javadocs
   How to Write Doc Comments for the Javadoc Tool

Javadoc Embedder
Embed Javadoc comments into your Java Source Files
  Javadoc comment generator

Java Code Beautifier
   java code beautifier

Scanner Class - Reading input from console

There's a scanner reading issue when you read an integer and then a string; its got to do with the newline character; here's a fix: scanner and the newline issue

Scanner Class - Reading from textfiles ( source )    ( words.txt )

Great Stuff


Data Files

words.txt -- a dictionary of 172,710 words

small.txt -- a small 7 word dictionary

ingwords.txt  -- 12,557 words ending in ing

Random Number Generator
random.java -- a random number generator

EQUAL method
real-number-equals-method.java -- used to see if two real numbers are equal to each other

Linear Search method
linear search -- used to search an array of integers for a target

Binary Search method
binary search -- used to search an array of integers for a target BUT the array MUST be sorted

Bubble Sort method
bubble sort -- used to sort an array of integers

Quick Sort method
quick sort -- used to sort an array of integers

1-on-1 Zoom Extra Help Session(s)
Click on the link below to Join

Note: the chat will end early if no one appears in the first 10 minutes.


Thur Jan 13, 2022 at 7:30p
Fri Jan 14, 2022 at 7:30p
Sat Jan 15, 2022 at 2:00p
Sun Jan 16, 2022 at 7:30p
Wed Jan 19, 2022 at 7:30p
Thu Jan 20, 2022 at 8:30p
Sat Jan 22, 2022 at 3:30p
Sun Jan 23, 2022 at 11:30a
Mon Jan 24, 2022 at 8:30a
Wed Jan 26, 2022 at 8:30a




       
Class Topics Tasks Extra

14

2-Hour Practice Final College Exam    (headphones, texting, emailing to other people are NOT permitted)
Q1. Strings / While / IF / Methods (30 mins)
question-specs product-shot how-to-video
Q2. 1-d Arrays & Loops (30 mins)
question-specs product-shot how-to-video
Q3. Graphics (30 mins) ( download e3p.zip to start )
question-specs product-shot how-to-video

13

Data Validation

Software Disasters

  • What are Algorithms. Who creates algorithms.
  • Algorithms vary with respect to speed, space requirements etc.
  • Judging one algorithm against another.

Video 130: What is an Algorithm? (5:27 mins)

Video 131: Floating Point Numbers (9:15 mins)


What we covered in class today:

Today, we learned about algorithms. How they are made and tested. What happens if poor testing is allowed to happen.

Practice Quiz#3 - more Arrays

Be sure to adequately document your program.

specifications

Video 132: how-to-video (7:04 mins)

Help? If you can't do this problem in 30 minutes, you need assistance. Assistance on this and any worksheets are available at the fireside chats.

12

Reading & Writing Text Files

Example 1: Reading a Dictionary called, words.txt    readfiles.zip

Example 2: Write a Java program to visit every word in the dictionary and display the words which contain the string ing. Also, display the total number of words found that end in ing.
Recall, the string method .contains   This program requires the words.txt dictionary file.   code

Example 3: Write a Java program which displays a random word from the dictionary.    This program requires the words.txt dictionary file.   solution

Example 4: Writing a File This program creates and writes one line to a new text file.

Video 120: Reading from Files (5:33 mins)

Video 140: Writing to Files (4 mins)


What we did in class today:

Today, we were introduced to reading files in Java. We practiced reading the dictionary with two Simon Says exercises. Next we watched a video on how to construct a guessing game. Finally today's worksheet asked us to find all of the palindromes in the English language.

Today's Worksheets

  • W29 - Files - Palindromes
    Consider getting your program to work first on a small.txt dictionary


Quiz#2

Be sure to adequately document your program.


specifications

(download this Q2.zip )

(when you're finished, ZIP up your quiz, called it, Q2-finished.zip and email it to dave.slemon@mohawkcollege.ca)

A NanoSecond StopWatch

import java.util.Locale; //for nanoTime
long start = System.nanoTime();

long finish = System.nanoTime();
double elapsedTime = (double)( (finish - start) / 1E9 );
System.out.println("Time was: " + elapsedTime + " secs");




11

Random Numbers

Example: Random Numbers Program

Try each of these programs, before looking at the code.

Example 1: Write a main pgm to populate a 20 element integer array, called A, with random numbers between 0 - 25 inclusive. code

Example 2: Write a main pgm to add up the sum of all the numbers found in a random double array, called weights, of 10 elements. Make the numbers between 1 - 25 inclusive. code

Example 3: Write a main pgm and two static methods called, smallest and frequency, which run through an array, called data, of 100 integers values. Reports back the smallest integer found. Make the numbers between 0 - 99 inclusive. In addition, report back as to how many times the smallest element occurred in the 100 element integer array called data. code

Video 110: Guessing Game (12:35 mins)


What we covered in class today:

Today's Worksheets

  • W30 - Word Game
    Consider getting your program to work first on a small.txt dictionary

    how-to-video Guessing Game (12:35 mins)


More about Random Numbers in Java


10

Sparse 2-D Arrays

A matrix is sparse if many of its coefficients are zero. Many large programs in nature can been represented by sparse arrays.

sparsity =
    (count of zero elements / total element count)


Example1: Walking through 2d Array
   Traversing a 2d Array

Example2: Triplet Example
   a start to the Triplet worksheet

Video 101:   2-D Arrays (8:50 mins)

Video 102:   Sparse Matrix Algorithm -- Triplet Algorithm (4:40 mins)


What we covered in class today:

Today, we learned how to create 2-D arrays. The sparse array takes up lots of space, so we learned how to compress such arrays with the triplex algorithm.

Today's Worksheets

  • W27 - For loops -- Histogram
  • W28 - Sparse Matrices - Triplet Algorithm

a sparse array
Fig 8.1 A Sparse Matrix
a sparse array
Fig 8.2 Java Declaration

Java Declaration


9

Using Arrays

Become an expert at arrays by doing all 36 questions. Recommended: do a question, then check the answer to that question, etc.
36 Array Practice Problems    ( solutions )



Friends example: product-shot    java code    zip

Video 90: About the Voting Machine (12:30 mins)


What we did in class today:

Today we discussed the requirements of the voting machine. We then coded an array example, involving finding your youngest friend and displaying their name and age.

Assignment #2 - Voting Machine

The Voting Machine Specifications      ( rubric )      ( data structures )

Video 90: About the Voting Machine (12:30 mins)


8

HSAFX - Graphics

   a simple graphics pgm    ( product-shot )    ( zip )

   displaying a car image    ( product-shot )    ( zip )


What we did in class today:

Today, downloaded and explored the HSAFX java graphics package.

Today's Worksheets

  • W23 - Stop Sign / Boardwalk


7

Loop Practice & Tracing

Video 70:   Nested For Loops (3:53 mins)


What we did in class today:

Today, we reviewed and practiced using, FOR loops.

Today's Worksheets

  • W24 - For loops -- Counting grains of sand.    ( BigIntegers )
  • W25 - For loops -- N factorial.
  • W18 - IF statements - Numeric to Letter Grade  ( main )   ( method )

Practice Quiz#2


Practice Quiz #2    ( main )    ( solution )
( The real quiz #2 will occur in the last 30 minutes of class #8)

6

For Loops & Built-In Arrays

Video 60:   Arrays (8:26 mins)

Video 61:   For Loop (10:02 mins)


What we did in class today:

Today, we studied the for loop and built-in arrays.

Today's Worksheets

  • W20 - IF statement - 5 Course Grades
  • W21 - While - The Beer Song Lyrics
  • W22 - Arrays - finding min and max

Senior Programmers ONLY!

    Video 62:   For Loops (2 mins)
       ( also demonstrates how to iterate over an array )



5

While Loops & Switch

While Loops ( pptx )

Example 1: While Loops with Sentinels (break & continue) code

Video 50:   While loops (6:32 mins)

Video 51: switch statement - a more complicated type of IF statement (8:45 mins)


What we did in class today:

Today, we studied the while loop, do-while loop, and switch statement.

Today's Worksheets

  • W14 - Length Conversions  (ans)
  • W16 - Switch Statement   (main)  (method)
  • W17 - Logic Problem
  • W19 - IF statement ( a flawed algorithm for temperature )



Senior Programmers ONLY!

  • W15 - Substring Function   (ans)


Assignment #1 - Triangles

Senior Programmers have a choice to do the Triangles assignment or the Banking ATM assignment.
assignment 1

rubric

sample code

law-of-cosines

A very good triangle checker where you can enter 3 ordered pairs. triangle calculator


4

IF Statements

Video 40: if statements with AND ( && ) and OR ( || )(12:28 mins)

Video 41: if statements & boolean comparisons with >=, <=, !=, ==, >, <   (5:55 mins)


What we did in class today:

Today, we studied the IF statement along with the AND and OR boolean operators. For a more longer set of cases we introduced the switch statement.

Today's Worksheets

  • W7 - more String Functions  (ans)
  • W10 - building isOdd( ) static method  (ans)
  • W12 - Temperature Converter   ( method,   main )

Senior Programmers ONLY!




3

Static Methods

Video 30: User-defined Methods (10 mins)
Demonstrates how to create a method which is called from the main program. The method also contains 2 arguments and 2 parameters.

Video 31: Returning Information from a Method (7:15 mins)


What we did in class today:

Today, we wrote user-defined string and numeric methods. These methods all accept input parameters and return a value.

Today's Worksheets

  • W6 - String Functions  (ans)
  • W8 - Math Functions  (ans)

Practice Quiz#1

(based on W0 - W6)

Quiz #1   ( docs   main )
( The real quiz #1 will occur in the last 30 minutes of class #5)


Useful Links

   Method Overloading example



Senior Programmers ONLY!

  • W9 - Math Functions  (ans)

    (for more information about the distance formula, watch this)

2

Strings & Numbers

Strings ( pptx )   ( code )

Numbers ( pptx )

Video 20: Getting User Input (7.9 mins)
  This video shows how to accept user input with the Scanner object.

Video 21: String Functions (17:02 mins)  This video begins discussing special characters, e.g. \n, \", \\, and + Next various string methods are demonstrated, e.g. toUpperCase(), toLowerCase(), .length(), contains(), charAt(), indexOf(), substring()

Video 22: Number Functions (2.5 mins)
  ( demonstrates numeric operations, .pow(),.sqrt(),.round() )


What we did in class today:

We introduced several string functions including concatenation, accessing characters in a string, lower and upper case, and the len( ) of a string function. We also introduced float( ), int( ). The mod operator also called the remainder operator was discussed, %.

Today's Worksheets

  • W3 - Input/Output Practice (Integers)  (ans)
  • W4 - Scanner - Getting User Input   (ans)
  • W5 - Strings versus Integers  (ans)

It's ok to peek at the answers if you need to. Be sure to MATCH the sample session EXACTLY. Don't put any creativity into these worksheets, just write code which matches the sample session.

Useful Links

   String Method Cheat Sheet

   Java Math Class

   AskForAge Program    ( code )

   Java Scanner Class
There's a scanner reading issue when you read an integer and then a string; its got to do with the newline character; here's a fix: scanner and the newline issue


1

Variables, Data types & Expressions

Video 10: About Java   (8:37 mins)
(installing bluej; then creating and running a Java program)

Video 11: Drawing a Shape (4.8 mins)
(demonstration of how to display a triangle on the console)

Video 12: Variables (12.4 mins)
(a demonstration of variables datatypes, String & int)


What we covered in class today:

Today we are running our first Java program. The first worksheet is about choosing a figure and writing a program to create that figure on the video screen.

Today's Worksheets

Complete each worksheet and demonstrate and/or hand the worksheet into your teacher.

  • W1 - Drawing a Shapes  (ans)   (r2)
  • W2 - Variables  (ans)

It's ok to peek at the answers if you need to. Be sure to MATCH the sample session EXACTLY. Don't put any creativity into these worksheets, just write code which matches the sample session.