Category Archives: Assessment

Input/Output: StdDraw Exercises

MyTriangles_YI.java
Use StdDraw to implement MyTriangles_YI.java to create a drawing of 3 different triangles: acute, obtuse, and right. Look at this example.

MyFunctionGraph_YI.java
Run and understand how FunctionGraph.java works and modify it to create the graph of this function
y = sin(4x) + cos(20x)

Write a short paragraph about how the graph changes as the input argument changes.

<pre>

/******************************************************************************
 *  Compilation:  javac FunctionGraph.java 
 *  Execution:    java FunctionGraph n
 *  Dependencies: StdDraw.java
 *
 *  Plots the function y = sin(4x) + sin(20x) between x = 0 and x = pi
 *  by drawing n line segments.
 *
 ******************************************************************************/

public class FunctionGraph {
    public static void main(String[] args) {

        // number of line segments to plot
        int n = Integer.parseInt(args[0]);

        // the function y = sin(4x) + sin(20x), sampled at n+1 points
        // between x = 0 and x = pi
        double[] x = new double[n+1];
        double[] y = new double[n+1];
        for (int i = 0; i <= n; i++) {
            x[i] = Math.PI * i / n;
            y[i] = Math.sin(4*x[i]) + Math.sin(20*x[i]);
        }

        // rescale the coordinate system
        StdDraw.setXscale(0, Math.PI);
        StdDraw.setYscale(-2.0, +2.0);

        // plot the approximation to the function
        for (int i = 0; i < n; i++) {
            StdDraw.line(x[i], y[i], x[i+1], y[i+1]);
        }
    }
}
</pre>

Mystery Graph
Download the text file below and find out what the points draw.
Is the file missing anything?
Take a screenshot for submission.
Hint: use the PlotFinder.java

OOD – Encapsulation

Clearly explain the difference and similarities between these two lines of code:

harrysChecking.balance -= 500; // assume balance is not private (no encapsulation)

harrysChecking.withdraw(500); // encapsulation is enforced

Which one would you pick if you were Harry? Support your answer.

GUI – DialogDemo.java

Check edmodo.com for questions on content.

import javax.swing.JOptionPane;


/* 1.4 example used by DialogDemo.java. */
class DialogViewer1{
   public static void main(String[] args) {
       JOptionPane.showMessageDialog(null, "Hello, World!"); System.exit(0);
    }
}
import javax.swing.JOptionPane;

public class DialogViewer2 {
    public static void main(String[] args) {
    
        String name = JOptionPane.showInputDialog("What is your name?"); 
        System.out.println(name);
        System.exit(0);

    }
 }

Homework: Prepare for more questions including JOptionPane.