INSY4305 - Advanced Application Development
Increment and Decrement Operators
++a (prefix increment) - Increment a by 1, the use the new value of a in the expression in which a resides. a++ (postfix increment) - Use the current value of a in the expression in which it resides, then increment a by 1. For example: int a=3; System.out.println(a); Prints: 3 a++; (use current value and then increment 3+1=4) System.out.println (a); Prints: 4 ++a; (4+1=5) System.out.println (a); Prints: 5 For example: If we make the calculations in the System.out.println statements, the result is different. Do not forget that statements are sequential. int a=3; System.out.println(a); System.out.println (a++); System.out.println (++a); Prepared by Dr. Ezgi Akar For the class of INSY 4305 at the UT Arlignton 13 Prints: 3 3 5 For decrements, the logic is the same. --d d--
Commenting on Programs
// my first comment it is called end-of-line comment /* my first comment is here */ it is called traditional comment
Compiling and Executing Application
Compilation javac Welcome1.java Execution Java Welcome1
Scanner import java.util.Scanner;
Enables program to read input
printf %b
For boolean place holder/format specifier
printf %f
For double, float place holder/format specifier
NOTE: An integer division yields an integer result.
For example; int total= 100; double average= total/6; prints: 16 (although average is double, total and 6 are integers) double average = (double) total/6; (double) - unary cast operator, makes total and 6 as double temporarily.
printf %d
For int place holder/format specifier
printf %s
For string place holder/format specifier
Primitive Types
Int, Double, Float, Boolean, Char, Short, Long, and Byte (NOT STRING)
Constructor
Like a method, but is called implicitly by the new operator to initialize an object's instance variables when the object is created public class Employee { private String name; private double salary; public Employee (String name, double salary) //constructor { this.name = name; this.salary = salary;} } // class name and constructor name must be the same, otherwise syntax error // When you create an object from Employee, you must declare the arguments, otherwise syntax error
For exponential we use:
Math.pow(1+rate, year) year being the exponent
%.3f
Prints three digits after decimal point 19.150
%.2f
Prints two digits after decmial points = 19.15
Non-primitive Types
Reference Types A class which specify the types of object, are reference types. Account myaccount = new account(); This statement creates an object from class Account, then assigns to the variable myaccount a reference to that Account object myaccount can be used to call methods such as setName, getName. Primitive-types do not refer to objects, so they cannot be used to call methods
Creating an object
Scanner input = new Scanner(System.in);
Sequential Execution
Statements in a program are executed one after the other in the order in which they are written. Note: In Java, we do not have "goto" statement but the word "goto" is reserved by Java and should not be used as an identifier in programs
Conditional operator
System.out.print(myvalue==50 ? "Hello" : "Bye");
Performing Output
System.out.println ("Welcome to Java Programming"); (this is called a statement) System.out = standard output object
Boolean Logical AND (&) and Boolean Logical Inclusive OR (|) Operators
These operators always evaluate both of their operands. (no short-circuit evaluation) if ((age==50) | (score==50)) evaluates score==50 regardless whether age equals to 50. false & false = false if ((age==50) & (score==50)) false & true = false if ((age==50) & (score==55)) true & false= false if ((age==65) & (score==50)) true & true = true if ((age==65) & (score==55)) false | false = false if ((age==50) | (score==50)) false | true = true if ((age==50) | (score==55)) true | false= true if ((age==65) | (score==50)) true | true = true if ((age==65) | (score==55))
Boolean Logical Exclusive OR (^)
This operator returns true if and only if one of its operands is true and the other is false. false ^ false = false if ((age==50) ^ (score==50)) false ^ true = true if ((age==50) ^ (score==55)) true ^ false= true if ((age==65) ^ (score==50)) true ^ true = false if ((age==65) ^ (score==55))
Sentinel-Controlled Iteration
This type of iteration is also known as indefinite iteration, because we do not know the number of iterations. We use a sentinel value (signal value, dummy value, or a flag value) to indicate "end of data entry". Example: System.out.print ("Please enter a grade or -1 to terminate"); int grade= input.nextInt(); while (grade!=-1) { System.out.println ("Perfect"); } NOTE: we do not need a control variable, it will perfect until user enters -1. While (input.hasNext()) hasNext() method check whether there is an input, or the iteration is terminated by end-of-file indicator. It returns true or false.
Iteration Statement
While Counter-Controlled Iteration int product= 3; while (product <=100) { product = 3* product; System.out.print(product); } Prints: 9 27 81 243 Prepared by Dr. Ezgi Akar For the class of INSY 4305 at the UT Arlignton 11 int counter=1; while (counter <=10) { System.out.print (counter + ","+ " "); counter++; } Prints: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
Should s in System always be capitalized?
Yes, if S in System is not capitalized = Syntax Error
Compound Assignment Operators
c = c+7 OR c+=7 c= c-7 OR c-=7 c= c/7 OR c/=7
%20s
creates 20-character position, left justified System.out.printf("%20s is my favorite class", "Java"); ________________Java is my favorite class
%-20s
creates 20-character position, right justfied (16 characters + 4 characters (Java)= 20 characters) System.out.printf("%-20s is my favorite class", "Java"); Java________________ is my favorite class
System.out.print
does not position the output cursor at the beginning of text line - next character the program displays will appear immediately after the last character that print displays.
Conditional AND (&&) Operator
false && false = false if ((age==50) && (score==50)) false && true = false if ((age==50) && (score==55)) true && false= false if ((age==65) && (score==50)) true && true = true if ((age==65) && (score==55))
Conditional OR (||) Operator
false || false = false if ((age==50) || (score==50)) false || true = true if ((age==50) || (score==55)) true || false= true if ((age==65) || (score==50)) true || true = true if ((age==65) || (score==55))
Break and Continue Statements
for (count=1; count <=10; count++) { if (count==5) { break; } System.out.printf ("%d ", count); } Prints: 1 2 3 4 Breaks at 5 16 for (count=1; count <=10; count++) { if (count==5) { continue; } System.out.printf ("%d ", count); } Prints: 1 2 3 4 6 7 8 9 10 Skips 5
For Interations
for (int control=1; control <=10; control++) { System.out.print (control); } control, control variable control=1, initialize a value to it control <=10, loop-continuation condition control++, increment control variable NOTE: control is declared in for header statement (int control), so it can used only in for. For example: for (int control=1; control <=10; control++) { System.out.print (control); } System.out.print (control); → it will give a syntax error, because control is restricted to only for. For example; int control; for (control=1; control <=10; control++) { System.out.print (control); } System.out.print (control); → no syntax error, because control is declared (int control) outside of for. It will print the latest value of control after the loop. for (int control=1; control <=10; control=control+1) for (int control=1; control <=10; control++) Prepared by Dr. Ezgi Akar For the class of INSY 4305 at the UT Arlignton 15 for (int control=1; control <=10; ++control) for (int control=1; control <=10; control+=1) They are also same.
Logical Negation Operator (!)
if (! (grade==50)) it means that grade is not equal to 50
double-selection statement
if (myvalue==50) {System.out.print("Hello");} else if (myvalue==30) {System.out.print ("Bye");} else {System.out.print ("Try Again");}
Single-selection statement
if (myvalue==50) {System.out.print("Hello");}
Logical Operators
int age=65; int score=55;
Do... while iterations
int counter=1; do { System.out.print (counter+ " " ); counter++; } while (counter <=10); The body (do) executes at least one time, even if the loop-continuation condition is not satisfied. Prints: 1 2 3 4 5 6 7 8 9 10 int counter=1; do { System.out.print (counter+ " " ); counter++; } while (counter ==0); It only prints: 1 (condition is not satisfied but the body is executed once).
Math.PI
is for pi number
If Methods are public
it means that other classes can call these methods. But, these methods are not static, so an object must be created to use these methods
%n
newline
What is private String name; initialized too?
null
System.out.println
positions the output cursor at the beginning of text line - next character the program displays will appear in the new line. In System.out.print and System.out.println, you can use backslash (\) (escape character) to create tabs and new lines. For example; System.out.print ("Welcome to\n my new house\t today!"); Prints Welcome to my new house today \\ = prints \ \" = prints double quote.
%%
prints %
Instance Variables
public class Account { private String name; // name is an instance variable // all methods in the class can access to instance variables // only the methods of this class can access to this instance variable since it's private // Every instance variable has a default initial value, for Strings it is null reference to nothing
Declaring a Class
public class Welcome1 { Name of class and file must be the same. Otherwise will cause syntax error. A class name cannot begin with a digit and does not contain spaces Valid identifiers (class names): Welcome1, value, _value, button7 If braces do not occur in matching pairs = syntax error.
Get Methods
public double getSalary() {return salary;} //the method returns a double value //if salary is an integer, then it is a syntax error, because method returns a double value
Set Methods
public void setName (String name) {myname=name;} //assign parameter to the instance variable //void means that the method will not return anything
Local Variables
public void setName (String name) // name is a local variable or parameter { this.name = name; } // we are using this to differentiate instance variable and local variable from each other this. name (instance variable) = name (local variable) // only this method can access to local variable // local variables are not initialized by default
Multiple-selection statement
switch (grade/10) { case 9: case 10: System.out.println("You are so clever"); break; case 8: System.out.println ("You can earn more points"); Prepared by Dr. Ezgi Akar For the class of INSY 4305 at the UT Arlignton 10 break; default: System.out.println ("You will get an F grade"); break; }
%t
tab
Decision Making
x == y x != y x > y, x >= y x < y, x <= y
What is private double salary; initialized too?
zero
In all itterations, the rules are:
• You need a control variable • You need to initialize a value to control variable (It cannot be initialized to "0" unless you do not initialize "0". Then, it is a syntax error). • You need a loop-continuation condition • You need to increment the control variable