Which of the following are valid statements?
1) System.out.println(1+1);
2) int i=2+'2';
3) c
4) byte b=255;
Answer:
1) System.out.println(1+1);
2) int i=2+'2';
Option 3 is not valid because single quotes are used to indicate a character constant and not a string. Several people have emailed me to say that option 3 will compile. When they eventually compiled the exact code they have agreed, it will not compile. Let me re-state that
String s="on"+'one'; Will NOT compile.
Option 4 will not compile because 255 is out of the range of a byte
Saturday, 13 June 2009
Friday, 12 June 2009
Java Certification Question 0002
Which of the following are Java keywords?
1) NULL
2) new
3) instanceOf
4) wend
Answer:
2) new
The option NULL (note the upper case letter) is definitely not a keyword. There is some discussion as to if null is a keyword but for the purpose of the exam you should probably assume it is a keyword.
The option instanceOf is a bit of a misleading option that would probably not occur on the exam. The real keyword is instanceof (note that the of has no capital letter O). I had the incorrect version in an earlier version of this tutorial as it looks more likely to my eyes. The instanceof keyword looks like a method, but it is actually an operator.
The option wend is probably valid in some other language to indicate the end of a while loop, but Java has no such keyword.
1) NULL
2) new
3) instanceOf
4) wend
Answer:
2) new
The option NULL (note the upper case letter) is definitely not a keyword. There is some discussion as to if null is a keyword but for the purpose of the exam you should probably assume it is a keyword.
The option instanceOf is a bit of a misleading option that would probably not occur on the exam. The real keyword is instanceof (note that the of has no capital letter O). I had the incorrect version in an earlier version of this tutorial as it looks more likely to my eyes. The instanceof keyword looks like a method, but it is actually an operator.
The option wend is probably valid in some other language to indicate the end of a while loop, but Java has no such keyword.
Thursday, 11 June 2009
Java Certification Question 0001
Which of the following are legal statements?
1) float f=1/3;
2) int i=1/3;
3) float f=1.01;
4) double d=999d;
Answer:
1) float f=1/3;
2) int i=1/3;
4) double d=999d;
The fact that option 3 does not compile may be a surprise. The problem is because the default type for a number with a decimal component is a double and not a float. The additional trailing d in the option with 999 doesn't help, but it doesn't harm.
1) float f=1/3;
2) int i=1/3;
3) float f=1.01;
4) double d=999d;
Answer:
1) float f=1/3;
2) int i=1/3;
4) double d=999d;
The fact that option 3 does not compile may be a surprise. The problem is because the default type for a number with a decimal component is a double and not a float. The additional trailing d in the option with 999 doesn't help, but it doesn't harm.
Tuesday, 21 August 2007
Operators and Assignments (Part 14)
Mathematical Functions - cont
Let’s try out an example now in order to make sure that we know how to use the contents of the Math class.
Type out the following program, which will calculate the radius of a circle in feet and inches, given that it has an area of 100 square feet.
public class MathCalculation{
public static void main(String[ ] args){
double radius = 0.0;
double circleArea = 100.0;
int feet = 0; int inches = 0;
radius = Math.sqrt(circleArea/Math.PI);
// Get the feet as a whole number
feet = (int)Math.floor(radius);
// Calculate the number of inches
inches = (int)Math.round(12.0 * (radius – feet));
System.out.println(“The radius of a circle with area “ + circleArea + “ square feet is\n” + feet + “ feet “ + inches + “ inches”);
}
}
Save the program as MathCalculation.java, compile and run it. You should get the following output:
The radius of a circle with area 100 square feet is
5 feet 8 inches
The first calculation we need, after defining the variables uses the sqrt( ) method to calculate the radius.
Since the area of a circle, with radius r, is given by the formula pir², the radius must be sqrt(area/pi), and we specify the argument to the sqrt( ) method as the expression circleArea/Math.PI. The result is in feet as double value. To get the number of the whole feet we use the floor( ) method. Note that cast to int is necessary, otherwise you will get an error from the compiler.
Lastly we get the number of inches by subtracting the value of whole feet from the original radius, multiplying the fraction of foot by 12 to get the equivalent inches, and then rounding the result to the nearest integer using the round( ) method.
Note how we output the result. We did encounter the System.out.println in our “Hello World” application, but we didn’t explain exactly how it works, so let’s do it now. System is the name of a standard class that contains variables and methods for supporting simple keyboard input and character output to the display. It is contained in the package java.lang so it is always accessible just by using the simple class name, System.
The object out represents the standard output stream – your display screen, and is data member of the class System. This member is referenced by using the class name System separated from the member name out by a period – System.out . The bit at the rightmost end, println(…. ), calls the println( ) method of the object out. This method outputs the text string that appears between the parentheses to your display.
You might be wondering what is the + operator doing here, it’s not arithmetic we are doing, is it? No, but the plus has a special effect when used with character strings: it joins them together (concatenation). Neither circleArea, feet or inches are of type String, but they are all converted to a character string to be compatible with “The radius of a circle with area “.
Exercise 3 – 6 Using math functions
Create an application that gets a random number between 0 and 1000, finds the square root of the number, the log of the number, the sin, cos, and tan of the number and prints them (with suitable text) to the screen. To generate the random number, use the Random class in the java.util package. If your not sure of how these are generated, look up this class and it’s methods in the online documentation.
Let’s try out an example now in order to make sure that we know how to use the contents of the Math class.
Type out the following program, which will calculate the radius of a circle in feet and inches, given that it has an area of 100 square feet.
public class MathCalculation{
public static void main(String[ ] args){
double radius = 0.0;
double circleArea = 100.0;
int feet = 0; int inches = 0;
radius = Math.sqrt(circleArea/Math.PI);
// Get the feet as a whole number
feet = (int)Math.floor(radius);
// Calculate the number of inches
inches = (int)Math.round(12.0 * (radius – feet));
System.out.println(“The radius of a circle with area “ + circleArea + “ square feet is\n” + feet + “ feet “ + inches + “ inches”);
}
}
Save the program as MathCalculation.java, compile and run it. You should get the following output:
The radius of a circle with area 100 square feet is
5 feet 8 inches
The first calculation we need, after defining the variables uses the sqrt( ) method to calculate the radius.
Since the area of a circle, with radius r, is given by the formula pir², the radius must be sqrt(area/pi), and we specify the argument to the sqrt( ) method as the expression circleArea/Math.PI. The result is in feet as double value. To get the number of the whole feet we use the floor( ) method. Note that cast to int is necessary, otherwise you will get an error from the compiler.
Lastly we get the number of inches by subtracting the value of whole feet from the original radius, multiplying the fraction of foot by 12 to get the equivalent inches, and then rounding the result to the nearest integer using the round( ) method.
Note how we output the result. We did encounter the System.out.println in our “Hello World” application, but we didn’t explain exactly how it works, so let’s do it now. System is the name of a standard class that contains variables and methods for supporting simple keyboard input and character output to the display. It is contained in the package java.lang so it is always accessible just by using the simple class name, System.
The object out represents the standard output stream – your display screen, and is data member of the class System. This member is referenced by using the class name System separated from the member name out by a period – System.out . The bit at the rightmost end, println(…. ), calls the println( ) method of the object out. This method outputs the text string that appears between the parentheses to your display.
You might be wondering what is the + operator doing here, it’s not arithmetic we are doing, is it? No, but the plus has a special effect when used with character strings: it joins them together (concatenation). Neither circleArea, feet or inches are of type String, but they are all converted to a character string to be compatible with “The radius of a circle with area “.
Exercise 3 – 6 Using math functions
Create an application that gets a random number between 0 and 1000, finds the square root of the number, the log of the number, the sin, cos, and tan of the number and prints them (with suitable text) to the screen. To generate the random number, use the Random class in the java.util package. If your not sure of how these are generated, look up this class and it’s methods in the online documentation.
Monday, 20 August 2007
Operators and Assignments (Part 13)
Mathematical Functions
Sooner or later you are likely to need mathematical functions in your programs, even if it’s only obtaining an absolute value or calculating a square root. Java provides a range of methods that support such functions as part of the standard library stored in the package java.lang, all these are available in your program automatically.
The methods that support various additional mathematical functions are implemented in the class Math, so to reference a particular method you need to write Math and a period in front of the method name. For example, to use sqrt( ) which calculates the square root of whatever you place between parentheses, you should write Math.sqrt(aNumber).
The class Math includes a range of numerical functions. Some of the important ones are listed below in the table:
Method Function
abs(arg) Calculates absolute value of the argument
max(arg1, arg2) Returns the larger of the two argument
min(arg1, arg2) Returns the smaller of the two arguments
ceil(arg) Returns the smallest integer that is greater than or equal to the argument
floor(arg) Returns the largest integer that is less than or equal to the argument
round(arg) Calculates the nearest integer to the argument value
The mathematical functions available in the class Math are:
Method Function
sqrt(arg) Calculates the square root of the argument
pow(arg1, arg2) Calculates the first argument raised to the power of the second argument
exp(arg) Calculates e raised to the power of the argument
log(arg) Calculates the natural logarithm (base e) of the argument
random( ) Returns a pseudorandom number between 0.0 and 1.0
The Math class also defines double values for e and pi, which you can access as Math.E and Math.PI respectively. To find out more about the Math class and the methods it provides please refer to Java Documentation.
Sooner or later you are likely to need mathematical functions in your programs, even if it’s only obtaining an absolute value or calculating a square root. Java provides a range of methods that support such functions as part of the standard library stored in the package java.lang, all these are available in your program automatically.
The methods that support various additional mathematical functions are implemented in the class Math, so to reference a particular method you need to write Math and a period in front of the method name. For example, to use sqrt( ) which calculates the square root of whatever you place between parentheses, you should write Math.sqrt(aNumber).
The class Math includes a range of numerical functions. Some of the important ones are listed below in the table:
Method Function
abs(arg) Calculates absolute value of the argument
max(arg1, arg2) Returns the larger of the two argument
min(arg1, arg2) Returns the smaller of the two arguments
ceil(arg) Returns the smallest integer that is greater than or equal to the argument
floor(arg) Returns the largest integer that is less than or equal to the argument
round(arg) Calculates the nearest integer to the argument value
The mathematical functions available in the class Math are:
Method Function
sqrt(arg) Calculates the square root of the argument
pow(arg1, arg2) Calculates the first argument raised to the power of the second argument
exp(arg) Calculates e raised to the power of the argument
log(arg) Calculates the natural logarithm (base e) of the argument
random( ) Returns a pseudorandom number between 0.0 and 1.0
The Math class also defines double values for e and pi, which you can access as Math.E and Math.PI respectively. To find out more about the Math class and the methods it provides please refer to Java Documentation.
Subscribe to:
Comments (Atom)