Download Program Coins1.java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1
Chapter 2: Fundamental Data Types
Chapter 2
Fundamental Data Types
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
2
Chapter 2: Fundamental Data Types
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);
}
}
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
3
Chapter 2: Fundamental Data Types
Number types
• int : integer
• double : double-precision floating-point
numbers
• Variable declaration:
int n;
double total = 0.5;
• Quality tip: Use descriptive variable names
int nickels;
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
4
Chapter 2: Fundamental Data Types
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);
}
}
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
5
Chapter 2: Fundamental Data Types
Assignment
• Assign a new value to a variable
• variableName = expression;
• total = total + dimes * 0.1;
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
6
Chapter 2: Fundamental Data Types
Figure 1
Assignment
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
7
Chapter 2: Fundamental Data Types
Increment and decrement
• month++;
month--;
• Shortcuts for
month = month + 1;
month = month - 1;
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
8
Figure 2
Incrementing
a
Variable
Chapter 2: Fundamental Data Types
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
9
Chapter 2: Fundamental Data Types
Type conversion
• In assignment, types must match.
double total = "a lot"; // no
• Use “cast” (int)to convert floating-point
values to integer values:
int pennies
= (int)(total * 100);
• Use Math.round for rounding:
int dollar =
©2000, John Wiley & Sons, Inc.
(int)Math.round(total);
Horstmann/Java Essentials, 2/e
10
Chapter 2: Fundamental Data Types
Static method calls
• ClassName.MethodName( parameters )
• Invoke a method that doesn't operate on an
object
• Example: Math.round(3.14)
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
11
Chapter 2: Fundamental Data Types
Program Volume.java
public class Volume
{ public static void main(String[] args)
{ final double BOTTLE_VOLUME = 2.0;
final double CAN_VOLUME = 0.355;
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
12
Chapter 2: Fundamental Data Types
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
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
13
Chapter 2: Fundamental Data Types
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");
}
}
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
14
Program Coins3.java
Chapter 2: Fundamental Data Types
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
int
int
int
pennies = 8; // the purse contains 8 pennies,
nickels = 0; // no nickels,
dimes = 4; // four dimes,
quarters = 3; // and three quarters
// compute total value in pennies
int total = pennies * PENNY_VALUE
+ nickels * NICKEL_VALUE
+ dimes * DIME_VALUE
+ quarters *
QUARTER_VALUE;
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
15
Chapter 2: Fundamental Data Types
// 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");
}
}
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
16
Chapter 2: Fundamental Data Types
Figure 3
Analyzing an Expression
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
17
Chapter 2: Fundamental Data Types
Arithmetic
• Operators
+ - * /
• Integer division
9 / 4 is 2 and not 2.25!
9 % 4 is 1
• Common functions
Math.pow(x,y)
Math.sqrt(x)
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
18
Chapter 2: Fundamental Data Types
Figure 4
On-Line Help
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
19
Chapter 2: Fundamental Data Types
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);
}
}
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
20
Chapter 2: Fundamental Data Types
Strings
• String constants: "Carl"
• String variables:
String name = "Carl";
• String length:
int n = name.length();
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
21
Chapter 2: Fundamental Data Types
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
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
22
Chapter 2: Fundamental Data Types
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 string:
String a = "Agent";
String name = a + 7;
©2000, John Wiley & Sons, Inc.
• name is "Agent7"
Horstmann/Java Essentials, 2/e
23
Chapter 2: Fundamental Data Types
Converting between strings and
numbers
• Convert to number:
int n = Integer.parseInt(str);
double x =
Double.parseDouble(x);
• Convert to string:
String str = "" + n;
str = Integer.toString(n);
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
24
Chapter 2: Fundamental Data Types
Formatting numbers
• NumberFormat formatter =
NumberFormat.getNumberInstance();
• formatter.setMaximumFractionDigits(2);
formatter.setMinimumFractionDi
gits(2);
• formatter.format(tax);
• prints 0.30
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
25
Chapter 2: Fundamental Data Types
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();
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
26
Chapter 2: Fundamental Data Types
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));
}
}
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
27
Chapter 2: Fundamental Data Types
Reading input
• ConsoleReader console = new
ConsoleReader(System.in);
• int pennies = console.readInt();
• Also readDouble , readLine
• Not a standard Java class. Include
ConsoleReader.java in same directory,
or paste into source file
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
28
Chapter 2: Fundamental Data Types
Program Coins5.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Coins5
{ public static void
{ try
{ final double
final double
final double
final double
main(String[] args)
PENNY_VALUE = 0.01;
NICKEL_VALUE = 0.05;
DIME_VALUE = 0.1;
QUARTER_VALUE = 0.25;
InputStreamReader reader
= new InputStreamReader(System.in);
BufferedReader console
= new BufferedReader(reader);
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
29
Chapter 2: Fundamental Data Types
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);
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
30
Chapter 2: Fundamental Data Types
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);
}
}
}
©2000, John Wiley & Sons, Inc.
Horstmann/Java Essentials, 2/e
Related documents