mdinfotech.net  



Section 1

Java

This is Java.

  • Programming Exercises
  • Regular 100/80/60 Quizzes
  • Short Answer Quizzes
  • Unit Exam
  • Game Project (15% of final mark)

Resources



Do this:
Programming
  1. Write a while loop that prints 10 random numbers (all between 1 and 10).
  2. Modify the loop from question 1 so that if one of the random numbers is 5, it prints "You win" and ends the loop.
  3. Write a while loop that prints the digits -100 up to 100.
  4. Write a while loop that prints the digits 100 down to -100.
a) Monty Hall
  1. Watch The Monty Hall Problem.
  2. Watch Monty Hall's Denial of the problem.
  3. Download and play this working version of MontyHall.java
  4. Modify this game so that the user can play repeatedly in a single run of the program. Count the number of times the player wins, and print the percentage of wins at the end of the program.
b) Grade Average
  1. Watch this video on Ending a Loop with a Sentinel Value (C++)
  2. Write a program that reads any number of grades in numerical format, averages them, and then displays the average to the user. Use a while or do...while loop to enter grades and a sentinel variable to indicate when to stop entering grades. Allow the program user to determine when to stop entering the grades.
Bonus: Make both programs Ms. Wear proof. This means I cannot crash it and it will never give weird results, like NaN% or incorrect averages.
  1. Watch this video on the For Loop
  2. Read this Going Loopy Tutorial.
  3. Complete these Loops Exercises **on paper**.
  4. When you are done, confirm your answers by running the code.
c) A Single For Loop
  1. Write a program to find the sum of first 42 positive numbers using a for loop.
d) Nested For Loops: Stars Triangle
  1. Write and run a program that reads a positive integer n and then prints a triangle of asteriks in that number of rows. Use two nested for loops. For example, if n is 4, then the output would be:
    *
    **
    ***
    ****
    Hints to solve this algorithm:
    1) Print a single star using System.out.print("*").
    2) Get the program to print n stars on one line (a single for loop).
    3) Then get it to print an nxn square of stars (another for loop around the first for loop). Use System.out.println(""); to add a hard return where you need one.
    4) Now change only one thing in the program will give you the triangle you seek.

    NOTE: Solutions with two for loops that are not nested, or with only one for loop will not be accepted.
Lesson in class on Arrays.
Resources: Read Hurray for Arrays.
Write a program that
  1. Creates an array of 200 integers
  2. Loads the array with 200 random values between 1 and 100 (Hint: use a for loop)
  3. Prints the elements of the array in order from index 0 to index 199. (Hint: use another for loop)
  4. Prints the elements of the array in REVERSE order from index 199 to index 0. (Hint: use a 3rd for loop)

Theory: When a variable with primitive data type is passed to a method, it is 'pass by value', which means a copy of the variable is passed into the method. When an array or object is passed to a method, thereference is 'passed by value', which means a copy of its reference is passed into the method. If the method modifies an array that has been passed in, the original array gets modified.

Read this: Passing to Methods.

In Class Exercise:
  1. Download this incomplete program.
  2. Complete the method absoluteValues() that replaces all the negative numbers in an array of integers with their absolute values.
  3. Complete the method indexOfMinimum() that returns the index of the minimum value stored in an array.
  1. Declare a 10x10 2D array of integers.
  2. Populate the array with random integers between (and including) 0 and 9.
  3. Call a method named print2DArray and pass it the above array. The method will print the array by line as shown below:
         1 2 3 4 0 9 4 3 2 6 
         4 3 2 1 5 3 4 2 0 9 
         5 6 7 8 5 3 7 5 8 1 
         8 7 6 5 0 8 9 5 7 3 
         5 6 7 8 5 3 7 5 8 1
         0 9 8 0 5 3 2 5 3 7
         4 3 2 1 5 3 4 2 0 9
         7 2 0 8 9 2 6 4 6 5
         1 0 7 9 3 5 7 4 6 3
         0 5 4 9 4 9 0 5 3 5
    	 
  4. Call a method named findSum and pass it the above array. The method will find the sum of the numbers in the array and print it.

In this assignment, you will learn to read data from a file, process the data, and write data to a file. The skills learned here will be required to complete Assignment 2.

Watch Ms Wear's Lesson on File I/O (Input/Output) to complete this exercise.

  1. Create a new Eclipse Project. Call your main class FileInputOutput.
  2. Copy and paste File Input and Output Code into your main class.
  3. Copy and paste this list of numbers. Save it into a text file called nums.txt in the bin folder of your Eclipse project.
  4. Run the code. You should see the contents of nums.txt print to the console.
  5. Look into the Eclipse Project folder using Windows Explorer (the file manager on Windows) and notice a new file called output.txt has appeared. Open it, and you should see a duplicate of the numbers in nums.txt. This file was created by your program!
  6. Now uncomment line 21, and write the method sumIntegers(). Run the program.
  7. It should print the sum of the values in the console. Check with other students to see if your sums agree.
Watch this video lesson on Writing the Dictionary to write a method called isValidWord() which accepts one string and returns true if it is a valid 4 letter English word.
  1. Put this four letter list in a file called dictionary.txt.
  2. Format the dictionary so that the first line contains all the words beginning with a, the second line contains all the words beginning with b, and so on.
Write a method called getRandomWord() the uses the dictionary from the IsValidWord Exercise to return a random 4 letter word. Display the word to the user.
Write a method called sameLetters which accepts two strings and returns true if they contain the exactly the same letters (case insensitive). Example: tame and mate contain exactly the same letters. meet and temt do not.
Watch Fisher-Yates algorithm. Implement the Fisher-Yates algorithm in a method called shuffle that accepts a string and returns the same string with its letters shuffled.

You will be asked to create a program that utilizes the methods written in earlier programs: sameLetters, isValidWord, getRandomWord, and shuffle.

Resources allowed: Previous exercises you have written for this course. No other resources allowed.

Evaluation (/60)
  • Program Functionality (/24)
  • Test Cases (/16)
  • Commenting and Formatting (/10)
  • Efficiency and Design (/5)
  • Worth 6.7% of FINAL MARK.

    21 Multiple Choice Questions

    Focuses on material and algorithms from this unit
    • Loops Exercises
    • Methods
    • Arrays (1d and 2d)
    • File I/O
    • Nested for loops (stars program)
    • Array manipulation
    • Sum a number of values (Grades Average)
    • Sentinel Value (Grades Average)
    • Passing Arrays to Methods
    • Finding minimum location in an array (Passing Arrays to Methods)
    • All algorithms from Assignment 2

      You will be assigned a partner, and one of the projects below. Enhanced use of dialog boxes to maximize user friendliness will be assessed. (see block below)

    1. Second Hand :

      Create a fun to play computer game out of this old calculator game. Part of the design process is automating anything that can be programmed and making it fun for the user. There must be a 1 player game (against a computer) and a 2 player version. Algorithms, user interface and error checking evaluated. It is expected that some of the JOptionPane Resources below will be used to improve your user interface.

    2. 1001: Create a fun to play computer game out of this old calculator game described below. Part of the design process is automating anything that can be programmed and making it fun for the user. There must be a 1 player game (against a computer) and a 2 player version. Algorithms, user interface and error checking evaluated.
      1001 Game Description

      EQUIPMENT: 1 Player, 1 Calculator, 1 Pair of Dice

      OBJECT: To get to 1001 as fast as possible.

      THE PLAY: Throw the dice and consult the following chart:
      1 = +
      2 = -
      3 = *
      4 = /
      5 = %
      6 = +
      One die will be used to determine the number entry. The other will be used in conjunction with the chart to determine operation entry. For example, if the roll was 3 and 5, you could enter 3 as the number and enter * as the function. Or, you could enter 5 as the number and enter * as the function. You have to get EXACTLY to 1001 in the fewest number of turns. Each roll of the dice means you enter one number and one operation.

      STRATEGY: If the dice allow it, build up slowly so you will have some choice in later rolls. Take your time in deciding which number will be played and which number will determine the function. Try to plan for the best chances for the next roll. If you get it can serve to your advantage as it will get you to a number from which you will easily achieve 1001.

      SAMPLE PLAY
      RollDie RolledChosen NumberChosen OperationCalculation this TurnNew Total with Resulting Expression
      16, 16+66 +
      25, 35*5 + 6 = 1111 *
      31, 44+11 * 4 = 4444 +
      41, 11+44 + 1 = 4545 +
      54, 24-45 + 4 = 4949 -
      61, 44+49 - 4 = 4545 +
      71, 44+45 + 4 = 4949 +
      83, 66*49 + 6 = 5555 *
      96, 36*55 * 6 = 330330 *
      101, 33+330 * 3 = 990990 +
      116, 6 6+990 + 6 = 996996 +
      121, 55+996 + 5 = 1001<==========WIN

    Evaluation (/70)

    Milestone 1 Demonstration (/5) (Tuesday April 29)
    Demonstration in class to teacher:
    1. Display a menu with the options: Instructions, 1 Player, 2 Player, Quit
    2. When one of the menu options is selected, display a new dialog that indicates at least the name of your selection.
    3. When the dialog box from part 2 is closed, it goes back to the menu with options.
    4. Select quit, or clicking cancel on any input dialog box is the only way to end the game
    5. The game never throws exceptions

    Milestone 2 Demonstration (/5) (Friday May 2)
  • The 2 player game is completely working. May have a few bugs, or interface improvements needed.
  • Milestone 3 Demonstration (/5) (Thursday May 8)
  • The 1 player game is completely working. May have a few bugs, or interface improvements needed.

  • Final Project Due Monday May 12
    All error checking works. It rocks. No bugs. No issues. Awesome interface. No bugs. Instructions.
    To hand in
    1. put all the code/images in a folder titled the names of your team
    2. hand into Wear_IT > Hand-in > CompSci 11 > Java Game
    3. print your code and hand it in to the wire inbox on my desk, yes, your code

    Evaluation of Running Game (/40)
    The game will be assessed on the following properties:
    1. Functionality and Completeness (/10)
      • Is the game finished and playable?
      • Are core features and expected game mechanics present and working?
      • Does the game run without crashing?
    2. Design, Creativity, and Interface (/10)
      • Does the game show creative thinking or unique ideas?
      • Have visual elements like colors, fonts, and dialog boxes been thoughtfully improved?
    3. User Experience (/10)
      • Are instructions clear and easy to follow?
      • Is the game intuitive and easy to navigate?
      • Are there helpful prompts, labels, or messages for the user?
      • Is there good error checking to prevent crashes and confusion?
    4. Polish and Overall Quality (/10)
      • Does the game feel complete and well-presented?
      • Does everything work smoothly, with attention to detail?
      • Is the overall presentation polished and engaging?
    Code (/15)
    • commenting/formatting (/10)
    • efficiency/design (/5)

    JOptionPane Resources:

    1. Option Panes
    2. Variations on dialog boxes: JOptionPane. See this JOptionPane Tutorial for more information.
    3. Controlling Font in TextBoxes: Download, examine and run DialogFontControl.java.
    4. Change the entire appearance of dialog boxes with UIManager: UIManager.java.
    5. Combining Graphics and Text Boxes:
    6. How to Add HTML to your Dialog Boxes: Examine and run this code: