Vector doubt of objects

Asked

Viewed 70 times

1

I’m having a question of how to proceed, there are syntax errors as you can see. But my knowledge is basic and I’m trying to improve. I have the following program. A register of Flights, only with number, origin, destination and number of vacancies.

I created a Class

 package aeroporto;


public class aviao {
    aviao[] voo = new voo[i];

     int numero;
     String origem;
     String destino;
     int vagas;


 for(int i=0; i<5; i++)
void Cadastrar(int numero,String origem,String destino, int vagas)
{
    voo[i].numero = numero;
    voo[i].origem = origem;
    voo[i].destino = destino;
    voo[i].vagas = vagas;

}
}

and the main program is this

package aeroporto;
import java.util.Scanner;
public class Aeroporto {

public static void main(String[] args) {
  Scanner teclado = new Scanner(System.in);

  int numero,vagas,i=0;

  String origem,destino;


  int op;
  do{
      System.out.println("****Menu Aeroporto****");
      System.out.println("1. Cadastro de vôo");
      System.out.println("2. Consultar vôo");
      System.out.println("3. Reservar Passagem");
      System.out.println("5. Sair");
      //ler entrada
     op =teclado.nextInt();
     if(op==4)
     {
         System.exit(1);
     }
     switch(op){ 
        case 1:

            for(i=0 ; i<5 ; i++){

                System.out.print ("Número do voo: ");
                numero =teclado.nextInt();
                System.out.print("Origem : ");
                origem = teclado.next();
                System.out.print("Destino : ");
                destino = teclado.next();
                System.out.print("Número de vagas : ");
                vagas = teclado.nextInt();

                aviao.Cadastrar (numero, origem, destino,vagas);
            }

            break;
        }      

   }while(true);
}

}

I ask you to point out my mistakes and how to proceed. Note: I am trying to make this vector of objects instead of using Arraylist, because I have no idea how it does.

  • 4

    There’s so many mistakes I don’t even know where to start. I’ll tell you something I think is the best I can do for you, but most people who hear this ignore it and keep bumping heads. Start at the beginning, learn one thing at a time, only move on to the next when you understand the current one well. Keep increasing the complexity gradually, let OOP last (this code has nothing OO). Until you know the reason for each character, even the space in your code can’t think of more sophisticated things. I started improving, but he has serious conceptual errors,

  • 1

    so I let it go. There are no more syntax errors, but it’s still very wrong: http://tpcg.io/X9TuHo

  • @Maniero the way I did, was how I learned in college. But good to know that there are conceptual errors. I will seek to learn more. Thank you.

1 answer

0

To use an arraylist, do so.

Collection collection = new Arraylist();

after that you have created your Arraylist. To add items simply paste the name of your collection. add and inside () the object to be added. example: colecao.add(object 1);

As Arraylist could help you with your problem. So, first I advise you to create a class beans1, it adds its variables, like this.

public class Beans1 {
    int numero,vagas;
    String origem,destino;
    Collection colecao = new ArrayList();
}

and on top of the variables right-click on the Netbeans screen and select the "Insert Code" option, select the Getter and Setter option, check all options and you’re done.

Inside the first class where your Get’s and Set’s will be create a void method to return the values.

Now in your main class create an Array that returns the Beans1 class objects.

    ArrayList <Beans1> obj = new ArrayList<Beans1>();
    Beans1 o = new Beans1();

    o.getNumero();
    o.getVagas();
    o.getOrigem();
    o.getDestino();
    obj.add(o);
  • Collection colecao = new ArrayList();?? I think this is wrong, huh, it wouldn’t be List<tipo> colecao = new ArrayList();??

  • Yes, can the Collection type "collection name" = new Arraylist(); it serves for collection type Object, and List<Integer> collection= new Arraylist<Integer>(); only serveria for a type of list, as in the case list type whole objects. As in the question above he says he needs a collection with several different types, I quoted this one.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.