Assignment 2
Task
Explore data types, operator precedence, expressions, variable declaration, and assignment.
Steps: Part 1
Write a short program named Assignment2 that starts with a radius and prints out the following details for a sphere:
- Diameter (2r)
- Circumference (of the sphere's great circle) (πd)
- Surface area (4πr2)
- Volume (4/3 * πr3)
Use double variables (not int s). For π, use the pre-defined Java constant Math.PI . Below is a few lines to get you started. You will have to replace the comments (//... ) with your own lines of code. Remember that you should have to change only the value assigned to radius to produce correct output for a sphere of different size.
public class Assignment2 {
public static void main(String[] args) {
double radius = 5;
//...
double circumference = diameter * Math.PI;
//...
System.out.println("For a sphere with radius " + radius + ": ");
//...
}
}
Your output should look something like this:
For a sphere with radius 5.0:
Diameter = 10.0
Circumference = 31.41592653589793
Surface area = 314.1592653589793
Volume = 523.5987755982989
Steps: Part 2
Below are 10 short sections of code. Each one includes one System.out.println statement. For each section, if the code will run successfully, tell me the following things:
- What is printed to the screen?
- What is the data type of the argument (the thing in the parentheses) of the
println method call? (If this argument is an expression, tell me what the data type is of the resulting single value.)
If the code contains an error that will prevent it from compiling or printing normal output, instead tell me the following things:
- Does that error occur at compile time or runtime?
- What is the cause of the error?
Here are two example code sections and the appropriate answers:
int answer = 3;
System.out.println("Answer: " + answer);
Prints: "Answer: 3"
Data type: String
int average = 3;
int count = 0;
System.out.println(average / count);
Error: runtime
Cause of error: cannot divide by 0 (count contains the value 0)
You should first try to answer these questions by just examining the code. Then, to check your answers, paste each section into a main method, compile it, and run the resulting program.
Questions
-
int side = 9;
int volume;
volume = side * side * side;
System.out.println("The volume of a cube with side " +
side + " is " + volume);
-
int total = 0;
int count = 0;
total = total + 10;
count = count + 1;
total += 6;
count++;
System.out.println("Average: " + total / count);
-
int num = 3;
System.out.println(num / 4);
-
double num = 3;
System.out.println(num / 4);
-
int num = 3;
System.out.println(num / 4.0);
-
int num = 3.0;
System.out.println(num / 4);
-
int num = 1;
int pre = ++num;
int post = num++;
System.out.println("Pre: " + pre + ". Post: " + post + ". Num: " + num);
-
String hi = "";
hi += "He";
hi += "LLo";
System.out.println(hi);
-
int num = 0;
System.out.println(3 < 4 && num++ == 1);
-
boolean result;
int num = 3;
result = (num != 0);
System.out.println(result);
What to submit
Attach your Assignment2.java file to an email, and paste your answers to the 10 programming question in the body of the email.
FAQs
- How do I do powers, such as r3?
- There is a way to do it using the
Math.pow() function,
such as in int cube = Math.pow(radius, 3) . But for now, it's probably
easier to just do it manually: radius * radius * radius .
- My volume calculation isn't coming out correctly!
- Be careful of integer math. Remember that
System.out.println(4 / 3); prints out 1 , not
1.3333333 . So you want to use double (floating
point) literals, rather than
int (integer) literals here.
Grading
Out of 10 points:
- 1 - Submission
- Follows required submission policies.
- 1 - Program compiles
- 2 - Program is correct
- Prints correct results for diameter, circumference, surface area, and volume.
- 1 - Program's radius is not hard-coded
- The radius's value only occurs once in your program: when you initialized the
radius variable
- 5 - Ten coding questions
- 0.5 points each
|