Anda di halaman 1dari 9

Java Basics

Software Development Process


GEEN163
• Requirements – high level description of
Introduction to what the system is supposed to do
Computer Programming • Specifications – detailed description
• Design – how will the system do this
• Coding – writing the program
Java Basics
• Integration – putting the pieces together
• Testing – make sure it works correctly
• Maintenance – updates and corrections

Class Programs Structure of a Java Program


• Requirements – faculty will define briefly import package.class;
• Specifications – faculty will define public class progName {
• Design – student responsibility public static void main(String[] args) {
• Coding – student responsibility // your simple program here
• Testing – informal student responsibility }
}

GEEN163 Honors 1
Java Basics

import import example


• If you use existing objects in your program, import javax.swing.JOptionPane;

you need to tell Java where to find them.
JOptionPane.showMessageDialog(…);
• import tells Java to look in this package for
classes you are using.
or without import
• import is not required. You can always use
the full name of an object.
javax.swing.JOptionPane.showMessageDialog(…);

Class statement main method


• Java is an Object-Oriented language. • Methods are functions or programs that
• Objects are defined by classes. can be executed.
• All Java programs are an object. • The method with the name “main” is
• Much more on objects to come later. always executed first.
• main has an array of String parameters
that can be used to pass command line
arguments.

GEEN163 Honors 2
Java Basics

Names Java Conventions


• While the Java language allows you to use
• Names or identifiers in Java (such as upper and lower case letters as you please,
program names, class names, variable tradition dictates you follow some rules.
names, etc.) can be as long as you like. • Variables and methods names start with a
• Names should make sense to humans. lower case letter.
• Names can contain letters, numbers • Class names start with an upper case letter
underscores (_), and dollar signs ($). • Constants are all upper case
• Names should start with a letter. • When you combine two English words,
• Java is case sensitive, upper and lower upper case the first letter of the second
case letters are different. word (i.e. firstPlace, mySchool, whoCares)

Which of these are valid names? Which of these are valid names?
a) abc e) ABC i) a_1 a) abc e) ABC i) a_1
b) m/h f) 25or6to4 j) studentNumber b) m/h f) 25or6to4 j) studentNumber
c) main g) 1_time k) Main c) main g) 1_time k) Main
d) double h) first name l) ______ d) double h) first name l) ______

GEEN163 Honors 3
Java Basics

Common Operations on
Variables Variables
• Variables hold data. They represent a • Common Operations for many types of
memory location. variables include these four
– Declaration Construct an variable
• Variables have a:
Initialization and optionally initialize its value
– Name – Assignment Modify the value of an variable
– Type – Input Read the value from keyboard
– Value – Output Write the value of an variable
• Before you set a variable’s value, it may to the screen
not have a reliable value.

Simple Data Types Other Simple Data Types


• int – integer whole numbers. • char – single characters
• double – numbers with decimal points. • boolean – true or false

• float – a double with 7 digit accuracy


• short – a 2 byte int for number < 16384
• long – a 8 byte int for numbers > 2 billion
• byte – a 1 byte int for numbers < 128

GEEN163 Honors 4
Java Basics

A Not-So-Simple Data Type Constants


• String – A string of characters. • int 1 2 3 -6 123456
• Any character on the keyboard (and more) • double -1.234 0.000123 1.23e-4
can be used. • String “inside double quotes”
• String is a Java class. We will talk more • boolean true false
about classes and complex data types • character ‘x’ // a single character in single quotes
later.

Declaring Variables Example Declarations


• All variables must be declared before the double interestRate;
are used in the program. int numPenguins;
• A variable is declared by writing the data String myName;
type followed by the variable name. boolean doit;
• More than one variable may be declared int first, second;
on the same line as the same type by
separating the variable names by commas

GEEN163 Honors 5
Java Basics

Assigning Values Not All Are Equal


• You can set a variable to a value during • The equals sign is used to set a variable to
execution by putting the name of variable, a value. It does not mean equal in the
an equals sign followed by a value and mathematical sense.
semicolon int a, b;
numPenguins = 6; a = 3;
first = 3; b = 5;
interestRate = 4.75; a = b; // a has the value 5
An old computer language used to use the arrow
character to indicate assignment.
• The type of the variable and the value a ← 3; b ← 5; a ← b; // not C++
must match.

Variable Initialization Example Initializations


• When you declare a variable, you can give double interestRate = 0.075;
it an initial value. int numPenguins = 7;
• If you don’t give a variable an initial value, String myName = “Ken”;
it will have some unknown random value. boolean doit = false;
• In the declaration after the variable name, int first = 1, second = 2;
put an equals sign followed by the value.
int bad = “This is wrong”;
• The type of the variable and the value
must match.

GEEN163 Honors 6
Java Basics

What is an Expression in Java? Operators can be


• An expression or equation is a valid binary involving 2 operands 2+3
arrangement of variables, constants,
and operators. unary involving 1 operand -3

• in Java each expression can be


evaluated to compute a value of a ternary involving 3 operands later
given type

• the value of the expression


9.3 * 4.5 is 41.85

Some Java Arithmetic Operators Modulus Operator


12 + 3 addition • the modulus operator % can only be used
12 - 3 subtraction with integer type operands and always has an
integer type result
12 * 3 multiplication
• its result is the integer type remainder of an
12 / 3 division integer division
12 % 3 modulus or remainder EXAMPLE
-12 negative 11 % 4 has value 3 because
+12 positive (pointless, do not use) R=3
4 ) 11

GEEN163 Honors 7
Java Basics

Integer Division Operator Priority


• Integers can only hold whole numbers Precedence Operator Description
• If division results in a fractional part, the Higher ( ) Parenthesis
fraction is dropped and the result is just + Positive
the whole number. - Negative
* Multiplication
/ Division
8/2 is 4 7 / 3 is 2 % Modulus (remainder)
+ Addition
1 / 2 is 0 5 / 4 is 1 - Subtraction
Lower = Assignment

Expression Evaluation Evaluate the Expression


• Higher precedence determines which
2*3 equals 6
operator is applied first in an expression
having several operators 2*3+1 equals 7
• An expression is evaluated by performing 1+2*3 equals 7
the operations in precedence order. (1 + 2) * 3 equals 9
• Operations of equal precedence are 3 + 7 / 2 + 1 equals 7
performed left to right. (3 + 7) / 2 + 1 equals 6
• Expressions inside parenthesis are (3 + 7) / (2 + 1) equals 3
evaluated first.

GEEN163 Honors 8
Java Basics

Evaluate the Expression Parentheses


7 * 10 - 5 % 3 * 4 + 9 • parentheses can be used to change the
means (7 * 10) - 5 % 3 * 4 + 9 usual order
70 - 5 % 3 * 4 + 9 • parts in ( ) are evaluated first
70 - (5 % 3) * 4 + 9
• evaluate (7 * (10 - 5) % 3) * 4 + 9
70 - 2 * 4 + 9
(7 * 5 % 3 ) * 4 + 9
70 - ( 2 * 4 ) + 9
( 35 % 3 ) * 4 + 9
70 - 8 + 9
2 * 4 + 9
( 70 - 8 ) + 9
8 + 9
62 + 9
17
71

GEEN163 Honors 9

Anda mungkin juga menyukai