Fundamental Data Types - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Fundamental Data Types

Description:

NumberFormat formatter = NumberFormat.getCurrencyInstance(); System. ... formatter.format(total)); 2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e ... – PowerPoint PPT presentation

Number of Views:10
Avg rating:3.0/5.0
Slides: 31
Provided by: Hub82
Category:

less

Transcript and Presenter's Notes

Title: Fundamental Data Types


1
Chapter 2
Fundamental Data Types
2
Program Coins1.java public class Coins1
public static void main(String args) int
pennies 8 // the purse contains 8 pennies,
int dimes 4 // four dimes,
int quarters 3 // and three quarters
// compute total value of the coins
double total pennies 0.01 dimes
0.10 quarters 0.25
// print result System.out.print("Total
value ") System.out.println(total)

3
Number types
  • int integer
  • double double-precision floating-point numbers
  • Variable declarationint ndouble total 0.5
  • Quality tip Use descriptive variable namesint
    nickels

4
Program Coins2.java public class Coins2
public static void main(String args) int
pennies 8 // eight pennies in the purse
double total pennies 0.01 int dimes
4 // four dimes in the purse // add value
of dimes total total dimes 0.10
int quarters 3 // three quarters in the
purse // add value of quarters
total total quarters 0.25
System.out.print("Total value ")
System.out.println(total)
5
Assignment
  • Assign a new value to a variable
  • variableName expression
  • total total dimes 0.1

6
Figure 1 Assignment
7
Increment and decrement
  • monthmonth--
  • Shortcuts formonth month 1month month -
    1

8
Figure 2 Incrementing a Variable
9
Type conversion
  • In assignment, types must match.double total
    "a lot" // no
  • Use cast (int)to convert floating-point values
    to integer valuesint pennies (int)(total
    100)
  • Use Math.round for roundingint dollar
    (int)Math.round(total)

10
Static method calls
  • ClassName.MethodName( parameters )
  • Invoke a method that doesn't operate on an object
  • Example Math.round(3.14)

11
Program Volume.java public class Volume
public static void main(String args)
final double BOTTLE_VOLUME 2.0 final
double CAN_VOLUME 0.355
12
Constants
  • final TypeName VariableName Expression
  • Defines a constant and assign its value
  • Example final double CAN_VOLUME 0.355
  • Useful constants Math.PI, Math.E
  • Quality tip No magic numbers

13
int bottles 4 // we have four bottles
int cans 10 // and ten cans //
compute total volume double total
bottles BOTTLE_VOLUME cans
CAN_VOLUME // print result
System.out.print("The total volume is ")
System.out.print(total) System.out.println(
liters")
14
Program Coins3.java public class Coins3 public
static void main(String args) final int
PENNY_VALUE 1 final int NICKEL_VALUE
5 final int DIME_VALUE 10 final
int QUARTER_VALUE 25 final int
DOLLAR_VALUE 100 int pennies 8 //
the purse contains 8 pennies, int nickels
0 // no nickels, int dimes 4 // four
dimes, int quarters 3 // and three
quarters // compute total value in
pennies int total pennies PENNY_VALUE
nickels NICKEL_VALUE dimes
DIME_VALUE quarters
QUARTER_VALUE
15
// use integer division to convert to
dollars, cents int dollar total /
DOLLAR_VALUE int cents total
DOLLAR_VALUE System.out.print("Total value
") System.out.print(dollar)
System.out.print( dollars and ")
System.out.print(cents) System.out.println(
" cents")
16
Figure 3 Analyzing an Expression
17
Arithmetic
  • Operators - /
  • Integer division9 / 4 is 2 and not 2.25!9 4
    is 1
  • Common functionsMath.pow(x,y) Math.sqrt(x)

18
Figure 4 On-Line Help
19
Program MakePassword.java public class
MakePassword public static void main(String
args) String firstName "Harold"
String middleName "Joseph" String
lastName "Hacker" // extract initials
String initials firstName.substring(0, 1)
middleName.substring(0, 1)
lastName.substring(0, 1) // append age
int age 19 // the age of the user
String password initials.toLowerCase() age
System.out.println("Your password is
password)
20
Strings
  • String constants "Carl"
  • String variables String name "Carl"
  • String lengthint n name.length()

21
Substrings
  • String greeting "Clown"String sub
    greeting.substring(1, 4)
  • Supply start and past the end position
  • First position is at 0
  • 0C1l2o3w4n
  • substring length past the end - start

22
Concatenation
  • String fname "Harry"String lname
    "Hacker"String name fname lname
  • name is "HarryHacker"
  • If one operand of is a string, the other is
    converted to a stringString a "Agent"String
    name a 7
  • name is "Agent7"

23
Converting between strings and numbers
  • Convert to numberint n Integer.parseInt(str)
    double x Double.parseDouble(x)
  • Convert to stringString str "" nstr
    Integer.toString(n)

24
Formatting numbers
  • NumberFormat formatter NumberFormat.getNumberIns
    tance()
  • formatter.setMaximumFractionDigits(2)formatter.s
    etMinimumFractionDigits(2)
  • formatter.format(tax)
  • prints 0.30

25
Program Coins4.java import java.text.NumberFormat
public class Coins4 public static void
main(String args) final double
PENNY_VALUE 0.01 final double
NICKEL_VALUE 0.05 final double
DIME_VALUE 0.1 final double
QUARTER_VALUE 0.25 ConsoleReader
console new ConsoleReader(System.in)
System.out.println("How many pennies do you
have?") int pennies console.readInt()
System.out.println("How many nickels do you
have?") int nickels console.readInt()
System.out.println("How many dimes do you
have?") int dimes console.readInt()
System.out.println("How many quarters do you
have?") int quarters console.readInt()

26
double total pennies PENNY_VALUE
nickels NICKEL_VALUE dimes
DIME_VALUE quarters QUARTER_VALUE
// total value of the coins
NumberFormat formatter
NumberFormat.getCurrencyInstance()
System.out.println("Total value "
formatter.format(total))
27
Reading input
  • ConsoleReader console new ConsoleReader(System.i
    n)
  • int pennies console.readInt()
  • Also readDouble , readLine
  • Not a standard Java class. Include
    ConsoleReader.java in same directory, or paste
    into source file

28
Program Coins5.java import java.io.BufferedReader
import java.io.InputStreamReader import
java.io.IOException public class Coins5
public static void main(String args) try
final double PENNY_VALUE 0.01
final double NICKEL_VALUE 0.05 final
double DIME_VALUE 0.1 final double
QUARTER_VALUE 0.25 InputStreamReader
reader new InputStreamReader(System.
in) BufferedReader console
new BufferedReader(reader)
29
System.out.println("How many pennies do
you have?") String input
console.readLine() int pennies
Integer.parseInt(input)
System.out.println("How many nickels do you
have?") input console.readLine()
int nickels Integer.parseInt(input)
System.out.println("How many dimes do you
have?") input console.readLine()
int dimes Integer.parseInt(input)
System.out.println("How many quarters do you
have?") input console.readLine()
int quarters Integer.parseInt(input)



30
double total pennies PENNY_VALUE
nickels NICKEL_VALUE
dimes DIME_VALUE quarters
QUARTER_VALUE // total value
of the coins System.out.println("Total
value total)
catch(IOException e) System.out.println(e
) System.exit(1)
Write a Comment
User Comments (0)
About PowerShow.com