Experiments in Java


Experiments for Session J4: Conditionals

Name: ________________
ID:_______________

Experiment J4.1: Extending the SimpleDate class

Step 1. Make a list of at least three capabilities you might want to include in the SimpleDate class. You might consider changing dates, getting other types of information about dates, or working with multiple dates.


 
 
 
 
 
 

Step 2. Express each of the capabilities described in Step 1 as a Java method. For each method, you need only provide type, name, and parameters. You need not provide any code in the bodies of the methods (in fact, it is the purpose of this lab to help you develop these bodies).


 
 
 
 
 
 

Experiment J4.2: Simple boolean methods

Required files:

Step 1. Make copies of SimpleDate.java and DateTester.java. Compile both files and execute DateTester. Record the results.


 
 
 
 
 
 

Step 2. Add the inJanuary method to the SimpleDate class. Here is the code for that method.

  /** 
   * Determine whether the current date falls in January.
   */
  public boolean inJanuary() {
    return this.month == 1;
  } // inJanuary()

Update DateTester to print out whether each day is in January with

    out.println("Is " + day_one.toString() + " in January? " +
                day_one.inJanuary());

Recompile both files and execute DateTester. Record the new results.


 
 
 
 
 
 

Step 3. Add an inAugust method to the SimpleDate class. You will need to write this method yourself. Update DateTester to print out whether each day is in August. Enter the code for your new method here.


 
 
 
 
 
 

Step 4. Add an inFirstHalf method to the SimpleDate class. This method should test whether a date falls in the first half of the year (in any month through June). You can assume that the month field contains a number between 1 and 12. Recompile and test your method until you are confident that it works. Enter the method, the test code, and the output here.


 
 
 
 
 
 
 
 
 
 
 
 
 
 

Experiment J4.3: More boolean methods

Step 1. Add the isFirstDay method to the SimpleDate class. Update DateTester to use that method. Add a few more dates to DateTester to make sure that your testing is more comprehensive. Recompile the files and execute DateTester. Record the results here.


 
 
 
 
 
 

Step 2. Add the isWinter method to the SimpleDate class. Update DateTester to test that method (perhaps adding more dates if appropriate). Recompile the files and execute DateTester. Record the results here.


 
 
 
 
 
 

Step 3. Update the isWinter method so that it correctly (rather than approximately) answers the question Is it winter? In particular, your method should only claim that days on or after the winter solstice but before the vernal equinox are in winter. Record your code for isWinter here.


 
 
 
 
 
 
 
 
 
 
 
 
 
 

Step 4. Run DateTester. Note that you should not need to make any changes to DateTester. Record your results here.


 
 
 
 
 
 

Step 5. Finish the equals method begun below. This method should return true if the parameter is equal to the current date and false otherwise.

  /**
   * Determine whether the parameter is the same as this date.
   */
  public boolean equals(SimpleDate d) {
    
    
    
  } // equals(SimpleDate)

Experiment J4.4: Improved output

Step 1. Update DateTester so that for each sample date it prints

Brr ... the date falls in winter.

for dates that fall in winter and

Fortunately, the date does not fall in winter.

for those dates that do not fall in winter. Summarize your code here.


 
 
 
 
 
 

Step 2. Update DateTester so that for each sample date it prints

It seems that the date falls in the first half of the year.

for dates that do just that. It should not print anything for other dates. Summarize your code here.


 
 
 
 
 
 

Experiment J4.5: More fun with conditionals

Step 1. Add the precedes method to the SimpleDate class. Update DateTester to check at least three interesting pairs of dates and print appropriate messages concerning their relative appearance in the year. Record your output here.


 
 
 
 
 
 

Step 2. Write a daysInMonth method that returns the number of days in the current month. For now, your method need only work for dates in January, February, and March (return some default value for every other date). Summarize your code here.


 
 
 
 
 
 
 
 
 
 
 
 
 
 

Step 3.

Update DateTester to test the daysInMonth method. Summarize the test and enter the output here.


 
 
 

Step 4.

Write a new constructor,

  public SimpleDate(int y, String m, int d)

that allows one to create a new date by using an appropriate string for the month (e.g., March). Note that to compare two strings, you need to use the equals method, as in

    if (s1.equals(s2)) {
      out.println("'" + s1 + "' is the same as '" + s2 + "'");
    }

Your constructor need only work with the months January, February, and March, although it should support both the full names of the months and the abbreviated versions. Summarize your code here.


 
 
 
 
 
 
 
 
 
 
 
 
 
 

Experiment J4.6: Using the switch statement

Step 1. Using a switch statement, write a daysInMonth method that returns the number of days in the current month. Summarize the code for the method here.


 
 
 
 
 
 
 
 
 
 
 
 
 
 

Step 2. Update DateTester to test the daysInMonth method. Enter the output here.


 
 
 
 
 
 

Step 3. Using your daysInMonth method from Step 1 above, write a daysLeftInMonth method. Update DateTester to test this new method. Enter the output here.


 
 
 
 
 
 

Step 4. Using a switch statement, write a new constructor,

  public SimpleDate(int y, String m, int d)

that allows one to create a new date by using an appropriate string for the month (e.g., March). Test your constructor. Does it seem to do what you expect? Why or why not?


 
 
 
 
 
 

When you have answered this question, you may wish to read the notes on this step.

Experiment J4.7: Applet parameters

Required files:

Step 1. Make copies of ConfigurableGreeting.java and greeting.html. Consider the code for ConfigurableGreeting.java. Explain, in your own words, what each part of the paint method does.


 
 
 
 
 
 

Step 2. Look at greeting.html. What message do you expect this page will display?


 
 
 

Step 3. Compile ConfigurableGreeting and load the applet with greeting.html. Describe the output.


 
 
 

Step 4. Change the line in greeting.html that reads

    <param name="message" value="A Configurable Applet">

to instead read

    <param name="message" value="Reconfigured">

What do you expect to happen if you load the applet using the modified greeting.html? Note that you have not recompiled ConfigurableGreeting.


 
 
 

Load the applet using greeting.html to confirm your answer.

Step 5. Change the line in greeting.html that reads

    <param name="message" value="Reconfigured">

to read

    <param name="MESSage" value="Reconfigured">

That is, change the capitalization of message. What effect do you expect this change to have?


 
 
 

Step 6. Load the applet using greeting.html to confirm your answer. You may also wish to consult the notes on this step.

Change the line in ConfigurableGreeting.java that reads

    message = getParameter("message");

to instead read

    message = getParameter("MESSAGE");

That is, change the capitalization of message. What effect do you expect this change to have?

Recompile ConfigurableGreeting and then load the applet using greeting.html to confirm your answer. You may also wish to consult the notes on this step.


 
 
 

Step 7. Change the line in greeting.html that reads

    <param name="MESSage" value="Reconfigured">

to read

    <param name="greeting" value="Reconfigured">

Change the line in ConfigurableGreeting.java that reads

    message = getParameter("MESSAGE");

to read

    message = getParameter("greeting");

What effect do you expect these changes to have?


 
 
 

Recompile ConfigurableGreeting and then load the applet using greeting.html to confirm your answer. You may also wish to consult the notes on this step.

Step 8. Change the line in greeting.html that reads

    <param name="greeting" value="Reconfigured">

to again read

    <param name="message" value="Reconfigured">

Do not change ReconfigurableGreeting. What effect do you expect the change to greeting.html to have?


 
 
 

Load the applet using greeting.html to confirm your answer. You may also wish to consult the notes on this step.

Step 9. As you may have observed, the difficulty here is that the HTML file no longer contains a greeting parameter. Good programming style dictates that our applet have a default message. Change the line in ReconfigurableGreeting that reads

    message = getParameter("greeting");

to read

    message = getParameter("greeting");
    if (message == null) {
      message = "No message specified.";
    }

What effect do you expect this change to have?


 
 
 

Recompile ConfigurableGreeting and then load the applet using greeting.html to confirm your answer.

Step 10. What changes would we need to make to ConfigurableGreeting in order to support a font parameter? Do not make the changes, just summarize them.


 
 
 
 
 
 

Step 11. Add a line to greeting.html that reads

    <param name="font" value="SansSerif">

What effect do you expect this line to have?


 
 
 

Step 12. Add the following line to the paint method of ConfigurableGreeting.

    fontName = getParameter("font");

This should follow the steps used to get the message and precede the call to setFont. What effect do you expect this change to have?


 
 
 

Confirm your answer by recompiling ConfigurableGreeting and then loading the applet with the modified greeting.html.

Step 13. Replace the line in greeting.html that reads

    <param name="font" value="SansSerif">

with

    <param name="font" value="Monospaced">

What effect do you expect this change to have?


 
 
 

Step 14. Replace the line in greeting.html that reads

    <param name="font" value="Monospaced">

with

    <param name="font" value="NoSuchFont">

What effect do you expect this change to have?


 
 
 

Step 15. For a robust applet, the one line added in step 12 is not enough. Why not? What other lines should we also include?


 
 
 
 
 
 

Experiment J4.8: More applet parameters

Required files:

Before you begin, if you have not already done so, make copies of ConfigurableGreeting.java and greeting.html. Make sure that you understand the code in ConfigurableGreeting.java.

Step 1. Add the following lines to greeting.html within the applet tags.

    <param name="size" value="24">
    <param name="style" value="bold">
    <param name="color" value="white">
    <param name="shadow" value="black">

What effect do you expect these changes to have?


 
 
 

After recording your answer, you may wish to consult the notes on this step.

Step 2. Add the following lines to the paint method of ConfigurableGreeting. They should fall after the other calls to getParameter, but before the calls to setFont and setColor.

    String colorName = getParameter("color");
    if ("black".equals(colorName)) { fontColor = Color.black; }
    else if ("blue".equals(colorName)) { fontColor = Color.blue; }
    else if ("green".equals(colorName)) { fontColor = Color.green; }
    else if ("purple".equals(colorName)) { fontColor = new Color(255,0,255); }
    else if ("red".equals(colorName)) { fontColor = Color.red; }
    else if ("white".equals(colorName)) { fontColor = Color.white; }
    else if ("yellow".equals(colorName)) { fontColor = Color.yellow; }

What effect do you expect these new lines to have?


 
 
 

Confirm your answer by recompiling ConfigurableGreeting and loading the applet from greeting.html.

Step 3. Replace the line in greeting.html that reads

    <param name="color" value="white">

with one that reads

    <param name="color" value="yellow">

Do not recompile ConfigurableGreeting. What effect do you expect this change to have?


 
 
 

Confirm your answer by loading the applet from greeting.html.

Step 4. Replace the line in greeting.html that reads

    <param name="color" value="yellow">

with one that reads

    <param name="color" value="WHITE">

Do not recompile ConfigurableGreeting. Load the applet from greeting.html. What color does the text appear in? Why?


 
 
 

After recording your answer, you may wish to consult the notes on this step.

Step 5. Update ConfigurableGreeting so that it also reads the color of the shadow from the HTML file. Summarize your changes here.


 
 
 
 
 
 

Step 6. Update ConfigurableGreeting so that it also reads the font style from the HTML file. Summarize your changes here.


 
 
 
 
 
 

Step 7. Add the following lines to the paint method of ConfigurableGreeting. They should fall after the other calls to getParameter, but before the calls to setFont and setColor.

    String sizeParam = getParameter("Size");
    try {
      fontSize = Integer.parseInt(sizeParam);
    }
    catch (Exception e) {
      fontSize = 12;
    }

Recompile ConfigurableGreeting and load the applet from greeting.html. What effect did this new code have?


 
 
 

Step 8. Replace the lines added in step 7 with the following two lines.

    String sizeParam = getParameter("size");
    fontSize = Integer.parseInt(sizeParam);

Recompile ConfigurableGreeting and load the applet from greeting.html. What effect did this new code have?


 
 
 

Step 9. It seems that it's just as easy to use the shorter code from step 8 rather than the longer code from step 7. However, this isn't always so. Try replacing the line in greeting.html that sets the font size with

    <param name="size" value="twenty">

Load the applet from greeting.html. What happens?


 
 
 

Step 10. Replace the two lines inserted in step 8 with the longer code from step 8. Recompile ConfigurableGreeting. Load the applet from greeting.html. What happens? What does that suggest about the try/catch clause?


 
 
 

After recording your answer, you may wish to consult the notes on this step.

Step 11. Replace the line in greeting.html that reads

    <param name="size" value="twenty">

with one that reads

    <param name="size" value="32">

What effect does this have?


 
 
 


Copyright (c) 2000 Addison Wesley Longman. All rights reserved.

Source text last modified Mon Mar 31 08:36:33 2003.

This page generated on Mon Mar 31 08:36:44 2003 by Siteweaver.

Contact our webmaster at rebelsky@math.grin.edu