Java Training
Java Training
Review the end of Lecture # 1 and attempt to compile and run the code for both the application and the
applet presented. Do not worry about the nature of the code yet; you will understand it soon enough!
If the error “ 'javac' is not recognized as an internal or external command operable program or batch file ”
appears on the screen upon compilation, then most likely your path has not been set to handle Java-related
commands. In order to solve this problem, do the following:
The path tells Windows where to look for valid commands. By setting a new path to the Java bin folder,
we can now use all Java-related commands as specified by the Software Development Kit (SDK).
Exercise 1:
Exercise 2:
Enter or change the following code according to the comments in the given order.
class CreateNum {
public static void main(String[] args) {
int numberOne // There is something wrong with this statement. Fix it.
// Set the two previous variables equal to the numbers 1 and 2, respectively.
// Now, both declare the variable, numberFour, as a short and in the same statement
assign the variable the value of 4.
1
Exercise 3:
1. What do the following expressions evaluate to? Specify values and types of evaluation
result.
a) 3 / 2;
b) 3 / 2.0;
c) 3 / 2.0F;
2. Write the results of the following expressions.
1 % 3;
7 % 3;
3. What are the values of i and d after the following piece of code is executed?
int i = 0, d;
d = i++;
Exercise 4:
Using what you learned from Java so far, fill in the following class to calculate the percentage of degree
students versus the total number of students in AAU. The following information is given:
# of degree students is 33
total # of students is 220
At the end, your program should print out the percentage that you calculated.
class DegreeStudents {
public static void main(String[] args) {
Exercise 5:
Write a program that defines two floating point variables. The first variable will represent your annual
income which is one thousand two hundred and thirty six dollars and twenty cents. The second variable
will correspond to a tax rate of 35 percent. Calculate the amount of tax you must pay on your income and
store it in a variable called taxTotal.
Your program should output 432.67.
class taxes {
public static void main (String args[]) {
System.out.println("The taxes I must pay for this year are " + taxTotal);
}
}
2
Exercise 6:
A: Write a program that initializes two numbers (any numbers), multiplies them, and prints out the result
on the screen.
B: On the same program, initialize a variable to store the number 37. This number is the normal
temperature of a person in Celsius. Augment your original code to convert the 37 degrees Celsius to
Fahrenheit (formula for conversion to Fahrenheit (9/5)*Celsius + 32). Print the result on the console.
C: On the same program, augment a piece of code that contains variables that hold the ages of the siblings
in your family. Find the average of their ages and print them on the screen
// Write code to convert 37 degrees Celsius to Fahrenheit and prints the answer.
// Of course you have to initialize the relevant variables.
// Write a program that sums the ages of your siblings and finds the average.
// Of course you have to initialize the variables that will hold the ages and any other variables
// that you think you will need.
}
}