mdinfotech.net  



Course and Lab Introduction
  • course introduction and expectations
  • the backup talk
  • logging in
  • H drive vs C drive vs Student_Collab Drive
  • evaluation
There are 4 types of assessments used to help you learn to program:
  1. 100/80/60 Quizzes: short 5 minutes programming problems given at the beginning of class. 60% - indicates you tried, 80% indicates you have the solution with at least one mistake, 100% indicates you have the correct solution.
  2. In-Class Exercises and Programming Exercises: small exercises that take a few minutes to a couple of periods to solve and students are encouraged to keep trying until the solution is achieved. These exercises teach individual skills.
  3. Programming Assignments: complex problems that take about a week to complete, and give you the opportunity to apply all the individual skills you have learned to a large scale problem. Programming assignments are marked against test cases that are not provided ahead of time, and your code will also be collected and assessed for formatting, commenting and chosen algorithms.
  4. Term Project: a large scale problem that will take about two weeks to solve, in a group or individually. Problem complexity will try to be matched with programmer skill. Each of these projects is worth 15% of your mark.
Ms. Wear will teach a lesson that covers these questions. Answer them as the lesson is taught.

Answer the questions in a Google document
  1. Define software.
  2. What is another word for program?
  3. Why was binary a bad way for humans to enter data into a computer?
  4. What was the first improvement on binary?
  5. What was the first high-level language?
  6. List other high-level languages.
  7. Is Java a high level or low level language?
  8. All running programs are stored in ______________.
  9. What is an operating system?
  10. List 3 categories of software and an example of each.
  11. List examples of operating systems.
  12. How do operating systems decide how to prioritize resources?
Resources
You decide.
  1. Read Do Programmers Actually Need Touch Typing?.
  2. Test your code typing speed Typing.io.
  3. Learn to type at Typing.com.

In class, we will create and run a "Hello World" Java program using an Integrated Development Environment called Eclipse. Eclipse is available for free download so you may also work from home.

Resources for Programming with Eclipse Resources for Installing Eclipse at Home

If you have trouble running Eclipse at home, you may try another lightweight IDE like IntelliJ, Dr Java, or jedit.

If you have trouble using Eclipse at school, try Notepad++ and follow these instructions for running Java on Notepad++.

Read Hello World for an explanation of HelloWorld.java.
A quick review of output, integers, doubles, and if statements in Java.
Use the Scanner class to get input from the keyboard.
This needs to be the first line in your program:
                 import java.util.Scanner;
                 
Create an instance of the scanner class. This goes at the top of main:
                 Scanner in = new Scanner(System.in);
                 
To get integer data, create an integer variable at the top of main and get the input after asking the user to enter a number:
                 int x = 0;
                 ⋮
                 System.out.print("Please enter a number: ");
                 x = in.nextInt();
                 
To get numbers with decimals, create an double variable at the top of main and get the input after asking the user to enter a number:
                 double y = 0;
                 ⋮
                 System.out.print("Please enter a number: ");
                 y = in.nextDouble();
                 
To get a string up to the next hard return, create a String variable at the top of main and get the input after asking the user to enter some text:
                 String s = "";
                 ⋮
                 System.out.print("Please enter some text: ");
                 s = in.nextLine();
                 
nextLine() can also be used to empty the input stream of extra characters.

Additional methods can be found at the Scanner API

Resource: Watch Using the Scanner Class
  1. Define the problem.
  2. Understand the problem.
  3. Formulate a solution (flow chart or a list of steps)
  4. What classes do I need to import?
  5. What variables do I need?
  6. Write the program.
  7. Test the program.
  1. Create a new program that asks the user for a mark on a test using the Scanner class. It should support decimal values since sometimes half marks are given. This should be a double.
  2. It then asks the user for the total marks possible on the test using the Scanner class. This should be an int.
  3. Finally it will display the score as a percent in the console.
Sample Input
                  3.5
                  6
Sample Output
                  58.33333%
                  
  1. Create a new program that asks the user for 3 numbers. It should support decimal values. These should be doubles.
  2. It will then display the average of the three numbers in the console.
Sample Input
                  5.5
                  6.5
                  7.5
Sample Output
                  6.5
                  
You're first 100/80/60 Quiz.
There are 10 kinds of people in the world, those that understand binary and those that don't.
Answer the following questions in a Google Doc document during the lesson:
  1. What is the minimum value of an 8 digit binary number in binary? What is the minimum value of an 8 digit binary number in decimal?
  2. What is the maximum value of an 8 digit binary number in binary? What is the maximum value of an 8 digit binary number in decimal?
  3. Write the numbers 0 to 7 in binary.
  4. What is a bit?
  5. What is a byte?
  6. What is a kilobyte? Megabyte? Gigabyte? Terabyte? Petabyte?
  7. Approximately how many kilobytes are in a Terabyte? Megabytes in a Gigabyte? etc.
  8. How are positive integers stored?
  9. How are decimal values (or floating point values) stored?


Resources:
Activity: Run this code.
       short largestShort = Short.MAX_VALUE;
       int largestInteger = Integer.MAX_VALUE;
       double largestDouble = Double.MAX_VALUE;
       System.out.println("The largest integer value is " + largestShort);
       System.out.println("The largest integer value is " + largestInteger);
       System.out.println("The largest double value is " + largestDouble);
       
Complete these steps:
  1. Now modify this code so that it also prints the MINIMUM values of a short, integer, and a double.
  2. short,int, and double are called Primitive Data Types. Look up 8 primitive data types in Java at w3Schools.
  3. Now add a variable of each type to your program (except boolean and char). Print the minimum and maximum values of each.
  4. For the boolean type, there are only two possible values. Add this code to your program:
          boolean isCSAwesome = true;
          System.out.println("isCSAwesome? " + isCSAwesome);
          
  5. Now assign isCSAwesome the other value it can store, and print its value as well.
  6. For the char type, any character can be assigned using single quotes. Add this code to your program:
          char myInitial = 'W';
          System.out.println("My last initial is " + myInitial);
          
  7. Now assign myInitial your first initial and print its value as well.
  8. This exercise will be used to complete some of the questions in the next lesson.
Lesson and notes:
  • Primitive Data Types,
  • the size of each type
  • and the relationship between maximum and minimum values.

  • Copy, paste, and answer the questions below into a Google Docs document. Use the output from In Class Exercise 3, the lesson, and the resources below to complete.

    Variable Size, Min and Max Values
    1. Complete this table:

      Data TypeSizeMinimum ValueMax Value3 Example Values
      int4 bytes (32 bits)-31, 2, 100324333
      short
      long
      byte
      char
      boolean
      double
      float

    2. What is the relationship between the size of an integer data type and it's minimum and maximum values?
    3. List all the variable types considered to be integer types (there are 5)?
    4. List all the variable types considered to be floating types (there are 2)?
    Variable Names
    1. List all the syntax rules for naming variables:
    2. List all the conventions for naming variables:
    3. For each of the following variable names list if it is valid or invalid and explain why?
      Variable NameValid/InvalidWhy?
      num
      num1
      1num
      $num
      num 1
      num_1
      Num
      numberOne
      numberone
    4. This style of naming a variable: firstNumber, where the second word has a capital letter, is called _________________.
    5. If a variable is not named with the notation described in the previous question, will the program compile?
    Other Stuff to Know
    1. Define: scope.
    2. When assigning a variable with type char a value, what symbols must go around the value?
    3. What is the output of the following code?
      System.out.println("The Answer to the Ultimate Question of Life, the Universe, and Everything is " + 21 + 21);
      
    4. What is the output of the following code?
      System.out.println("The Answer to the Ultimate Question of Life, the Universe, and Everything is " + (21 + 21));
      
    5. Why is there a difference between the output of the above two code segments?
    6. In Java, = is not called the "equals sign", what is it called?
    7. How do you make a single line comment?
    8. How do you make a multi-line comment?
    Resources
    1) Read Variables Tutorial.
    2) Read Variable Types Tutorial.
    3) Read Character Tutorial (You'll need the ASCII Table).
    4) Read Odd's n' Sods.
    1. Download MathClassSampler.java.
    2. Examine and run the code.
    3. Use the Math API to complete this task.
    4. Calculate sin and cos of the same number as tan and print all 3 values with descriptive output.
    5. Calculate min of the same numbers as max and print both values with descriptive output.
    6. Calculate round of the same number as square root and print both values with descriptive output. Find the rounded value of the number entered by the user, not the rounded value of the square root.
    In class notes
    • Arithmetic Operators (+, -, *, /, %),
    • Simple Precedence (BEDMAS, left to right),
    • Special Case for Division: Casting and Integer Division.
    Activity:
    1. Do this with a partner.
    2. Read this code JavaMath.java.
    3. Assume the user enters 10 for the first number and 7 for the second number. Write your predictions for the program output on a sheet of paper. Use your notes and the materials below to make your predictions.
    4. When you are done, complete the program.
    5. Compare your predictions to the actual output.
    6. If any of your predictions are incorrect, figure out why or ask.
    Support Materials:
    1. Arithmetic Operators
    2. Integer Division, Casting and Modulus
    You're first Written Quiz.
    1. Video: Transistors - Why Computers Use Binary
    2. Lecture: Von Neumann Architecture
    3. See and discuss: Case, Power Supply, RAM, HDD, SSD, CPU, motherboard, Graphics/Audio/Network Expansion Cards vs Onboard, Optical Disk Drive (CD/DVD).
    In your online notes, explain the purpose of each of the following, and include an image of each:
    1. Case,
    2. Power Supply,
    3. RAM,
    4. HDD, SSD,
    5. CPU,
    6. motherboard,
    7. Graphics/Audio/Network Expansion Cards (explain the pros and cons of Expansion Cards vs Onboard),
    8. Optical Disk Drive (CD/DVD).
    Resource: parts of hardware. This material will be quizzed on.
    Watch:

    Use Wikipedia's Computer Storage to complete the following table:

    Explanation: Hierarchy (primary, secondary, tertiary, offline), Volatility (non-volatile, or volatile), Mutability (r/w, read only, slow write/fast read), Accessibility (random or sequential), Type (semi-conductor, magnetic, optical, paper)

    Storage TechnologyDefinitionHierarchy
    (primary, secondary, tertiary, offline)
    Volatility
    (non-volatile, or volatile)
    Mutability
    (r/w, read only, slow write/fast read)
    Accessibility
    (random or sequential)
    Type
    (semi-conductor, magnetic, optical, paper)
    RAM
    Solid State Drives
    Hard Drives
    USB Sticks
    DVD-R
    DVD-RW
    Remember to bring a Philips screw driver on BLOCK B: TUESDAY and BLOCK D: FRIDAY

    Hardware Lab

    String Processing is using a program to analyze the properties of a string. String processing is used extensively in programming. For example, whenever you do a Google search, the string you enter into the search field must be processed to produce the search results.

    The Java API is the online documentation for all Java classes. As a Java programmer, it is imperative you learn to use the API. In this exercise you will use the String API to process a string entered by the user.

    Read the following:
    1. Read Java For Students Strings Tutorial
    2. Scroll to "Method Summary" in the String API to see a list of all methods you can use to process Strings.
    3. To see an example of how to use each String method, read Using String Methods with Examples. Click on the name of the string method to see an example of how to use it.

    Create a program that uses the console to ask the user to enter a favourite quote. Analyze the quote as described below. For each item in the list you will need to use one string processing method from the String API. It is your job to read the API and determine which is the best method for the job. All output must be in the console using System.out.println().

    1. Print the letter that appears at index 10. Note: If the quote has less than 10 characters this will throw an exception.
    2. Print the index of the first occurrence of the letter t.
    3. Print the number of characters in the quote.
    4. Replace every occurrence of the word "the" with "foobar". Print the new quote.
    5. Convert the entire string to all caps. Print the new string.
    6. If the quote contains the word "horse", print "I like your taste". Hint: You will need an if statement.
    In class notes
    • Compound Operators,
    • Increment and Decrement,
    • Pre/Post.
    • Precedence

    Sample code: How to Count the Number of Digits in an Integer

    int num = 654321;
    int temp = num;
    int numDigits = 0;
    
    do { 
        System.out.println(temp);
        temp /= 10;
        numDigits++;
    } while (temp > 0);
    
    System.out.println("There are " + numDigits + " digits in " + num);
    			 
    Activity - predict the output on paper, then run code to check results
    1. What is the output of this code segment?
    int i = 1;
    int j = 2;
    int k = 3;
    int m = 4;
    i += j + k;
    j *= k = m + 5;
    k -= m /= j * 2;
    System.out.println(i);
    System.out.println(j);
    System.out.println(k);
    System.out.println(m);
    
    2. What is the output of this code segment?
    int a = 1;
    int b = 2;
    int c = 3;
    System.out.println(++a * b - c--);
    System.out.println(a);
    System.out.println(b);
    System.out.println(c);
    
    3. What is the output of this code segment?
    int a = 1;
    int b = 2;
    int c = 3;
    int d = 4;
    System.out.println(++b / c + a * d++);
    System.out.println(a);
    System.out.println(b);
    System.out.println(c);
    System.out.println(d);
    
    Support Materials:
    1. Compound Operators
    2. Pre/Post Increment
    3. Pre/Post Increment and Decrement in more detail
    4. Precedence
    Class Discussion
    • How integers and floating point values are stored
    In Class Exercise: Round Off Error
    1. Run this program. RoundoffError.java
    2. Repeat the mathematical calculations done by the program on paper. What value do you get on paper? Does the program give the same answer?
    3. Using the links and diagram below, formulate an answer as to why the values different.
    4. Try writing a solution to the problem so the program agrees with your calculated answer.

    In your notes, summarize what Round off Error is, why it exists and how to deal with it.

    In Class Exercise: Integer Overflow
    1. Download and run MaxVariablesDemo.java
    2. Use the increment operator (++) to add one to the variables largestByte, largestShort, largestInteger, and largestLong after printing the values of each (around line 26)
    3. Immediately after increasing the value of each by one, print the values of each again.
    4. What are the new values?
    5. Using the links and diagram below, formulate an answer as to why the values are what they are.

    In your notes, summarize what Integer Overflow is, why it exists and how to deal with it.

    Resources:
    In class lesson:
    1. In class lesson on Binary, Hexadecimal and Decimal and how to count. Resources: Intro to Binary, Intro to Hex
    2. In class lesson on conversion between Binary to Decimal, Hex to Decimal, Hex to binary, Binary to Hex.
    3. In class lesson on binary addition. Resource: Binary Addition
    4. Practice: Bin->Dec and Dec->Bin Quiz
    5. In Class Exercise that will be collected: Number Systems Worksheet.
    When you are done: Play Binary Bonanza for Decimal to Binary practice or play Attack of the Hexadecimal for Hex to Binary Practice.
    Discuss Variable Scope.
    Variable Scope Tutorial
    a) PowersCalculator
    1. Download PowersCalculator.java.
    2. Examine and run the code.
    3. Add three more METHODs: powerOfThree(), powerOfFour(), and powerOfFive().
    4. Add code to main so that the message window displays the number to the power of 2, 3,4 and 5.
    b) PlayDice
    1. Download PlayDice.java.
    2. Examine and run the code.
    3. Modify the program so that it rolls three dice and asks the user to guess their total before they are rolled. Tell the user if their guess (between 3 and 18) is correct or not.
    c) CalcAverage
    1. Use this incomplete template to write a method called average() that accepts four numbers and returns the average of those numbers.
    1. Read if..else at W3 Schools.
    2. Read Boolean Expressions Tutorial
    3. In class lesson on:
      • if,
      • if..else,
      • if..else if..else,
      • boolean operators, <, >, <=, >=, ==, !=, and, ||, !
      • Compound Boolean Operators, &&, ||
      • Boolean Expressions
    4. In Class Exercise:

      Get a partner and complete the following worksheet: Boolean Expressions Exercises

    Resources:
    1. Iffy Programming Tutorial
    2. Or else... Tutorial
    3. True or False?
    4. Expressions, Statements and Blocks Tutorial.
    5. Control Flow Statements Tutorial.
    1. Read this article about Boolean Methods
    2. Write a method called isLeap() that has one formal parameter: the year, and returns true if it is a leap year, otherwise it returns false.
    3. Only use if...else statements, do not use the ternary operator.
    4. Write a test driver to your method for a number of valid leap years, valid non-leap years and extreme cases like -1, 0 and Integer.MAX_VALUE.
    5. The rules to determine if a year is a Leap Year are:
      1. A year divisible by 4 is a leap year (2004, 2008...), unless
      2. it is also divisible by 100 (2100, 2200...) in which case it is not a leap year.
      3. There is an exception. A year divisible by 400 is a leap year (2000, 2400...).
    6. Carefully consider the order in which the above checks should be done to write a simple, accurate, algorithm

    A test driver is a main method that tests your method isLeap() with a variety of inputs to ensure it works. A sample test driver might look like this:

    			System.out.println("2000 is a leap year: " + isLeap(2000));
    			System.out.println("2001 is a leap year: " + isLeap(2001));
    			System.out.println("2002 is a leap year: " + isLeap(2002));
    			System.out.println("2003 is a leap year: " + isLeap(2003));
    			System.out.println("2004 is a leap year: " + isLeap(2004));
    			System.out.println("1800 a leap year: " + isLeap(1800));
    			System.out.println("-1 is a leap year: " + isLeap(-1));
    			System.out.println(Integer.MAX_VALUE + " is a leap year: " + isLeap(Integer.MAX_VALUE));
    			System.out.println("1800 a leap year: " + isLeap(1800));
    			
    			

    Write a test driver that proves your method isLeap() works.

    Read this website about the ?: operator, a shorter version of the if statement.

    switch statement for integers, and enumerated types like Strings, fall through, and using fall through to achieve > or

    1. Read about the ?: operator.
    2. Read this Switch Statement Tutorial
    3. Write a method called getMaxDays(int m, int y) that has two parameters: m which is the numerical value of the month (Jan = 1, ..., Dec = 12) and y which is the year.
    4. Use a switch statement to return the number of days in the month (Jan has 31, Feb has 28 OR 29, ..., Dec 31)
    5. For February, use the ternary operator AND a call to isLeap to decide whether to return 28 or 29. **Note: You will need to copy and paste isLeap into the current program.
    6. If the month is not between 1 and 12, return -1. Note: -1 is an invalid number of days and indicates to the calling method that the input was invalid. It is the job of the calling method, not getMaxDays to ensure the input is valid.
    7. Write a test driver to test your method for all 12 months and extreme values such as -1, 0, and 13.

    Write code that will not crash, is user friendly, and uses dialog boxes.

    Topics include Option Panes, and Try...Catch, used with do..while and break/continue. Program along with Ms. Wear in class.

    If the user clicks "Cancel" on a dialog box, you want the program to shutdown cleanly. You will need the code below:

    
            String input;  // user input always comes in as string data
    
            // get user input
            input = JOptionPane.showInputDialog("Enter stuff!");
    
            // if the user clicks cancel, input will be null, then exit the program
            if (input == null) {
                System.out.println("You clicked cancel");
                System.exit(0);
            }
    Resources:
    1. Option Panes
    2. Catching Exceptions with Try Catch
    3. Variations on dialog boxes: JOptionPane. See this JOptionPane Tutorial for more information.
    In this exercise, you will write methods with the purpose to get input from the user, or to display output to the user. These are called Input/Output methods.
    1. Write an input method called getYearFromUser()
      • it uses a dialog box to ask the user for the year and returns the value of the year.
      • This method has no parameters
      • If the user does not enter a positive integer, the method should repeat the question until valid input is gathered.
      • It should return an int.
    2. Write an output method called showResult() that has a string as a parameter and shows the string in a JOptionPane.showMessageDialog.
    3. Use this test driver to test your methods:
                            int year = getYearFromUser();
                            showResult("You entered the year " + year);
                       
    Commenting and Formatting: things to remember in all your code:
    1. Include program comments (program name, author, date, purpose).
    2. Include in-code comments.
    3. Leave a blank line before in-code comments
    4. Do not put code inside in-code comments.
    5. Label closing brackets.
    6. Indent 4 spaces after every {.
    7. Put spaces around operators.
    8. Use meaningful variable and method names.
    9. Create all your variables at the top of the method.
    10. No spelling errors in variable names, user interface, or comments.
    11. Follow Java Coding Conventions
    Efficiency and Design
    It's good enough if my program works right?
    
    Wrong.
    
    Your programming working is the first, and most important, thing to consider. 
    After all, a program that doesn't work is no good to anyone, and worth
    no marks.
    
    If you assume a program works, then is there more that should be considered?
    
    Yes.  
    
    These principles of programming should be considered in EVERY program you write and in the following order.
    
    1) Readability
     Many programs will require modification by you or another coder in the future.  A poorly written program can take more time to read and understand than the actual
     modification. Programs should be written in a way that is easy to comprehend quickly.  This includes the following practices:
       a) define your variables at the top of methods
       b) comment your code
       c) use algorithms that are easy to understand. For example, avoid nested ternary operators.
    
    2) Reusability
      Avoid duplicating code. Always write reusable methods instead. If a modification needs to be made, it then only has to be made in one location.
      
    3) Efficiency
      When time permits, write algorithms that minimize the use of RAM and CPU time. For example, if you have nested for loops, does each loop need to 
      run n times, or can the inner loop run fewer times?
      
    4) Elegance 
      Elegance is very subjective.  However, some things are clear. Read Programming is an Art. 
                     
    Taught in class.
    Write a program that will read in the number of the day, the month, and the year a person was born.

    For example, someone born on September 3, 1982, would enter the numbers

    3, 9, and 1982.

    into three separate JOP.showInputDialogs.

    If an incorrect value is entered, the program should give a very specific error message explaining why it is invalid and ask the user to enter the information again. A user should never have to reenter more than one of the values when an invalid entry is made (example, if the day is invalid, the user should only have to reenter the day, not the month or the year).

    The program will then tell the person their birthdate with the following format:

    You were born September 3, 1982.

    The format of the date must be as shown above.

    Important
    • The program MUST do error checking for invalid months (valid between 1 and 12)
    • The program MUST do error checking for invalid years (valid >= 1800)
    • The program MUST do error checking for invalid day of month (valid between 1 and maxDay in month (30, 31, 28 or 29))
    • The program MUST only allow Feb 29 on LEAP YEARS only
    • Make it Ms Wear Proof.
    • This program is to be handed in with complete commenting on tba. Print out the code.
    • Try the following test cases:
      1. 1 1 2001
      2. 31 9 1945
      3. 29 2 1988
      4. 29 2 1987
      5. 1 24 1934
      6. 29 2 2000
      7. 29 2 2100
      8. 29 2 2400

    Evaluation (/46)

    Test Cases(/20)
    - user clicks cancel,
    - invalid values,
    - valid dates,
    - invalid dates
    Code
    - Commenting and Formatting (/10)
    - Efficiency and Design (/5)
    Requirements
    - Asks for day, month, year in a user friendly manner (/2)
    - Prints birth date in correct format (/4)
    - Errors accurately describe user mistake in same dialog box as the questions and repeat question (/5)
    • All topics in this unit will be covered
    • 50 multiple choice questions
    • closed everything