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);
}
}
All computer systems (and computers) have 4 basic components: input,
storage, processing, and output, generally
organized like this:

In this Java program:
PCs can use the following input devices (not all PCs have all these devices)
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).
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
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:
Input 6 grades (call them g1, g2, g3, g4, g5, g6).
Add up the grades and divide by 6 to get your GPA.
|
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 |
|
|
Name |
Grades |
Lowest Possible |
Highest Possible |
|
Gertrude |
1 , 7 , 5 , |
|
|
|
Hans |
6 , 6 , 6 , 6 |
|
|
|
Irving |
1 , 3 , 5 , 7 |
|
|
|
Jerry |
4 , 4 , 4 |
|
|
| 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
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 ... more methods ... } Remember that a class must be saved using exactly the
same name as used |
|
Structure of a Java Method static void MethodName( ) |
There are other ways to structure classes and methods, but these two patterns are the ones we will use in the near future.