Author Archives: graceliana@gmail.com

Input/Output: Graphics: Simple Bar Graph

Screen Shot 2014-12-07 at 12.13.44 PM


Looking at the printf method
A printf format reference page (cheat sheet)

// This class shows how to initialize an array 
// How to use the printf output formatting method
public class InitPrintfArray 
{
   public static void main( String[] args )
   {
      // initializing the array when it is created
      int[] arrayA = { 87,56,78,99,102 };

      System.out.printf( "%s%7s\n", "Index", "Value" ); // column headings
   
      // output each array element's value 
      for ( int i = 0; i < arrayA.length; i++ )
         System.out.printf( "%5d%5d\n", i, arrayA[ i ] );
   } // end main
} // end class InitPrintfArray

A "simplified" version of a bar chart by using the elements of an array as counters

// Bar graph printing program.

public class BarGraph 
{
   public static void main( String[] args )
   {
      int[] arrayA = { 0, 0, 0, 0, 0, 0, 1, 2, 4, 2, 1 };

      System.out.println( "Grade distribution:" ); 

      // for each array element, output a bar of the chart
      for ( int i = 0; i < arrayA.length; i++ ) 
      {
         // output bar label ( "00-09: ", ..., "90-99: ", "100: " )
         if ( i == 10 )
            System.out.printf( "%5d: ", 100 ); 
         else
            System.out.printf( "%02d-%02d: ", 
               i * 10, i * 10 + 9  ); 
 
         // print bar of asterisks
         for ( int stars = 0; stars < arrayA[ i ]; stars++ )
            System.out.print( "*" );

         System.out.println(); // start a new line of output
      } // end outer for
   } // end main
} // end class BarGraph

Grade distribution:
00-09:
10-19:
20-29:
30-39:
40-49:
50-59:
60-69: *
70-79: **
80-89: ****
90-99: **
100: *
Claswork/Homework:
Tracing 101: Trace the i, arrayA[i], and the output as the inner and outer loops traverse arrayA.

Arrays: The Locker Puzzle

Screen Shot 2014-12-07 at 12.13.44 PM

A school has 100 lockers and 100 students. All lockers are closed on the first day of school. As the students enter, the first student, denoted S1, opens every locker. Then the second student, S2, begins with the second locker, denoted L2, and closes every other locker. Student S3 begins with the third locker and changes every third locker (closes it if it was open, and opens it if it was closed). Student S4 begins with locker L4 and changes every fourth locker. Student S5 starts with L5 and changes every fifth locker, and so on until student S100 changes L100.
After all the students have passed through the building and changed the lockers, which lockers are open?

Write a program, WhichLockers_YI.java to find your answer and display all open locker numbers separated by exactly one space.

Hint: Use an array of 100 Boolean elements, each of which indicates whether a locker is open (true) or closed (false). Initially, all lockers are closed.

Arrays: Magic Square

Screen Shot 2014-12-07 at 12.13.44 PM

2D Arrays

Classwork/Homework:
Magic squares. A n × n matrix that is filled with the numbers 1,2,3,…,n^2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value.
Write a program that reads in 16 values from the keyboard and tests whether they form a magic square when put into a 4 × 4 array. This is only an example of a 4 x 4 square.

Screen Shot 2014-11-23 at 9.53.25 PM

You need to test two features:
1. Does each of the numbers 1, 2, …, 16 occur in the user input?
2. When the numbers are put into a square, are the sums of the rows, columns, and diagonals equal to each other?

Testing and output:
1. Display the given magic square and the value rows, columns and diagonals add up to.
2. Find one more magic square online, display it and show the value all the columns, rows and diagonals are equal to.
3. Display a square that it is not magic but it follows the rules of being filled 1 through n^2.

NOTE: print the input numbers in matrix format, rows by columns.

Strong Recommendation:
Follow the rules by tracing them on paper first. Then, find a pattern!

Input/Output: Animations: Ballistic Motion I

Animations using java
duke

Screen Shot 2015-02-23 at 12.19.21 AM

Classwork:
Projectile motion with drag. Use the supplied program and make the necessary changes to show during the animation the following:
Ballistic Motion Sim

Projectile motion with drag. Write a program YI_BallisticMotion1.java that plots the trajectory of a ball that is shot with velocity v at an angle theta. Account for gravitational and drag forces. Assume that the drag force is proportional to the square of the velocity. Using Newton’s equations of motions and the Euler-Cromer method, update the position, velocity, and acceleration according to the following equations:

Use the given program to enhance it to show the image bellow:
1. The values should change as the ball travels through the air.
2. The values should move with the ball.
3. The blue text and dotted curve is not included in the GUI.

project motion math

[spoiler title=’BallisticMotion.java’]

/******************************************************************************
* Compilation: javac BallisticMotion.java
* Execution: java BallisticMotion v theta
* Dependencies: StdDraw.java
*
* Simluate the motion of a ball fired with velocity v at theta degrees
* with coefficient of drag C. Uses Euler-Cramer method to numerically
* solve differential equation.
*
* % java BallisticMotion 180.0 60.0
*
* % java BallisticMotion 20.0 45.0
* Copyright © 2000–2011, Robert Sedgewick and Kevin Wayne.
* Last updated: Mon Aug 3 12:15:11 EDT 2015.
******************************************************************************/

public class BallisticMotion {

public static void main(String[] args) {
double G = 9.8; // gravitational constant m/s^2
double C = 0.002; // drag force coefficient

double v = Double.parseDouble(args[0]); // velocity
double theta = Math.toRadians(Double.parseDouble(args[1])); // angle in radians

double x = 0.0, y = 0.0; // position
double vx = v * Math.cos(theta); // velocity in x direction
double vy = v * Math.sin(theta); // velocity in y direction

double ax = 0.0, ay = 0.0; // acceleration
double t = 0.0; // time
double dt = 0.01; // time quantum

double maxR = v*v / G; // maximum distance without drag force
StdDraw.setXscale(0, maxR);
StdDraw.setYscale(0, maxR); // set the same as for X-scale

// loop until ball hits ground
while (y >= 0.0) {
v = Math.sqrt(vx*vx + vy*vy);
ax = -C * v * vx;
ay = -G – C * v * vy;
vx += ax * dt;
vy += ay * dt;
x += vx * dt;
y += vy * dt;StdDraw.filledCircle(x, y, 0.25);
StdDraw.show();
StdDraw.pause(5);
StdDraw.clear();

}

StdDraw.show();
}

}

[/spoiler]

Homework:
Work on the ballistic motion assignment.

Input/Output: Animations: Array: Intro to Conways’ Game of Life

Conways’ Game of Life
Gospers_glider_gun

Answer the following questions in edmodo.com
1. What is the shape of the cells?
2. What are the two possible states of a cell?
3. How many neighboring cells each cell interact with?
4. Explain the relative location of these neighboring cells.
5. What are the rules at each step in time?
6. What does the initial pattern constitute?
7. What is it referred by a “tick”?
8. What are the criteria that must be met for the proper emulation to be successful?
9. What are the different types of pattern that occur in the Game of Life?
10. On a piece of graph paper draw the 3 following patterns and show the life of the cell at each of 5 ticks.
Beehive
Screen Shot 2014-12-15 at 11.12.40 PM

Beacon
Screen Shot 2014-12-15 at 11.13.08 PM

Glider
Screen Shot 2014-12-15 at 11.13.32 PM

Write the first draft of your algorithm using only pseudocode.
One of many Fun sites


Animation: RandomBlock_YI.java

Implement an animation, RandomBlock_YI.java. The CheckerBoard.java can easily be changed to randomly select a random number of squares on the grid and become a solid color (“alive”) and then randomly it will turn back to white (“dead”) again. ‌ Re-Use-Code: these are good resources for this assignment: ‌

BouncingBall.java

CheckerBoard.java

MyGraphPaper.java

If you have successfully completed the random block program, you should be able to get started with the game of life assignment.
Start small. Here is the glider:

OOD: First Video from BlueJ

Classwork:
Chapter 1: VN 1.1 Introduction to key concepts
Take video notes.

Chapter 1: VN 1.2 Creating and using objects within BlueJ

Chapter 1: VN 1.3 methods and parameters

Assignment:

Using BlueJ’s video notes and source files on edmodo.com, create a new picture where there are more than one person in different locations, a house, a sun and grass.

Screen Shot 2015-01-13 at 10.07.43 AM house

OOD: BlueJ House

January 13th, 2015

Classwork:
Using BlueJ’s video notes and source files on edmodo.com, create a new picture where there are more than one person in different locations, a house, a sun and grass.

Screen Shot 2015-01-13 at 10.07.43 AM

If you don’t remember exactly how it was done, the video links are on yesterday’s post.

Homework:
6. Implement a class Employee. An employee has a name (a string) and a salary (a double). Write a default constructor, a constructor with two parameters (name and salary), and methods to return the name and salary. Write a small program that tests your class.

OOD – Java and BlueJ Videos


OOD – Refresher
Java Video Tutorial 7


Chapter 3: VN 3.1 Fields of class types


Chapter 3: VN 3.2 Constructors and field initialization


Chapter 3: VN 3.3 Solving the 12-hour clock exercise

Look into edmodo.com for assignments and due date

Homework:
Watch this video:

If you are moving ahead:
Screen Shot 2015-02-02 at 10.03.46 AM

Use these libraries, download StdIn.java, StdOut.java, StdDraw.java, and StdAudio.java
Look at Standard output and input.
Redirection and piping.
Standard drawing.
Standard audio.
Graphical user interfaces.
Look at exercises 1 through 9 and work on exercise 10. Submit it to edmodo.com.