Variables
Objective:
In this practice assignment, you will create a Java program. Instructions and requirements are given below. This assignment will test your knowledge of Java variables.
Instructions:
Create a Java project in Eclipse called CoreJavaBasics. In the src folder, make a package and name it
com.perscholas.java_basics
. Create a class with amain( )
method to run the following exercises. (You may name the class whatever you like. Examples: JavaBasicsClass, HomeClass, IndexClass, PracticeClass, etc.).Write a program that declares two integer variables, assigns an integer to each, and adds them together. Assign the sum to a variable. Print out the result.
Write a program that declares two double variables, assigns a number to each, and adds them together. Assign the sum to a variable. Print out the result.
Write a program that declares an integer variable and a double variable, assigns numbers to each, and adds them together. Assign the sum to a variable. Print out the result. What variable type must the sum be?
Write a program that declares two integer variables, assigns an integer to each, and divides the larger number by the smaller number. Assign the result to a variable. Print out the result. Now change the larger number to a decimal. What happens? What corrections are needed?
Write a program that declares two double variables, assigns a number to each, and divides the larger by the smaller number. Assign the result to a variable. Print out the result. Now, cast the result to an integer. Print out the result again.
Write a program that declares two integer variables, x and y, and assigns the number 5 to x and the number 6 to y. Declare a variable q and assign y/x to it and print q. Now, cast y to a double and assign it to q. Print q again.
Write a program that declares a named constant and uses it in a calculation. Print the result.
Write a program where you create three variables that represent products at a cafe. The products could be beverages like coffee, cappuccino, espresso, green tea, etc. Assign prices to each product. Create two more variables called subtotal and totalSale and complete an “order” for three items of the first product, four items of the second product, and two items of the third product. Add them all together to calculate the subtotal. Create a constant called SALES_TAX and add sales tax to the subtotal to obtain the totalSale amount. Be sure to format the results to two decimal places.
Last updated