Assignment 2
In-Lab-Work
Part 1
Get familiar with variables. Do the following:
- Declare an integer n set to 1 (int n = 1).
- Declare another integer m set to 10 (int m = 10).
- Declare a float q set to 10 (float q = 10.0f).
- Declare a float p set to 3.14f (float p = 3.14f).
Now print the following results:
- This is the variable n 1;
For this you can use the command:
g.drawString("This is the variable n " + n, 50, 50);
- This is m - n 9
- This is the float p 3.14
- This is n/m 0
- This is n/q 0.1
- This is 10 mod 3 1 (mod means the remainder of 10 divided by 3.
Part 2
- This code gives the dimensions of your applet window (width and height), and
places them in the integer variables w and h.
int w = getSize().width;
int h = getSize().height;
Draw a vertical line which splits the screen exactly in half, using the variables w and h.
- Convert 60 Fahrenheit degrees to Celsius. Use the following formula:
Celsius = 5/9(Fahrenheit - 32).
Part 3
- Find at least five compilation errors in the following code,
so that after fixing them, the program "MyProgram.java" compiles succesfully.
import java.applet.Applet;
public class myfile extends Applet{
public void paint(Graphic g){
float p = 3.4;
int n = 1
g.drawString("This is 1+3.4 "+ n+p);
}
}
- Define the variable int centerx = 300, int centery = 300.
Use this variables to draw three circles with a common center.
These variables refer to the upper left corner.
The actual center is obtained from cenerx and centery by doing a simple calculation.
The first circle should have radius equals to
20, the second 50, and the third 80.
Programming Assignment
The following instructions represent what you need to do
to complete your assignment 2.
First, you are given some code
which gives this output.
When you finish, your
applet should look as similar as possible to
this output. All questions should run in the same applet.
For each question, it is required to use at least one variable.
- Calculate
- 1+2+3+4+5+6+7+8+9+10.
- (1+2+3+4+5+6+7+8+9+10)/10.
- The area of the circle of radius 10.5. (The area of a circle is the product of pi*radius*radius, pi = 3.14).
- Draw the rectangle whose upper right corner is the point (450, h/2+50), and whose lower left corner
is the point (400, h/2+150), as its shown in the graph.
(h is a variable defined in your code).
- To the given representaion of the solar system,
- add the Earth's orbit.
- add the Earth.
- add the Moon's orbit.
- add the Moon.
Note. Use only circles.
- Swap the black square with the solar system (For this,
modify the values of some variables).
Create an applet whose dimensions (width by height) are 550 by 500.
Submit your Assignment2.java file (don't use a different name like assignment2.java)
under Assignment 2 on using Vincent submission system. Be sure to read and carefully follow the Vincent submission instructions on the course web
page. You don't need to submit your html file.
Due date: midnight Nov 6th.