5/16/2012 – Class assignment

Today, you have a short reading and written assignment.

Read Section 9.6 on pg. 317.  It’s a whopping half a page of reading!  Then as a written assignment, complete Question #’s 1 and 3 from Exercise 9.6 at the bottom of pg. 317.  This is due to me tomorrow morning in class (Thursday 5/17/2012).

Also, work on Chapter 9 Program 1.  Due next Monday, 5/21/2012.

If you’re done with all that, I’ve added a bonus program for Chapter 9, so work on that to get some bonus points for the quarter.  Chapter 9 Bonus Program 1 is due by end of day next Thursday, 5/24/2012.

5/15/2012 – What we covered

Today we covered some examples of the array concepts covered in Sections 9.1-9.4.  We saw how to declare an array of a primitive variable type (an array of ints for example), and how it differs from declaring a simple primitive variable:

        int a;  // declaring a simple int variable
        a = 1;

        int[] abc; // declaring an array of ints
        abc = new int[500];  // need to use the "new" keyword

Then we walked through an example of using a loop to get values from the user, and storing those values into an array, and then using a second loop to go back through the array, printing out the user-supplied values in reverse order:

        int[] abc = new int[5];

        Scanner reader = new Scanner(System.in);
        for (int i = 0; i < 5; i++) {
            System.out.print("Enter a number: ");
            int val = reader.nextInt();
            abc[i] = val;
        }

        for (int j = 4; j >= 0; j--) {
            System.out.println(abc[j]);
        }

 

 

5/14/2012 – Class assignment

Here’s your assignment for today’s class.  If you haven’t finished it yet, work on the final Chapter 7 program, Program 3, which is due by end of day tomorrow (Tuesday, 5/15/2012).

Then, I’ve assigned Chapter 9 Program 1, to give you some exercise using arrays.  Chapter 9 Program 1 will be due a week from today, Monday, 5/21/2012.

And just to let you know ahead of time, next week we’ll be having our final test of the year, which will be a test that will include what we’ve covered in Chapters 7 and 9.

 

5/9/2012 – Class assignment

Today in class, work on:

Program 2 due this Thursday, 5/10/2012

Program 3, due next Tuesday, 5/15/2012

If you’re done with both of those, work on other classwork quietly.  If you’re really bored, you can read ahead: read the first couple pages of Chapter 9, pp. 305-308, which Mr. Rees will cover in class on Thursday in my absence.

Have a good day everyone.

5/8/2012 – What we covered

Today we briefly finished up Section 7.4.  We covered the format flags on page 254, and we covered the String.format method on pg. 256, and saw how it behaves almost identically to the System.out.printf statement that we had covered earlier in Section 7.4 last week.

Then we briefly covered Section 7.5, introducing Java’s try-catch statements as a means to handle errors.  We looked at an example of that with our temperature conversion query-controlled loop example from earlier in Chapter 7, and used a try-catch block to more gracefully handle an error when the user typed in text when we were expected a double.

5/7/2012 – Class assignment

Alright, last Thursday, we covered a good bit of Section 7.4.  Well, today you have a new program assignment to give you some practice with it: Chapter 7 Program 3.  Refer to the posting from last Thursday, and to the charts in Section 7.4 on pgs. 253 & 254 if you need some help with how to format using the printf statement.  Program 3 will be due next Tuesday, May 15th.

So, if you didn’t finish Program 1 yet (was due last Thursday, 5/3/2012), finish that first.  If you still have Program 2 to finish, work on that next, since that is due this Thursday, 5/10/2012.  Then if you’re done with both of them, work on Program 3.

Tomorrow we’ll wrap up our coverage of Chapter 7.

 

5/3/2012 – What we covered

Today we began covering Section 7.4, dealing with formatting output.  We introduced Java’s printf statement, and showed examples of how we can use it to format doubles, ints, and Strings.

First we looked at an example of how a double that is used to represent dollars and cents looks like when just using the println statement that we’ve become familiar with:

        double dollars = 24;
        double salestax = dollars * 0.0125;
        System.out.println("price: " + dollars);
        System.out.println("tax: " + salestax);
        System.out.println("total: " + (dollars + salestax));

The output looks like this:

price: 24.0
tax: 0.30000000000000004
total: 24.3

We’d like to make sure that exactly two digits are displayed to the right of the decimal point (and three for the tax amount), and so we introduce the printf statement and how it lets us do that

        System.out.printf("price: %f   \n", dollars);
        System.out.printf("price: %.2f   \n", dollars);
        System.out.printf("tax: %.3f \n", salestax);

Using these statements, we get the following output:

price: 24.000000
price: 24.00
tax: 0.300

We then showed a couple of examples of how to use a printf statement when we want more than one value formatted in the string; the second statement below makes both of the formatted values 10 characters long:

        System.out.printf("price: %.2f  tax: %.3f  \n", dollars, salestax);
        System.out.printf("price: %10.2f  tax: %10.3f  \n", dollars, salestax);

With these statements, we get this output; note the 10 character lengths in the second line of output:

price: 24.00  tax: 0.300
price:      24.00  tax:      0.300

Finally, we showed an example that had us format a 10-character String, left justified, for a name, and a 10-character decimal for the age value:

        String name = "Scott";
        int age = 16;
        System.out.printf("NAME       AGE       \n");
        System.out.printf("====       ===       \n");
        System.out.printf("%-10s %-10d \n", name, age);

The output of this code is below:

NAME       AGE
====       ===
Scott      16

 

 

5/1/2012 – What we covered

Today we finished covering Section 7.3, and finished our example of the temperature-coversion menu-driven program.  Today we added the multiway if statement inside of the while loop, and in each of the if / else-if block we added code that performs the appropriate behavior for the option chosen:

public class ConvertWithMenu {

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);
        Thermometer thermo = new Thermometer();
        String menu;
        int menuOption;

        menu = "\n1) Convert from Fahrenheit to Celsius" +
               "\n2) Convert from Celsius to Fahrenheit" +
               "\n3) Quit" +
               "\nEnter your option: ";

        menuOption = 4;

        while(menuOption != 3) {
            System.out.print(menu);
            menuOption = reader.nextInt();
            System.out.println("");

            if (menuOption == 1)  {   // F->C
                System.out.print("Enter degrees Fahrenheit: ");
                double f = reader.nextDouble();
                thermo.setFahrenheit(f);
                System.out.println("The equivalent in Celsius is " + thermo.getCelsius());
            }
            else if (menuOption == 2) {  // C->F
                System.out.print("Enter degrees Celsius: ");
                double c = reader.nextDouble();
                thermo.setCelsius(c);
                System.out.println("The equivalent in Fahrenheit is " + thermo.getFahrenheit());
            }
            else if (menuOption == 3) {  // quit
                System.out.println("Goodbye!");
            }
            else {  // any other value
                System.out.println("Invalid option");
            }

        }

    }
}