Programming and Problem Solving with Java

(やまだぃちぅ) #1
711

final intPOUNDS = 10 ; // Weight in pounds
final intOUNCES = 12 ; // Additional ounces
int totOz; // Total weight in ounces
doubleuCost; // Computed cost per ounce
// Calculations
totOz = 16 * POUNDS;
totOz = totOz + OUNCES;
uCost = TOT_COST / totOz;
System.out.println("Cost per unit: "+ uCost);
}
}


  1. //
    // Rectangle application
    // This application finds the perimeter and the area
    // of a rectangle, given the length and width
    //

    public classRectangle
    {
    public static voidmain(String[] args)
    {
    doublelength; // Length of the rectangle
    doublewidth; // Width of the rectangle
    doubleperimeter; // Perimeter of the rectangle
    doublearea; // Area of the rectangle


length = 10.7;
width = 5.2;
perimeter = length 2.0+ width 2.0; // Calculate perimeter
area = length * width; // Calculate area
// Print perimeter
System.out.println("The perimeter of a rectangle with length "+
length + " and width "+ width + " is "+ perimeter);
// Print area
System.out.println("The area of the rectangle is "+ area);
}
}
11.//**
// This application computes the sum and product of two integers
//**
public classSumProd
{
public static voidmain(String[] args)
{
final intINT2 = 8 ;
final intINT1 = 20 ;

Free download pdf