Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Java Basics1

Uploaded by

dokumanuels
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Basics1

Uploaded by

dokumanuels
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

CSCI 1301

COMPUTER SCIENCE I
JAVA BASICS
JAVA PROGRAMS

Class Header

Method Header

Keywords
Comment

Statement
CLASS HEADERS

 Marks the beginning of a class definition


 Serve as a container for an application
 All Java Programs must have at least one
 File must have the same name as the class*
 Only one class definition per file

 Keywords
 public
 Access specifier – controls where the class may be accessed from

 class
 Indicates the beginning of a class definition

 Case sensitive

 Curly Braces
METHOD HEADERS
 Main method header
 Every Java application must have a method named main
 Starting point of application
COMMENTS
 Short notes intended for programmers
 Provide better understanding of the code
 Ignored by compiler
STATEMENTS
 A executable snippet of code that represents a complete command
 Terminated by a semicolon
SEMICOLONS
 Do not put a semicolon at the end of:
 Comments
 Class headers
 Method headers
 Braces
STYLE AND READABILITY
 Indentation
 White space
 Comments
 Curly Brace Placement
OUTPUT
PRINTLN
 Part of Java’s Application Programmer Interface (API)
 System Class
 out object
 println method

 String literals as arguments


 Double quotes
 Cannot span more than one line
 Displays the argument to the screen and moves the cursor to the next
line
PRINTLN
PRINTLN
 If the text that we want to display the screen is contained in quotation
marks, how could we display actual quotation marks?

Example:
“This statement”
produces 3
lines of output
ESCAPE SEQUENCES
 Common Escape Sequences

Sequence Used to Represent… Description

\t Tab Move to the next tab stop

\n New Line Advances the cursor to the next line

\” Quotation Marks Prints a double quotation mark

\\ Backslash Prints a backslash


PRINT
 Does not go to the next line after outputting the argument to the
screen

Can these lines be rewritten while still using the print method and achieve the
desired effect?
QUIZ TIME!
 Which of the following is the correct way to output a message?
a. System.out("Programming is fun!");
b. System.println("Programming is fun!");
c. System.out.println("Programming is fun!");
d. System.out.Println("Programming is fun!");

 What is the output of the following code?


QUIZ TIME!
 Identify the errors in the following program:
VARIABLES, LITERALS, AND PRIMITIVE DATA
TYPES
VARIABLES & LITERALS
 Variables
 Named storage location in a computer’s memory

 Literal
 Value written into the code of a program
IDENTIFIER RULES
 Must begin with a letter
 Can only contain the following:
 Letters
 Numbers
 Underscore

Examples:
thisIsAVariable
this_isaVariable
thisis1Variable
variable1
QUIZ TIME
 Are the following legal identifier names?
week day
1997june
wee7Day
day_of_the_week
week@day
carTestTrack
JAVA NAMING CONVENTIONS
 Class names begin with a capital letter
 Method and variable names begin with lowercase letters
 Try to limit to one word
 If you must use more than one word, capitalize the first letter of each word
after the first word
 Constants have uppercase letters and the words are separated by
underscores

Examples:
AllMyChildren //class
allMyChildren //variable or method
ALL_MY_CHILDREN //constant
PRIMITIVE DATA TYPES
 Name for a category of data values that are all related

Commonly used primitive data types


Type Description Examples

int Whole numbers 42, -3, 18, 2814, 0

double Floating-point numbers 7.12, 14.9, -19.82743

char Single Characters ‘a’, ‘Z’, ‘!’

boolean Logical Values true, false


QUIZ TIME!
 What data type would you use for a variable that stored…
Letter grade
Number of textbooks
Whether or not it is raining
Bank balance
Gender (M or F)
Sales tax rate
Whether or not a burger has cheese
The value 14
The letter J
Lights on or off
DECLARATION & ASSIGNMENTS
 <type> <name>;

 = operator
 Stores the value on the right to the variable on the left
WATCH OUT!
 42 = age; is not the same as age = 42;
 Remember:
 The assignment operator assigns the value on the right to the
variable on the left
 It doesn’t work the other way around
PRINTING VARIABLE VALUES

 String concatenation operator


 Appends one string to another
INITIALIZATION STATEMENT
 Declare a variable and assign it a value on the same line

This… Is the same as…

 Digits only!
PRINTING WITH FORMATTING
 No commas or dollar signs in variable declarations, but we can print
them when we print the variable.
 Printf method
SCOPE
 Part of the program where it may be accessed by name

 Begins for variables when they are declared/initialized and ends when
the method it is in ends
ARITHMETIC
OPERATORS
 Three types
 Unary
 Binary
 Ternary
Table of Binary Operators
Operator Meaning Example

+ Addition total = cost + tax

- Subtraction cost = total - tax;

* Multiplication tax = cost * rate;

/ Division salePrice = original / 2;

% Modulus remainder = value % 3;

amount = 4 + 8;
markUp = 12 * 0.25;
INTEGER DIVISION
 If both operands are integers, the result will also be an integer

 Example:
PRECEDENCE & ASSOCIATIVITY
 Operator Precedence 5+2*4
 Negation
2+3*4–6
 Multiplication, Division, Modulus
22 + 4 * 2
 Addition, Subtraction
 Associativity 4 * 3 / 8 + 2.5 * 2
 Negation – right to left
 All other operations, left to right
COMPOUND OPERATORS
 balance = balance + deposit;
 balance = balance – withdrawal;
 number = number / 2;
 number = number * 2;

 Java provides shorthand


 balance += deposit;
 balance -= withdrawal;
 number /= 2;
 number *= 2;
NAMED CONSTANTS
 Variable whose value is read only
 Cannot be changed during program execution
 Keyword final
STRINGS
STRINGS
 Not primitive
 Sequence of Characters

 Class in Java API defined to contain characteristics and actions of a


String object

 Declared, assigned, and initialized the same way as primitive data


types
REFERENCE VARIABLES

number

12

name

address

“Jillian”
STRING METHODS
Method Name Description Example

length Returns the number of int size = name.length();


characters in a string
charAt Returns the character at a char initial = name.charAt(0);
specific index in the string
toUpperCase Returns a string with all the String largeName = name.toUpperCase();
characters in uppercase if they
were not already
toLowerCase the characters in lowercase if String tinyName = name.toUpperCase();
they were not already
KEYBOARD INPUT
SCANNER CLASS
 Used in conjunction with System.in
 Import statement

Data type

Connects it to a System.in object


Reference variable
Creates an object in memory
SCANNER CLASS METHODS
 nextLine
 next
 nextInt
 nextDouble
 nextBoolean
JOPTIONPANE CLASS

 Import from javax.swing


 No reference variable necessary
 Only returns a string so you have to parse the output to store it in a numeric variable
 Integer.parseInt
 Double.parseDouble

You might also like