1 / 16

IDS 201 Introduction to Business Programming (Fall 2013)

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

aiden
Download Presentation

IDS 201 Introduction to Business Programming (Fall 2013)

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. IDS 201Introduction to Business Programming (Fall 2013) Week2

  2. 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.

  3. String • Predefined object type • A String is a sequence of characters. • It is surrounded by double quotes. • String s = “Hello World.”;

  4. Escape Sequences • A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler.

  5. Example • String s = "I said, \"Are you listening!\"\n”; • System.out.print(s); • I said, “Are you listening!” with a linefeed at the end.

  6. 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;

  7. 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.

  8. 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’;

  9. 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.

  10. 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.

  11. Declare & Assign enum Variables • enum Season {SPRING, SUMMER, FALL, WINTER}; • Season vacation; • vacation = Season.SUMMER; • System.out.println(vacation); // output SUMMER, without Season

  12. 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.

  13. 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,…

  14. 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)

  15. %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

  16. 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

More Related