Upload
marion-wilkerson
View
220
Download
0
Embed Size (px)
DESCRIPTION
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
Citation preview
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 <F5> 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 2016 CMPE212 - Prof. McLeod 1
Today…• Continue Introduction to Java class structure.
• Compare C and Java.
• Get into Building Expressions.
Winter 2016 CMPE212 - Prof. McLeod 2
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 2016 CMPE212 - Prof. McLeod 3
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 2016 CMPE212 - Prof. McLeod 4
Winter 2016 CMPE212 - Prof. McLeod 5
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];
Winter 2016 CMPE212 - Prof. McLeod 6
The return Statement - Cont.• The type of “literal|expression” must match
the return type specified in the method declaration statement.
Winter 2016 CMPE212 - Prof. McLeod 7
Method Examplespublic 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
Winter 2016 CMPE212 - Prof. McLeod 8
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;
elsereturn b;
} // end lowest
Attribute Examplespublic static double aVar;
public static int aNum = 100;
private static String hello = “Hello”;
Winter 2016 CMPE212 - Prof. McLeod 9
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 2016 CMPE212 - Prof. McLeod 10
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 classWinter 2016 CMPE212 - Prof. McLeod 11
CMPE212 - Prof. McLeod 12Winter 2016
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.
CMPE212 - Prof. McLeod 13Winter 2016
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.
CMPE212 - Prof. McLeod 14Winter 2016
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:
CMPE212 - Prof. McLeod 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 2016
CMPE212 - Prof. McLeod 16Winter 2016
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
CMPE212 - Prof. McLeod 17Winter 2016
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.
CMPE212 - Prof. McLeod 18Winter 2016
Primitive Types in Java• Both C and Java have:
– char– short– int– long– float– double
• Only Java has:– boolean– byte
19Winter 2016 CMPE212 - Prof. McLeod
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.
20Winter 2016 CMPE212 - Prof. McLeod
Real Primitive Types• Also called “Floating Point” Types:
• float, double
• For float, (4 bytes) roughly ±1.4 x 10-38 to ±3.4 x 1038 to 7 significant digits.
• For double, (8 bytes) roughly ±4.9 x 10-308 to ±1.7 x 10308 to 15 significant digits.
21Winter 2016 CMPE212 - Prof. McLeod
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.
22Winter 2016 CMPE212 - Prof. McLeod
Boolean Primitive Type• boolean is either true or false.
23Winter 2016 CMPE212 - Prof. McLeod
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!”);
24Winter 2016 CMPE212 - Prof. McLeod
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.
25Winter 2016 CMPE212 - Prof. McLeod
Literal Values• Integers, for example:
12 -142 0 333444891
• 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:
43L 9999983475L -22233487L
CMPE212 - Prof. McLeod 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 2016
CMPE212 - Prof. McLeod 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 2016
28Winter 2016 CMPE212 - Prof. McLeod
Literal Values - Cont.• Real or “Floating Point” numbers, for example:
4.5 -1.0 3.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.456F 5.678E-10F -321.0F
29Winter 2016 CMPE212 - Prof. McLeod
Literal Values - Cont.• char literals:
‘A’ ‘5’ ‘\u0032’
• boolean literals:
true false
• String literals:
“Hello there!” “456.7” “West of North”