30
Fundamental concepts in Java

Fundamental concepts in Java. Lesson plan Variable declaration, assign statement & practice Design document & practice

Embed Size (px)

Citation preview

Fundamental conceptsin Java

Lesson plan

• Variable declaration, assign statement & practice

• Design document & practice

Numeric data

Variable declaration:

<data type> <variable_name>;

If more than one variable has the same data type:

<data type> <name1>, <name2>..;

/*** Sample program …**/import javax.swing.*;import java.text.*;

public class LoanCalculator{

public static void main(String[] args) {

double loanAmount; // represents the amount being borroweddouble annualInterestRate; // represents the interest rate annuallydouble monthlyPayment; // represents the amount of money one has todouble totalPayment; // represents the amount of money one has to pay int loanPeriod; // represents the number of years of the loanString inputStr; // is a place holder to get input value from a user

…..

VariableDeclaration

Six numerical data types

• byte: -128 to 127

• short:-32768 to 32767 (-215 to 215-1)

• int: -231 to 231-1

• long: -263 to 263-1

• float: -3.4E+38 to 3.4E+38

• double:-1.797E+308 to 1.797E+308

Assignment statement<variable name> = <expression>;

Example:

x =2*5+6-1;

Variable names

• It must be a legal identifier.

• It must not be a keyword, a boolean literal (true or false), or the reserved word null.

• It must be unique within its scope.

Variable name (cont.)

• Legal identifier:be composed of letters, numbers, _ and $. Identifiers may only begin with a letter, _, or $.

• Keyword:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html

• Variable names begin with a lowercase letter• Class names begin with an uppercase letter

Arithmetic Expression

• Any expression involving numerical value is called arithmetic expression

For example:

x+15

y /16

Arithmetic operators• Addition: +

x+y• Subtraction: -

x-y• Multiplication: *

x*y• Division: /

x/y• Modulo: %

x%y

Constant/*** Sample program …**/import javax.swing.*;import java.text.*;

public class LoanCalculator{

public static void main(String[] args) {

final int MONTHS_IN_YEAR = 12;

// double loanAmount; // represents the amount being ..int loanPeriod; // represents the number of years of the loanString inputStr;…..inputStr = JOptionPane.showInputDialog(null,"Loan Amount

(dollars.cents):");

Constant and variables

• Constant:– Value it contains doesn’t change

final int MONTHS_IN_YEAR = 12;

• Variables:– Value it contains may vary

double loanAmount;

• loanAmount =0;

• loanAmount = 1000.99;

Formula representation

• X = 10• Y = 5

What are the values of: sum = x+y difference = x-y product = x*y

quotient = x/y remainder = x%y

24= Math.pow(2,4)

Practice

import javax.swing.*;import java.text.*;public class PrintProduct{

public static void main(String[] args) {double first Number; // invalid variable name, space is in betweendouble 12_second%Number; //start with a number, % is not alloweddouble _13_thirdNumber; //OKdouble $product; // OKfirstNumber=10.12; //12_second%Number=12.6; product== first Number * 12_second%Number; //invalid assignment

}}

Integer division and type casting

• Integer division:– Integer/integer = integer, 7/2 = 3

– Integer/double = double, 7/2.0 = 3.5

– Double/integer = double, 7.0/2 = 3.5

• Type casting: a process that converts a value of one data type to another data type.

Implicit casting

Explicit casting

Type casting (cont.)

• Implicit casting:– Operand is converted from a lower to a

higher precision– Higher precision: a data type with a larger

range of values• Double has a higher precision than float• Int has a higher precision than short

– Operand: can be a constant, variable, method call or another arithmetic expression

Type casting (cont.)

• Explicit casting– (<data type>) <expression>– Example:

float result;

result = (float) ((3+5)/6);

and

result = ((float) (5+3))/6;

More formula representation

• xn – Math.pow(x,n)

How about: (1+i)n

– Math.pow((1+i),n)

More formula representation

How about:

P=V*(i/(1-(Math.pow(1+i,-n))))

ni

iVP

)1(1

LoanCalculator.java

• Steps:– Describe the program/problem (optional)– Get input from users– Computation (compute monthly payment

and total payment)– Display the results to users

Get input values from users

• Example:inputStr = JOptionPane.showInputDialog(null,”<message>”);

If variable has double data type:

<variable_name> = Double.parseDouble(inputStr);

If variable has integer data type:

<variable_name> = Integer.parseInt(inputStr);

Get input values from usersUsing System.in: import java.io.*; try {

InputStreamReader in = new InputStreamReader( System.in ); BufferedReader stdin = new BufferedReader( in ); System.out.print( "Loan amount (dollars.cents: " ); inputStr = stdin.readLine();} catch(IOException e) {

System.out.println("Error in reading a user's input");System.exit(-1);

}loanAmount = Double.parseDouble(inputStr);

• …..

Display results to users

• System.out.println(“message :”+<variable name>);

Example:

System.out.println("Loan Amount: $"+ loanAmount);

System.out.println("Annual Interest Rate: "+ annualInterestRate +"%");

Design

• Identify classes based on naming convention:– Class name starts with Capital letter

DesignClass Purpose

LoanCalculator -What is it?Example: The main class of the program-What does it do? It computes the monthly payment and total payment for a loan

JOptionPane -What is it? Java library -What does it do?It displays a dialog box to get inputs from a user

Design

String - What is it? Java library- What does it do?String manipulation, represents

a place holder for storing a user’s inputs

DecimalFormat - What is it? Java library- What does it do?Use to format numbers

Design

String -What is it? Java library-What does it do?String manipulation, represents a place holder for storing a user’s inputs

DecimalFormat -What is it? Java library-What does it do?Use to format numbers

Design

Double -What is it? Java library-What does it doRepresent double data type as an object

Integer -What is it? Java library-What does it do?Represent integer data type as an object

Design

Math -What is it? Java library-What does it do?Perform numeric operations

System -What is it? Java library-What does it do? Print a message to the screen and exit the program

Design

LoanCalculator

JOptionPane

System

Integer

Double

String

DecimalFormat

Math