110CT: Lecture 9 Portfolio Questions 2006-7

 

  • 9.1:

    Write a declaration for an array variable news which could be used to refer to an array of newsItem objects (based loosely on exercise 4.40 from Barnes and Kolling 3rd edition)

  • 9.2:

    What is wrong with the following array declarations? Correct them.

      String[10] names;
      []double values;
      Object[20] taken = new Object[20];
    

    (based loosely on exercise 4.43 from Barnes and Kolling 3rd edition)

  • 9.3:

    Given the following declarations:

      String[] names;
      double[] values;
      Object[] taken;
    

    write assignment statements which do the following:- make names refer to an array able to hold 60 String values; make values refer to an array able to hold 10 double real numbers and taken refer to an array able to hold 10 objects of class Object. (based loosely on exercise 4.44 from Barnes and Kolling 3rd edition)

  • 9.4:

    Write a for loop which prints the values 1, 4, 7, 10 .. through to 40. You are advised to use the CodePad to test your code works. (not from Barnes and Kolling)

  • 9.5:

    This question has 3 stages. Submit just the final version. You are advised to use the CodePad to test your code works at each stage.

    1. Write a for loop which prints the value 1 on each of 10 separate lines. (very easy)

    2. Modify your code to include a second inner for loop. Together your code should produce the 10 separate lines as shown below:

      1
      1 2
      1 2 3
      1 2 3 4 
      1 2 3 4 5 
      1 2 3 4 5 6 
      1 2 3 4 5 6 7 
      1 2 3 4 5 6 7 8 
      1 2 3 4 5 6 7 8 9 
      1 2 3 4 5 6 7 8 9 10
      
      
    3. Modify your code so that your code produces the 10 lines as shown below:

      *
      * *
      * * * 
      * * * * 
      * * * * * 
      * * * * * * 
      * * * * * * * 
      * * * * * * * * 
      * * * * * * * * * 
      * * * * * * * * * *
      

    ... so your final version should use nested loops to produce the required pattern

    (not from Barnes and Kolling)

  • 9.6: new project 'rainfall-analyzer'

    • Create a new project in BlueJ. Save it as 'rainfall-analyzer'.
    • Create a new class called RainfallAnalyzer. This is provided by BlueJ with sample methods and fields. Delete any you don't require.
    • This class should have single field, an array which can hold 12 monthly rainfall integer values.
    • The RainfallAnalyzer constructor does not need any contents at this stage.

    (not from Barnes and Kolling)

  • 9.7: project 'rainfall-analyzer'

    Write a method setRainfallValues which puts values into the rainfall array. (Ok, not very realistic. This will be changed in portfolio 10.)

    Include a call to this method in the constructor to ensure a new RainfallAnalyzer object contains rainfall data.

    Write a method printRainfall which prints out all elements from the array, in one line. (Ensure you do this efficiently, using a loop.)

    (not from Barnes and Kolling)

  • 9.8: project 'rainfall-analyzer'

    Write a method getWettestMonth which returns the wettest month. This should check through the rainfall array to find the element with the biggest value and return the index value of the wettest month. Use a for loop.

    Which month is returned if more than one month was equally wet? Why?

    In your portfolio, provide answers to the questions in addition to providing your code.

    (not from Barnes and Kolling; similar to 4.54)

  • 9.9: project 'rainfall-analyzer'

    Write a method printWettestMonth which calls getWettestMonth to identify the wettest month. It should then printout the name of the wettest month (use 'switch') and its rainfall level.

    (not from Barnes and Kolling)

  • 9.10: project 'rainfall-analyzer'

    Write a method getDriestMonth which returns the index value of the driest month. Use a while loop.

    Be sure this works for all combinations of rainfall values: all zero; all the same; the driest first; the driest last. Are there any situations in which your code will not work?

    In your portfolio, as well as your code, provide evidence of your testing and answers to the questions.

    (not from Barnes and Kolling)

  • 9.11: project 'rainfall-analyzer'

    Write a method getWettestTwoMonths. This should identify the pair of adjacent months with the wettest total rainfall. It should returns the index value of the first of the pair of months. (You don't need to concern yourself about Dec-Jan periods.)

    (not from Barnes and Kolling; similar to 4.57)

  • 9.12: project 'rainfall-analyzer'

    Add a second class RunAnalyzer to your project. Cut 'n paste the code for this from below.

    /**
     * Write a description of class RainfallAnalyzer here.
     * 
     * @author Lisa Payne 
     * @version Dec 06; for portfolio 9.12
     */
    public class RunAnalyzer
    {
      private RainfallAnalyzer analyzer;
    
      /**
       * Constructor for objects of class RunAnalyzer
       */
      public RunAnalyzer()
      {
          // create analyzer including setting values
        analyzer = new RainfallAnalyzer(); 
      }
    
      /**
       * Runs RainfallAnalyzer methods to test them
       */
      public void run()
      {
        System.out.println("Test 'printRainfall' ");
        System.out.print("    ");	     
        System.out.print("Rainfall data: ");
        analyzer.printRainfall();
             
        System.out.println("Test 'getWettestMonth' ");
        System.out.print("    ");	     
        System.out.println("Wettest month: " + analyzer.getWettestMonth());
             
        System.out.println("Test 'printWettestMonth' ");
        System.out.print("    ");	     
        analyzer.printWettestMonth();
             
        System.out.println("Test 'getDriestMonth' ");
        System.out.print("    ");	     
        System.out.println("Driest month: " + analyzer.getDriestMonth());
             
      /*
       *  include code here to demonstrate that 'getWettestTwoMonths' works
       */
    
      }
    }
    
    

    Complete the run method so that it also tests the analyzer's method getWettestTwoMonths

    (not from Barnes and Kolling)

  • 9.13: project 'rainfall-analyzer'

    Use your new class to test the operation of your 'rainfall-analyzer' methods running appropriate sets of rainfall data. (You'll need to change the data in your code and recompile each time.)

    In your portfolio submit the data used and the results output

    (not from Barnes and Kolling)

 

Index | Previous | Next

 

Last modified: 5 Jan 2007
Webpage: © Lisa Payne, Coventry University, 2006-7
Some exercises and projects: © Barnes and Kolling