Category Archives: Uncategorized

Chapter 6: Data set, coin and purse classes (non-generic)

/**
   This class computes the average of a set of data values.
*/
public class DataSet
{
   /**
      Constructs an empty data set.
   */
   public DataSet()
   {
      final int DATA_LENGTH = 100;
      data = new double[DATA_LENGTH];
      dataSize = 0;
   }

   /**
      Adds a data value to the data set
      @param x a data value
   */
   public void add(double x)
   {
      if (dataSize >= data.length)
      {  
         // make a new array of twice the size
         double[] newData = new double[2 * data.length];
         // copy over all elements from data to newData
         System.arraycopy(data, 0, newData, 0, data.length);
         // abandon the old array and store in data
         // a reference to the new array
         data = newData;
      }
      data[dataSize] = x;
      dataSize++;
   }

   /**
      Gets the average of the added data.
      @return the average or 0 if no data has been added
   */
   public double getAverage()
   {
      if (dataSize == 0) return 0;
      double sum = 0;
      for (int i = 0; i < dataSize; i++)
         sum = sum + data[i];
      return sum / dataSize;
   }

   private double[] data;
   private int dataSize;
}

import java.util.Random;

/**
   This program tests the DataSet class by adding 10,000 numbers
   to the data set and computing the average.
*/
public class DataSetTest
{
   public static void main(String[] args)
   {
      Random generator = new Random();
      DataSet data = new DataSet();
      final int COUNT = 10000;
      System.out.println("Adding " + COUNT + " random numbers.");
      for (int i = 0; i < COUNT; i++)
      {
         double x = generator.nextDouble();
         data.add(x);
      }
      double average = data.getAverage();
      System.out.println("average=" + average);      
   }
}
/**
   A coin with a monetary value.
*/
public class Coin
{
   /**
      Constructs a coin.
      @param aValue the monetary value of the coin.
      @param aName the name of the coin
   */
   public Coin(double aValue, String aName) 
   { 
      value = aValue; 
      name = aName;
   }

   /**
      Gets the coin value.
      @return the value
   */
   public double getValue() 
   {
      return value;
   }

   /**
      Gets the coin name.
      @return the name
   */
   public String getName() 
   {
      return name;
   }

   public boolean equals(Object otherObject)
   {  
      Coin other = (Coin)otherObject;
      return name.equals(other.name) 
         && value == other.value; 
   }

   private double value;
   private String name;
}

import java.util.ArrayList;

/**
   A purse holds a collection of coins.
*/
public class Purse
{
   /**
      Constructs an empty purse.
   */
   public Purse()
   {
      coins = new ArrayList();
   }

   /**
      Add a coin to the purse.
      @param aCoin the coin to add
   */
   public void add(Coin aCoin)
   {
      coins.add(aCoin);
   }

   /**
      Get the total value of the coins in the purse.
      @return the sum of all coin values
   */
   public double getTotal()
   {
      double total = 0;
      for (int i = 0; i < coins.size(); i++)
      {
         Coin aCoin = (Coin)coins.get(i);
         total = total + aCoin.getValue();       
      }
      return total;
   }

   private ArrayList coins;
}


import javax.swing.JOptionPane;

/**
   This class tests the Purse class by prompting the
   user to add coins into a purse and computing the total
   value of the purse.
*/
public class PurseTest
{
   public static void main(String[] args)
   {
      double NICKEL_VALUE = 0.05;
      double DIME_VALUE = 0.1;
      double QUARTER_VALUE = 0.25;

      Purse myPurse = new Purse();

      boolean done = false;
      while (!done)
      {
         String input 
            = JOptionPane.showInputDialog("Enter coin name or Cancel");
         if (input == null) 
            done = true;
         else
         {
            double value = 0;
            if (input.equals("nickel"))
               value = NICKEL_VALUE;
            else if (input.equals("dime"))
               value = DIME_VALUE;
            else if (input.equals("quarter"))
               value = QUARTER_VALUE;
            if (value != 0)
            {
               Coin c = new Coin(value, input);
               myPurse.add(c);
               double totalValue = myPurse.getTotal();
               System.out.println("The total is " + totalValue);
            }
         }
      }
      System.exit(0);
   }
}

Input/Output: Origami Half Areas

If you fold a square diagonally, you will create a right isosceles triangle. We will take its area to be 1. Continue to halve this triangle again and again by placing the acute angles on top of each other. Write a java program, HalfAreas_YI.java and prompt the user how many times wants the square to be folded.

As an example, let’s say the user’s response to the prompt is 5.

Output: in full sentence display the number of folds and the area of the final triangle.

Find an origami paper on the last table and do it manually first. Do the calculations so you can check your program is doing the right thing.

Once you confirm your program is running properly, run the program for an input of 7 folds. Submit both, the hand calculation, the output for the test and the solution for the 7 folds.

NOTE: if you are aware of finding the solution by using a formula, you can add that information to your documentation. For this assignment you must use loops.

First Days: Cryptography – Caesar Cipher

The Caesar Cipher

The key for the Caesar Cipher will be a number from 1 to 26. Unless you know the key (that is, know the number used to encrypt the message), you won’t be able to decrypt the secret code.

The Caesar Cipher was one of the earliest ciphers ever invented. In this cipher, you encrypt a message by taking each letter in the message (in cryptography, these letters are called symbols because they can be letters, numbers, or any other sign) and replacing it with a “shifted” letter. If you shift the letter A by one space, you get the letter B. If you shift the letter A by two spaces, you get the letter C. Figure 14-1 is a picture of some letters shifted over by three spaces.

To get each shifted letter, draw out a row of boxes with each letter of the alphabet. Then draw a second row of boxes under it, but start a certain number (this number is the key) of spaces over. After the letters at the end, wrap around back to the start of the boxes. Here is an example with the letters shifted by three spaces:

Making paper cryptography paper tools

Assignments:
1. En-Crypt: Caesar Cipher – Clwk 9/8/2017 – Cryptography

Use the cipherwheel to encrypt a message. In a piece of paper, share the encrypted message with a partner.
In this post, you will type the message, the encrypted message and the one piece of information needed for your partner to decrypt it.

NOTE: Include your partner’s name

2. De-Crypt: Caesar Cipher – Clwk 9/8/2017 – Cryptography

In this post type your partner’s encrypted message and the one piece of information needed to decrypt it. What is the message?

NOTE: Include your partner’s name

3. Your Cipher – Clwk 9/8/2017 – Cryptography
Turned In (105) Due: September 11, 2017 11:45 pm
Design your own cipher and a device to easily encrypt and decrypt messages.

  1. Describe your cipher.
  2. Draw a diagram for your device.
  3. Build your device.
  4. Type here decryption instructions.

4. Friend’s Cipher – Clwk 9/12/2017 – Cryptography

Find a classmate and exchange ciphers, instructions and encrypted message. After you checked with your classmate that you were able to decrypt the message, comment on the following:
1. Efficiency of the cipher
2. Quality of the instructions

NOTE: If the instructions were not clear or you couldn’t follow them, help your classmate make the right changes.
Include the name of your classmate.

Brute Force – Clwk 9/13/17 – Cryptography
Due: September 13, 2017 11:45 pm
If you didn’t have a key to decipher an encrypted message, how would write a program to decrypt it?
Write the pseudocode for you decrypting program using brute force.

GUI: Event handler exercises 1314

November 6th, 2013

Exercise 8: Write a program that animates two cars moving across a frame in opposite direc- tions (but at different heights so that they don’t collide.)

Exercise 9: Change the RectangleComponent for the mouse listener program so that a new rectangle is added to the component whenever the mouse is clicked. Hint: Keep an ArrayList and draw all rectangles in the paintComponent method.

Exercise 10: Write a program that prompts the user to enter the x- and y-positions of the center and a radius, using JOptionPane dialogs.When the user clicks a “Draw” button, draw a circle with that center and radius in a component.

Exercise 10: Write a program that allows the user to specify a circle by typing the radius in a JOptionPane and then clicking on the center. Note that you don’t need a “Draw” button.

Exercise 11: Write a program that allows the user to specify a circle with two mouse presses, the first one on the center and the second on a point on the periphery. Hint: In the mouse press handler, you must keep track of whether you already received the center point in a previous mouse press.