Thursday, October 30, 2014

Java use Pro



BouncingBall.java

public class BouncingBall 
{ 
 public static void main(String[] args) 
{
        StdDraw.setXscale(-1.0, 1.0);
        StdDraw.setYscale(-1.0, 1.0);
        double rx = 0.480, ry = 0.860;     
        double vx = 0.015, vy = 0.023;     
        double radius = 0.05;              
         while (true)  
            { 
            if (Math.abs(rx + vx) > 1.0 - radius) vx = -vx;
            if (Math.abs(ry + vy) > 1.0 - radius) vy = -vy;
            rx = rx + vx; 
            ry = ry + vy; 
            StdDraw.setPenColor(StdDraw.GRAY);
            StdDraw.filledSquare(0, 0, 1.0);
            StdDraw.setPenColor(StdDraw.BLACK); 
            StdDraw.filledCircle(rx, ry, radius); 
            StdDraw.show(20); 
        } 
    } 
} 

PlayThatTune.java

public class PlayThatTune 
{
              public static void main(String[] args) 
{
   while (!StdIn.isEmpty()) 
    {
           int pitch = StdIn.readInt();
                       double duration = StdIn.readDouble();
                        double hz = 440 * Math.pow(2, pitch / 12.0);
           int N = (int) (StdAudio.SAMPLE_RATE * duration);
           double[] a = new double[N+1];
           for (int i = 0; i <= N; i++) 
           {
            a[i] = Math.sin(2 * Math.PI * i * hz / StdAudio.SAMPLE_RATE);
            }
                      StdAudio.play(a);
        }
    }
}

Transition.java

public class Transition 
{
 public static void main(String[] args) 
{
       int N = StdIn.readInt();           
        int[][] counts = new int[N][N];   
        int[] outDegree = new int[N];      
        while (!StdIn.isEmpty())  
        {
            int i = StdIn.readInt(); 
            int j = StdIn.readInt(); 
            outDegree[i]++; 
            counts[i][j]++; 
        } 
        StdOut.println(N + " " + N); 
                   for (int i = 0; i < N; i++)  
                   {
                    for (int j = 0; j < N; j++) 
                   {
                double p = .90*counts[i][j]/outDegree[i] + .10/N; 
                StdOut.printf("%7.5f ", p); 
            } 
            StdOut.println(); 
        } 
    } 
} 

No comments:

Post a Comment