Math Problems with Input

Example

This program uses the ez standard input commands to input data from the user, then performs a calculation, and uses the ez standard output command to display the result.  

 public class Problems
{
   public static void main(String[] args)
   {
      double s = ez.inputDouble("Speed?");
      double t = ez.inputDouble("Hours?");
      double d = s*t;
      ez.output("Distance = " + d);
   }

}

Standard Computer System Elements


All computer systems (and computers) have 4 basic components:  input, storage, processing, and output, generally organized like this:

    img1.gif

In this Java program:


In an expensive GPS Navigation system in a car:

Input Devices

PCs can use the following input devices (not all PCs have all these devices)

Input Commands

Java does not include any standard keyboard input commands (although these have now been added in the latest verstions.  Java's concept is that you must create (or copy) a class to control the interface to the hardware device.  This class contains methods (commands) to control the input process.  Many programmers, especially beginners, find it strange that there are no keyboard input commands built in.  But using classes and methods results in a more flexible, powerful and extensible system.  

The IB class was created for the IB Computer Science syllabus.  It contains all the most common commands for keyboard input and monitor output.  The listing is shown below.  You are not expected to understanding how this class was written, but you should learn to use its methods (commands).  

Linking Classes

The Java compiler can link lots of classes together to create a large program.  But the compiler will need to find the classes it needs.  The simplest way to help the compiler is to put all the classes you need inside a single file.  Only one of the classes can be declared public, and that is the class with the same name as the file itself.  If you put the Problems class and the ez class together in a single file, the file must be named Problems.java if you wish to compile and run the Problems program.  Then the ez class cannot be declared public.  You can download the ez class here:  ez.java

public class ez
{  
 //===========================================================
 //=== IBIO simplified input/output commands - GUI version ===
 //===========================================================
   public static void output(String message)
   {  javax.swing.JOptionPane.showMessageDialog(null,message);  }

   public static void outputString(String message)
   {  output(message);  }

   public static void output(char info)
   {  output(info + ""); }

   public static void output(byte info)
   {  output(info + ""); }

   public static void output(int info)
   {  output(info + ""); }

   public static void output(long info)
   {  output(info + ""); }

   public static void output(double info)
   {  output(info + ""); }

   public static void output(boolean info)
   {  output(info + ""); }

   //----- Numerical input methods return 0 on error ------------
   public static String input(String prompt)
   {  return javax.swing.JOptionPane.showInputDialog(null,prompt);  }

   public static String inputString(String prompt)
   { return input(prompt);  }

   public static String input()
   { return input("");     }

   public static char inputChar(String prompt)
   { char result=(char)0;
     try{result=input(prompt).charAt(0);}
     catch (Exception e){result = (char)0;}
     return result;
   }

   public static byte inputByte(String prompt)
   { byte result=0;
     try{result=Byte.valueOf(input(prompt).trim()).byteValue();}
     catch (Exception e){result = 0;}
     return result;
   }

   public static int inputInt(String prompt)
   {  int result=0;
      try{result=Integer.valueOf(
                        input(prompt).trim()).intValue();}
      catch (Exception e){result = 0;}
      return result;
   }

   public static long inputLong(String prompt)
   {  long result=0;
      try{result=Long.valueOf(input(prompt).trim()).longValue();}
      catch (Exception e){result = 0;}
      return result;
   }

   public static double inputDouble(String prompt)
   {  double result=0;
      try{result=Double.valueOf(
                          input(prompt).trim()).doubleValue();}
      catch (Exception e){result = 0;}
      return result;
   }

   public static boolean inputBoolean(String prompt)
   {  boolean result=false;
      try{result=Boolean.valueOf(
                         input(prompt).trim()).booleanValue();}
      catch (Exception e){result = false;}
      return result;
   }

   //============================================================
   //=========== end IBIO ===========================================//
}

More Math Problems

After copying the Problems class and the IB class into the editor, compile and run the program - then type in a radius and the program will compute the area of the circle.

There are many standard math problems that can be solved by a computer if the user types in the correct data.  You can see LOTS of examples by downloading this program:   http://www.matheass.de  

Choose any problem that you understand.  Here are a few examples:

Grade-Point-Average

Input 6 grades (call them g1, g2, g3, g4, g5, g6).
Add up the grades and divide by 6 to get your GPA.

  1. Find the average for the grades:  5,4,5,4,3,4,5
  2. Thanks to the input commands, it's easy to run the program over and over again and type in a variety of sets of grades.  Fill in this table: 
  3. Name

    Grades

    Grade Point Average

    Alexandra

    5 , 6 , 7 , 6 , 5 , 6

     

    Bobby

    4 , 4 , 4 , 4 , 4 , 7

     

    Yoyo

    5.3 , 5.5 , 5.3 , 5.5 , 5.3 , 5.5

     

    Zeke

    2 , 3 , 4 , 5 , 6 , 7

     

  1. You can do "what if" investigations.  For example, assume a student has scored 4 , 4, 6 , and 7 on four tests. If there are 2 tests remaining in the semester, what grades does the student need to get in order to achieve a grade 6 average at the end of the semester?  Run the program several times and try various grades to answer this question.
      
  2. Perform another "what if" investigation.  For each of the following sets of grades, figure out the highest and lowest possible final grades for the semester, assuming there will be a total of 6 tests in the semester.
  3. Name

    Grades

    Lowest Possible

    Highest Possible

    Gertrude

    1 , 7 , 5 ,

     

     

    Hans

    6 , 6 , 6 , 6

     

     

    Irving

    1 , 3 , 5 , 7

     

     

    Jerry

    4 , 4 , 4

     

     

  1. Change the program so that it averages 5 grades instead of 6.

Various Problems

A teacher might have 5 test grades in one class, but 6 test grades in another, and maybe 8 grades in another class.  They could run several different programs, but it's simpler to combine all the different calculations in a single program.  Here is a program that combines the 5-grade average and 6-grade average calculations in a single program.
 
public class GPAS
{
  public static void main(String[] args)
  {
     int num = ez.inputInt("How many grades do you have?");
     if (num == 5)
     {  fiveGrades(); }
     if (num == 6)
     {  sixGrades(); }
  }     
   
  static void fiveGrades()
  {
    double a = ez.inputDouble("First grade");
    double b = ez.inputDouble("Second grade");
    double c = ez.inputDouble("Third grade");
    double d = ez.inputDouble("Fourth grade");
    double e = ez.inputDouble("Fifth grade");

    double ave = (a+b+c+d+e)/5.0;
    
    ez.output("Average = " + ave);
  }

  static void sixGrades()
  {
    double a = ez.inputDouble("First grade");
    double b = ez.inputDouble("Second grade");
    double c = ez.inputDouble("Third grade");
    double d = ez.inputDouble("Fourth grade");
    double e = ez.inputDouble("Fifth grade");
    double f = ez.inputDouble("Sixth grade");

    double ave = (a+b+c+d+e+f)/6.0;
    
    ez.output("Average = " + ave);
  }
}

Notice this will only work if the ez.java class is saved in the same folder with the GPAS56 program.

Each section of the program (static void ...) is called a method.  There are 3 methods in the program - main, fiveGrades, and sixGrades .   You can add as many methods as you wish.  But each time you add a method, you also need to add an if.. choice in the main method.

Practice

  1. Copy the GPAS56 program and save it in a folder with ez.java.
    Get it running correctly.
  2. Add a methods for 4 grades and 8 grades.  
  3. Explain why double variables are used instead of int variables.

Modules (packages)

There are two different ways to "package" parts of a java program in separate modules.   

In each case, the "packaging" is provided by some extra commands:

Structure of a Java Program Class

     public class ProgramName
    {
          public static void main(String[] args)
          {
             ... write some commands here ...
          }

         .... method ...

         ... more methods ...

      }

Remember that a class must be saved using exactly the same name as used
in the first public class ...  command.
 

Structure of a Java Method

      static void MethodName( )
      {
          ... write commands here ...
      }
 

There are other ways to structure classes and methods, but these two patterns are the ones we will use in the near future.