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 ez 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 ez 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.

Pythagoras

Use     to calculate the hypotenuse of a right triangle.

Input:  legs a,b             Calculate : Math.sqrt( a2 + b2)        

Quadratic Formula

Input a, b, c for the quadratic equation  :   a x2 + bx + c = 0
Use the quadratic formula (twice) to calculate two solutions:

       ,    

nth Term of an AP

Input the first (a) , common difference (d) , and the number of terms (n)
Use the formula :  un = a + (n-1)d  to find the value of the nth term.

Choose a problem, figure out what the inputs are, what calculation must be done, and what should appear as output.  Make a separate program for each problem.  In the very near future, you will learn how to put several problems into a single program.