Homework Set02

Review

  1. Primitive numeric data types:
    1. integer data types: byte, short, int, long
    2. floating-point data types: double, float
  2. Variables: define variable of different data types
  3. Assignment statement; arithmetic expression evaluation
  4. Implicit conversion/explicit conversion of numbers among different data types
  5. Java class: Scanner – Get input from console

Exercise

  1. Evaluate the following expressions:
    1. (int)(34.567 * 100) / 100
    2. (int)(34.567 * 100) / 100.0
    3. (int)(34.567 * 100 / 100)
    4. (int)(34.567) * 100 / 100.0
  2. Define variables in main() method:
    1. doublevariable var1 and initialize it with the value 1.0567
    2. float variable var2 andinitialize it with the value -1.0582
    3. int variable var3 andinitialize it with the value -10000
    4. short variable var4 andinitialize it with the value 1118
    5. constant byte variable ONE_HUNDRED and initialize it with value 100
  3. Write a program and complete the following operations:
  4. (1) Define a double variable y0, initialize it with value 90
  1. Define a float variable y1, initialize it with value 0
  2. Assign y1 with value in y0
  3. Display y0 and y1
  1. (1) Define an int variable x0, initialize it with value 99
  1. Define a short variable x1, initialize it with value 0
  2. Change x1 so x1 will hold a number which is the half of the number in x0
  3. Display x0 and x1
  1. Write a program:
    1. Define 2 int variables vx & vy. Use Scanner object to read from console and assign input values to them respectively.
    2. Display the values in console: “vx=a-value; vy=a-value”
    3. Swap the values saved in vx and vy, display vx and vy again
  1. Given a floating-point number, extract its integer part and fractional part, and display. For example, given number 123.456, the integer part and fractional part are: 123 and 0.456
  2. For the following function, write Java code to display results:

y = ax2 + bx + c

Enter 3 floating-point numbers and assign them to coefficients a, b, c; then display the following table (? will be replaced by real numbers)

  1. -5, y=?
  2. -3, y=?
  3. -1, y=?
  4. 0, y=?
  5. 1, y=?
  6. 3, y=?
  7. 5, y=?
  1. Write program:
  2. Given 3 digits x, y, and z, generate an integer number which is 100 * x + 10 * y + z (for example, if x, y, z are 1, 2, 3, the number will be 123). Display this number
  3. Given 3 digits m, n, and k, generate a float-pointing number which is m*10 + n + k*10-1 (for example: if m=1, n=2, k=3, then the number will be 12.3). Display this number
  4. Given 2 double variables r1 and r2 which are the radius of 2 circles. Write a program to calculate and display:
  5. the total area of these 2 circles
  6. the total circumference
  7. Given the circumference of a circle, write a program to calculate the area of this circle.
  8. A square circumscribes a circle as is shown in below. Write a program to calculate the area that’s blue colored in the picture. The radius of the circle is input from console.
A picture containing clock

Description automatically generated