1
ECE 161 WEEK 4 Introduction to Programing in Java 26.10.05 Prof Dr. Ziya B. Güvenç Res. Asst. Barbaros Preveze Java creator and Software Development Kit J(2S)DK is used for compiling any portion of the code and it stands for Java Development ToolKit. First Run If this is the first time that you re running JCREATOR, then you have to make some preparations for the first time. 1) run Start programs Jcreator Pro Jcreator Pro 2) click close for the tips 3) select Configure Options JDK Profiles New 4) select c:\j2dk1.4.2_03 5) Then click ok ok apply ok respectively Note that all these preparations will only be done once and only at first run. There are 3 ways to use as an output media in Jcreator and these are a) console b) individual GUI (graphical user interface) c) applets Creating First Console Application 1) Create a folder c:\ece161 ( we will save our works under this directory) 2) Run JCreator 3) Select File New Files Java File 4)... c:\ece161\ 5) assign a name for your first program and click OK 6) write the following code onto editor for your first program public class firstprog public static void main(string[] args) System.out.println("This is my first java Program"); 7) To compile file : select Build Compile file 8) Correct the errors if there exists (take care about case sensitivity of java) 2
9) To execute the code : select Build Execute File You will see the output below... This is my first java Program Press any key to continue Creating First GUI Application 1. Create a folder c:\ece161 ( if you haven t created before) 2. Run JCreator 3. Select File New Files Java File 4.... c:\ece161\ 5. assign a name for your second program and click OK 6. write the following code onto editor for your first program import javax.swing.joptionpane; public class second public static void main(string[] args) JOptionPane.showMessageDialog(null,"This is my first GUI Application","title",JOptionPane.PLAIN_MESSAGE); System.exit(0); 7) To compile file : select Build Compile file 8) Correct the errors if there exists (take care about case sensitivity of java) 9) To execute the code : select Build Execute File You will see the output below... This type may be used more frequently during our Lab Hours 3
Some Usefull Information about programming in Java 1) Java is Case sensitive 2) Class name and file name must always be same 3) All import declerations must be declared before the first class decleratiın of the file otherwise syntax errors will occur. 4) Java is an Object Oriented Programming Language 5) /*...*/ and // can be used as a comment line in Java 6) if there a a syntax error a.class file will not be created by java 7) The file extention.java must always exist 8) main is the name of program building block, every java program will have a method called main 9) omitting the semicolon (;) will cause a syntax error 10) program error messages don t always tell the correct reason of errors and the correct line number 11) System.out is known as the standart output object and System.in is known as standart input object System.out.println( ) shows the text written inside this statement on the consol and goes to the next line System.out.print( ) shows the text written inside this statement on the consol but doesn t go to the next line. 12) While using the statement System.out you may use several lines one after the other this will make the same work on your text line 13) number1 = input.nextint(); takes an integer input from the user and assigns it to the variable called number1 but you have to create a scanner in order to be able to retreive inputs from the console and this can be done by the statement Scanner input = new Scanner( System.in ); 14) \n new line \t Horizontal tab \r goes to the beginning of the current line and owerrides the output \\ used to print backslash character \ used to print a double quote character 15) If you want to display some formatted data on the console, you will use System.out.printf( "to be written is %d\n", sum ); where sum is a variable namethat is assumed to be used in our program System.out.printf( " %s\n%s\n", wellcome to, Java Programming! ); Gives us an output as wellcome to Java Programming! 4
Arithmetic Operators Java Operation Algebraic Operator Algebraic Expression Java Expression Addition + 3 + 8 3 + 8 Subtraction - 3 8 3 8 Multiplication * 3 x 8 3 * 8 Division / ⅜ 3 / 8 Remainder % 3 mod 8 3 % 8 Comperation Operators Standard Equality and relational operators java Equality and relational operators Sample java condition Meaning of Java Condition = == x == y X is equal to y!= x!= y X is not equal to y > > x > y X is greater than y < < x < y X is smaller than y >= x >= y X is greater than or equal to y <= x <= y X is less than or equal to y Note that if statement is frequently used while using these comperation operatorsin programming and the usage of if statement is as illustrated below Example usage of if statement if (number1 >= number2) System.out.printf( %d eşittir %d\n,number1,number2) LAB WORK & HOMEWORK The Java application program illustrated belkow performs addition of 2 integers which are entered by the user during the running of application. 1) After investigation of that program 2) Write the program below 3) Compile and Run it as you have learned in the lab. 5
import java.util.scanner; // program uses class Scanner public class Addition // main method begins execution of Java application public static void main( String args[] ) // create Scanner to obtain input from command window Scanner input = new Scanner( System.in ); int number1; // first number to add int number2; // second number to add int sum; // sum of number1 and number2 System.out.print( "Enter first integer: " ); // prompt number1 = input.nextint(); // read first number from user System.out.print( "Enter second integer: " ); // prompt number2 = input.nextint(); // read second number from user sum = number1 + number2; // add numbers System.out.printf( "Sum is %d\n", sum ); // display sum // end method main // end class Addition HOMEWORK (due to 1.11.2005 time 23:59 by mailing to b.preveze@cankaya.edu.tr) Mail subject has to be ECE 161 lab4 hw (your own name) Mailinizin gözden kaybolmaması içingönderdiğiniz mailde konu kısmına konuyu ECE 161 lab4 hw (kendi isminiz) şeklinde girin. Write a program that ; a) Retrieves an integer value between 0 and 100 from the user b) Decides the letter grade of that value from AA to FF according to the table below Letter Grade Value AA 90-100 BA 85-90 BB 75-85 CB 69-74 CC 60-69 DC 55-59 DD 50-54 FD 40-49 FF 0-39 c) Displays that value on the console screen 6