Skip to content

Using Primitive data types

Estimated time to read: 3 minutes

IDE Workspace

Setup a workspace within the IDE that you wish to use, in this instance, I am using VSC (Visual Studio Code). VSC has plugin support for Java which enables you to create Java projects from the main UI.

Bootstrap

Bootstrap a file with the following content:

package io.entityfour.exercises;

/**
 * @author developer
 *
 */

public class Lab2 {

 public static void main (String[] args) {
  System.out.println("Hello World!");
 }
}

Save the file in the corresponding location ./src/io/entityfour/exercises/Lab2.java

This creates class 'Lab2' within the 'exercises' package.

Exercise - Define primitive data types

At the beginning of the main() method, declare the following variables with the specified characteristics:

  • An int with the name width and no initial value.
  • An int with the name height and no initial value.
  • An int with the name area and no initial value.
  • A double with the name radius and an initial value of 10.0.
  • A double with the name pi and an initial value of 3.14.
  • A boolean with the name result and an initial value of true.

In the body of the main() method, perform the following variable assignments:

  • Assign a value of 8 to width.
  • Assign a value of 12 to height.
  • Assign a value of 96 to area.

- Near the bottom of the main() method, print the values of each variable using System.out.println().

Example

public class Lab2 {

 public static void main (String[] args) {
  int width, height, area; //This statement defines the three variables
  double radius = 10.0;
  double pi = 3.14;
  boolean result = true;

  width = 8;
  height = 12;
  area = 96;

  System.out.println("The value of width is " + width);
  System.out.println("The value of height is " + height);
  System.out.println("The value of area is " + area);
  System.out.println("The value of radius is " + radius);
  System.out.println("The value of pi is " + pi);
  System.out.println("The value of result is " + result);
 }
}

Exercise -Define Arrays

Near the top of your main() method, define the following arrays:

  • An array of 12 ints named daysInMonths.
  • An array of 12 String references named monthNames – initialize this array at the time it is declared with the names of the 12 months (refer to your course book for the syntax).

Write 12 lines of code to assign the number of days in each month to the daysInMonths[] array elements (do not worry about leap year!). See output in step 16 below for days in months.

Write a print statement for each month that will display the name of the month and the number of days it contains.

package io.entityfour.exercises;

/**
 * @author developer
 *
 */

public class Lab2_2 {

 public static void main (String[] args) {

 // Declare an int array named daysInMonths and initalise the length
 // int[] daysInMonths = {0,0,0,0,0,0,0,0,0,0,0,0}; 
 // or even better, use int[size_of_array]
 int [] daysInMonths = new int[12];

 String[] monthNames = {"January", "February", "March", "April", "May","June","July","August","September","October","November","December"}; // Declare an array with string values

 // Assign days in months to their relevant positions. This is silly, why not just init these when it is declared 
 daysInMonths[0] = 31;
 daysInMonths[1] = 28;
 daysInMonths[2] = 31;
 daysInMonths[3] = 30;
 daysInMonths[4] = 31;
 daysInMonths[5] = 30;
 daysInMonths[6] = 31;
 daysInMonths[7] = 31;
 daysInMonths[8] = 30;
 daysInMonths[9] = 31;
 daysInMonths[10] = 30;
 daysInMonths[11] = 31;

 // Print the month and number of days in a line
 System.out.println( monthNames[0] + " has " + daysInMonths[0] + " days.");
 System.out.println( monthNames[1] + " has " + daysInMonths[1] + " days.");
 System.out.println( monthNames[2] + " has " + daysInMonths[2] + " days.");
 System.out.println( monthNames[3] + " has " + daysInMonths[3] + " days.");
 System.out.println( monthNames[4] + " has " + daysInMonths[4] + " days.");
 System.out.println( monthNames[5] + " has " + daysInMonths[5] + " days.");
 System.out.println( monthNames[6] + " has " + daysInMonths[6] + " days.");
 System.out.println( monthNames[7] + " has " + daysInMonths[7] + " days.");
 System.out.println( monthNames[8] + " has " + daysInMonths[8] + " days.");
 System.out.println( monthNames[9] + " has " + daysInMonths[9] + " days.");
 System.out.println( monthNames[10] + " has " + daysInMonths[10] + " days.");
 System.out.println( monthNames[11] + " has " + daysInMonths[11] + " days.");

 }
}

w3schools Java Arrays reference guide