Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Fundamental concepts in Java. Lesson plan Variable declaration, assign statement & practice Design document & practice."— Presentation transcript:

1 Fundamental concepts in Java

2 Lesson plan Variable declaration, assign statement & practice Design document & practice

3 Numeric data Variable declaration: ; If more than one variable has the same data type:,..;

4 /** * Sample program … **/ import javax.swing.*; import java.text.*; public class LoanCalculator{ public static void main(String[] args) { double loanAmount; // represents the amount being borrowed double annualInterestRate; // represents the interest rate annually double monthlyPayment; // represents the amount of money one has to double totalPayment; // represents the amount of money one has to pay int loanPeriod; // represents the number of years of the loan String inputStr; // is a place holder to get input value from a user ….. Variable Declaration

5 Six numerical data types byte: -128 to 127 short:-32768 to 32767 (-2 15 to 2 15 -1) int: -2 31 to 2 31 -1 long: -2 63 to 2 63 -1 float: -3.4E+38 to 3.4E+38 double:-1.797E+308 to 1.797E+308

6 Assignment statement = ; Example: x =2*5+6-1;

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

8 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/nu tsandbolts/_keywords.html Variable names begin with a lowercase letter Class names begin with an uppercase letter

9 Arithmetic Expression Any expression involving numerical value is called arithmetic expression For example: x+15 y /16

10 Arithmetic operators Addition: + x+y Subtraction: - x-y Multiplication: * x*y Division: / x/y Modulo: % x%y

11 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 loan String inputStr; ….. inputStr = JOptionPane.showInputDialog(null,"Loan Amount (dollars.cents):");

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

13 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 2 4 = Math.pow(2,4)

14 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 between double 12_second%Number; //start with a number, % is not allowed double _13_thirdNumber; //OK double $product; // OK firstNumber=10.12; // 12_second%Number=12.6; product== first Number * 12_second%Number; //invalid assignment }

15 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

16 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

17 Type casting (cont.) Explicit casting –( ) –Example: float result; result = (float) ((3+5)/6); and result = ((float) (5+3))/6;

18 More formula representation x n –Math.pow(x,n) How about: (1+i) n –Math.pow((1+i),n)

19 More formula representation How about: P=V*(i/(1-(Math.pow(1+i,-n))))

20 LoanCalculator.java Steps: –Describe the program/problem (optional) –Get input from users –Computation (compute monthly payment and total payment) –Display the results to users

21 Get input values from users Example: inputStr = JOptionPane.showInputDialog(null,” ”); If variable has double data type: = Double.parseDouble(inputStr); If variable has integer data type: = Integer.parseInt(inputStr);

22 Get input values from users Using 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); …..

23 Display results to users System.out.println(“message :”+ ); Example: System.out.println("Loan Amount: $"+ loanAmount); System.out.println("Annual Interest Rate: "+ annualInterestRate +"%");

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

25 Design ClassPurpose 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

26 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

27 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

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

29 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

30 Design LoanCalculator JOptionPane System Integer Double String DecimalFormat Math


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

Similar presentations


Ads by Google