java
play

Java Java Basics Java Program Statements Java Review Conditional - PDF document

Java Java Basics Java Program Statements Java Review Conditional statements Repetition statements (loops) Writing Classes in Java Selim Aksoy Class definitions Bilkent University Encapsulation and Java modifiers


  1. Java � Java Basics � Java Program Statements Java Review � Conditional statements � Repetition statements (loops) � Writing Classes in Java Selim Aksoy � Class definitions Bilkent University � Encapsulation and Java modifiers Department of Computer Engineering � Method declaration, invocation, and saksoy@cs.bilkent.edu.tr parameter passing � Method overloading Summer 2004 CS 111 2 Programming Rules of Thumb Introduction to Objects � Learn program patterns of general utility � An object represents something with which (branching, loops, etc.) and use relevant we can interact in a program patterns for the problem at hand � An object provides a collection of services � Seek inspiration by systematically working test that we can tell it to perform for us data by hand and ask yourself: “what am I doing?” � The services are defined by methods in a � Declare variables for each piece of information class that defines the object you maintain when working problem by hand � A class represents a concept, and an object � Decompose problem into manageable tasks represents the embodiment of a class � Remember the problem’s boundary conditions � Validate your program by tracing it on test � A class can be used to create multiple objects data with known output Summer 2004 CS 111 3 Summer 2004 CS 111 4 Java Program Structure Java Program Structure � In the Java programming language: // comments about the class � A program is made up of one or more classes public class MyProgram { � A class contains one or more methods c l as s he ade r � A method contains program statements // comments about the method � Attributes/properties correspond to fields (or public static void main (String[] args) variables) { c l as s body m e t hod he ade r � Behaviors/operations correspond to methods m e t hod body � A Java application always contains a method } called main } Summer 2004 CS 111 5 Summer 2004 CS 111 6

  2. Variables Primitive Data � There are exactly eight primitive data types in � A variable is a name for a location in Java memory � Four of them represent integers: � A variable must be declared by � byte , short , int , long specifying the variable's name and the � Two of them represent floating point type of information that it will hold numbers: � float , double dat a t ype var i abl e nam e � One of them represents characters: int total; � char int count, temp, result; � And one of them represents boolean values: � boolean M ul t i pl e var i abl e s c an be c r e at e d i n one de c l ar at i on Summer 2004 CS 111 7 Summer 2004 CS 111 8 Numeric Primitive Data Arithmetic Expressions � The difference between the various � An expression is a combination of one numeric primitive types is their size, or more operands and their operators and therefore the values they can store: � Arithmetic expressions use the operators: Addition + Type St or age M i n Val ue M ax Val ue Subtraction - byte Multiplication * 8 bi t s - 128 127 short Division / 16 bi t s - 32, 768 32, 767 int (no ^ operator) Remainder % 32 bi t s - 2, 147, 483, 648 2, 147, 483, 647 long < - 9 x 10 1 8 > 9 x 10 1 8 64 bi t s � If either or both operands associated 3. 4 x 10 3 8 wi t h 7 s i gni f i c ant di gi t s float with an arithmetic operator are floating 32 bi t s +/ - double 1. 7 x 10 3 0 8 wi t h 15 s i gni f i c ant di gi t s 64 bi t s +/ - point, the result is a floating point Summer 2004 CS 111 9 Summer 2004 CS 111 10 Division and Remainder String Concatenation � The string concatenation operator (+ ) is used � If both operands to the division operator to append one string to the end of another ( / ) are integers, the result is an integer � The plus operator (+ ) is also used for (the fractional part is discarded) arithmetic addition � The function that the + operator performs 14 / 3 4 e qual s ? depends on the type of the information on 8 / 12 e qual s ? 0 which it operates � If at least one operand is a string, it performs � The remainder operator (% ) returns the string concatenation remainder after dividing the second � If both operands are numeric, it adds them operand into the first � The + operator is evaluated left to right 14 % 3 e qual s ? 2 � Parentheses can be used to force the operation order 8 % 12 e qual s ? 8 Summer 2004 CS 111 11 Summer 2004 CS 111 12

  3. Data Conversions Data Conversions � In Java, data conversions can occur in three � Casting is the most powerful, and dangerous, ways: technique for conversion � assignment conversion � Both widening and narrowing conversions can be � arithmetic promotion accomplished by explicitly casting a value � casting � To cast, the type is put in parentheses in front of � Assignment conversion occurs when a value the value being converted of one type is assigned to a variable of another � For example, if total and count are � Only widening conversions can happen via integers, but we want a floating point result assignment when dividing them, we can cast total : � Arithmetic promotion happens automatically result = (float) total / count; when operators in expressions convert their operands Summer 2004 CS 111 13 Summer 2004 CS 111 14 Creating Objects Creating Objects � Generally, we use the new operator to � A variable holds either a primitive type or a reference to an object create an object � A class name can be used as a type to title = new String ("Java Software Solutions"); declare an object reference variable String title; Thi s c al l s t he St r i ng cons t r uct or , whi c h i s � No object is created with this declaration a s pe c i al m e t hod t hat s e t s up t he obj e c t � An object reference variable holds the � Creating an object is called instantiation address of an object � An object is an instance of a particular � The object itself must be created separately class Summer 2004 CS 111 15 Summer 2004 CS 111 16 Conditional Statements The if Statement � A conditional statement lets us choose � The if statement has the following which statement will be executed next syntax: � Therefore they are sometimes called The condition m us t be a bool e an e xpr e s s i on. selection statements if i s a Java I t m us t e val uat e t o e i t he r t r ue or f al s e . r e s e r ve d wor d � Conditional statements give us the if ( condition ) power to make basic decisions statement1 ; � Java's conditional statements are else statement2 ; � the if statement � the if-else statement I f t he condition i s t r ue , statement1 i s e xe c ut e d. � the switch statement I f i t i s f al s e , statement2 i s e xe c ut e d. Summer 2004 CS 111 17 Summer 2004 CS 111 18

  4. Boolean Expressions Logical Operators � A condition often uses one of Java's equality � Boolean expressions can use the following operators or relational operators , which all logical operators : return boolean results: ! Logical NOT && Logical AND == equal to || Logical OR != not equal to � They all take boolean operands and produce < less than > boolean results greater than <= less than or equal to � Logical NOT is a unary operator (it operates >= greater than or equal to on one operand) � Logical AND and logical OR are binary � Note the difference between the equality operators (each operates on two operands) operator ( == ) and the assignment operator ( = ) Summer 2004 CS 111 19 Summer 2004 CS 111 20 Repetition Statements The while Statement � Repetition statements allow us to execute a � The while statement has the following statement multiple times syntax: � Often they are referred to as loops while ( condition ) � Like conditional statements, they are while i s a statement ; controlled by boolean expressions r e s e r ve d wor d � Java has three kinds of repetition statements: � the while loop I f t he condition i s t r ue , t he statement i s e xe c ut e d. The n t he condition i s e val uat e d agai n. � the do loop � the for loop � The programmer should choose the right kind The statement i s e xe c ut e d r e pe at e dl y unt i l t he condition be c om e s f al s e . of loop for the situation Summer 2004 CS 111 21 Summer 2004 CS 111 22 Example The do Statement //******************************************************************** // Counter.java Author: Lewis/Loftus � The do statement has the following // // Demonstrates the use of a while loop. syntax: //******************************************************************** public class Counter do { //----------------------------------------------------------------- do and { // Prints integer values from 1 to a specific limit. while ar e //----------------------------------------------------------------- statement ; public static void main (String[] args) r e s e r ve d { } final int LIMIT = 5; wor ds while ( condition ) int count = 1; while (count <= LIMIT) { System.out.println (count); The statement i s e xe c ut e d onc e i ni t i al l y, count = count + 1; } and t he n t he condition i s e val uat e d System.out.println ("Done"); } } The statement i s e xe c ut e d r e pe at e dl y unt i l t he condition be c om e s f al s e Summer 2004 CS 111 23 Summer 2004 CS 111 24

Recommend


More recommend