Lab Assignment #4: More objects and strings

Due by 11:59PM on Wednesday, Feb 24.


Reading Assignment

Read sections 4.1 to 4.7 of the text Computing Concepts with Java Essentials. This covers material on Java's branching statements: if and if-else.

Pay particular attention to Common Error 4.3 and Common Error 4.4 on pages 156-158.

You can safely ignore the material under the "Advanced Topic" headers. In particular, you are not responsible for the ?: operator discussed on page 145 or the switch statement discussed on pages 149-150.

Please note the time that this assignment is due. Because of the exam, you have two weeks to finish this lab. However, we strongly suggest you start this lab early, especially the first two problems. The third problem will require knowledge of applets which you may not be able to do until we cover applets in class and in an in-lab exercise next week.


In-Lab Work

Note: A second in-lab exercise is now available for practice in the second week of this lab.

There is only one warm-up exercises to be performed in lab this week.

For the in-lab exercise, we are going to start with the same Piglatin example that we worked on in the last class. If you did not finish the in-lab in the last lab, you can ask your AI for a working solution for the pig-latin program.

What were the major things things that were wrong with this piglatin program? Here are a few, at least:

  1. Obviously, the words starting with a vowel were not handled properly.

    Okay, since a lot of people have been asking aobut the piglatin rules, here they are. I have to honestly admit, even I don't know what exactly piglating does, but here is from what I know:

    1. If a word starts with a consonant, move the consonant to the end, and add "ay" to the word.
    2. If a word starts with a vowel, do not move the vowel to the end, instead add the suffix "bay" to the end of the word.
    3. If the sentence ends with a period, the period should stay at the end of the sentence. So ... dance. should become anceday. and not ance.day. However, if the sentence does not end with a period, you should also be able to handle it.
    4. If a word starts with a capital letter (start of a sentence or a name) the resulting word should also start with a capital letter. So, I would become Ibay, John would become Ohnjay, etc.
  2. If there was any word with one letter in it (try this!) such as 'I' or 'a', the program would crash.
  3. If the user did not end his/her sentence with a period, then the last word would get jumbled up.
  4. If there is a name (say, Joe), the appropriate piglatin should be "Oejay" - or so it would seem. But our broken piglatin does not support this.
  5. And there are others - one of them of course, is the fact that we can only handle 4 word sentences... but thats the agenda for next week.

In the lab, we will fix the first three problems. First thing, of course, would be for you to try the above and see for yourself that the above problems exist. Then find out how if statements come to our rescue to fix this problem.

Name this program Piglet.java and submit it by sending an email to a201@cs.indiana.edu with a subject that will be given to you in class.

NOTE: Next week we are going to post another in-lab exercise that will also apply to this assignment in the Assignments page.


Lab Assignment

Please take care to use the exact filenames specified in the problems below. Note that the proper extention on filenames is .java, not .Java.

  1. Fix all the problems in the Piglatin program (except for the 4 word limitation). If you find any other errors, fix them as well, and place comments at the top of your program saying what additional problems you have found and fixed.

    Name this program Smartpiglet.java

  2. Exercise P4.10, Page 174.

    This program can be written rather straight-forwardly as follows:

    if (letterGrade.equals("A+"))
       System.out.println(4.0);
    else if (letterGrade.equals("A"))
       System.out.println(4.0);
    else if (letterGrade.equals("A-"))
       System.out.println(3.7);
    else if (letterGrade.equals("B+"))
       System.out.println(3.3);
    // and so on...
    

    However, that is not very interesting. The purpose of this exercise is to get more practice using variables, and to write some more complicated branching conditions.

    You must use the following algorithm as a model for your code:

    Save your program in a file named Lettergrade.java.

    For the Ambitious This is not part of the basic assignment, so students can certainly earn a grade of B on this lab without doing this part.

    After you have completely implemented the above algorithm, go back and put in some error checking. That is, if the user input is "F+" or "F-" or "I" or "A!" or "A+++", then print a suitable error message, perhaps something of the form:

    Error: A+++ is an illegal grade.

    Notice that the user input is echoed in the error message exactly as it was entered. If the user enters a grade of "VeryGood", you should print a message like this:

    Error: VeryGood is an illegal grade.

    not like this:

    Error: verygood is an illegal grade.

    Don't bother implementing this error checking unless your basic program is working perfectly!

  3. Note that this is an applet program, and we are not going to cover applets until after the midterm. You can start working on this part if you are familiar and comfortable with applets. But you can also wait until we have covered applets in class and labs.

    Write a graphics applet that asks the user to specify two circles. Each circle is input by clicking on the center and then clicking on a point on the circle. Draw the two circles filled with the same color. After the center point of each circle is given by the user, draw the point. Now, if the two circles intersect, display the message "Circles Intersect" (in some other color) at the center of the canvas. Otherwise, display "Circles Don't Intersect".

    Hints:

    Save your program in a file named Intersect.java.

    You can look at a working demonstration of this program.


Submitting Your Lab Assignment

For this lab, you should submit three files:

Do not submit .prj, .class or .html files. Only the files named above should be submitted.

In order to submit files, click on the following link to send mail to:

a201@cs.indiana.edu

with the following Subject:

lab4

and then Attach your program files to the e-mail message. You may attach multiple files in the same message, but you don't have to. You can submit your programs in separate e-mails if you wish. Do not type anything in the body of the message! You must use the filenames specified in this assignment and you must attach the files to the mail message.