120 likes | 141 Views
Programming in Java. You are basically translating something in English into it’s equivalent in Java. Example:. In english you might say: if two variables have the same value then they are the same. Otherwise they are not the same. In Java you would write a method as follows:
E N D
Programming in Java You are basically translating something in English into it’s equivalent in Java.
Example: • In english you might say: if two variables have the same value then they are the same. Otherwise they are not the same. • In Java you would write a method as follows: public boolean sameAs( int a, int b) { if( a = = b) return true; return false; }
areDifferent • What do you think the method areDifferent would look like public boolean areDifferent( int a, int b) { }
areDifferent ->answer public boolean areDifferent( int a, int b) { if( a != b) return true; return false; } or public boolean areDifferent( int a, int b) { boolean result = (a = = b); if( result = = false) return true; return false; } or public boolean areDifferent( int a, int b) if( a == b) return false; return true; } As in any language you can say the same thing in more than one way. The three methods above are equivalent.
Every variable has a type • int a; // Primitive type int • double b; // Primitive type double • Integer c; // c is an Integer object • String d; // d is a String object • Double e; // e is a Double object • ArrayList f; // f is an ArrayList object • ArrayList<Integer> g; // g is an ArrayList of Integer Objects • ArrayList<String> h; // h is an ArrayList of String Objects • int[ ] i; // an array of primitive int ‘s • String[ ] j; // an array of String Objects Note: ArrayList can only store Object types. It cannot store primitive types. However arrays can be used to store both primitive and Object types
Two assign values objects must have the same type. • int a = 2; double b = 1.5; int c = 3; • a = c; // this is allowed, changes a to 3 • c = b; // incompatible types, not allowed String d = 5; // not allowed, 5 is of type int String e = “5”; // allowed, putting quotes around a sentence changes the type to String.
When you compare two objects they must have the same type. String a = “hi”; String b = “bye”; String c = “hi”; Integer d = new Integer(5); Integer e = new Integer(1); if( a.equals(b) ) // Compares a and b, return true; return false; // will return false since a is not equal to b if( a.equals(c) ) // compares a and c return true; // will return true since they are equal return false; if( a.equals( d ) ) // incompatible types, will not compile return true; return false; If( a.compareTo( b ) > 0 ) // recall that the compareTo method returns 0, positive number, or negative System.out.println( “bigger”); // number. If( a.compareTo( b ) = = 0 ) System.out.println( “equal”); If( a.compareTo( b ) < 0 ) System.out.println(“smaller”); To compare objects you use the equals method or the compareTo method. You cannot use operators such as >, < or ==, for objects. a = = b; ( this is not allowed) a.equals(b); ( this is allowed)
Comparing Primitive Types You can use the ==, <, and > operator with most primitive types. int a = 3; int b = 4; int c = 4; if( a = = b ) System.out.println(“a and b are the same”); if( a < b ) System.out.println( “ a is less than b” ); Note: a.equals( b ); // this is not allowed, primitive types can only be passed in as parameters, they cannot call methods. a = = b; this is allowed
Parameters must have the appropriate type. Here are some methods in the Face class. • public void drawEyes( Color color ) • public void drawMouth( boolean smile ) • public void setAge( int age ) What are the type of parameters for each method? Which are primitive types and which are Object types? Give an example of how each method might be called.
Answer The drawEyes method takes in a Color object as the parameter, which is an Object.Here is what a call to this method might look like: drawEyes( Color.blue ); or Color color = new Color(255, 0, 255); drawEyes( color ); The drawMouth method takes in a boolean , which is a primitive type. Here is what a call to this method might look like: drawMouth( true ); or boolean a = true; drawMouth(a); The setAge Method takes in an int as the parameter which is a primitive type. Here is what a call to this method might look like: setAge( 5 ); or int a = 5; setAge( a );
Finally, return types must match what is returned in the method public int getNumber( ) { int a = 5; return a; // returns an int } public int getNumber( ) { double b = 5.0; return b; // incompatible return type // the method definition says it should return // an int, however a double is returned } public int getNumber( ) { int a = 5; // this won’t compile, because nothing is returned // if the return type is int, then an int must be returned // if you don’t want to return anything, the return type // should be void }
Conclusion • So as you can see, types are very important when programming in Java. • If the types don’t match up, the program won’t compile. • However, on the AP Exam, there is no compiler, so you need to make sure that you are performing legal operations when writing your code.