Chapter 4: Numbers and Arithmetic

The exercises here are taken from my forthcoming book The Java Developer's Resource.

Quiz

  1. In one line, how would you find the minimum of three values? of four? of five?

    
    int min1 = (n1 < n2 ? n1 : n2) < n3 ? (n1 < n2 ? n1 : n2) : n3;
    int min1 = ((n1 < n2 ? n1 : n2) < n3 ? (n1 < n2 ? n1 : n2) : n3) < n4 ? ((n1 < n2 ? n1 : n2) < n3 ? (n1 < n2 ? n1 : n2) : n3) : n4;
    int min1 = (((n1 < n2 ? n1 : n2) < n3 ? (n1 < n2 ? n1 : n2) : n3) < n4 ? ((n1 < n2 ? n1 : n2) < n3 ? (n1 < n2 ? n1 : n2) : n3) : n4) < n5 : (((n1 < n2 ? n1 : n2) < n3 ? (n1 < n2 ? n1 : n2) : n3) < n4 ? ((n1 < n2 ? n1 : n2) < n3 ? (n1 < n2 ? n1 : n2) : n3) : n4) : n5;
    
  2. Write the Java equivalent of the following formulas:

    	Area of a square:				A = s2
    	Area of a circle:				A = pir2
    	Circumference of a circle:			C = 2pir
    	Positive root of the quadratic equation *:	*
    	Slope of the line between two points:	*
    
    
    double A = s*s;
    double A = 3.141592 * r * r;
    double C = 2 * 3.141592 * r;
    double root = (-b + Math.sqrt(b*b - 4*a*c))/(2 * a);
    double slope = (Y2 - Y1)/(X2 - X1); 
    
    The quadratic equation was an unfair question since you haven't yet seen the Math.sqrt() method. It's coming in Chapter 8. In that chapter you'll also learn that you could use the double constant Math.PI instead of the literal 3.141592 to represent the value of pi.

Exercises

  1. Sales tax in New York City is 8.25%. Write a program that accepts a price on the command line and prints out the appropriate tax and total purchase price.

    class salestax {
    
      public static void main (String[] args) {
      
          double price = Double.valueOf(args[0]).doubleValue();
          double salestax = price * 0.0825;     
          System.out.println("Sales tax is " + salestax);
      
      }
    
    }
    If you know about exceptions, you can make this example somewhat more robust against bad user input as follows:
    
    class salestax {
    
      public static void main (String[] args) {
      
        try {
        
          double price = Double.valueOf(args[0]).doubleValue();
          double salestax = price * 0.0825;
          
          System.out.println("Sales tax is " + salestax);
        }
        catch (NumberFormatException e) {
          System.err.println("Usage: java salestax price" );
        }
        catch (ArrayIndexOutOfBoundsException e) {
          System.err.println("Usage: java salestax price" );
        }
        catch (Exception e) {
          System.err.println(e);
        }
      
      
      }
    
    
    }
    
  2. Modify the sales tax program to accept an arbitrary number of prices, total them, calculate the sales tax and print the total amount.

    class salestax {
    
      public static void main (String[] args) {
      
          double total = 0;
          double price;
          
          int i = 0;
          while (i < args.length) {
            price = Double.valueOf(args[i]).doubleValue();
            total = total + price;
            i = i + 1;
          }
          double salestax = total * 0.0825;     
          System.out.println("Sales tax is " + salestax);
      
      }
    
    }
    
  3. Write a program that reads two numbers from the command line, the number of hours worked by an employee and their base pay rate. Then output the total pay due.

    class payday {
    
      public static void main (String[] args) {
      
          double hours = Double.valueOf(args[0]).doubleValue();
          double rate = Double.valueOf(args[1]).doubleValue();
          double pay = rate * hours;
         
          System.out.println("The paycheck is " + pay + " dollars.");
      
      }
    
    }
    
  4. Modify the previous program to meet the U.S. Dept. of Labor's requirement for time and a half pay for hours over forty worked in a given week.

    class payday {
    
      public static void main (String[] args) {
      
          double hours = Double.valueOf(args[0]).doubleValue();
          double rate = Double.valueOf(args[1]).doubleValue();
          double pay;
          
          if (hours > 40) {
            pay = rate * 40 + 1.5 * rate * (hours - 40);      
          }
          else {
            pay = rate * hours;
          }
             
          System.out.println("The paycheck is " + pay + " dollars.");
      
      }
    
    }
    
  5. Add warning messages to the payroll program if the pay rate is less than the minimum wage ($4.35 an hour as of mid-1996) or if the employee worked more than the number of hours in a week.

    class payday {
    
      public static void main (String[] args) {
      
          double hours = Double.valueOf(args[0]).doubleValue();
          double rate = Double.valueOf(args[1]).doubleValue();
          double pay;
          
          if (hours > 40) {
            pay = rate * 40 + 1.5 * rate * (hours - 40);      
          }
          else {
            pay = rate * hours;
          }
             
          System.out.println("The paycheck is " + pay + " dollars.");
          if ( rate < 4.35) {
            System.err.println("This employee is not getting the legally required minimum wage.");     
          }
          if ( hours > 7*24) {
            System.err.println("Did this employee really work " + hours + " hours?");     
          }
      
      }
    
    }
    
  6. There are exactly 2.54 centimeters to an inch. Write a program that takes a number of inches from the command line and converts it to centimeters.

    class intocm {
    
      public static void main (String[] args) {
      
          double inches = Double.valueOf(args[0]).doubleValue();
          double cm = inches * 2.54;     
          System.out.println(cm + " centimeters");
      
      }
    
    }
    
  7. Write the inverse program that reads a number of centimeters from the command line and converts it to inches.

    class cmtoin {
    
      public static void main (String[] args) {
      
          double cm = Double.valueOf(args[0]).doubleValue();
          double inches = cm / 2.54;     
          System.out.println(inches + " inches");
      
      }
    
    }
    
  8. There are 454 grams in a pound and 1000 grams in a kilogram. Write programs that convert pounds to kilograms and kilograms to pounds. Read the number to be converted from the command line. Can you make this one program instead of two?

    class lbstokg {
    
      public static void main (String[] args) {
      
        double lbs = Double.valueOf(args[0]).doubleValue();
        double kg = lbs * 454.0/1000.0;     
        System.out.println(kg + " kg");
      
      }
    
    }
    

    class kgtolbs {
    
      public static void main (String[] args) {
      
        double kg = Double.valueOf(args[0]).doubleValue();
        double lbs = kg * 1000.0/454.0;     
        System.out.println(kg + " kg");
      
     }
    
    }
    
    to make this one program you need a way of telling whether the number entered on the command line is in pounds or kilograms. Let's adopt the convention that if the number is given in kilograms, then the second command line argument will bekg. Thus

    class lbkg {
    
      public static void main (String[] args) {
      
        if (args[1].equals("kg")) {
          double kg = Double.valueOf(args[0]).doubleValue();
          double lbs = kg * 1000.0/454.0;     
          System.out.println(kg + " kg");
        }
        else {
          double lbs = Double.valueOf(args[0]).doubleValue();
          double kg = lbs * 454.0/1000.0;     
          System.out.println(kg + " kg");
        }
      
      }
    
    }
    
  9. The equivalent resistance of resistors connected in series is calculated by adding the resistances of the individual resistors. Write a program that accepts a series of resistances from the command line and outputs the equivalent resistance.

    class seriesResistance {
    
      public static void main (String[] args) {
      
          double total = 0;
          double resistance;
          
          int i = 0;
          while (i < args.length) {
            resistance = Double.valueOf(args[i]).doubleValue();
            total = total + resistance;
            i = i + 1;
          }
          System.out.println("The total resistance is " + total);
      
      }
    
    }
    
  10. The formula for resistors connected in parallel is a little more complex. Given two resistors with resistances R1 and R2 connected in parallel the equivalent resistance is given by the inverse of the sum of the inverses, i.e.

    If there are more than two resistors you continue to invert the sum of their inverses; e.g. for four resistors you have:

    Write a program that calculates the resistance of a a group of resistors arranged in parallel.

    class parallelResistance {
    
      public static void main (String[] args) {
      
          double total = 0;
          double resistance;
          
          int i = 0;
          while (i < args.length) {
            resistance = 1.0/Double.valueOf(args[i]).doubleValue();
            total = total + resistance;
            i = i + 1;
          }
          total = 1.0/total;
          System.out.println("The total resistance is " + total);
      
      }
    
    }
    


[ Exercises | Cafe Au Lait | Books | Trade Shows | Links | FAQ | Tutorial | User Groups ]

Copyright 1996, 1997 Elliotte Rusty Harold
elharo@sunsite.unc.edu
Last Modified November 7, 1997