Download Do Now - mrnateghiaslitechnology.com

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
Do Now
• Login to your computer.
• Mr. Nateghi‐Asli needs to buy a pick‐up truck for his farm. He is very interested in the new 2016 Ford F‐150’s . He is trying to figure out what would the total cost of the truck be with tax. Below is a program that will help him figure out the total cost.
• On the index card given to you, write down what you think the output will look like in the console. You will see some new code here that we are going to learn today. Predict how this code will work with the program. You have 5 minutes to write your output.
Java Foundations, 3rd Edition, Lewis/DePasquale/Chase
3 ‐ 46
Objectives and Agenda
Objectives
• By the end of the period students will be able to:
• Use the number format and decimal format classes in order to create programs that allow for specified decimal places, monetary format, and percent.
Java Foundations, 3rd Edition, Lewis/DePasquale/Chase
Agenda
• Do Now
• Number Format and Decimal Format PPT.
• Printf method (if we have time today)
• Practice Projects.
3 ‐ 47
Formatting Ouput
• It is often necessary to format values in certain ways so that they can be presented properly
• The Java API contains classes that provide formatting capabilities
• The NumberFormat class allows you to format values as currency or percentages
• The DecimalFormat class allows you to format values based on a pattern
• Both are part of the java.text package
Java Foundations, 3rd Edition, Lewis/DePasquale/Chase
3 ‐ 48
Formatting Output
• The NumberFormat class has static methods that return a formatter object
getCurrencyInstance()
getPercentInstance()
• Each formatter object has a method called format that returns a string with the specified information in the appropriate format
Java Foundations, 3rd Edition, Lewis/DePasquale/Chase
3 ‐ 49
Formatting Output
• Some methods of the NumberFormat class:
Java Foundations, 3rd Edition, Lewis/DePasquale/Chase
3 ‐ 50
//********************************************************************
// Purchase.java
Java Foundations
//
// Demonstrates the use of the NumberFormat class to format output.
//********************************************************************
import java.util.Scanner;
import java.text.NumberFormat;
public class Purchase
{
//----------------------------------------------------------------// Calculates the final price of a purchased item using values
// entered by the user.
//----------------------------------------------------------------public static void main(String[] args)
{
final double TAX_RATE = 0.06; // 6% sales tax
int quantity;
double subtotal, tax, totalCost, unitPrice;
Scanner scan = new Scanner(System.in);
NumberFormat fmt1 = NumberFormat.getCurrencyInstance();
NumberFormat fmt2 = NumberFormat.getPercentInstance();
Java Foundations, 3rd Edition, Lewis/DePasquale/Chase
3 ‐ 51
System.out.print("Enter the quantity: ");
quantity = scan.nextInt();
System.out.print("Enter the unit price: ");
unitPrice = scan.nextDouble();
subtotal = quantity * unitPrice;
tax = subtotal * TAX_RATE;
totalCost = subtotal + tax;
// Print output with appropriate formatting
System.out.println("Subtotal: " + fmt1.format(subtotal));
System.out.println("Tax: " + fmt1.format(tax) + " at "
+ fmt2.format(TAX_RATE));
System.out.println("Total: " + fmt1.format(totalCost));
}
}
Java Foundations, 3rd Edition, Lewis/DePasquale/Chase
3 ‐ 52
Formatting Output
• The DecimalFormat class can be used to format a floating point value in various ways
• For example, you can specify that the number should be truncated to three decimal places
• The constructor of the DecimalFormat class takes a string that represents a pattern for the formatted number
Java Foundations, 3rd Edition, Lewis/DePasquale/Chase
3 ‐ 53
Formatting Output
• Some methods of the DecimalFormat class:
Java Foundations, 3rd Edition, Lewis/DePasquale/Chase
3 ‐ 54
//********************************************************************
// CircleStats.java
Java Foundations
//
// Demonstrates the formatting of decimal values using the
// DecimalFormat class.
//********************************************************************
import java.util.Scanner;
import java.text.DecimalFormat;
public class CircleStats
{
//----------------------------------------------------------------// Calculates the area and circumference of a circle given its
// radius.
//----------------------------------------------------------------public static void main(String[] args)
{
int radius;
double area, circumference;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the circle's radius: ");
radius = scan.nextInt();
Java Foundations, 3rd Edition, Lewis/DePasquale/Chase
3 ‐ 55
area = Math.PI * Math.pow(radius, 2);
circumference = 2 * Math.PI * radius;
// Round the output to three decimal places
DecimalFormat fmt = new DecimalFormat("0.###");
System.out.println("The circle's area: " + fmt.format(area));
System.out.println("The circle's circumference: "
+ fmt.format(circumference));
}
}
Java Foundations, 3rd Edition, Lewis/DePasquale/Chase
3 ‐ 56
Practice
• Programming Projects 3.5, 3.6 and #3
Java Foundations, 3rd Edition, Lewis/DePasquale/Chase
3 ‐ 57