Gaddis-ch2

2-29 Payroll

posted Jan 24, 2014, 5:00 AM by Dlovan ismail   [ updated Jan 24, 2014, 5:00 AM by Karwan Jacksi ]

//This program demonstrates the Scanner class.

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

public class Payroll 
{
    public static void main(String[] args) 
    {
           String name;                // To hold a name
           int hours;                      // Hours worked
           double payRate;       // Hourly pay rate
           double grossPay;      // Gross pay

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

           // Get the user's name.
           System.out.print("What is your name? ");
           name = keyboard.nextLine();

           // Get the number of hours worked this week.
           System.out.print("How many hours did you work this week? ");
           hours = keyboard.nextInt();

           // Get the user's hourly pay rate.
           System.out.print("What is your hourly pay rate? ");
           payRate = keyboard.nextDouble();

           // Calculate the gross pay.
           grossPay = hours * payRate;

           // Display the resulting information.
           System.out.println("Hello, " + name);
           System.out.println("Your gross pay is $" + grossPay);
    }  
}

2-27 Compact

posted Jan 24, 2014, 4:42 AM by Dlovan ismail   [ updated Jan 24, 2014, 4:42 AM by Karwan Jacksi ]


package compact;

public class Compact 
{
    public static void main(String[] args)
    {
           int shares = 220; 
           double averagePrice = 14.67; 

           System.out.println("There were " + shares + " shares sold at $" + averagePrice +" per share.");

    }   
}

2-23 Scope

posted Jan 24, 2014, 4:29 AM by Dlovan ismail   [ updated Jan 24, 2014, 4:29 AM by Karwan Jacksi ]

// This program can't find its variable.

package scope;

public class Scope
{
    public static void main(String[] args)
    {
           System.out.println(value); // ERROR!
           int value = 100;
    }  
}

2-15 Letters2

posted Jan 23, 2014, 5:56 AM by Dlovan ismail   [ updated Jan 23, 2014, 5:56 AM by Karwan Jacksi ]

// This program demonstrates the close relationship between
// characters and integers.

package letters2;

public class Letters2 
{
    public static void main(String[] args)
    {
            char letter;

            letter=65;
            System.out.println(letter);
            letter=66;
            System.out.println(letter);
    }  
}

2-13 TrueFalse

posted Jan 23, 2014, 5:50 AM by Dlovan ismail   [ updated Jan 23, 2014, 5:50 AM by Karwan Jacksi ]

// A program for demonstrating boolean variables.

package truefalse;

public class TrueFalse 
{
    public static void main(String[] args)
    {
           boolean bool;
           
           bool = true;
           System.out.println(bool);
           bool = false;
           System.out.println(bool);
    } 
}

2-10 IntegerVariables

posted Jan 23, 2014, 5:40 AM by Dlovan ismail   [ updated Jan 23, 2014, 5:40 AM by Karwan Jacksi ]

// This program has variables of several of the integer types.

package integervariables;

public class IntegerVariables
{
    public static void main(String[] args)
    {
            int checking;         // Declare an int variable named checking.
            byte miles;            // Declare a byte variable named miles.
            short minutes;     // Declare a short variable named minutes.
            long days;              // Declare a long variable named days.
            
            checking = -20;
            miles = 105;
            minutes = 120;
            days = 189000;
            
            System.out.println("We have made a Journey of " + miles + " miles.");
            System.out.println("It took us " + minutes + " minutes.");
            System.out.println("Our account balance is $" + checking);
            System.out.println("About " + days + " days ago Columbus " + "stood on this spot.");
    } 
}

2-9 Literals

posted Jan 23, 2014, 5:26 AM by Dlovan ismail   [ updated Jan 23, 2014, 5:26 AM by Karwan Jacksi ]

// This program has literals and a variable.

package literals;

public class Literals 
{
    public static void main(String[] args)
    {
           int apples;
           
           apples = 20;
           System.out.println("Today we sold " + apples + " bushels of apples.");
    } 
}

2-8 Variable2

posted Jan 23, 2014, 5:17 AM by Dlovan ismail   [ updated Jan 23, 2014, 5:18 AM by Karwan Jacksi ]

// This program has a variable.

package variable2;

public class Variable2
{
    public static void main(String[] args) 
    {
            int value;
          
            value = 5;
            System.out.print("The value is ");
            System.out.println("value");
    }   
}

2-7 Variable

posted Jan 23, 2014, 5:12 AM by Dlovan ismail   [ updated Jan 23, 2014, 5:12 AM by Karwan Jacksi ]

// This program has a variable.

package variable;

public class Variable 
{
    public static void main(String[] args)
    {
            int value;
            value = 5;
            System.out.print("The value is ");
            System.out.println(value);
    } 
}

2-6 Tabs

posted Jan 23, 2014, 5:03 AM by Dlovan ismail   [ updated Jan 23, 2014, 5:03 AM by Karwan Jacksi ]

// Another well-adjusted printing program

package tabs;

public class Tabs 
{
    public static void main(String[] args)
    {
           System.out.print("These are our top sellers:\n");
           System.out.print("\tComputer games\n\tCoffee\n") ;
           System.out.println("\tAspirin");
    }   
}

1-10 of 26