160 likes | 348 Views
IDS 201 Introduction to Business Programming (Fall 2013). Week2. Literals. A name for a constant value is called a literal. Character literals ‘A’, ‘*’, ‘t’: tab, ‘<br>’: linefeed, ‘’’: single quote, ‘\’: backslash Real literals d ouble: 1.2, 1.2e3 :1.2*10 3 f loat: 1.2F
E N D
IDS 201Introduction to Business Programming (Fall 2013) Week2
Literals • A name for a constant value is called a literal. • Character literals • ‘A’, ‘*’, ‘\t’: tab, ‘\n’: linefeed, ‘\’’: single quote, ‘\\’: backslash • Real literals • double: 1.2, 1.2e3 :1.2*103 • float: 1.2F • Integer literals • Ordinary integers such as 1000 and -32 are literals of type byte, short, or int, depending on their size. You can make a literal of type long by adding “L” as a suffix.
String • Predefined object type • A String is a sequence of characters. • It is surrounded by double quotes. • String s = “Hello World.”;
Escape Sequences • A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler.
Example • String s = "I said, \"Are you listening!\"\n”; • System.out.print(s); • I said, “Are you listening!” with a linefeed at the end.
Variable Declaration • ⟨type-name ⟩ ⟨variable-name-or-names ⟩; • The ⟨variable-name-or-names⟩ can be a single variable name or a list of variable names separated by commas. • Good programming style is to declare only one variable in a declaration statement. • intnumberOfStudents; • String name; • double x, y; • booleanisFinished; • char firstInitial, middleInitial, lastInitial;
Default Values The special null literal that can be used as a value for any reference type. null may be assigned to any variable, except variables of primitive types.
Examples • intnumberOfStudents; • intnumberOfStudents = 30; • String name; • String name = “ids201”; • double x, y; • double x=0.5, y=12.3; • booleanisFinished; • booleanisFinished = false; • char firstInitial, middleInitial, lastInitial; • char firstInitial=‘I’, middleInitial=‘D’, lastInitial=‘S’;
Build-in Subroutines and FunctionsMethods • Static class methods: belong to a class and are shared by all objects instanced by that class. [public] static <type> <method_name>(parameters){ } • System.out.println() • Math.sqrt() • Math.random(), which returns a randomly chosen double in the range 0.0 <= Math.random() < 1.0.
Enumerated Type: enum • An enum is a type that has a fixed list of possible values, which is specified when the enum is created. (NOT A PRIMITIVE TYPE) • Definition enum ⟨enum-type-name⟩ { ⟨list-of-enum-values⟩ } e.g. enumSeason { SPRING, SUMMER, FALL, WINTER } • Upper case by convention • Each value is a constant.
Declare & Assign enum Variables • enum Season {SPRING, SUMMER, FALL, WINTER}; • Season vacation; • vacation = Season.SUMMER; • System.out.println(vacation); // output SUMMER, without Season
ordinal() • enum is a class. • Each value has a method/subroutine: ordinal(). • When used with an enum value, it returns the ordinal number of the value in the list of values of the enum, starting from 0. • Season.SPRING.ordinal() is the int value 0. • Season.WINTER.ordinal() is the int value 3.
Input/Output • Formatted output • System.out.printf • It takes two or more parameters. The first parameter is a format specifierthat specifies the format of the output. The remaining parameters specify the values that are to be output. • %[width][.precision]code • Code: d, s, c, f,…
Format Specifier • Every format specifier begins with a percent sign (%) and ends with a letter, possibly with some extra formatting information in between. • int x = 50; System.out.printf(“x = %d”, x);// output: x = 50 • %d, %s, %f, %1.2f, %12d,… • %12d: output an integer with a minimum number of spaces 12. • Right-justified: add blank spaces in front to bring up to 12 if the integer is fewer than 12 spaces, print it out otherwise. • int x = 50; System.out.printf(“x=%5d”,x); • Output: x= 50(3 spaces in front of 50)
%x.yf: in a general floating-point format • x: total number of characters to output. • y: number of characters after the decimal point. • System.out.printf("%7.2f",874.9163); // 874.92 • %x.ye: in an exponential format • x: total number of characters to output. • y: number of characters after the decimal point • System.out.printf("%e",874.9163); //8.749163e+02 • %s: in a string format • String s = “Helloworld”; • System.out.printf(“the string is: %s”, s); • // the string is: Helloworld
Separate big numbers into groups by using comma. • The comma should come at the beginning of the format specifier, before the field width. • System.out.print(“%,d”,x); // 1,000,000 if x is a million • %,12.3f