Gaddis-ch3

3-24 Format4

posted Feb 1, 2014, 9:43 AM by Dlovan ismail   [ updated Feb 1, 2014, 9:43 AM by Karwan Jacksi ]

/*
This program uses the DecimalFormat class to display
formatted numbers in a message dialog.
*/

package format4;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Format4 
{
    public static void main(String[] args) 
    {
           double number1 = 0.12;
           double number2 = 0.05;

           // Create a DecimalFormat object.
           DecimalFormat formatter = new DecimalFormat("#0%");

          // Display the formatted variable contents.
           JOptionPane.showMessageDialog(null, formatter.format(number1));
           JOptionPane.showMessageDialog(null, formatter.format(number2));
    }   
}

3-23 Format3

posted Feb 1, 2014, 9:41 AM by Dlovan ismail   [ updated Feb 1, 2014, 9:41 AM by Karwan Jacksi ]

/*
 This program uses the DecimalFormat class to display
 formatted numbers in a message dialog.
*/

package format3;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Format3 
{
    public static void main(String[] args)
    {
           double number1 = 123.899;
           double number2 = 1233.899;
           double number3 = 12345.899;
           double number4 = 123456.899;
           double number5 = 1234567.899;

           // Create a DecimalFormat object.
           DecimalFormat formatter = new DecimalFormat("#,##0.00");

           // Diiplay the formatted variable contents.
           JOptionPane.showMessageDialog(null,formatter.format(number1));
           JOptionPane.showMessageDialog(null,formatter.format(number2));
           JOptionPane.showMessageDialog(null,formatter.format(number3));
           JOptionPane.showMessageDialog(null,formatter.format(number4));
           JOptionPane.showMessageDialog(null,formatter.format(number5));
       
    } 
}

3-22 Format2

posted Feb 1, 2014, 9:39 AM by Dlovan ismail   [ updated Feb 1, 2014, 9:39 AM by Karwan Jacksi ]

/**
This program uses the DecimalFormat class to display
formatted numbers in a message dialog.
*/
package format2;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Format2 
{
    public static void main(String[] args) 
    {
            double numberl=0.166666666666667;
            double number2=1.666666666666667;
            double number3=16.666666666666667;
            double number4=166.666666666666667;

            // Create a DecimalFormat object.
            DecimalFormat formatter = new DecimalFormat("00.00");

            // Display the formatted variable contents.
            JOptionPane.showMessageDialog(null, formatter.format(numberl));
            JOptionPane.showMessageDialog(null, formatter.format(number2));
            JOptionPane.showMessageDialog(null, formatter.format(number3));
            JOptionPane.showMessageDialog(null, formatter.format(number4));
    }    
}

3-21 Format1

posted Feb 1, 2014, 6:55 AM by Dlovan ismail   [ updated Feb 1, 2014, 6:55 AM by Karwan Jacksi ]

/*
This program uses the DecimalFormat class to display
formatted numbers in a message dialog.
*/

package format1;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Format1 
{
    public static void main(String[] args)
    {
            double numberl = 0.166666666666667;
            double number2 = 1.666666666666667;
            double number3 = 16.666666666666667;
            double number4 = 166.666666666666667;

           // Create a DecimalFormat object.
            DecimalFormat formatter = new DecimalFormat("0.00");

           // Display the formatted variable contents.
            JOptionPane.showMessageDialog(null, formatter.format(numberl));
            JOptionPane.showMessageDialog(null, formatter.format(number2));
            JOptionPane.showMessageDialog(null, formatter.format(number3));
            JOptionPane.showMessageDialog(null, formatter.format(number4));
    }   
}

3-20 LeftJustified

posted Feb 1, 2014, 6:51 AM by Dlovan ismail   [ updated Feb 1, 2014, 6:51 AM by Karwan Jacksi ]

/*
This program displays a variety of
numbers left - justified in columns.
*/

package leftjustified;

public class LeftJustified 
{
    public static void main(String[] args)
    {
            // Declare a variety of int variables.
            int numl = 123;
            int num2 = 12;
            int num3 = 45678;
            int num4 = 456;
            int num5 = 1234567;
            int num6 = 1234;

           // Display each variable left-juatified
           // in a field of 8 spaces.
            System.out.printf("%-8d%-8d\n",numl,num2);
            System.out.printf("%-8d%-8d\n",num3,num4);
            System.out.printf("%-8d%-8d\n",num5,num6);
    }   
}

3-19 LeadingZeros

posted Feb 1, 2014, 6:48 AM by Dlovan ismail   [ updated Feb 1, 2014, 6:48 AM by Karwan Jacksi ]

//This program displays numbers padded with leading zeros.

package leadingzeros;

public class LeadingZeros 
{
    public static void main(String[] args)
    {
            // Declare a variety of double variables.
            double number1 = 1.234;
            double number2 = 12.345;
            double number3 = 123.456;

            // Display each variable with leading
            // zeros, in a field of 9 spaces, rounded
            // to 2 decimal places.
            System.out.printf("%09.2f\n", number1);
            System.out.printf("%09.2f\n", number2);
            System.out.printf("%09.2f\n", number3);
    }   
}

3-18 CurrencyFormat

posted Feb 1, 2014, 6:45 AM by Dlovan ismail   [ updated Feb 1, 2014, 6:48 AM by Karwan Jacksi ]

/*
This program demonstrates how to use the System.out.printf
method to format a number as currency.
*/

package currencyformat;

public class CurrencyFormat
{
    public static void main(String[] args) 
    {
            double monthlyPay = 5000.0;
            double annualPay = monthlyPay * 12;
            System.out.printf("Your annual pay is $%,.2f\n", annualPay);
    }  
}

3-17 Columns

posted Feb 1, 2014, 6:40 AM by Dlovan ismail   [ updated Feb 1, 2014, 6:40 AM by Karwan Jacksi ]

/*
This program displays a variety of
floating-point numbers in a column
with their decimal points aligned.
*/

package columns;

public class Columns 
{
    public static void main(String[] args)
    {
            // Declare a variety of double variables.
            double num1 = 127.899;
            double num2 = 3465.148;
            double num3 = 3.776;
            double num4 = 264.821;
            double num5 = 88.081;
            double num6 = 1799.999;

            // Display each variable in a field of
            // 8 spaces with 2 decimal places.
            System.out.printf("%8.2f\n",num1);
            System.out.printf("%8.2f\n",num2);
            System.out.printf("%8.2f\n",num3);
            System.out.printf("%8.2f\n",num4);
            System.out.printf("%8.2f\n",num5);
            System.out.printf("%8.2f\n",num6);
    }   
}

3-16 Seasons

posted Jan 31, 2014, 6:29 AM by Dlovan ismail   [ updated Jan 31, 2014, 6:29 AM by Karwan Jacksi ]

/**
This program translates the English names of
the seasons into Spanish.
*/

package seasons;
import java.util.Scanner;

public class Seasons 
{
    public static void main(String[] args) 
    { 
            String input;

            // Create a Scanner object for keyboard input.
            Scanner keyboard = new Scanner(System.in);

            // Get a day from the user.
            System.out.print("Enter the name of a season: ");
            input = keyboard.nextLine();

            // Translate the season to Spanish.
            switch (input)
            {
                case "spring":
                    System.out.println("la primavera");
                    break;
                case "summer":
                    System.out.println("el verano");
                    break;
                case "autumn":
                case "fall":
                    System.out.println("el otono");
                    break;
                case "winter":
                    System.out.println("el invierno");
                default:
                    System.out.println("Please enter one of these words:\n"
                             + "spring, summer, autumn, fall , or winter.");
                    break;
            }
    }    
}

3-15 PetFood

posted Jan 30, 2014, 12:52 PM by Dlovan ismail   [ updated Jan 30, 2014, 12:52 PM by Karwan Jacksi ]

//This program demonstrates a switch statement.

package petfood;
import java.util.Scanner; // Needed for the Scanner class

public class PetFood 
{
    public static void main(String[] args) 
    {
            String input;   // To hold the user's input
            char foodGrade; // Grade of pet food

            // Create a Scanner object for keyboard input.
            Scanner keyboard = new Scanner(System.in);

            // Prompt the user for a grade of pet food.
            System.out.println("Our pet food is available in "+"three grades:");
            System.out.print("A, B, and C. Which do you want "+"pricing for? ");

            input =keyboard.nextLine();
            foodGrade =input.charAt(0);

            // Display pricing for the selected grade.
            switch(foodGrade)
            {
                case 'a':
                case 'A':
                    System.out.println("30 cents per lb.");
                    break;
                case 'b':
                case 'B':
                    System.out.println("20 cents per lb.");
                    break;
                case 'c':
                case 'C':
                    System.out.println("15 cents per lb.");
                    break;
                default :System.out.println("Invalid choice.");
            }   
    }   
}

1-10 of 23