ECE 161 WEEK 4 Introduction to Programing in Java

Similar documents
Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

2.8. Decision Making: Equality and Relational Operators

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Introduction to Java Applications

download instant at

Copyright 1999 by Deitel & Associates, Inc. All Rights Reserved.

Object Oriented Programming. Java-Lecture 1

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Introduction to Java Applications; Input/Output and Operators

Course Outline. Introduction to java

CS111: PROGRAMMING LANGUAGE II

Java Programming Language. 0 A history

AP Computer Science Unit 1. Writing Programs Using BlueJ

CS141 Programming Assignment #6

Section 2.2 Your First Program in Java: Printing a Line of Text

Section 2.2 Your First Program in Java: Printing a Line of Text

Programming with Java

4 WORKING WITH DATA TYPES AND OPERATIONS

Control Statements: Part 1

AP Computer Science Unit 1. Programs

C: How to Program. Week /Mar/05

Introduction to Java Unit 1. Using BlueJ to Write Programs

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

AP Computer Science Unit 1. Writing Programs Using BlueJ

Introduction to Computer Science Unit 2. Exercises

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

I. Variables and Data Type week 3

2.5 Another Application: Adding Integers

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

1. Find the output of following java program. class MainClass { public static void main (String arg[])

Full file at

Building Java Programs. Introduction to Programming and Simple Java Programs

Introduction to Computation and Problem Solving

Eng. Mohammed Alokshiya

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Chapter 2 - Introduction to C Programming

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

13 th Windsor Regional Secondary School Computer Programming Competition

Introduction to Java Applications

First Java Program - Output to the Screen

Mr. Monroe s Guide to Mastering Java Syntax

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Programming for Engineers Introduction to C

Getting Started with Eclipse/Java

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 45 Obtained Marks:

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

AP Computer Science A Unit 2. Exercises

Elementary Programming

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Lesson 10: Quiz #1 and Getting User Input (W03D2)

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

1. Download the JDK 6, from

AP Computer Science A Summer Assignment 2017

CS110: PROGRAMMING LANGUAGE I

COMP 202 Java in one week

CS11 Java. Fall Lecture 1

Eng. Mohammed S. Abdualal

Midterm Examination (MTA)

Introduction to C# Applications

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Eclipse. JVM, main method and using Eclipse. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Section 2: Introduction to Java. Historical note

2: Basics of Java Programming

Software Practice 1 Basic Grammar

Arrays. Eng. Mohammed Abdualal

Oct Decision Structures cont d

Week 2: Data and Output

1.00 Lecture 2. What s an IDE?

Supplementary Test 1

B.V. Patel Institute of BMC & IT, UTU 2014

Chapter 02: Using Data

CS520 Setting Up the Programming Environment for Windows Suresh Kalathur. For Windows users, download the Java8 SDK as shown below.

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Chapter 2: Using Data

Introduction to Software Development (ISD) David Weston and Igor Razgon

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

Methods. Eng. Mohammed Abdualal

3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS)

COMP 202. Java in one week

Lecture Set 2: Starting Java

AP Computer Science Summer Work Mrs. Kaelin

JAVA PROGRAMMING (340)

Introduction to Java & Fundamental Data Types

Lab # 2. For today s lab:

Lecture Set 2: Starting Java

Chapter 2: Programming Concepts

COMP 110 Project 1 Programming Project Warm-Up Exercise

CEN 414 Java Programming

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Transcription:

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