Você está na página 1de 3

FEE 231: LAB 1

1. Write a Java program for the problem of prime numbers. The program should be based on the
flowchart provided in class.
2. When a resistor (R), capacitor (C) and battery (V) are connected in series, a charge Q builds up
on the capacitor according to the formula Q(t) = CV(1 et/RC) if there is no charge on the
capacitor at time t =0. The problem is to monitor the charge on the capacitor every 0.1 seconds in
order to detect when it reaches a level of 8 units of charge, given that V =9, R=4 and C =1.
i.
Draw a flowchart for the problem.
ii.
Create a Java method for computing Q(t)
iii.
Write a Java application program (that incorporates the class in (a)) which displays the
time and charge every 0.1 seconds until the charge first exceeds 8 units (i.e. the last
charge displayed must exceed 8).

Hint: Track the change in charge every 0.1 seconds using print/println and consider the problem
of series (sum of the series 12 +22 +32 . . .) we solved in class
3. Write a Java program that computes and outputs the real roots of a quadratic equation. You
should be able to input the relevant coefficients.
4. The greatest common divisor (GCD) of two integers is the largest integer that evenly divides
each of the two numbers.
i.
Draw a flowchart for the solution to this problem.
ii.
Write a method gcd that returns the greatest common divisor of two integers.
Incorporate the method into an application that reads two values from the user and
displays the result.
5. Imagine a two-way straight railway and two trains approaching each other from opposite
directions. You need to compute how long (in minutes) would it take for these trains to come
side by side. You will also compute how many miles each train travels till then.
Draw a flowchart for the problem.
Write a Java program to read (input) in the distance (in miles), speeds of the trains (in
miles per hour), and output the time to meet (in miles and use 3 digits of precision after
the decimal point for displaying it) and the distance traveled by each train (use 2 digits
of precision).
Input Specification
The distance (in miles) between the trains.
The speed of trains (in miles per hour).
Output Sample
Here is one sample output of running the program. You should test your program with different
data than is shown here based on the specifications given.

Sample Run #1
What is the distance between trains in miles?
24
What is the speed of the first train in miles per hour?
12
What is the speed of the second train in miles per hour?
16
It will take 51.429 minutes for the trains to meet.
The first train will travel 10.29 miles.
The second train will travel 13.71 miles
6. Define the MyRectangle2D class that contains:
i. Two double data fields named x and y that specify the center of the rectangle with get and set
methods. (Assume that the rectangle sides are parallel to x- or y- axes.)
ii. The data fields width and height with get and set methods.
iii. A no-arg constructor that creates a default rectangle with (0, 0) for (x, y) and 1 for both width and
height.
iv. A constructor that creates a rectangle with the specified x, y, width, and height.
v. A method getArea() that returns the area of the rectangle.
vi. A method getPerimeter() that returns the perimeter of the rectangle.
vii. A method contains(double x, double y) that returns true if the specified point (x, y) is inside this
rectangle.
Write a test program that creates a MyRectangle2D object r1 (new MyRectangle2D(2, 2, 5.5, 4.9)), displays
its area and perimeter.
7. Design a class named MyDate. The class contains:
i. Data fields year, month, and day that represent a date.
ii. A no-arg constructor that creates a MyDate object for the current date.
iii. A constructor that constructs a MyDate object with a specified elapsed time since midnight, Jan 1,
1970, in milliseconds.
iv. A constructor that constructs a MyDate object with the specified year, month, and day.
v. Three get methods for the data fields year, month, and day, respectively.
vi. A method named setDate(long elapsedTime) that sets a new date for the object using the elapsed
time.

8. Visualizing Recursion: It is interesting to watch recursion "in action." Modify the factorial method we
wrote in class to print its local variable and recursive-call parameter. For each recursive call, display
the outputs on a separate line, and add a level of indentation. Do your utmost to make the outputs
clear, interesting and meaningful. Your goal here is to design and implement an output format that
makes it easier to understand recursion. You may want to add such display capabilities to other
recursion examples and exercises throughout the text.

9. The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of
the two numbers. Write a method gcd that returns the greatest common divisor of two integers.
Incorporate the method into an application that reads two values from the user and displays the
result. The gcd of x and y is defined recursively as follows: If y is equal to 0, then gcd( x, y ) is
x; otherwise, gcd( x, y ) is gcd( y, x % y ), where % is the remainder operator.

10. Palindromes: A palindrome is a string that is spelled the same way forwards and backwards. Some
examples of palindromes are "radar," "able was i ere i saw elba" and (if spaces are ignored) "a man a
plan a canal panama." Write a recursive method testPalindrome that returns boolean value true if
the string stored in the array is a palindrome and false otherwise. The method should ignore spaces
and punctuation in the string.

Você também pode gostar