COMP 10062 - Programming in Java

ROAD MAPv1.04

Instructor: dave.slemon@mohawkcollege.ca


Learning Outcomes: week1   week2   week3   week4   week6   week7   week9   week11  

Join the Class on Zoom: zoomLink


Announcements

28 Years Ago, Steve Jobs Said What Separates Successful People From Everyone Else Boils Down to 1 Simple Word.

pic of steve jobs
That simple word is: perseverance.


If you can't develop this quality, you may as well toss in the towel now.





Software Off the Shelf
(reusable java code)
Extra

Formatting with String.format( ) or .printf( )

output    code (demos String.format & printf)    ( video )

Conversions

Conversions Cheat Sheet

Best Practice: Always read in data from the user as a String, then convert it to what you need ...

String -> int


String -> double


int -> String


double -> String



Javadoc Generator
Add Javadoc comments into your Java Source Files
  Javadoc comment generator



Java Code Beautifier
   java code beautifier



Online Timer


table of contents

How to do in Java

  1. String Methods (e.g. .charAt(), .contains(), indexOf(), trim(), toUpperCase() )
  2. FOR loop - Enhanced For Loop with Arrays video
  3. Math Class Methods
  4. Conversions
  5. Files

  6. Sound
  7. JavaFx: GUI

UML templates

Java starting templates

highlight reel
( pptx )

About the Scanner Class


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 )
 

Datafiles


codes.txt -- ticket bar codes

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

small.txt -- a small 7 word dictionary

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



Great Stuff

Reusable Code

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

askQuestion method (graphios only)
askQuestion.java -- use when asking for integer input -- data validatiom

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

isNumeric(str)
isNumeric(str) -- used to determine if a string is a number

Circle Class
Circle( ) -- a Circle class

Rectangle Class
Rectangle( ) -- a Rectangle class

Dice Class
Die( ) -- a Dice class

       
Week Content Tasks Extras

4

OOPs Object Oriented Programming

Built-in Array Review 1
questions    solutions


Used Car Lot
question --> UML

Solution: TestProgram.java     Car.java     Plate.java


Pizza
question --> UML

Solution: PizzaSolution.zip

Soon you will be building GUIs, like this.


Data Validation - Coffee or Tea
question    solution



Assignment #2 - Chickens

  • Specifications

  • Point Form

  • Sample Session

  • Required:
    1. A UML diagram of the Chicken object.

    2. A working BlueJ project with two classes, a Chicken class and a ChickenCraft class. The latter class contains the main program. Your project should look like this.

    3. Hand in as an email with a ZIP attached, e.g. a2-chickens-firstName-lastName.zip


Video 402: A running start to Chicken Assignment (30:27 mins)


3



Wk 2

outcomes

guide

Control Statements




if pic
for pic
for pic
for pic
switch pic
for pic


Scanner's Secret Issue ( wrt to consuming the newline character left in the buffer )

When using Scanner in Java to read input, you may encounter issues with newline characters being left in the input buffer, especially when switching between reading different types of data (e.g., reading an int followed by a double).

In such cases, you can use input.nextLine() to consume the newline character and move to the next line.

Here's an example: Scanner's Secret Problem
( if you run this program you'll see the issue. To fix the issue, uncomment the scanner.nextLine(). )



Java Arrays

Below are flash cards with a lot of repetition to help you build and consolidate your understanding of Java arrays. Practice using the POWERPOINT file, running through these slides until you're knowledge of arrays is strong.

Array Flashcards   ( pptx )



JavaFX Graphics - with Circle( )

This program uses pre-built classes and illustrates how to ...

random circles

  • ask the user a question in a graphics program.
  • insist the user's response is in a given range.
  • generate random integers.
  • use an already built Circle( ) class in your graphics program.
To run this program, you'll need:
  1. RandomCircles.java
  2. Circle( ) class.
  3. getRandomInteger( ) static method.


Solution: ( code )    ( RandomCircles.zip )




Reading & Writing Text Files

Reading a File
Reading a Dictionary of 173K works called, words.txt
( code )  ( zip )


Identifying words ending in ...ING
Write a Java program to visit every word in the dictionary and display the words which end in ing. Save the ing words in a file called, INGwords.txt. 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 )   ( zip )


Picking Random 5-Letter Word
Write a Java program which displays a random 5-letter word from the dictionary.    This program requires the words.txt dictionary file.   ( code )   ( zip )


Writing to a File
This program creates and writes one line to a new text file.   ( code )


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

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

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

Video 33: Writing to Files (4 mins)


Lecture 2.1: If, While, Boolean (47:01 mins)

Lecture 2.2: For Loop (15:11 mins)


What we did in class: Wed Sep 18 2024

We began by reaching back to class 2 and introduced Java Arrays, using the Array flash card powerpoint file. This is a stack of flashcards which you should review over-and-over until you become strong in arrays.

The Scanner class has a weird issue regarding reading ints and doubles. We discussed this issue and saw an example which illustrates what happens if you don't clear the buffer after reading an int or double and then read a String.

We began by discussing the syntax of DO WHILE. The DO WHILE loop is a good choice for validating input data. We did a Simon Says example where we used a Circle() class and a DO WHILE to generate random circles on the canvas. We then used a Rectangle() class to do the same thing. This example might help with A1-Arkanoid.

Next, we discussed the SYNTAX of various control structures, like IF, WHILE, DO WHILE, and FOR.

Next, we designed the UML for a RemoteToyHelicopter

Lastly, we discussed the new worksheet(s).




Today's Worksheets ( protocol )

  • W4 - The Beer Song  (ans)
    (Be careful to get the last verse correct.)


MATCH the sample session EXACTLY, then add a small piece of CREATIVE code.



Remote Helicopter Class


Expect a question like this on your next test.

  • specifications

  • Draw the UML for your design.

  • Implement your UML by coding your methods into Java.

Solution:

2



Wk 2

outcomes

guide

Strings & Numbers

Java Strings   ( pptx )   ( code )

Java Numbers   ( pptx )

Scope

In Java, the scope of a variable or object refers to the region of the program where the variable or object can be accessed or modified.

  Example 1: ( code )   ( zip )

  Example 2: ( code )   ( zip )

Casting

In Java, casting refers to the process of explicitly converting a value from one data type to another.

cast pic

  ( code )   ( zip )

Java Arrays

Below are flash cards with a lot of repetition to help you build and consolidate your understanding of Java arrays. Practice using the POWERPOINT file, running through these slides until you're knowledge of arrays is strong.

Array Flashcards   ( pptx )

Documentation Standards


Guide

What are packages, and what are they used for?


  • Create a simple bank account app which allows deposits, withdrawals, transfers and the ability to display the customer's balance.

    1. UML: BankAccount model.

    2. Start with: TestProgram.java

    3. Code the BankAccount.java

    4. Next, add JavaDoc to your BankAccount class

    5. GUI Solution:    ( zip )


Video 20: Java Documentation (11:19 mins)

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() )


Lecture 1.4: Data Types (34:21 mins)

Lecture 1.5: Strings (12:22 mins)


What we did in class: Tue Sep 10 & Wed Sep 11

We began by discussing string methods as well as common methods used with numbers. Block level scope was discussed. We fixed two programs that had scope error issues.

Next, we discussed casting and where and when it is commonly used.

It was emphasized that a good understanding of Arrays are essential to this course. The Array Flashcards should be mastered.

We outlined the importance of good documentation and the minimum standards required for documentation when submitting your program. Packages are a good way to organize like-minded java files.

Today's worksheet centers on mastering format strings found in printf and String.format( )



Today's Worksheets ( protocol )


MATCH the sample session EXACTLY, then add a small piece of CREATIVE code.

Useful Links

   String Method Cheat Sheet

   Conversions

   Java Math Class


1

Java - Review

primitive types vs objects [ == and .equals( ) ]


TipCalculator
   System.out.printf( )  ( code )   ( ref )
   using a method:       ( code )

  • All objects start with a capital letter, e.g. String
  • All primitive types start with a lower case letter, e.g. int
  • Use camelCase for naming variables & methods, e.g. int secretNumber = 10;



JavaFX Graphics

Canvas Coordinate System: the canvas

Drawing tools
happyface   ( code )  ( zip )

Importing an Image (jpg, png etc)
the Car   ( code )  ( zip )

Animation
the Moving Ball   ( code )  ( zip )




What we did in class this class:

We began by reviewing data types, primitives versus objects, and methods using the tip calculator.A special discuss took place contrasting == and .equals().

Next, we did a simon says on the TipCalculator. The emphasis was on reviewing scanner and how to clear the line, as well as how to implement methods in Java.

Next, we ran our first JavaFx program called, the happyface program. We discussed starting templates and ran a program which imports images as well as a program which demonstrates animation with a ball rolling across the canvas.

The first assignment was discussed. This A1 involves using and documenting 3 java classes. The assignment features drawing a picture using the 3 given java classes.




Assignment #1 - Make a Picture
(using classes; documenting with JavaDoc)

In this assignment, we make a picture out of circles, rectangles and triangles. Begin by making 3 classes, namely Circle(), Rectangle(), Triangle() and test each one separately. Then, make your unique-to-the-class picture.

  • Specifications

  • product_shot

  • Hints: GeometricShapes.zip

  • Rubric: rubric    ( xlsx )
    Mark your own assignment before handing it in using this rubric.

    NOTE: Be careful to make sure you document your constructors and methods adequately. This means make sure you use @param and @return where necessary as well as a short descriptive sentence telling what the method does.