Programming with Java Variables and Output Statement Lecture 03 First stage Software Engineering Dep. Saman M. Omer 2017-2018
Objectives ü Declare and assign values to variable ü How to use eclipse ü What is the steps for writing a program ü The rules of programming language ü How to start writing a program in java ü How data will be printed ü Join messages in output commands by using the concatenation (+) operator
Outlines Ø What is a Variable? Ø Declaring a Variable Ø Assignment statement Ø Eclipse Ø Programming Languages and Programming in Java Ø A Java program Ø Output statement Ø Examples
The computer's memory q Think of the computer like a calculator for a moment. o We have already seen how to calculate values. q A flexible calculator has "memory" keys to store and retrieve a computed value. o In what situation(s) is this useful? q How can we save and restore a value that our Java program previously calculated?
What is a Variable? q Variable: A piece of your computer's memory that is given a name and type, and can store a value. q A variable is a placeholder used for storing data. q Data may be numeric, or string of characters. q The data inside variable called Value, it can be change or delete. q Named location that stores a value of one particular type
Declaring a Variable q Syntax: datatype variablename; q Example: ² int score ; ² int book ; q Names of variables should be meaningful and reflect the data they will store o This makes the logic of the program clear q score and book, are two variables of type integer, they may hold values like 1, 50, 200. q note that we use int type, because score or book, both are measured in integer numbers, (i.e, there is no 3.6 books!!!!!)
Declaring a Variable v For values that contain fraction parts like 3.5, or 7.21, we use type double: ² double weight; ² double w; ² double x; v Note: all java statements must be ended with a semicolon. (;) v Think of variables as an empty box that you can put values in. v Example: int book ; v This statement will reserve a location in computers memory. Name: book Type : int
Java Variable Naming rules 1 2 3 4 5 6 Java variable names are case sensitive. The variable name money is not the same as Money or MONEY. Characters allowed to be used in names are: 1 All English Capital Letters (Upper Case Letters ) 2 All English Small Letters (Lower Case Letters) 3 Underscore ( _ ) 4 Currency symbols ( $,, ) 5 Digits( 0, 1, 2,,8,9). Variable name must begin with either a letter or the dollar sign "$", or the underscore character "_". Digits are not allowed to be used at the beginning of the name, Spaces are also not allowed, Java keywords are not allowed to be used as variable name.
Assignment statement q Assignments allow values to be put into variables. q Simple assignment: q datatype variablename = value ; q Examples: 1. int score = 7 ; Name: score Type : int Value : 7 2. int x,y ; x = 3 ; y = 5 ; Name: y Type : int Value : 5 Name: x Type : int Value : 3
Assignment statement The variable must be declared before use.
Eclipse How to create a project and classes for writing a program In
² To create a project: File à New à Java Project ² Enter a name for the project, then click Finish
² To create a class, click on the File à New à Class ² Enter a name then finish
Programming Languages q Programming languages have a set of rules : o Syntax: refers to the structure (or form) of code that a programming language specifies. If a program contains the syntax error that will not be compiled. ² Things that can cause a compiler error are incorrect keywords, missing brackets, semicolons and many others. o Semantics: refer to the meaning of the program, symbols, characters and words are arranged with that structure. ² The most common semantic error is one in which the code uses a variable that isn t initialized properly.
Programming in Java q To create a working program in Java, you need to three steps. : Ø Create a program by typing it into a file named, say, MyCode.java. Ø Compile the program which is translate a source language into a machine language (0 and 1). Ø Run (or execute) the program.
A Java program public class CLASS-NAME { public static void main (String args[]) { // your program goes here } // end of main } // end of class
Hello world program Here is the Hello world program in Java. public class Hello { public static void main (String args[]) { System.out.println( Hello world! ); } // end of main } // end of class Output Hello word
Analyses the program q That piece of program is a part of a complete program. q public class CLASS-NAME o o Java is referred to as an Object-Oriented Programming(OOP) language. The OOP languages require the program to be written in separate unit called classes. q All statements, and braces are needed to be written inside a class (any name) and a method called main. q public static void main (String args[]) o The main method is the method that's defined to be called at the start of an application. Without it, there is no place to start running. q What is method, what is class, both of them will be explained in next lectures.
Output statement q This is the way, always stuffs will printed on a sample text screen. ² System.out.println(variable-name); prints the value of variable <variable-name> to the user ² System.out.println( any message ); prints the message within quotes to the user ² System.out.println( hello + world + a + plus + b); assuming the value of a is 3 and b is 7, it prints helloworld3plus7 q println( ) is used for breaking current line, and starting a new one. q method print ( ) is used for printing any string or variable, directly after last print. q \n read as Slash n, can be used with method print( ), and work exactly as prinln ( ) method.
Example 1 public class Hello { public static void main (String args[]) { System.out.println( This is my first Java program! ); System.out.println( I like Java. ); System.out.println( I think Java is cool. ); } // end of main } // end of class Output This is my first Java program! I like Java. I think Java is cool. Ø Exercise: change the println to print. Lets see what is the difference?
Example 2 public class MyFirstJavaProgram { public static void main (String args[]) { int myage, myfriendage; myage = 20; myfriendage = myage + 1; System.out.println( Hello, \n I am + myage + years old,\n and \n my friend is + myfriendage + years old ); System.out.println( Goodbye ); } // end of main } // end of class Output Hello, I am 20 years old, and my friend is 21 years old Goodbye
Example 3 q What will be output from the following program?
The next lecture will explain an Input statement, Identifiers, Naming rules and Data Types