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:

  1. 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 a main( ) method to run the following exercises. (You may name the class whatever you like. Examples: JavaBasicsClass, HomeClass, IndexClass, PracticeClass, etc.).

  2. 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.

  3. 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.

  4. 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?

  5. 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?

  6. 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.

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

  8. Write a program that declares a named constant and uses it in a calculation. Print the result.

  9. 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.

/**
 * 
 */
package com.perscholas.java_basics;

/**
 * @author yanzhiwang
 *
 */
public class JavaBasicsClass {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
        // Write a program that declares two integer variables, assigns an integer to each,
        int intVar1 = 100;
        int intVar2 = 200;
        // adds them together. Assign the sum to a variable. Print out the result
        int sum1 = intVar1 + intVar2;
        System.out.println(sum1);

        // declares two double variables, assigns a number to each
        double doubleVar1 = 100.1;
        double doubleVar2 = 200.2;
        // adds them together. Assign the sum to a variable. Print out the result
        double sum2 = doubleVar1 + doubleVar2;
        System.out.println(sum2);

        // declares an integer variable and a double variable, assigns numbers to each
        int intVar3 = 100;
        double doubleVar3 = 200.1;
        // adds them together. Assign the sum to a variable. Print out the result
        double sum3 = intVar3 + doubleVar3;
        System.out.println(sum3);

        // 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.
        int intVar4 = 100;
        int intVar5 = 200;
        double div1 = intVar5 / intVar4;
        System.out.println(div1);
        // Now change the larger number to a decimal. What happens?
        // The type is wrong
        // What corrections are needed
        // We need to cast it to double

        // Write a program that declares two double variables,
        // assigns a number to each
        double doubleVar4 = 100.1;
        double doubleVar5 = 200.1;
        // and divides the larger by the smaller number.
        // Assign the result to a variable. Print out the result.
        double div2 = doubleVar5 / doubleVar4;
        System.out.println(div2);
        // Now, cast the result to an integer. Print out the result again
        System.out.println((int) div2);

        // Write a program that declares two integer variables, x and y,
        // and assigns the number 5 to x and the number 6 to y.
        int x = 5;
        int y = 6;
        // Declare a variable q and assign y/x to it and print q.
        double q = y / x;
        System.out.println(q);
        // Now, cast y to a double and assign it to q. Print q again
        q = (double) y;
        System.out.println(q);

        // Write a program that declares a named constant and uses it in a calculation.
        // Print the result
        final int CON = 100;
        System.out.println(CON + 1);

        // 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.
        int coffee = 100;
        int cappuccino = 200;
        int espresso = 300;
        // 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.
        int subtotal = 3 * coffee + 4 * cappuccino + 2 * espresso;
        System.out.println(subtotal);
        // Create a constant called SALES_TAX and add sales tax to the subtotal to obtain the totalSale amount.
        final double SALES_TAX = 0.139;
        double totalSale = subtotal + subtotal * SALES_TAX;
        // Be sure to format the results to two decimal places
        System.out.println(String.format("%.2f", totalSale));

	}

}

Last updated