mdinfotech.net  



Java

This is Java.

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

Resources



  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.
  1. Watch this video on the For Loop
  2. Read this Going Loopy Tutorial.
  3. Read this For Loop in Java.
  4. Complete these Loops Exercises **on paper**.
  5. 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.
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)
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, it is 'pass reference 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. Extra help: Pass-by-Value, Passing Arrays 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: Change One Letter.

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.
Chars are a type of integer. Chars work with integer addition and comparisons.
          char initial = 'k';
          int numValue = (int)initial;
          System.out.println(initial + " has numerical value " + numValue);
          if (initial < 'a' || initial > 'z') {
              System.out.println("initial is not a letter");
          }
          
Video Lessons
  • Intro to Change One Letter
  • How to Change One Letter, and answers to other questions
  • Writing the Dictionary

  • Assignment Description

    The "Change One Letter" game has a 4 letter start word and a 4 letter goal word. The challenge is to get from the start word to the goal word by changing one letter per turn.

    Sample Play
    Start Word: wall
    Goal Word: boil

    wall
    mall
    mail
    hail
    tail
    toil
    boil
     

    You are to write a program that allows two people to play the game until there is either a winner OR someone clicks cancel.

    Each play of the game will have the following format:

    1. Player 1 enters a 4 letter start word into a Input Dialog Box, which must be validated against an English dictionary file then displayed in the console.
    2. Player 2 enters a 4 letter goal word into an Input Dialog Box, which must be validated against an English dictionary file.
    3. Player 1 changes a letter using an Input Dialog Box, it is validated, the new word is displayed in the console.
    4. Player 2 changes a letter, it is validated using an Input Dialog Box, it is validated, the new word is displayed in the console.
    5. When the goal word is reached, display it in the console. Then display in the console the winner and the number of steps it took. Then the game ends.

    To change a letter, the user must specify the position of one letter to change (0 for the first letter, 1 for the second letter etc), followed by a space, followed by a uppercase OR lowercase letter to change it to in order to make a new word. If the format of this input is not valid, display a descriptive error message and require the user to enter it again.

    Example

    Start Word: care
    New Word: case
    The user would enter 2 s to change the r to s.

    The format to change the letter is considered invalid if:

    1. The number is not an integer between 0 and 3
    2. The letter is not a character between a and z
    3. There is not a space between the number and letter
    4. There is more than one space between the number and letter
    5. The new word created by the changed letter is not in the English dictionary
    Sample InputOutput
    wAlL
    boil
    0 m
    2 i
    0 H
    0 t
    1 o
    0 B
    
    wall
    mall
    mail
    hail
    tail
    toil
    boil
    Player 2 wins in 6 steps.
    


    Things You Need to Know
    1. Plan the program first: flow chart it, outline the methods you will need.
    2. Program one step at a time, test it, then move on to the next step.
    3. Implement the whole game first without the dictionary.
    4. Since you cannot change the individual characters of a String, but you can change the individual elements of an array, you will need to convert a String to a character array and convert the character array back to a String.
      1. To convert a String to a character array:
        String someString = "this is a string";
        char  someCharacterArray;
        
        someCharacterArray = someString.toCharArray();
                            
      2. To convert a character array to a String:
        someString = new String (someCharacterArray);
                        
    5. Once the game works without the dictionary, put this four letter list in a file called dictionary.txt. Use this to determine if a word is a valid 4 letter word.

    Evaluation (/51)

  • 2 Working Test Cases (/20)
  • 8 Non-working Test Cases (/16)
  • Commenting and Formatting (/10)
  • Efficiency and Design (/5)
  • To hand in:

    Submit on Google Classroom.
    You and a partner will be assigned one of the following projects:

    Level 1: Change One Letter - dialog box version
    Level 2: Change One Letter - graphical version
    Criteria for Both Levels
    1. Write clear, formatted, user friendly instructions.
    2. Create a 2 human player version AND a 1 human, 1 computer version.
    3. The most basic computer version just picks random words that meet the game criteria. An improved computer player will make use of various strategies to improve its chance of winning. **include a 5th line in the program comments titled "Description of Computer Player:"
    4. The user enters the next word, instead of the index and character. The program will check that the new word is only a 1 letter change from the current word, and that it is a valid English word.
    5. All other criteria is carried forward from Assignment 2 - Change 1 Letter Game.
    How To Hand In
    1. hand in your Eclipse project to Wear_IT > Hand-in > CompSci 11 > Java Game
    2. rename it to contain both people's names
    3. do not print your code this time
    Milestone 1 Demonstration Tuesday Oct 27
    Demonstration in class to teacher: Working game with human or computer players. Can win or lose.

    Milestone 2 Demonstration (Thursday Oct 29)
    Demonstration in class to teacher: Working game with human AND computer players. Can win or lose. Winner is identified.

    Final Project Due Friday Oct 30
    All error checking works. It rocks. No bugs. No issues. Awesome graphics and interface. No bugs. Instructions.

    Assessment (/75):
    UX Design (User Experience)				
    UX shows creativity and is astheically pleasing. All instructions are clear and formatted in an easy to follow manner.  
    Error checking is descriptive and clear.  User experiences no confusion during game interaction.  Creativity of design.				
    	10	              8	                  4	            2
    	Meets Standard	  Approaches Standard	Below Standard	   Incomplete
    	All criteria met. Most criteria met.	Some criteria met. Criteria not met.
      
    Completeness, Robustness, Reliability				
    All game criteria met. Users enter the next word instead of the index and letter. Game never throws exceptions. 
    Robustness is the ability of a computer system to cope with errors during execution and cope with erroneous input.  
    Reliability is the probability of failure-free operation during execution.				
    	30	              25	                20	                10
    	Meets Standard	    Approaches Standard	    Below Standard	    Incomplete
    	All criteria met.	Most criteria met.	Some criteria met.	Criteria not met.
      
    Code Commenting & Formatting				
    As outlined at http://mdinfotech.net/?course=compsci11&unit=2#id44				
    	10	                8	                    6	                  1
    	Meets Standard	  Approaches Standard	  Below Standard	    Incomplete
    	All criteria met.	1-2 errors	  3-4 or more errors	Many criteria not met.
      
    Code Efficiency and Design				
    Modular design that is readable and reusable. Algorithms written are as efficient as possible with level of coding taught.  
    Code is not unnecessarily complicated. No unnecessary variables, statements, iteration, or comparisons.  
    Dictionary is loaded read from hard drive only once. Dictionary iterated through only after other significant error checking passed.				
    	5	                4	                  3	                  1
    	Meets Standard	  Approaches Standard	    Below Standard	    Incomplete
    	All criteria met.	Most criteria met.	Some criteria met.	Criteria not met.
      
    Milestones				
    All milestones met completely and on time.				
    	20	              15	                10	                5
    	Meets Standard	  Approaches Standard	    Below Standard	    Incomplete
    	All criteria met.	Most criteria met.	Some criteria met.	Criteria not met.
    
    Worth 10% of FINAL MARK.

    39 Multiple Choice Questions

    Focuses on material and algorithms from this unit
    • Loops Exercises
    • Methods
    • Arrays (1d and 2d)
    • File I/O
    • Pass-by-Value vs Pass-by-Reference -Value
    • Nested for loops (stars program)
    • Array manipulation
    • Sum a number of values (Grades Average)
    • Sentinel Value (Grades Average)
    • Chars and the Ascii Table (Change 1 Letter)
    • Character Arrays (Change 1 Letter)
    Music.java plays musical scales.
    Examine and run this code: