The hoard of clutter within someone's memories

Some of it is useful

Any AI version of Marie Kondo should just give up!

So this is the beginning of a big project, something that can solve simple truss problems. So the idea is, start small and then build on it. Then it can become something that can solve truss problems that are more complex. It's tempting to dive straight in but before all of that, I always think it is best to start by determining the input is correct and in the correct format. So it is best to start up a template that will accept command-line arguments and filter out attempts with unwanted input in a way that does not cause the result to crash and inform of what the possible problem is. It's a good rule to follow, starting every module with the question "Is this safe to execute?"

So the basics of a truss problem is that forces are applied to joints between structural members. These structural members undergo either compression or tension depending on the truss design. As the truss is a static object and the joints stop moving once loaded, the sum of the forces at each joint is zero. Forces are vectors and thus have:

  • Magnitude
  • Direction

The magnitude is the amount of force, which can either be in Newtons (Europe) or pound-force (USA). In simple 2D cases, which is what things are going to start with, the direction is determined by a single angle which can be either in degrees or radians. Therefore I need a program to check for two inputs which need to be numeric, of double-precision ideally, and gracefully filter out any other input form. Once things have got this far, I can take a rest and thing about the next thing.

Alors, on y va?


Java

Although C and C++ are candidates, the final outcome is to have something graphical because truss problems are best seen. It helps if the truss problem set or designed on a piece of paper matches what is put into the computer and the best way to check it is to see a graphical representation. C and C++ either have no graphical libraries, limited graphical libraries or require some external coding environment to work (such as Qt). Java has better graphical capabilites as part of its development kit. Java bytecode also works with any computer set up to run it, compile once and run anywhere so having a different OS environment to deal with should not be a problem.

The forces in the truss problem are to be decomposed into horizontal and vertical force components, which will take a little bit of trigonometry but before that the Java class needs to filter out inputs that do not work. Like C and C++, Java deals with exceptions using try and catch blocks. The result needs to warn the user of not having enough command-line arguments or to take correcting action. This test involves having two command-line arguments as a requirement.

So, here's what I came up with.


public class Test1 {
private double Mag;
private double Angle;
protected void SetMag(double a){this.Mag=a;}
protected void SetAngle(double b){this.Angle=b;}
 public void main(String[] args) {
 try
{
     SetMag(Double.parseDouble(args[0]));
 }
catch (ArrayIndexOutOfBoundsException nullStr)
 {
 //No arguments. Print error message, then exit with an error code.
 System.out.println("No arguments!");
 System.exit(1);
 }
 catch (NumberFormatException nfe)
 {
 //The first argument is not a double. Print error message, then exit with an error code.
 System.out.println("The first argument must be a double value.");
 System.exit(1);
 }
 try
 {
     SetAngle(Double.parseDouble(args[1]));
 }
 catch (ArrayIndexOutOfBoundsException nullStr)
 {
 //No argument for angle. Print error message, then exit with an error code.
 System.out.println("No angle argument! Angle set to zero");
 SetAngle(Double.parseDouble("0"));
 }
 catch (NumberFormatException nfe)
 {
 //The second argument is not a double. Print error message, then exit with an error code.
 System.out.println("The second argument must be a double value, preferably greater than 0 and less than 360.");
 System.exit(1);
 }
System.out.println("Pass!");
 }
 }

As I have called this class Test1, save the file as Test1.java and compile using javac Test1.java to create the class file.


A quick explanation

Two double variables of Mag and Angle for magnitude and angle are initiated. The main sequence contains two try blocks with both have two catch blocks. One catch block is in case the input has not been defined, in the first case there are no arguments attached to the command line and in the second case the input is not a numeric of double-precision. For the lack of force and angle arguments, the result is the class file exits after printing a warning message. A lack of angle input however does not require stoppage, in this case no angle argument means that the angle variable is set to zero. Addition of further arguments will have no effect on the class file as these are ignored as this code only focuses on the first two arguments.

I suppose it isn't much right now but it's a start! Instead of just printing the line Pass! when the input criterium is reached, this can now be used to do something a bit more useful.

To run the class file just type java Test1 to run the class file, try running it with two numerical arguments to see when things go smooth (such as java Test1 123 45 to see the result of correct input and then try using just one number or making one of the inputs non-numeric. There should be an error message and, if necessary, a smooth exit.

If one thinks of mitigation at the beginning, there are likely to be fewer surprises when things do not go to plan. Starting to build code with this in mind makes for reduced correcting efforts later on.

I'm already looking forward to the next bit ;)

About

This is where I place the very basic notes on programming from those starting at the very beginning using Linux or similar operating systems. It's set up to be understood by everyone. If you have an opinion as to how this page is done, then you are already and intermediate or advanced programmer and I don't care!