Mr Brass's Java Pages

2007-08

---------------------------------

 

---------------------------------

The Decimal Format class is a poorly explained class in the java Documentation so i will clear up a little of it with an example.

you first need to import the DecimalFormat class:


import java.text.DecimalFormat;


then create a format object. this object can be used with doubles, as it uses a decimal. It uses a template String to teach Java how to ouput the objects.
*This example will be used as a standard format of money:


DecimalFormat money = new DecimalFormat("$0.00");


Now that you have a money object. (declared either globally or locally) you can now format your objects
*Note this example uses only 2 decimal places and a $, but any symbols or length is available.

As an example of prniting a double value:


double amount = 4.333333333;
System.out.println(money.format(amount));

------------------

Bank Reconciliation Page

Personal Bank Balance

 

Starting Balance $    1205.00                                                 Running  Balance

Interest                   +         .32

ATM Fee                -        5.00    

                                                                                                      1200.32

 

Checks Written

#1001                                               -825.00

#1002                                                 -75.00

#1003                                                 -96.00

                                                                                                        204.32

 

Other Activities

3/2  ATM                                   - 60.00                                          144.32

3/6  Automatic Deposit         + 351.96                                           496.28

3/7  ATM                                + 100.00                                          596.28

3/13  Automatic Deposit       + 351.96                                            948.24

3/19  ATM                               -  80.00                                            868.24

3/20  Automatic Deposit      +  351.96                                           1220.20

3/22  ATM                              -   40.00                                          1180.20

3/27  Automatic Deposit       + 351.96                       Final Balance                $ 1532.16

 

 

Bank Balance at End of Month

Starting Balance $    1205.00                                              Running  Balance

Interest                   +         .32

ATM Fee                -        5.00    

                                                                                                      1200.32

Checks # 1001 and #1002             - 900.00                                   300.32

ATM Deposits                              + 100.00

ATM Withdrawals                       - 180.00                                    220.32

 

Automatic Payroll Deposits   3  *  351.96 = 1055.88                 1276.20

 

Subtract Uncashed Check  #1003               -   96.00

Add Back Missing Deposit 3/27/08            + 351.96       Final Balance      $  1532.16

-----------------------------

The Guessing Game  Project 6-1

PSEUDOCODE -

Import Random and KeyboardReader

Give the program a name and open main

Initialize reader and generator

Initialize number, guess, numberofGuesses, and count=1

 Select a random number :            number = generator.nextInt( 100 ) + 1

Ask the user for a number from 1 to 100  :

Read the guess    :                            guess  =  reader.readInt()

while  ( guess != number) {

                   count++;

                         if   (guess > number)

                             print “You’re too high”

                         else

                            print “You’re too low”

                              print “Try another guess”

Read the next guess   :                      guess  =  reader.readInt()       }

print    “You guessed correctly ! It took you “  + count + “tries”

-------------------------------------------------------------------------

 

Pizza King Programs

Dari and Mark

Zeph and Cesar

Catie and Jonny

Sanayra and Tiffany

 

 

------------------------------------

2006

Using encapsulation to write a master class program - 

Nak, July, and Kna build a Master Class for Happy Burger...

      

What did the NakJulyKna team do to make their program robust ?

How did NahkJulyKna set their prices ?

 

Now, they test the class by having a user ordering  food from the Sub-Class.

-------------------------------------------------

Jim Tarr Does builds his own Master Class . What is the difference in programming style.

How did Jim set his prices above ?

Now, Jim tests his ordering program.

What did Jim  do to make his program robust ?

--------------------------------------------------------------------------

Mr. Brass's Car and TestCar

Car.java

import TurtleGraphics.*;
import java.awt.Color;

public class Car {

private StandardPen pen;

// Midpoint of top edge of car's body
private double xPosition, yPosition;
private double bodyWidth, bodyHeight, cabWidth, cabHeight;

public Car(){
xPosition = 0;
yPosition = 0;
bodyWidth = 100;
bodyHeight = 20;
cabWidth = 60;
cabHeight = 15;
pen = new StandardPen();
pen.setColor(Color.red);
}

public Car(double x, double y){
this();
xPosition = x;
yPosition = y;
}

public void draw(){

// Draw the body
double x1 = xPosition - bodyWidth / 2;
double x2 = xPosition + bodyWidth / 2;
double y1 = yPosition;
double y2 = yPosition - bodyHeight;
drawRectangle(x1, y1, x2, y2);

// Draw the cab
x1 = xPosition - cabWidth / 2;
x2 = xPosition + cabWidth / 2;
y1 = yPosition + cabHeight;
y2 = yPosition;
drawRectangle(x1, y1, x2, y2);

// Draw the wheels
drawCircle(xPosition - bodyWidth / 2 + 15, yPosition - bodyHeight, 10);
drawCircle(xPosition + bodyWidth / 2 - 15, yPosition - bodyHeight, 10);
}

public void erase(){
pen.setColor(Color.white);
draw();
pen.setColor(Color.red);
}

public void move(double x, double y){
xPosition = x;
yPosition = y;


private void drawCircle(double x, double y, double r){
double side = 2.0 * Math.PI * r / 120.0;
pen.up();
pen.move(x + r, y - side / 2.0);
pen.setDirection(90);
pen.down();
for (int i = 0; i < 120; i++){
pen.move(side);
pen.turn(3);
}
}

private void drawLine(double x1, double y1, double x2, double y2){
pen.up();
pen.move(x1, y1);
pen.down();
pen.move(x2, y2);
}

private void drawRectangle(double x1, double y1, double x2, double y2){
drawLine(x1, y1, x1, y2);
drawLine(x1, y2, x2, y2);
drawLine(x2, y2, x2, y1);
drawLine(x2, y1, x1, y1);
}
}

import TurtleGraphics.*;
import TerminalIO.*;
import java.awt.Color;

 Test Car.java

public class TestCar {

public static void main (String[] args){
KeyboardReader reader = new KeyboardReader();
double x, y, radius;
x = reader.readDouble("Initial x position: ");
y = reader.readDouble("Initial y position: ");
Car car = new Car(x, y);
car.draw();
reader.pause();

while (true){
x = reader.readDouble("New x position: ");
y = reader.readDouble("New y position: ");
car.erase();
car.move(x, y);
car.draw();
}
}
}

--------------------------

The SmilingFace program from the book does not work because it is missing specific info for the mouth to be sketched at the end of the program.

After the following line - - -

private void drawLine(doublex1,doubley1, double x2, doubley2) {

Add the following instructions relating to the pen -

pen.up( );

pen.move(xposition – 15, yposition – 20);

pen.setDirection(315);

pen.down( );

pen.move(5);

pen.setDirection(360);

pen.move(25);

pen.setDirection(45);

pen.move(5);

pen.up( );

}
}

--------------------------------------

Back to Home Page          Back to Teachers Page