18 March 2012

NetBeans 7 new features (edit)

NetBeans 7 is approved for release now. Here's a list of new features
  • Remote glassfish interaction
  • Java 7 support
    • Maven 3 support
    • HTML 5 support
    • JSON formatter
    • Git natively integrated
    • JUnit is now an unbundled plugin (Oracle legal had problems with the old CPL license)
    • easy JPA 2 metamodel generation
    The netbeans 7/Java 7/Glassfish 3.1 combination is still a bit flaky though.

        15 March 2012

        Java How To Program: alternate labs

        Chapter 2

        Preparation

        1. Make sure your PATH environment variable contains the JDK bin directory
          1. On windows separate PATH entries using ; (semicolon)
        Blogger Rijkswatch - Edit post - Mozilla Firefox_2012-03-14_21-40-17 - cropped

        Body Mass Index Calculator

        1. Using an editor create a file called BmiTest.java
        2. Add a public class called BmiTest to the file
        3. Add a main method to the BmiTest class. In the main method
          1. Ask the user for his weight in grams
          2. Ask the user for his height in centimeters
          3. Print the Body Mass Index of the user, using the formula
            BMI = weight x 10 / (height x height)
        4. Save the file
        5. Compile the file using the javac compiler
        6. Run the file using the java command
        7. Correct any errors.
        8. Compile, run and correct errors until all is well.

        Chapter 3

        Body Mass Index Calculator

        1. Create a new Java Application project called javase
          1. Consult the tutorial on creating a NetBeans Java project
            1. Do not create a main class
        2. In the project browser window, right click your project and select New => Java Class
          1. Call the class Bmi
        3. Add private double fields to the class called weight and height
        4. Add a constructor to the class
          1. constructor signature: public Bmi (double myWeight, double myHeight)
          2. In the constructor initialise the object fields using the constructor parameters
        5. Add a method that calculates the BMI
          1. Signature: public double calculate()
          2. In the method return the calculated bodymass. Adapt the calculation to use weight in kilogram and height in meters.
        6. Copy the BmiTest class to the project
        7. Modify the class to
          1. Request weight in kilogram and height in meters
          2. Accept floating point numbers
          3. Replace the calculation with
            1. Creation of a Bmi object called bmi
            2. Calculate the body mass index using the calculate() method on the  bmi object
        8. In the main method, print out the result of the calculation
        9. Run the main method by clicking on the green arrow button in the top toolbar
        10. Correct any errors and run again until all is well.

        Chapter 4

        Body Mass Index Calculator

        A normal BMI is between 18.5 and 24.9.
        1. In the BMI class add a method with signature: public double deviation()
          1. The method should return 0.0 if the BMI is within the normal range
          2. The method should return a negative number indicating the difference with 18.5 if the BMI is lower
          3. The method should return a positive number indicating the difference with 24.9 if the BMI is higher
        2. In the BmiTest main class
          1. print a message indicating that the weight is normal if the difference with the bounds is less than 0.1
          2. otherwise print a message indicating how much the weight should change to be within the normal range
        3. create a main class

        Chapter 5

        Facebook Growth Prediction

        In July 2010 facebook had 500 million users and was growing with a rate of 5% each month.
        Assume the  growth continues at this pace.
        Write a class called FacebookFuture with a main method that lists the number of users for each of the following months, until the number of users exceeds one billion. For each month print out a line with
        • an incrementing number indicating how many months this is from the start date
        • the month and year in a mm/yyyy format
        • the number of users
        Align numbers vertically in each line of output.
        Tip: since Java 7 you may use underscores in numbers to enhance readability. Example:
        long billion=1_000_000_000;
        Info: At the beginning of 2012 facebook numbered 850.000 users

        Chapter 6

        Facebook Growth Prediction

        To calculate the target amount you have after a number of months the formula is
        target = (1 + rate)months X amount
        To calculate the number of months it takes to the target amount, the formula is
        log(target/amount)/log(1+rate)=months
        1. Create a class GrowthPrediction. 
          1. Add an attribute for the current amount (double)
          2. Add a constructor that initializes the amount.
          3. Add a method with signature: public int growthCycles(double target, double rate).
        2. Create a test class called GrowthPredictionTest
          1. Right click the GrowthPrediction class and select Tools => Create JUnit Tests
            1. Accept all defaults
            2. Use JUnit 4
          2. In the testGrowthCycles method add a test that verifies the numbers you obtained in the previous excercise.
            1. Put the number of months obtained in the previous exercise in the expResult variable
            2. Remove the fail statement and the comment above it
            3. Print out a message saying how many months it would take for the number of facebook users to reach one billion
          3. Run the test using the top menu Run => test
          4. Using the same conditions add code to the test method to calculate how many months it would take for facebook to have as many users as a world population of 7_000_000_000
            1. Print out the result

        Chapter 7

        Game of Craps

        1. Make these modifications to the Craps code (Fig 6.9):
          1. Comment out all printing in the Craps class
          2. Make the number of rolls and the result attributes instead of local variables
          3. Add methods
            1. public int getRolls();
            2. public boolean getResult();
        2. Make these modifications to the CrapsTest class.
          1. Run the games for a number of times passed as a command like argument 
            • Run CrapsTest from the command line
            • If you run in netbeans, in the project pane, right click the project => properties => run and set the command line arguments
          2. Print out how many games were won and lost on the first, second,… , twentieth roll and after the twentieth roll.
          3. Print out the percentage of games won
          4. Print out the average number of rolls in a game

        Chapter 8

        Bank

        Implement the Bank case study.
        1. Put classes in two packages
          1. jhtp.bank
          2. jhtp.bank.atm
        2. Start with the classes at the right bottom of Fig 8.24 and work up
        3. Return dummy 0 equivalents from the methods to comply with the method return types
        4. Add attributes for the relations in Fig 8.25

        Cards

        1. Add two new enum classes to example 7.09:
          1. Face
          2. Suit
        2. Modify the example to use the enums
        3. Replace the decksize (52) with a number calculated from the available enums
        4. Run the test program and correct errors until it succeeds

        Chapter 9

        Bank

        1. Make the SavingsAccount from exercise 8.6 a subclass of the Account from the ATM case study.
        2. Replace the usage of the savingsBalance with getter/setter methods using the totalBalance of the Account class.
          1. Add the necessary support to the Account class
        3. Remove the savingsBalance attribute
        4. Test the modified savingsBalance using the SavingsAccount test and correct errors until it succeeds

        Javadoc

        1. Comment the bank classes, methods and attributes with javadoc
        2. In the project browser richt click on the project and select Generate javadoc
        3. Review the generated javadoc, adapt and regenerate
        4. Go to the files prowser tab and open the dist/javadoc directory where the javadoc is generated. Open the file stylesheet.css and append the contents of this file to it and save it. Reload the javadoc in another browser tab and check if anything changed in the presentation.

        Chapter 10

        Bank

        1. Add The Transaction class to the Bank exercise

        CommissionEmployee

        1. Start from the BasePlusCommissionEmployee code in example fig09_12_14
        2. BasePlusCommissionEmployee inherits from CommissionEmployee. We will replace inheritance with composition.
        3. Rename CommissionEmployee to CommissionOnlyEmployee
        4. Create an interface CommissionEmployee that contains the public methods of CommissionOnlyEmployee (except toString())
          1. In CommissionOnlyEmployee choose Refactor => Extract Interface
        5. Adapt BasePlusCommissionEmployee  to delegate to CommisionOnlyEmployee instead of inheriting from it
          1. Replace inheritance in BasePlusCommissionEmployee  with inplementation of the CommissionEmployee interface.
          2. Add an attribute CommissionOnlyEmployee called delegate to BasePlusCommissionEmployee 
          3. Instantiate the attribute in the constructor
          4. Replace all calls to super with calls to delegate
          5. implement all interface methods and call the corresponding method on delegate from them.

        Chapter 13

        Game Of Craps

        Add exception handling to handle bad program parameters to the CrapsTest class in the Game Of Craps from Chapter 7.
        Handle two specific exceptions using one catch statement:
        1. no command line parameter was supplied
        2. the command line parameter was not an integer

        Chapter 19

        FileMatch 1: Collections

        1. Rewrite exercise 14.8, this time loading all accounts in a Collection in memory.
        2. Make sure the output is still ordered by account number.

        FileMatch 2: ResourceBundles

        Localise the messages in the application using resource bundles.
        1. Pass a parameter to FileMatchTest indicating the language (fr or nl)
        2. In the main method construct a ResourceBundle with parameters
          1. “errormsg” as name for the properties files
          2. If the language was passed,  a Locale for this language
            1. if no language was passed, use the one parameter constructor
        3. Pass the ResourceBundle to the FileMatch constructor and save it in an attribute
          1. Print errormessages using the ResourceBundle
          2. Use MessageFormat to substitute parameters in the message
        4. In the src directory create
          • errormsg.properties (english)
          • errormsg_fr.properties  (french)
          • errormsg_nl.properties (dutch)
        5. Add the keys used in the FileMatch error messages to all files and add translated messages in the three languages

        Chapter 23

        FileMatch

        In this exercise we will process multiple transaction files concurrently.
        1. Create a class bank.TransactionProcessor that implements Runnable
          1. Add a constructor TransactionProcessor(Map<Integer, Account> accounts, Path transactionFile)
            1. store the parameters in attributes.
          2. Implement the run method of TransactionProcessor. Move the code from FileMatch that reads the transaction file and adds the transaction amount to the corresponding accounts here. Adapt the code to use the instance attributes.
        2. Make about 5 copies of the trans.txt file.
          1. Give them similar names so you can retrieve them using a wildcard.
        3. Adapt the FileMatch class
          1. Make the Account Collection synchronised to allow concurrent access.
          2. Read the oldmast.txt file and store all accounts in the Account Collection
          3. Create an ExecutorService cached trheadpool
          4. For each transaction file
            1. create a TransactionProcessor thread. Pass the Account collection and the file Path to it.
            2. execute the thread
          5. shutdown the threadpool and awaitTermination of all threads
          6. Write the updated Account Collection to the newmast.txt file.
        4. Add some System.out.println statements throughout your code to  track the processing of the files
        5. Run the program and verify the results.

        Chapter 30

        FileMatch

        Write a program that replaces the blank character separators in oldmast.txt with a colon (:) separator. A sequence of blank characters should be replaces with only one colon separator.

        13 March 2012

        QR code shopping

        Uniway adds QR codes to TV shopping program.
        You can consult product info on your smartphone and get called by the call center to take your order.