Recent Comments

Java

Java is a general purpose programming language in wide use.

Download software: Oracle Corp.

Cost: Free

Instructions to setup software: Download software for your platform from the “JDK” tab. You need to download the JSDK. If you just have the Java runtime (JRE), you will be able to run programs, but you will not be able to compile them.

Many of the Integrated Development Environments (IDE) contain Java and provide a framework for creating, debugging and testing programs.

Hardware requirements: PC, Mac

Operating systems supported: Win, Mac, Linux

Difficulty level: 4 out of 5


TRY IT!

1. Open a file named FahrenheitToCelsius.java” in your favorite editor.

2. Copy this Java code into your file:


// Example F to C in Java
// This file must be named FahrenheitToCelsius.java 
import java.util.Scanner;
public class FahrenheitToCelsius {
    public static final  double  LOW_TEMP_F_WARNING=0.;
    public static final  double  HIGH_TEMP_F_WARNING=100.;
    public static final  int     MAX_LOOP=5;
    public static void main(String[] args) {
        Scanner scanFaren = new Scanner(System.in);
        double Fahrenheit = 0;
        double Celsius = 0;
        for(int i=0; i<MAX_LOOP; i++){
        System.out.print("\nEnter a temperature in Fahrenheit: ");
            if(scanFaren.hasNextDouble())
                {
                    Fahrenheit=scanFaren.nextDouble();
                    Celsius = ( Fahrenheit- 32.)*5./9.;
                }else{
                System.out.println("Data entry error - try again\n");
                System.exit(-1); }
            System.out.println("The temperature in Celsius is: "+Celsius);
      
            // Check for high temperature and issue a warning if necessary
            if(Fahrenheit > HIGH_TEMP_F_WARNING){
                System.out.print("Remember to hydrate\n");}
            // Check for low temperature and issue a warning if necessary
            if(Fahrenheit < LOW_TEMP_F_WARNING ){
                System.out.print("Remember to pack Long underwear\n");}
        }
        System.exit(-1);
    }
}

3. Save the File

4. Compile the program by typing “javac FahrenheitToCelsius”

5. Run the program by typing “java FahrenheitToCelsius”


ABOUT THE PROGRAM — A WALK THROUGH

1. This program:

a. Prompts the user for the input temperature to convert.

b. Reads the temperature and converts it to Celsius.

c. Checks to see if the temperature is above 100 degrees F, and prints “Remember to hydrate” if that is true.

d. Prints “Remember to pack long underwear” if the temperature is below 32 degrees.

2. Java programs are organized into units called “classes”. A class has data and methods that operate on that data. Here is the declaration for the class “FarenheitToCelsius” in the example program.


public class FahrenheitToCelsius {
: : :
}

3. For a Java program to run, it must have a “main” method declaration. The line below shows a template for a main method declaration.


public static void main (String [] args) {
: : :
}

4. A variable to hold a value is declared by “double Farenheit = 0;”, and it is set to zero.

The statement “public static final double LOW_TEMP_F_WARNING=0.;” declares a variable that is fixed and cannot be changed

5. The following line is statement that prints output to the screen.

System.out.print("Remember to pack Long underwear\n");

6. An “if” statement compares the variable “Fahrenheit” to the static (fixed) variable “HIGH_TEMP_F_WARNING”. If the statement is true, the statements inside the “{“ and “}” are executed.

            If(Fahrenheit > HIGH_TEMP_F_WARNING){
                : : :
            }

7. A loop in Java is done with a “for” statement

        for(int i=0; i<MAX_LOOP; i++){
}

The declaration “Scanner scanFaren = new Scanner(System.in);” is a mechanism used to read input from the user. “Scanner” is not part of the standard Java libraries and functions. It is defined through an “import” statement, import java.util.Scanner;

This program does not use a powerful feature of Java called inheritance. Use of inheritance is left for more advanced examples.


TRY THIS

1. Change the temperatures used in the decisions –- change the lower temperature from 60 to 30 degrees, for example. Make sure you change it in two places! Save the file and refresh the browser (or restart the web page), and enter new numbers -– did the answers change at the new temperature?

2. Create a new temperature range from 30 to 60 degrees and have it display – “Bring hat and gloves!”

3. Change the wording of the phrases.

4. Add another text input -– ask for the wind speed, for example.

5. Add some conditional statements that evaluate the wind chill factor.

6. Add some text to display the wind chill result.


LEARN MORE

1. There are a number of tutorials at http://docs.oracle.com/javase/tutorial/
2. And http://www.roseindia.net/java/


RESOURCES, TIPS, TRICKS AND HINTS

There are many tutorials on Java. You can Google for “Java tutorials” or “Getting started in Java.”

19 Comments on Java

  1. 39 buried 0 found

  2. Syntax is what programmers pay when they have been misbehaving.

  3. there is just too much syntax, otherwise, it is very simple.

  4. ?

  5. the one and only d o double g // April 22, 2018 at 1:01 pm // Reply

    I took a class on this once…Do you want me to send anybody necessary files??

  6. Where do I find a java editor?

  7. Yes, you can make Mods with it, but I like making server plugins.

  8. I still don’t get it

  9. confusing

  10. The_amazing_owen // April 11, 2015 at 2:36 pm // Reply

    Sadly, my Minecraft mods on forge all crashed.

  11. i use java to mod Minecraft yet it makes no sense here!!!

  12. I Luv Planes // January 19, 2015 at 8:49 am // Reply

    Cool

  13. Love Java!!!!!!
    It can be very confusing though, like many object oriented languages.

  14. AHHHHHH SO FRUSTRATING

  15. thatwaseasy // May 8, 2014 at 3:48 pm // Reply

    this is great now that I know how to use it I can make mods for my games

  16. i have no idea how to use this!

  17. this is cool

  18. java ia awesome i use it alot its very confizing though

Leave a Reply to Lost Cancel reply

Please don't use your real name.