1 / 23

CISC124, Reminders

CISC124, Reminders. Course Web Site: http://research.cs.queensu.ca/home/cisc124w Last night we had 69 out of 110 students having filled out the lab section survey. Today…. Start OOP Syntax in Java. Questions from Last Lecture:. Suppose we have two kinds of errors: Syntax errors

Download Presentation

CISC124, Reminders

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CISC124, Reminders • Course Web Site: http://research.cs.queensu.ca/home/cisc124w • Last night we had 69 out of 110 students having filled out the lab section survey. CISC124 - Prof. McLeod

  2. Today… • Start OOP Syntax in Java. CISC124 - Prof. McLeod

  3. Questions from Last Lecture: • Suppose we have two kinds of errors: • Syntax errors • Runtime errors • What is the difference? • Considering the two step process of running a Java program, at what stages will these errors be caught and who does the catching? • Who does the fixing??? CISC124 - Prof. McLeod

  4. OOP in Java • A class or “object definition” or an “object”, consists of instance variables and/or methods. • By convention, instance variables are all declared before the methods: public class ShowStructure { // instance variables or “attributes” here // methods here } // end class ShowStructure CISC124 - Prof. McLeod

  5. OOP in Java – Cont. • In Java, a class is an Object, and an Object is a class (ratherZenis it not!) • Code and attributes cannot be defined outside of a class. • The only code that can exist outside a method are attributes or other (“inner”) class definitions. CISC124 - Prof. McLeod

  6. Using Methods Defined in Objects • Methods that are declared static can be invoked from a class without instantiation of that class. (System.out.println(), for example). • Non-static methods can only be invoked from instances of a class. (The Scanner class methods like .nextLine(), for example.) • Instantiation is through the use of the new keyword and usually involves the “customization” of a class by supplying particular values for class attributes. (Supplying System.in when instantiating the Scanner class, for example.) CISC124 - Prof. McLeod

  7. Attributes • Also called “class variables” or “instance variables” or “fields”. • Declared within a class at the same level as the method declarations. • These variables are known to all methods within a class (their “scope”). • You can control their privacy and the way they are stored in memory (using public/private/protected and static). • (We’ll talk more about these keywords later…) CISC124 - Prof. McLeod

  8. Attribute Declaration • Syntax: [private|public] [static] [final] type attributeName [= literalValue]; • Note that the type part is not optional – this is why java is a “declarative” language. • And, a variable cannot change its type later (“static typing”). • You cannot use a variable unless you have declared it first. CISC124 - Prof. McLeod

  9. Variable Declaration • Declaring a variable inside a method gives that variable the scope of just inside the method, not outside the method. • Generally a variable is only available inside the block ({…}) in which it is declared. • The syntax for declaration inside a method is the same except you don’t need the [private|public] [static] [final] parts. CISC124 - Prof. McLeod

  10. Method Declaration (a “Header”) • The syntax for simple method declaration: [private|public] [static] [final] returnTypemethodName ([parameterList]) {…} • If main invokes methods or uses attributes in the same class as itself then those attributes and methods must also be declared static. CISC124 - Prof. McLeod

  11. Method Declaration - Cont. • A method must have a returnType. • The returnType can be any single Object or a primitive type. (For example: an int, double, String, an array (like int[], or any other pre-defined Object.) • If the method does not return anything, then the keyword void is used instead. • The main method does not return any value, so that’s why it is declared as in: public static void main (String[] args) {…} CISC124 - Prof. McLeod

  12. Aside - The main Method • For the JVM to run an application, it must know where to start. • By design, the starting point is always the execution of the main method. • The JVM expects the main method to be declared exactly as shown – the only thing you can change is the name of the String array, called argsabove. CISC124 - Prof. McLeod

  13. Method Declaration - Cont. • parameterListprovides a means of passing items, or parameters, into a method. • It is optional. • It can consist of one or many parameters, separated by commas. • Each parameter type must be declared in the parameter list, as in type variableName, type variableName, … • Java does not have named or optional parameters as does Python. CISC124 - Prof. McLeod

  14. Aside - varargs • Use can use the ellipsis operator (three dots - …) to create a parameter that can take any number of arguments of that type. For example, suppose you have an object of type Bling: public void seeBling (Bling... blingers) {// code } • Within seeBling, you get at the individual arguments of type Bling by pretending that blingers is an array (use [ ] along with an index value.) CISC124 - Prof. McLeod

  15. Aside – varargs, Cont. • We’ll need to look at an example of this later. • It is not used too often, but • See the declaration of the printf method for example: public PrintStreamprintf(String format, Object... args) CISC124 - Prof. McLeod

  16. Methods - the return Statement • Unless the return type is void, the method must contain at least one return statement. A void method can also use a returnstatement without anything after it, as a means of terminating the method. • A method always stops (terminates) when a return statement is encountered. • Syntax: return [literal|expression]; CISC124 - Prof. McLeod

  17. The return Statement - Cont. • The type of “literal|expression” must match the return type specified in the method declaration statement. CISC124 - Prof. McLeod

  18. Method Examples public void printHello() { System.out.println(“Hello”); } // end printHello public void printHelloName(String yourName) { System.out.println(“Hello “ + yourName); } // end printHelloName public void printAvg(int a, int b) { System.out.println((a + b) / 2.0); } // end printAvg CISC124 - Prof. McLeod

  19. Method Examples - Cont. public double average(double a, double b) { return (a + b) / 2; } // end average public int lowest(int a, int b) { if (a <= b) return a; else return b; } // end lowest Does this have to be here? CISC124 - Prof. McLeod

  20. Attribute Examples public static double aVar; public static intaNum = 100; private static String hello = “Hello”; CISC124 - Prof. McLeod

  21. Example: A Simple Class public class Simple { public static intaNum = 100; public static intsumNums(int num1, int num2) { return num1 + num2; } public static void main(String[] args) { intanotherNum = 200; System.out.println(sumNums(aNum, anotherNum)); } } CISC124 - Prof. McLeod

  22. Another Simple Class // An example of the use of varargs (the ellipse...) // Also a for each loop! public class AnotherSimple { public static intsumNums(int... nums) { intsum = 0; for (intnum : nums) sum += num; return sum; } public static void main(String[] args) { System.out.println(sumNums(2, 5, 7, 10, 3)); } } // end AnotherSimple class CISC124 - Prof. McLeod

  23. Python versus Java, so Far • You can see some major syntactical differences already: • Variable type declaration in mandatory. Return type declaration is mandatory. Parameter type declaration is mandatory. • Variable types are not dynamic! • A class header is required. • public static, The ; • Indentation in Java is for style only. { } are used to indicate code blocks or code containment. • println() must be invoked as a member of the System.out object. CISC124 - Prof. McLeod

More Related