Docsity

Basics of a Java Program - Lecture Notes | CS 110, Study notes of Computer Science

Toggle sidebar
bg1
CS 110 Notes – Basics of a Java Program 1
Copyright C. Tanner 2009
• Language Statement/Rule Types
o Syntax – rules determining what makes up a legal statement
o Semantics – meaning of the statement
• Definitions
o Token – identifiable symbol in the language
Special single char tokens
• + - * / . ; , ( ) ?
• + - * / Math Symbols
• ; ends a statement
• . “of” qualifying symbol
• , separates items in a list
o Keywords
Special set of “reserved” words
• Can’t be used in any other manner
• List page 959 text
o Identifiers
Names used in a program
Variables, objects, classes, methods
Syntax rules
• Can contain letters, digits, _, and $
• Must begin with letter, _, or $
• Case sensitive
Style convention
• Lower case
• Capitalize start of new word
• Readable, meaningful names
o Data Types
Software handles information of different types
• Numeric, character
Definition of data type
• Set of values
o Domain
• Set of operations
Primitive types
• Integral types
o char
2 bytes (16 bits)
Values Æ 0--2 15 -1
Represents any character on keyboard
pf3
pf4
pf5
pf8

Partial preview of the text

Download Basics of a Java Program - Lecture Notes | CS 110 and more Study notes Computer Science in PDF only on Docsity!

  • Language Statement/Rule Types o Syntax – rules determining what makes up a legal statement o Semantics – meaning of the statement
  • Definitions o Token – identifiable symbol in the language ƒ Special single char tokens - + - * /. ; , ( )? - + - * / Math Symbols - ; ends a statement -. “of” qualifying symbol - , separates items in a list o Keywords ƒ Special set of “reserved” words - Can’t be used in any other manner - List page 959 text o Identifiers ƒ Names used in a program ƒ Variables, objects, classes, methods ƒ Syntax rules - Can contain letters, digits, _, and $ - Must begin with letter, _, or $ - Case sensitive ƒ Style convention - Lower case - Capitalize start of new word - Readable, meaningful names o Data Types ƒ Software handles information of different types - Numeric, character ƒ Definition of data type - Set of values o Domain - Set of operations ƒ Primitive types - Integral types o char ƒ 2 bytes (16 bits) ƒ Values Æ 0--2 15 - ƒ Represents any character on keyboard
  • Digits, letters, special chars
  • Single chars ‘A’ ‘a’ ƒ ASCII collating sequence
  • Table on page 965 of text
  • 0 Æ 48, 1 Æ 49, 9 Æ 57
  • A Æ 65, B Æ 66
  • a Æ 97 o byte ƒ 1 byte (8 bits) ƒ Values Æ -128 – 127 (2 7 -1) o short ƒ 2 bytes ƒ Value Æ -2 15 -- 2 15 - o int ƒ 4 bytes (32 bits) ƒ Values Æ -2 31 -- 2 31 - ƒ Positive numbers do not use + ƒ Negative number use – ƒ No comma o long ƒ 8 bytes (64 bits) ƒ Values Æ -2 63 -- 2 63 -
  • Boolean o 1 bit o ValuesÆ T, F o Logical expressions
  • Floating point o Decimal numbers o Use. o float ƒ Single precision ƒ 6 significant digits ƒ 4 bytes ƒ -3.4E-48 -- 3.4 E+ o double ƒ double precision ƒ 8 bytes ƒ 15 significant digits ƒ -1.7E-308 – 1.7E + 308
  • Sample Programs o Helloe.java, Hello.java

ƒ Comments o Enclosed in /** */ o Header Comment ƒ Name ƒ Date ƒ Lab instructors name ƒ Lecture instructors name ƒ Description of program ƒ Arithmetic Operations o Data type ƒ Values and operations ƒ int Æ + * / - % ƒ double Æ + / * - o expression ƒ operands and operators ƒ unary operators Æ - + ƒ binary operators Æ - + * / % o writing arithmetic expression ƒ b 2 – 4ac

  • bb-4a*c

c a

a b

1

1

  • x
  • a[-(b+c)]
  • Assignment Statements o variable = expression; o evaluate the expression, assign the value to the variable o examples ƒ x=5; ƒ x = x+1; ƒ x = x+y;
    • type conversion
    • (data type) variable
    • average = (double) total/numScores;
  • Sample Programs Rectangle.java, Circle.java
  • Inputting Information o Buffer Æ System.in o Java supplies a class Scanner to interact with the buffer o Compiler Æ loader Æ interpreter ƒ Scanner is loaded by the loader
    • Import statement o Identifies a class to the compiler (Æ loader) o import java.util.Scanner;
    • associate the Scanner with the buffer (System.in) and give it a name o Scanner sc = new Scanner (System.in);
    • Scanner’s Methods o next() – returns the next string ƒ delimiter – blank o nextln() – returns the string up to new line ƒ includes embedded whitespace o nextInt() – returns the next integer o nextDouble() – returns the next decimal value o next().charAt(0) – returns next character
    • Data Type Mismatch o Throws an exception
  • Sample Programs Sum.java, Hello2.java, Centigrade.java, Pyth.java, ExamAve.java
  • Homework Page 108 #10, 16
  • Program Format

header comment import statement(s) public class Class definition -- name given to class must match the filename { public static void main (String [] args) { Program statements } }