Вы находитесь на странице: 1из 2

RWE 214

Practical Week 6 2012


Hand-in date: 23:59 Sunday 25 March 2012. Complete the following questions from the Exercises section in the textbook, Introduction to Programming in Java. Resources for certain questions, such as code libraries you are asked to use and example input and output les which can be used for development of your programs can be found on the course website. You have to hand in your answers to these questions. In order to keep all the les in one place you have to tar your les. A quick tutorial on tar is given below.

Tar how-to
We use tar to archive multiple les in one le for convenience. For more information on tar see the man pages: man tar To create a tar archive: tar cvf archive.tar file1 file2 file3 To extract the les contained in a tar archive: tar xvf archive.tar You must use the format <student#> prac<prac#>.tar for your tar le name.

Web 3.2.4 [10]

Qubits. In quantum computing, a qubit plays the role of a bit. It is a complex number a + bi such that |a + bi| = 1. Once we measure a qubit, it decides to be a 1 with probability a2 and a 0 with probability b2 . Any subsequent observations will always yield the same value Implement a data type Qubit that has a constructor Qubit(a, b) and a boolean method Observe that returns
true or false with the proscribed probabilities.

Web 3.2.11 [10]

Euclidean points. Create a data type EuclideanPoint.java that represents a d-dimensional point. Include a method so that p.distanceTo(q) returns the Euclidean distance between points p and q.

Web 3.2.34 [50]

Electric eld lines. Michael Faraday introduced an abstraction called electric eld lines to visualize an electric eld. By Coulombs law, the electric eld at a point induced by a point charge qi is given by Ei = kqi /r2 and the direction points to qi if qi is negative and away from qi it is negative. If there are a group of n point charges, the electric eld at a point is the vector sum of the electric elds induced by the n individual point charges. We can compute it by summing up the components in the x- and y- directions. Fig. 1a below illustrates the eld lines for two equal positive point charges and Fig. 1b illustrates two point charges of opposite signs. The second conguration is called an electric dipole: the charges cancel each other out, and the electric eld weakens very quickly as you move away from the charges. Examples of dipoles can be found in molecules where charge is not evenly distributed. Oscillating dipoles can be used to produce electromagnetic waves to transmit radio and television signals.

(a)

(b)

(c)

(d)

Figure 1: Electric potential and eld lines of various charge congurations

Write a program FieldLines.java which draws 10 electric eld lines coming out of each charge. (We take some liberties since the number of eld lines per unit area should be proportional to the magnitude of the eld strength.) Each line starts on a 1-pixel circle around the charge, at twelve equally spaced angles. The electric eld at a point (x, y) from a point charge qi is given by Ei = kqi /r2 where qi is the magnitude of the charge i and r is the radial distance from it. The eld due to several charges is the vector sum of the eld due to each, and can be found by adding the x- and x-components. After calculating the electric eld strength, we move in the direction of the vector eld and draw a spot. We repeat this process until we reach the boundary of the region or another point charge. The gures below illustrate the electric potential and eld lines for several random charges of equal magnitude.

Web 3.2.47 Random walker [30]

Read the text in the textbook page 107111, describing the model known as the self-avoiding random walk. Then write a data type called RandomWalker2D that simulates the motion of a random walker on the plane that starts at the origin (middle of the plane) and makes a random step (left, right, up, or down) at each step. The plane must be drawn using a Picture or StdDraw object with a N N grid, as seen in the example on page 110. RandomWalker2D must be able to identify its current coordinates on the grid. The coordinates must be instance variables that are only accessible by the RandomWalker2D object which the coordinates belong to. The class must also include at least the following methods: step() that moves the random walker one step on the grid (the class must provide the user the option to draw the step on the grid); distance() that returns the distance the random walker is from the origin (as the crow ies); deadEnd() that detects if a dead end has been reached (see description of a dead end in the textbook); onGrid() that checks whether the random walker is still on the plane, which means the walker did not escape the grid. Write a client SelfAvoidingWalk2D, like the one on page 109, that runs T random walker trials on the N N grid. Your client should print the following information to standard output: The percentage of trials which were dead ends. The percentage of trials which escaped (passed the boundaries). The average lengths of the paths, where the path is not the distance to the origin, but the distance actually travelled. The average area of the smallest axis-oriented rectangle that encloses the path.

Вам также может понравиться