askIT

Data types

Lagre Download example files

We can choose between different data types when we create variables. We mostly use the data types String, double and int. The data type long is used when operating data bases.

Under creates we reach some variables with different information. These should represent a county. We want county number, name, population and land area.

oslo.jpg
Oslo, Norway

int number = 1;
String name = "Oslo";
int population = 658390;
double area = 454.09;

We can print the contents of the variables with System.out.println.

package datatyper;
public class Datatyper {
        public static void main(String[] args) {
                // Variables for county
                int number = 1;
                String name = "Oslo";
                int population = 658390;
                double area = 454.09;
               
                // Print data
                System.out.println(name + " is county number " + number);
                System.out.println("The population is " + population);
                System.out.println("The area is " + area);
        }       
}

Output:
Oslo is county number 1
The population is 658390
The area is 454.09

Exercises

1. Extend the program above to include the number of municipalities, the mayor and political party.

2. Create a corresponding application for land. It must contain the name, population and area.

3. Use the program you created in exercise 2. Expand it so it writes the population per km2 for Norway, Sweden and Iceland. Who has the greatest population density?
Hint: The population density is calculated by dividing the total population on the total land area.
Can you get the program to do this for you?

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.