Laboratory 1 Prepared by: Dr. Constandinos X. Mavromoustakis Date: 2011 1 University of Nicosia, Cyprus COMP 212- Object Oriented Programming Fall 2011 Title and objective of the Laboratory: Creating your first Java project and your first program Laboratory 1 Prepared by: Dr. Constandinos X. Mavromoustakis Date: 2011 2 Creating your first Java project and your first program The following will describe how to create a minimal Java program using Eclipse. It will be the classical "Hello World" program. Our program will write "Hello Eclipse!" to the console. Create project Select from the menu File -> New-> Java project. Maintain a sample name "de.vogella.eclipse.ide.first" as the project name. Select "Create separate source and output folders". Laboratory 1 Prepared by: Dr. Constandinos X. Mavromoustakis Date: 2011 3 Press finish to create the project. A new project is created and displayed as a folder. You can now open the folder "de.vogella.eclipse.ide.first" Create package Create now a package. A good convention is to use the same name for the top package as the project. Create therefore the package "de.vogella.eclipse.ide.first". Laboratory 1 Prepared by: Dr. Constandinos X. Mavromoustakis Date: 2011 4 Select the folder src, right mouse click on it and select New -> Package. Create Java class Right click on your package and select New -> Class Create MyFirstClass, select the flag "public static void main (String[] args)" first.program/src first.program Laboratory 1 Prepared by: Dr. Constandinos X. Mavromoustakis Date: 2011 5 Write down now the following code: package first.program; public class MyFirstClass { public static void main(String[] args) { System.out.println("Hello Eclipse!"); } } first.program/src first.program Laboratory 1 Prepared by: Dr. Constandinos X. Mavromoustakis Date: 2011 6 Run your project in Eclipse Now run your code. Right click on your Java class and select Run-as-> Java application Done! You should see the output in the console of eclipse. first.program Laboratory 1 Prepared by: Dr. Constandinos X. Mavromoustakis Date: 2011 7 Laboratory 1 Prepared by: Dr. Constandinos X. Mavromoustakis Date: 2011 8 Multiple choice exercise Given: 2. public class Choosy { 3. public static void main(String[] args) { 4. String result = ""; 5. int x = 7, y = 8; 6. if(x == 3) { result += "1"; } 7. else if (x > 9) { result += "2"; } 8. else if (y < 9) { result += "3"; } 9. else if (x == 7) { result += "4"; } 10. else { result += "5"; } 11. System.out.println(result); 12. } 13. } What is the result? (Choose all that apply.) (circle the correct answer) A. 3 B. 34 C. 35 D. 345 E. Compilation fails due to an error on line 5. F. Compilation fails due to errors on lines 8 and 9. Laboratory 1 Prepared by: Dr. Constandinos X. Mavromoustakis Date: 2011 9 G. Compilation fails due to errors on lines 7, 8, and 9. Another Programming exercise Lab #1 File name: GalToLit.java Converting Gallons to Liters Although the preceding sample programs illustrate several important features of the Java language, they are not very useful. Even though you do not know much about Java at this point, you can still put what you have learned to work to create a practical program. In this project, we will create a program that converts gallons to liters. The program will work by declaring two double variables. One will hold the number of the gallons, and the second will hold the number of liters after the conversion. There are 3.7854 liters in a gallon. Thus, to convert gallons to liters, the gallon value is multiplied by 3.7854. The program displays both the number of gallons and the equivalent number of liters. Step by Step 1. Create a new file called GalToLit.java. 2. Enter the following program into the file: /* Project 1-1 This program converts gallons to liters. Call this program GalToLit.java. */ class GalToLit { public static void main(String args[]) { double gallons; // holds the number of gallons double liters; // holds conversion to liters Laboratory 1 Prepared by: Dr. Constandinos X. Mavromoustakis Date: 2011 10 gallons = 10; // start with 10 gallons liters = gallons * 3.7854; // convert to liters System.out.println(gallons + " gallons is " + liters + " liters."); } } 3. Compile the program using the following command line: C>javac GalToLit.java 4. Run the program using this command: C>java GalToLit You will see this output: 10.0 gallons is 37.854 liters. 5. As it stands, this program converts 10 gallons to liters. However, by changing the value assigned to gallons, you can have the program convert a different number of gallons into its equivalent number of liters. Another Programming exercise Lab #1 File name: GalToLitTable.java Improving the Gallons-to-Liters Converter You can use the for loop, the if statement, and code blocks to create an improved version of the gallons-to-liters converter that you developed in the first project. This new version will print a table of conversions, beginning with 1 gallon and ending at 100 gallons. After every 10 gallons, a blank line will be output. This is accomplished through the use of a Laboratory 1 Prepared by: Dr. Constandinos X. Mavromoustakis Date: 2011 11 variable called counter that counts the number of lines that have been output. Pay special attention to its use. Step by Step 1. Create a new file called GalToLitTable.java. 2. Enter the following program into the file. /* This program displays a conversion table of gallons to liters. Call this program "GalToLitTable.java". */ class GalToLitTable { public static void main(String args[]) { double gallons, liters; int counter; counter = 0; for(gallons = 1; gallons <= 100; gallons++) { liters = gallons * 3.7854; // convert to liters System.out.println(gallons + " gallons is " + liters + " liters."); counter++; // every 10th line, print a blank line if(counter == 10) { System.out.println(); counter = 0; // reset the line counter } } } Laboratory 1 Prepared by: Dr. Constandinos X. Mavromoustakis Date: 2011 12 } 3. Compile the program using the following command line: C>javac GalToLitTable.java 4. Run the program using this command: C>java GalToLitTable Here is a portion of the output that you will see: 1.0 gallons is 3.7854 liters. 2.0 gallons is 7.5708 liters. 3.0 gallons is 11.356200000000001 liters. 4.0 gallons is 15.1416 liters. 5.0 gallons is 18.927 liters. 6.0 gallons is 22.712400000000002 liters. 7.0 gallons is 26.4978 liters. 8.0 gallons is 30.2832 liters. 9.0 gallons is 34.0686 liters. 10.0 gallons is 37.854 liters. 11.0 gallons is 41.6394 liters. 12.0 gallons is 45.424800000000005 liters. 13.0 gallons is 49.2102 liters. 14.0 gallons is 52.9956 liters. 15.0 gallons is 56.781 liters. 16.0 gallons is 60.5664 liters. 17.0 gallons is 64.3518 liters. 18.0 gallons is 68.1372 liters. Laboratory 1 Prepared by: Dr. Constandinos X. Mavromoustakis Date: 2011 13 19.0 gallons is 71.9226 liters. 20.0 gallons is 75.708 liters. 21.0 gallons is 79.49340000000001 liters. 22.0 gallons is 83.2788 liters. 23.0 gallons is 87.0642 liters. 24.0 gallons is 90.84960000000001 liters. 25.0 gallons is 94.635 liters. 26.0 gallons is 98.4204 liters. 27.0 gallons is 102.2058 liters. 28.0 gallons is 105.9912 liters. 29.0 gallons is 109.7766 liters. 30.0 gallons is 113.562 liters. Another multiple choice exercise Which of the following variable names is invalid? A. count B. $count C. count27 D. 67count Write a Java program where you prove each one of the above variables’ validity. Next lab session: Read from Keyboard and user- inputer and interaction