Programming

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 Farenheit = 0;
        double Celsius = 0;
        for(int i=0; i<MAX_LOOP; i++){
        System.out.print("\nEnter a temperature in Fahrenheit: ");
            if(scanFaren.hasNextDouble())
                {
                    Farenheit=scanFaren.nextDouble();
                    Celsius = ( Farenheit- 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(Farenheit > HIGH_TEMP_F_WARNING){
                System.out.print("Remember to hydrate\n");}
            // Check for low temperature and issue a warning if necessary
            if(Farenheit < 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 “Farenheit” to the static (fixed) variable “HIGH_TEMP_F_WARNING”. If the statement is true, the statements inside the “{“ and “}” are executed.

            If(Farenheit > 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.”

Comments about “Java”

  1. Canine505 says:

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

  2. Your mom says:

    AHHHHHH SO FRUSTRATING

  3. thatwaseasy says:

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

  4. mr chubbs says:

    i have no idea how to use this!

  5. james says:

    this is cool

  6. lol says:

    java ia awesome i use it alot its very confizing though

Write a comment about “Java”

Nickname:

Type your comment:

  • 1. Safety. Do the following:
      a. Show your counselor your current, up-to-date Cyber Chip.
      b. Discuss first aid and prevention for the types of injuries or illnesses that could occur during programming activities, including repetitive stress injuries and eyestrain.

    2. History. Do the following:
      a. Give a brief history of programming, including at least three milestones related to the advancement or development of programming.
      b. Describe the evolution of programming methods and how they have improved over time.

    3. General knowledge. Do the following:
      a. Create a list of 10 popular programming languages in use today and describe which industry or industries they are primarily used in and why.
      b. Describe three different programmed devices you rely on every day.

    4. Intellectual property. Do the following:
      a. Explain how software patents and copyrights protect a programmer.
      b. Describe the difference between licensing and owning software.
      c. Describe the differences between freeware, open source, and commercial software, and why it is important to respect the terms of use of each.

    5. Projects. Do the following:
      a. With your counselor’s approval, choose a sample program. Then, as a minimum, modify the code or add a function or subprogram to it. Debug and demonstrate the modified program to your counselor.
      b. With your counselor’s approval, choose a second programming language and development environment, different from those used for requirement 5a and in a different industry from 5a. Then write, debug, and demonstrate a functioning program to your counselor, using that language and environment.
      c. With your counselor’s approval, choose a third programming language and development environment, different from those used for requirements 5a and 5b and in a different industry from 5a or 5b. Then write, debug, and demonstrate a functioning program to your counselor, using that language and environment.
      d. Explain how the programs you wrote for requirements 5a, 5b, and 5c process inputs, how they make decisions based on those inputs, and how they provide outputs based on the decision making.

    6. Careers. Find out about three career opportunities in programming. Pick one and find out the education, training, and experience required. Discuss this with your counselor and explain why this career might be of interest to you.

  • Earning the Cyber Chip can help you learn how to stay safe while you are online and using social networks or the latest electronic gadgets.

    Topics include cell phone use, texting, blogging, gaming, cyberbullying, and identity theft. Find out more about the Cyber Chip at www.scouting.org/cyberchip.