Thursday 11 August 2011

Java: Simple Programs

Okay, now we have an IDE, let's write a program with the knowledge we have.

For our first example, let's have the program ask for the dimensions of a cuboid, and then give a message displaying its volume.



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

//The above "imports" tell the program what external files are being used. Your IDE should
//automatically let you know what you need up there if you use a common function from one.

public class Dimensions {
//The name of this class should be the name of the file you are working with

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static String input = null;
    public static String getInputStr() throws IOException {
    input = br .readLine();
    return input;
    }

//The above block of code was what we used for the input. Go back a couple of posts to refresh

    public static void main(String[] argv) throws IOException{
    //this is the main function that's called when the program is run. It must always be
    //public static void main and take an array of Strings as the argument. the "throws IOException"
    //is only there because of the input function above also requiring it.


    int height;
    int width;
    int depth;
    int volume;


    System.out.println( "Please enter the height of the cubeoid " );
    height = Integer.parseInt(  getInputStr() );   //Integer.parseInt is required, since input given is 
                                                                             //in string format and cannot simply be read as an int
    System.out.println( "Please enter the width of the cuboid: " );
    width = Integer.parseInt ( getInputStr() );


    System.out.println( "Please enter the depth of the cuboid: " );
    depth = Integer.parseInt( getInputStr() );


    volume = height * width * depth;                    //This simply calculates the volume. The entirety of
                                                                             //the reason for this program is in 1 line, everything
                                                                             //else is data collection and presentation. This is
                                                                             //more or less typical.


    System.out.println( "The volume of the cuboid is: " + volume );


    }

For our final example, let's try a program that prints out the Nth Fibonacci number.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//required imports

public class Fibonacci { //Our main class

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static String input = null;
    public static String getInputStr() throws IOException {
    input = br .readLine();
    return input;
    }  //Input functionality

    public static void main(String[] argv) throws IOException { //Main function

        System.out.println( "Which number of the Fibonacci sequence do you want?" );

        int seqNum = Integer.parseInt( getInputStr() );
        int currVal = 1;
        int prevVal = 0;

        for (int i = 1; i < seqNum ; i++ ) {   //This for loop means "For every integer, i, starting from 1, up until
                                                              //before seqNum (incrementing by 1), perform the following :
           currVal += prevVal;                    //Current value in the sequence is the previous 2 values, so
                                                              //current value (before change) + previous value (before change)
                                                              //gives the 2 sequential values before the value we want to store into        
                                                              //current value
           prevVal = currVal - prevVal;       //New current value - old previous value = new previous value
        }

        System.out.println(currVal);            //And bam, answer.

    }
}



No comments:

Post a Comment