Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Introduction to Java What is Java and why do I need it? Java is a programming language and computing platform first released by Sun Microsystems in 1995. Java is fast, secure, and reliable. From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere! It is considered architecturally neutral, meaning that it can run on any platform or operating system. Where to Acquire Java Copy of Java can be obtained from www.java.com There are many different versions of Java. We will primarily be working with an IDE (Integrated Development Environment) called Ready to Program. Ready to Program is a very streamlined, easy to use IDE, great for beginners. It does, however, use an older version of Java. It allows for all of the fundamental components of the language, including Object Oriented Programming (OOP), and is sufficient for everything you will need to learn here. Other, more sophisticated IDE’s, such as Eclipse, are also available. These IDE’s will contain a newer version of Java, however, the interface might be overwhelming for beginners. You can even use a simple editor, like NotePad, to write Java programs. Your First Java Program Date: _____________ // The “HappyBirthday” class. public class HappyBirthday { public static void main (String[ ] args) { System.out.println(“Happy Birthday!”); // Display message } // main method } // HappyBirthday class What does it all mean? Line 1 - // The “HappyBirthday” class. Comment line Comments begin with // ; Any commented line is ignored by the computer Longer or block comments are enclosed with /* and */ Line 2 - public class HappyBirthday Defines the class name *starts with a capital, no spaces Class name must agree with the file name; in this case, we save the code as: HappyBirthday.java **Case sensitive! Line 3-{ Opening brace Defines the start of the class Class is closed in line 8 with a closing brace and a comment: } // HappyBirthday class Lines 4 to 7 Method defined inside the class braces Our example shows just one method Line 4 Main method starts with line: public static void main (String[ ] args) o This is a required method that starts the program o All code within the {} of this method is executed by the computer Additional Points All Java programming statements end with a semicolon The contents of all classes are contained within opening and closing braces A series of characters in double quotes is called a literal string All Java applications must have a method named main() The statement System.out.print() will display output data and remain on the same line. The statement System.out.println() will display a line of output and advance to the next line. Using Keyboard Input Date: _____________ Let’s examine the “HappyBirthday” program one more time. Wouldn’t it be better to display the entire song for a specific person? Maybe the name of the birthday girl/boy can be entered through the keyboard and then used in the song. To do this: 1. Make available the Java input/output routines. Done through import statements that happen before the class is defined For keyboard input, the following statement must be added: import java.io.*; 2. Modify the main method to handle keyboard errors. Using keyboard or disk access means there is a possibility that an error could occur (called an exception) The statement: throws IOException must be added to the definition line of the main method 3. Define the input stream reader which allows information to come from the keyboard. Input and output is handled in streams of characters To create an input stream reader object: BufferedReader keyboardInput=new BufferedReader (new InputStreamReader (System.in)); - object is named keyboardInput for “the data entered in on the keyboard” 4. Create an object or a variable to hold the information that comes in from the keyboard. String birthdayName; //This creates a storage box called //birthdayName that can be used to hold the typed information 5. Now use the keyboard input command First prompt the user to enter a name using a print or println command Then, get the input System.out.print (“Please enter your name: ”); birthdayName = keyboardInput.readLine (); birthdayName will be assigned whatever the user enters on the keyboard. The entire program is shown below: import java.io.*; // The “Happy Birthday” class. public class HappyBirthday { public static void main (String[ ] args) throws IOException { BufferedReader keyboardInput=new BufferedReader (new InputStreamReader (System.in)); String birthdayName; //Ask user for name System.out.print (“Please enter your name: ”); birthdayName = keyboardInput.readLine (); //Display the birthday song with the name System.out.println(“Happy Birthday to you”); System.out.println(“Happy Birthday to you”); System.out.println(“Happy Birthday dear ” + birthdayName); System.out.println(“Happy Birthday to you”); } // main method } // HappyBirthday class Asst. 1