Java stand selector LEPA

Everything that has to do with the forum on a technical level. Logins, posts, formatting ...
User avatar
IAHM-COL
Posts: 6455
Joined: Sat Sep 12, 2015 3:43 pm
Location: Homey, NV (KXTA) - U.S.A
Contact:

Re: Java stand selector LEPA

Postby IAHM-COL » Fri Sep 16, 2016 7:59 pm

yes. OPORF77 is the correct approach.
https://raw.githubusercontent.com/IAHM-COL/gpg-pubkey/master/pubkey.asc

R.M.S.
If we gave everybody in the World free software today, but we failed to teach them about the four freedoms, five years from now, would they still have it?

User avatar
jwocky
Site Admin
Posts: 1833
Joined: Sat Sep 12, 2015 12:04 pm
Contact:

Re: Java stand selector LEPA

Postby jwocky » Fri Sep 16, 2016 8:06 pm

Well, before we put here something wrong in people's heads. That is a nice beginner approach to show how the first steps work. Later, with object orientation, you would do it with a stand class that holds a list of aircraft types and then make a list of stands. But lets do the basics first.
Free speech can never be achieved by dictatorial measures!

oscar6662
Posts: 246
Joined: Thu Oct 22, 2015 4:49 pm

Re: Java stand selector LEPA

Postby oscar6662 » Fri Sep 16, 2016 8:21 pm

Code: Select all

public class cacatua {

   public static void main(String[] args) {
      // TODO Auto-generated method stub
      String youraircraft = "TUI738 BAW320 IBE320";
      
      Scanner OK = new Scanner(System.in);
      
      System.out.println("enter the aircraft type pelase");
      youraircraft=OK.nextLine();
      System.out.println("your slected aircraft is " + youraircraft);
      
      if(youraircraft.equals(youraircraft.substring(1, 7))) {
         int m=8;int n=22;
         int r=(int)((Math.random()*(n-m))+m);
         System.out.println("Go to stand " +r);

THANKS A lot for replying and helping step by step 100%

Ive done something and its wrong I know perfectly something is wrong (haha) but no idea how to fix it.
[url]Image[/url]

oscar6662
Posts: 246
Joined: Thu Oct 22, 2015 4:49 pm

Re: Java stand selector LEPA

Postby oscar6662 » Fri Sep 16, 2016 8:32 pm

EDIT (Stupidness of not ending the video lol)
it's not 1, 7 it's 0, 6
[url]Image[/url]

User avatar
SHM
Posts: 1960
Joined: Mon Sep 14, 2015 3:32 pm
Location: India

Re: Java stand selector LEPA

Postby SHM » Fri Sep 16, 2016 8:36 pm

This is how you can use substring to break it into Airline and Aircraft type.

Code: Select all

String airline=youraircraft.substring(0,3);

Code: Select all

String aircrafttype=youraircraft.substring(3);
FG Pilot (2011-2018)
Prepar3d (2015 - 2023)
MSFS2020 (2020 - )
Image

User avatar
jwocky
Site Admin
Posts: 1833
Joined: Sat Sep 12, 2015 12:04 pm
Contact:

Re: Java stand selector LEPA

Postby jwocky » Fri Sep 16, 2016 8:50 pm

Okay, do you have a syntax error or what happened? Or are you talking about the algorithm?

look at OPFOR's post. You can make an array and fill it airline by airline. Just keep in mind, Java, like C++ and some other programming languages, starts counting with 0! Like

airlines[0,0]="TUI";
airlines[0,1]=100;
airlines[0,2]=199;

that means, TUI has gates 100-199


airlines[0,0]="DEL";
airlines[0,1]=200;
airlines[0,2]=299;

that means, Delta has 200-299

Now, that is as of yet, a bit clumsy because we write date firm into a program, but no worries, we can later load the stuff from a file or even a database, or, just for the heck of it, from a website. You can basically expand on this example all the way through your Java learning. So, no worries about clumsy for now.

The next thing is your aircraft string. It basically consists of the airline code followed by plane type, right? So, aircraft_airline=aircraft.substring(0,3); // just the first three letters.
Now you have in aircraft_airline just the three letters of the airline, the same as you have in your array. Means, instead of writing all the time

if (airlines[0,0].equals("TUI")
if (airlines[1,0].equals("DEL")
if (airlines[2,0].equals("BWA")
...

you can do your search in a loop. Just looking for the airline at this point:

Code: Select all

for (int i=0; i<numberofairlines; i++) {
   if (aircraft_airlines.equals(airlines[i,0])) {
      // ... we search the stand in the next step, for now we just tell, we found the airline
      System.out,println("Airline "+aircraft_airline+" found");
   }
}


Now, your problem here is "numberofairlines". You either have to define from hand a variable and set it or you can use airlines.length ... no brackets, it's actually a variable, not a function or something fancy, to feed numberofairlines.

See, most programmers are lazy bones, always on the search for a way to do things more elegant, faster and with less lines to type. Imagine, you have 50 airlines, then 50 if-lines would be a lot and we would also copy code and if there would be a a mistake in this copied code or the need to expand it later, we have to change 50 blocks of code. What can possibly go wrong there -> everything!
So, start with being a lazy bone as far as you can afford it. The more you know, the better you will get at it.
Free speech can never be achieved by dictatorial measures!

oscar6662
Posts: 246
Joined: Thu Oct 22, 2015 4:49 pm

Re: Java stand selector LEPA

Postby oscar6662 » Fri Sep 16, 2016 10:04 pm

Thanks for that but there are some questions before this runs.

First of all I wanna clear I am noob and I am not silly ( I hope)! And I dont know where to start because literally it does NOTHING make sence to me. (eather now when I am writing I dont know what to ask! I dont understand anything of that code)

"number of airlines" that is suposed to be an INT so I basicly did this:
int numberofairlines = As many as I wrote;
then with numberofairlines I supose I need to create an Array that will make 1-DLH, 2-BAW...
BUT where is the 320?
Why making a "for loop" for a stand chooser (I want one stand not a loop of stands?)

I understand that aircraft_airlines is "BAW" ? for example?
Also why comparing 0<with a number
And then why do you want to exponentially increment the number that its not even going to make sense because airline and stand!!!

I am not going to continue because I am just copying the code.
[url]Image[/url]

User avatar
jwocky
Site Admin
Posts: 1833
Joined: Sat Sep 12, 2015 12:04 pm
Contact:

Re: Java stand selector LEPA

Postby jwocky » Fri Sep 16, 2016 10:40 pm

"number of airlines" that is suposed to be an INT so I basicly did this:
int numberofairlines = As many as I wrote;
then with numberofairlines I supose I need to create an Array that will make 1-DLH, 2-BAW...


That is exactly correct.

BUT where is the 320?

Not there yet because we had no use in this step for it. We will get there, but for now, all we want is to find a stand that belongs to the same airline as the plane in question. Ypu want to park your A320 from BAW at a BAW stand, not a TUI stand.

Why making a "for loop" for a stand chooser (I want one stand not a loop of stands?)

You have maybe 100 gates for like 10 airlines. Or maybe more, I don't know the airport you use there and how many airlines fly in there. But bottom line is, you don't want to pick a stand randomly, what you really want is to pick a stand that belongs to this airline randomly. So, first, you need to find all the stands for this airline. This is as far as we went yet.
Once we have the airline, we can go and pick a stand.

Also why comparing 0<with a number

I'm not really sure which line you refer to. I assume, you mean the for-loop, but correct me if I am wrong.

Code: Select all

for (int i=0; i<numberofairlines; i++) {

A for-loop uses, in this syntax (there is another way to do it, I can show you later when we have a better usable example for it), a variable to count the runs through the loop. In this case I used "i".
"i" is nowhere defined and the for-loop allows us to do it in the loop-header.
the syntax is
for (loopvariable; condition; steps) {

so, int i=0 is nothing but "create i and set it to 0
the condition in our case is i<numofairlines
what happens is i counts one up every time, th eprogram runs through the loop. The first time, you come in, i=0
The second time i=1, third time i=2 ... and so on. Now, Java starts to count the index of your array with 0. That means, the last airline has a number one smaller as your number of aircrafts. If you have five airlines, you have the numbers 0,1,2,3,4 So as long as you look for an airline with a number<numberofairlines, you will get a valid airline. Once i reaches numberofairlines, you have reached the end of your list.

And then why do you want to exponentially increment the number that its not even going to make sense because airline and stand!!!

The number "i" makes a sense, only you didn't know the syntax. After you know the syntax, you know, why i is counting up with every repetition of the loop. Because we said i++ count one up. If you want for example a loop that runs backwards, you use i-- for step. You can also count every time five up or doing other funny things with the third parameter of a for-loop. But for now, all we want is a very simple loop counting 1 up every time, it is repeated.
Free speech can never be achieved by dictatorial measures!

oscar6662
Posts: 246
Joined: Thu Oct 22, 2015 4:49 pm

Re: Java stand selector LEPA

Postby oscar6662 » Sat Sep 17, 2016 9:24 pm

With avo this happened:

Code: Select all

package little;
import java.util.Random;
import java.util.Scanner;

public class Program {

    // catalog with airlines and stands
    public static HashMap<String, int[]> catalog = new HashMap<String, int[]>();
       
    public static void addAirline (String airline, int... arrayOfGateRanges) {
        catalog.put(airline, arrayOfGateRanges);
        /* what
        */
    }

    public static int[] getGates (String airline) {
        return catalog.get(airline);
    }

    public static void initialize () {
        addAirline ("BAW", 8, 22, 32, 48); // -> catalog.put("BAW", { 8, 22, 32, 48 });
        addAirline ("GECX", 306, 318);
        addAirline ("MON", 2, 6);
        addAirline ("RYRUK", 100, 103);
        // ...
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        // initialize catalog with airlines and stands
        initialize ();
       
        String airline;
       
        Scanner OK = new Scanner(System.in);
       
        System.out.println("enter the airline type pelase");
        airline=OK.nextLine();
        System.out.println("your selected airline is " + airline);
       
        // get stands for airline from the catalog (or null)
        int [] stands = getGates (airline);
        if (stands != null) { // "!=" ???
            // stands = { 8, 22, 32, 48} or = { 306, 318 } or ...
            int sum = 0;
            // stands.length = 4 or = 2 or ...
            for (int i=0; i<stands.length; i+=2) {     
                int firstGate = stands[i];
                int lastGate = stands[i+1];
                sum = sum + (lastGate-firstGate);
            }
            Random rand = new Random();
            //int randomNum = rand.nextInt(((lastGate-firstGate)+(lastehemGate-firstehemGate))/2) *2;
            int randomNum = rand.nextInt(sum / 2 + 1) *2;
            // done
            for (int i=0; i<stands.length; i+=2) {     
                int firstGate = stands[i];
                int lastGate = stands[i+1];
                if (firstGate + randomNum <= lastGate) {
                    randomNum = firstGate + randomNum;
                    break; // exit the for-loop because the matching range is found
                } else {
                    randomNum = randomNum - (lastGate-firstGate);
                }
            }
            System.out.println("Go to stand " + randomNum);
        } else{
            System.out.println("no, no, no!");
        }
       
    }

}


Thanks for the help avo.
[url]Image[/url]

User avatar
jwocky
Site Admin
Posts: 1833
Joined: Sat Sep 12, 2015 12:04 pm
Contact:

Re: Java stand selector LEPA

Postby jwocky » Sat Sep 17, 2016 10:14 pm

Well, if you understood that source code, you are further ahead than I assumed. The thing is, this program pulls a predefined object of the type HashMap ... so you end up with not knowing what a hash map is, what an object is and why this other method/subroutine hangs around there. Which is kind of the situation, I tried to avoid.

Okay, before you write a program, think, what you want and what you have. In your case, you have airlines and every airline has a number of stands. So, before you can assign any aircrafts to any stands, your program needs this thing, airlines and their stands. To avoid any of those complexer objects, we (ORPF and me) suggested to write them simply in an array. So your first task is to make an array of airlines and put it on the screen line by line. Just that, we go from there, step by step. Just

airlines[0]="BAW";
airlines[1]="TUI";
...

and then, find a way to give each of these airline codes out with
System.out.println(airlines[0]);
or something more elegant, if you come up with it. Just go this first step, then, when it works, we do the second. Because latest when you try three steps at once, it will happen to you as to everyone before you, you will fall on your nose. ''So, relax and do just the first step!
Free speech can never be achieved by dictatorial measures!


Return to “Technical Questions”

Who is online

Users browsing this forum: No registered users and 14 guests