Chapter 5

Test Your Thinking

If Java is your first computer language, then this chapter might have been more difficult for you because it has less to do with Java and more to do with programming concepts in general.  The heart of any computer program, regardless of the computer language, is the boolean logic controlling the flow control.  Computers are very fast and very diligent, but they are also very stupid and have absolutely no common sense.  Computers will do whatever you tell them to do, exactly, and to a fault.  As a programmer, you need to be able to give very exact and specific instructions to the computer because it doesn't know any better.  Good flow control is the key to getting the computer to do what you want.  Everything else in this book, and most other computer books, is just "How do you do this in Java?"  We will be showing you how to do many things in this book, but being able to tell the computer when and why to do these things is what separates a good programmer from a not-so-good programmer.

 

1) Consider your answer to Lab Exercise 5.2.1 Question e.  Write code to test the accuracy of your programming logic.  Use the same concept used in Lab Exercise 5.2.1 Question f to test your code from Question e.  Since there are an infinite amount of combinations for a student's situation, set some reasonable limits to the SAT and income boundary values.  Additionally, you can adjust your loop to increment the test score by 10 or 100 each time through the loop.

 

Answer:

The following code will do a "brute force" testing of the "scholarship" algorithm:

 

The term "brute force" means that the solution just uses the computer's speed to "do everything" instead of trying to be smart and only doing what's necessary.

 

Click here for the Scholarship.java source code.

 

Here are a couple of other tricks that we introduced:

·         The statement

    SAT += 100;

is just a shortcut for

    SAT = SAT + 100;

·         The '\t' character is the TAB character.

·         System.out.print() is very similar to System.out.println(), except that   it does not print a NEWLINE (\n) character at the end.  This causes   the next line that is printed to be printed on the same line.

 

Here is the output from this code:

Income  SAT     Athlete?        Scholarship?

25000   1200    true            Scholarship

25000   1200    false           Scholarship

25000   1300    true            Scholarship

25000   1300    false           Scholarship

25000   1400    true            Scholarship

25000   1400    false           Scholarship

25000   1500    true            Scholarship

25000   1500    false           Scholarship

25000   1600    true            Scholarship

25000   1600    false           Scholarship

26000   1200    true            Scholarship

26000   1200    false           Scholarship

26000   1300    true            Scholarship

26000   1300    false           Scholarship

26000   1400    true            Scholarship

26000   1400    false           Scholarship

26000   1500    true            Scholarship

26000   1500    false           Scholarship

26000   1600    true            Scholarship

26000   1600    false           Scholarship

27000   1200    true            Scholarship

27000   1200    false           Scholarship

27000   1300    true            Scholarship

27000   1300    false           Scholarship

27000   1400    true            Scholarship

27000   1400    false           Scholarship

27000   1500    true            Scholarship

27000   1500    false           Scholarship

27000   1600    true            Scholarship

27000   1600    false           Scholarship

28000   1200    true            Scholarship

28000   1200    false           Scholarship

28000   1300    true            Scholarship

28000   1300    false           Scholarship

28000   1400    true            Scholarship

28000   1400    false           Scholarship

28000   1500    true            Scholarship

28000   1500    false           Scholarship

28000   1600    true            Scholarship

28000   1600    false           Scholarship

29000   1200    true            Scholarship

29000   1200    false           Scholarship

29000   1300    true            Scholarship

29000   1300    false           Scholarship

29000   1400    true            Scholarship

29000   1400    false           Scholarship

29000   1500    true            Scholarship

29000   1500    false           Scholarship

29000   1600    true            Scholarship

29000   1600    false           Scholarship

30000   1200    true            Scholarship

30000   1200    false           No

30000   1300    true            Scholarship

30000   1300    false           No

30000   1400    true            Scholarship

30000   1400    false           No

30000   1500    true            Scholarship

30000   1500    false           Scholarship

30000   1600    true            Scholarship

30000   1600    false           Scholarship

31000   1200    true            Scholarship

31000   1200    false           No

31000   1300    true            Scholarship

31000   1300    false           No

31000   1400    true            Scholarship

31000   1400    false           No

31000   1500    true            Scholarship

31000   1500    false           Scholarship

31000   1600    true            Scholarship

31000   1600    false           Scholarship

32000   1200    true            Scholarship

32000   1200    false           No

32000   1300    true            Scholarship

32000   1300    false           No

32000   1400    true            Scholarship

32000   1400    false           No

32000   1500    true            Scholarship

32000   1500    false           Scholarship

32000   1600    true            Scholarship

32000   1600    false           Scholarship

33000   1200    true            Scholarship

33000   1200    false           No

33000   1300    true            Scholarship

33000   1300    false           No

33000   1400    true            Scholarship

33000   1400    false           No

33000   1500    true            Scholarship

33000   1500    false           Scholarship

33000   1600    true            Scholarship

33000   1600    false           Scholarship

34000   1200    true            Scholarship

34000   1200    false           No

34000   1300    true            Scholarship

34000   1300    false           No

34000   1400    true            Scholarship

34000   1400    false           No

34000   1500    true            Scholarship

34000   1500    false           Scholarship

34000   1600    true            Scholarship

34000   1600    false           Scholarship

 

Hopefully you can see that if isAthlete is true, then is doesn't matter what the SAT and familyIncome values are.  Also, when the income is less that 30000, then the scholarship is always awarded.  This is as it should be.

 


2) It is a well-known fact that we have a leap year every four years on years that are evenly divisible by four.  The years 1980, 1984, 1988, 1992 and 1996 have all been leap years.  What is not so well known is that every 100 years we do not have a leap year.  The year 1900 was not a leap year.  Will the year 2000 be a leap year?  The answer is "yes" because there is a third rule that says that every 400 years, the 100-year rule does not apply and we do have a leap year.  So, the years 2100, 2200 and 2300 will not be leap years, but the year 2400 will be.  If you think these rules are confusing, don't blame us!  This is all quite true! 

Write code to test to see if a given year is a leap year.  You might find it useful to use the modulo (%) function to determine if a year is a leap year.

Answer: The following code will test is a year is a leap year:

 

The key here is to test all of the conditions that can signal a leap year, and then combine these tests in a logical way.

 

Click here for the LeapYear.java source code.

 

The output of this code is as follows:

1000 is not a leap year.

1001 is not a leap year.

1900 is not a leap year.

1980 is a leap year.

1996 is a leap year.

2000 is a leap year.

2001 is not a leap year.

2092 is a leap year.

2096 is a leap year.

2100 is not a leap year.

2104 is a leap year.

2200 is not a leap year.

2400 is a leap year.

3000 is not a leap year.