Presentation is loading. Please wait.

Presentation is loading. Please wait.

Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.

Similar presentations


Presentation on theme: "Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or."— Presentation transcript:

1 Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or decide to add the new file to an existing project. 2.In Windows explorer, copy & paste the file to the src folder within the project chosen above. 3.In Eclipse, choose the project in the Package Explorer at the left and press to refresh the contents listed – the new file should show up in the listing. 4.Or, just paste the file into the project in Eclipse. Winter 2016CMPE212 - Prof. McLeod1

2 Today… Continue Introduction to Java class structure. Compare C and Java. Get into Building Expressions. Winter 2016CMPE212 - Prof. McLeod2

3 Aside - varargs You 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.) Winter 2016CMPE212 - Prof. McLeod3

4 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 PrintStream printf(String format, Object... args) Winter 2016CMPE212 - Prof. McLeod4

5 Winter 2016CMPE212 - Prof. McLeod5 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 return statement 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];

6 Winter 2016CMPE212 - Prof. McLeod6 The return Statement - Cont. The type of “ literal|expression ” must match the return type specified in the method declaration statement.

7 Winter 2016CMPE212 - Prof. McLeod7 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

8 Winter 2016CMPE212 - Prof. McLeod8 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

9 Attribute Examples public static double aVar; public static int aNum = 100; private static String hello = “Hello”; Winter 2016CMPE212 - Prof. McLeod9

10 Example: A Simple Class public class Simple { public static int aNum = 100; public static int sumNums(int num1, int num2) { return num1 + num2; } public static void main(String[] args) { int anotherNum = 200; System.out.println(sumNums(aNum, anotherNum)); } Winter 2016CMPE212 - Prof. McLeod10

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

12 Winter 2016CMPE212 - Prof. McLeod12 Java versus C What is the main difference between C and Java? Code must be contained within an Object. Creating a program is now all about defining Objects (or classes). In Java, you cannot have any code outside a class definition. The only code you can have outside a method definition are class attribute declarations.

13 Winter 2016CMPE212 - Prof. McLeod13 Java versus C, Cont. (Java code examples in white boxes) What Java has (and C does not!): A class definition line. All code (methods and attributes) must be contained within a class (within the { … } brackets). The class is saved in a text source code file called “ResistorCodes.java”. A class is an Object, and an Object is defined in a class.

14 Winter 2016CMPE212 - Prof. McLeod14 Java versus C, Cont. C has #include, Java has import : C has function prototypes, Java does not. C has global variables, Java does not. C has functions, Java has methods. C has a main() function, Java has a main method:

15 Java versus C, Cont. Some stuff is the same: { } used to delineate methods. ; at end of line. // for in-line comments. String literal enclosed in “ ”. ( ) contain arguments and parameter lists and can be used to establish precedence in expressions. Space used to delineate elements of expressions. Winter 2016CMPE212 - Prof. McLeod15

16 Winter 2016CMPE212 - Prof. McLeod16 What Makes an Expression? What are all the components available to a programmer that can be used to put a line of code together? –Variables –Literal Values –Keywords –Operators –Method Invocations –Punctuation

17 Winter 2016CMPE212 - Prof. McLeod17 Variable Declcaration Variables –Both C and Java are declarative languages. –Scope rules are the same. –Syntax of simple variable declaration is the same. –A variable is “statically typed” in both languages. In other words, once a variable is typed, in cannot be changed to another type.

18 Winter 2016CMPE212 - Prof. McLeod18 Primitive Types in Java Both C and Java have: –char –short –int –long –float –double Only Java has: –boolean –byte

19 Winter 2016CMPE212 - Prof. McLeod19 Integer Primitive Types byte, short, int, long For byte, from -128 to 127, inclusive (1 byte). For short, from -32768 to 32767, inclusive (2 bytes). For int, from -2147483648 to 2147483647, inclusive (4 bytes). For long, from -9223372036854775808 to 9223372036854775807, inclusive (8 bytes). A “byte” is 8 bits, where a “bit” is either 1 or 0.

20 Winter 2016CMPE212 - Prof. McLeod20 Real Primitive Types Also called “Floating Point” Types: float, double For float, (4 bytes) roughly ±1.4 x 10 -38 to ±3.4 x 10 38 to 7 significant digits. For double, (8 bytes) roughly ±4.9 x 10 -308 to ±1.7 x 10 308 to 15 significant digits.

21 Winter 2016CMPE212 - Prof. McLeod21 Character Primitive Type char from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535 (base 10) or 0 to ffff (base 16, or “hexadecimal”). A variable of the “ char ” type represents a Unicode character. Can also be represented as ‘a’ or ‘8’, etc.

22 Winter 2016CMPE212 - Prof. McLeod22 Boolean Primitive Type boolean is either true or false.

23 Winter 2016CMPE212 - Prof. McLeod23 String Objects String ’s are not primitive data types, but are Objects. A String is declared in the same way as a primitive type using the keyword: String. You can also instantiate a String as you would a normal object. The statement: String message = “Hello World!”; aliases the object message to the String literal object “Hello World!”, which is the same as saying: String message = new String(“Hello World!”);

24 Winter 2016CMPE212 - Prof. McLeod24 Legal Variable Names Java names may contain any number of letters, numbers and underscore (“_”) characters, but they must begin with a letter. Standard Java Naming Conventions: –Names beginning with lowercase letters are variables or methods. –Names beginning with uppercase letters are class names. –Successive words within a name are capitalized (“CamelCase”). –Names in all capital letters are constants.

25 Winter 2016CMPE212 - Prof. McLeod25 Literal Values Integers, for example: 12-1420333444891 If you write these kinds of numbers into your program, Java will assume them to be of type “ int ”, and store them accordingly. If you want them to be of type “ long ”, then you must append a “ L ” to the number: 43L9999983475L-22233487L

26 Binary, Octal and Hex Literals Use the prefix 0b (or 0B ) in front of the numbers to get a binary literal. Octal – just use 0 Hex use 0x You can use the underscore _ in-between digits, to aid in visualizing the number. System.out.println(), by default displays the numbers in base 10. Use printf to show the numbers in another codex or base. (See Part 2 of Exercise 1.) Winter 2016CMPE212 - Prof. McLeod26

27 Binary, Octal and Hex Literals, Cont. Summary examples of other literals: –binary: 0b111010111 –binary with underscores: 0b111_010_111 –octal: 07321023 –hex: 0xab10fc Winter 2016CMPE212 - Prof. McLeod27

28 Winter 2016CMPE212 - Prof. McLeod28 Literal Values - Cont. Real or “Floating Point” numbers, for example: 4.5-1.03.457E-10-3.4E45 These literals will be assumed to be of the “ double ” type. If you want them to be stored as “ float ” types, append an “ F ”: 3.456F5.678E-10F-321.0F

29 Winter 2016CMPE212 - Prof. McLeod29 Literal Values - Cont. char literals: ‘A’‘5’‘\u0032’ boolean literals: truefalse String literals: “Hello there!”“456.7”“West of North”


Download ppt "Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or."

Similar presentations


Ads by Google