Advertisement

Tuesday, 1 August 2017

A little bit of Java


Java is a comprehensive, general purpose language. It has an awful lot of libraries available for it, some come built in when you get it, others you can download. One of the most important libraries, Swing, let's you build very sophisticated user interfaces.

So Java can be used to build desktop applications but it is also widely used on the web, too. The are several frameworks for building web applications. It is also the language for building Android applications.

All in all, it's a useful language to learn. 

Java is fully object-oriented (although I won't be demonstrating that, here) and inherits much of its syntax from C and C++.

Here's an example. It’s the same guessing game that I've used in my other "A little bit of..."  posts. It gives you a flavour of the language and illustrates some basic programming constructs.

import java.util.Scanner;
import java.util.Random;
public class guess {
// Start here
public static void main (String args[]) {
Random r = new Random();
int x = r.nextInt(9)+1;
for(int i = 0; i < 5; i++){
int answer = getGuess();
     
if (answer == x){
System.out.println("Correct!");
break;
}else{
System.out.println("Wrong!");
}
}
 
System.out.print("The answer was ");
System.out.println(x);
    }
// define a function to get a guess
static int getGuess(){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a guess (1 to 10): ");
int a = keyboard.nextInt();
return a;
}
}

At the beginning of the program we see a couple of lines that import libraries. They come with Java but they still need to be explicitly imported into the program. 

The rest of the program consists of a single class that had the same name as the file. A class is the basic object-oriented building block in Java. Even when we are not actually writing in an object-oriented style, as here, we still need to wrap our code in a class declaration.

Inside the class are two functions, main and getGuess. The first is where the program starts. I'm going to go through this quite quickly because this is not really a Java tutorial, just a presentation of a simple program so you can get a flavour of Java,

Look at the main function. The first thing we do is create an object of the class Random -  the object is called r. A Random object lets us retrieve random values (surprise, surprise!) and we do this by invoking its nextInt function. We pass the value 9 to it and it returns a value between 0 and 9. We actually want a value from 1 to 10, so we simply add 1 to the result.

Following this is a for loop that iterates 5 times (we get 5 chances to guess the right number)  and inside that loop we get the guess from the user by calling the getGuess function. This function gets a value from the keyboard using a Scanner object.

We then check if the guess is correct and print out and appropriate response.

That's about it. As I said this is by no means a Java tutorial and it does not show its powerful object-oriented features but it, hopefully, gives you a feel for the language.

No comments:

Post a Comment