0% found this document useful (0 votes)
41 views

Java Training

This document contains exercises on Java programming concepts such as variables, data types, arithmetic operations, and basic input/output. The exercises include correcting code snippets involving variable declarations and assignments, evaluating expressions, writing programs to calculate percentages and taxes, and combining multiple calculations in one program. Instructions are provided for each exercise to guide the coding tasks.

Uploaded by

Delehuly
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Java Training

This document contains exercises on Java programming concepts such as variables, data types, arithmetic operations, and basic input/output. The exercises include correcting code snippets involving variable declarations and assignments, evaluating expressions, writing programs to calculate percentages and taxes, and combining multiple calculations in one program. Instructions are provided for each exercise to guide the coding tasks.

Uploaded by

Delehuly
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Warm-Up Exercise:

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:

1. Go to Start  Settings  Control Panel


2. Select the “System” icon and proceed to the “Advanced” tab.
3. Choose “Environment Variables…”
4. Select “Path” in “System Variables” and click “Edit…”
5. Add the following path to the current set of paths as typed: C:\j2sdk1.4.0_01\bin;
6. Open a new MS-DOS prompt and try to compile your code again.

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:

Correct the following statements:


1. byte population = 350;
2. short area = 527351;
3. float temperature = 25.98;
4. boolean test = 7;
5. char firstLetter = p;
6. int 2way = 89;
7. int player score = 8976543;

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.

// Declare a variable name, numberTwo, to be of type long.

// Set the two previous variables equal to the numbers 1 and 2, respectively.

numberThree = 3; // Fix what is wrong with this statement.

// Now, both declare the variable, numberFour, as a short and in the same statement
assign the variable the value of 4.

NumberOne = 5; // Fix the one thing wrong with this statement.

float floaterOne = 1.0; // Fix the problem with this statement.

System.out.println( numberOne + “ “ + numberTwo + “ “ + numberThree + “ “ +


numberFour + “ “ + floaterOne );
}
}

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

Hints: 1) Use the data type double for all variables


2) Use descriptive variable names

At the end, your program should print out the percentage that you calculated.

class DegreeStudents {
public static void main(String[] args) {

//Fill in your code here

System.out.println(“The percentage of degree students in AAU is “ + //Fill in the name of


your percentage variable here );
}
}

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[]) {

//Your variables go here

double taxTotal = ; //Your solution goes here

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

Below is the code structure;


class variables{
public static void main(String[] args){
//Initialize two integers and write code that multiplies the two numbers and prints the answer.

// 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.
}
}

You might also like