Create a Run Class

Asked

Viewed 81 times

-2

I need help to run a class, it’s a college exercise, I don’t know how to finish.
Only this last class left to finish the project.

I need to create a class TesteProjeto with the structure main.

In this structure I need to call a class and already pass the parameters on it. How do I do this?

Follow what I’ve done:

public class OrdemServico {
    private int id;
    private long dataSolicitacao;
    private long dataInicio;
    private long dataFim;
    private double ValorTotal;

    private Cliente cliente; 
    private Atendente atendente; 
    private Tecnico tecnico; 
    private Material material; 
    private Servico servico; 

        public OrdemServico(int id, long dataSolicitacao, long dataInicio, long dataFim, double ValorTotal, String cliente, String atendente, String tecnico, String Material, String Servico)
        {
            // inicializa variáveis de instância
            this.id = id;
            this.dataSolicitacao = dataSolicitacao;
            this.dataInicio = dataInicio;
            this.dataFim = dataFim;
            this.ValorTotal = ValorTotal;
            //this.cliente = new Cliente();
            //this.cliente = cliente.getNome();
        }

        public void exibeInformacoes(){
            System.out.println("OrdemServico: ");
            System.out.println("ID: " + id);
            System.out.println("Data Solicitacao: " + dataSolicitacao);
            System.out.println("Data Inicio: " + dataInicio);
            System.out.println("Data Fim: " + dataFim);
            System.out.println("Valor Total: " + ValorTotal);

            System.out.println("Cliente: " + cliente);
            System.out.println("Atendente: " + atendente);
            System.out.println("Tecnico: " + tecnico);
            System.out.println("Material: " + material);
            System.out.println("Servico: " + servico);
        }
    }

Now I need to do the main. What I’ve done, but it’s a little wrong:

class TesteProjeto
{ // classe de testes
    public static void main(String args[])
    { 
        OrdemServico ordem = new OrdemServico();
        System.out.println("Teste");
    }
}

You can help me in how?

  • I don’t know if I understand what you want, but basically just call the builder you created by passing the arguments you want, this is what you don’t know how to do?

  • 1

    You have not demonstrated or mentioned this in the question, so the question is not clear.

2 answers

2


Good afternoon,

The reason for the "error" is because you are instantiating the Ordemservic object with an empty constructor

OrdemServico ordem = new OrdemServico();

Java by default always keeps an empty and public constructor in all classes, even when undeclared, because of this, its object is being started without data.

To solve just enter the data at the time of instantiating the object as follows, following the parameter declaration of your constructor:

OrdemServico ordem = new OrdemServico(1, 20052019, 20052019, 22052019, 50.00, "Orivaldo", "Maria", "Reinaldo", "Material X,Y,Z", "Serviço 1");

This way you are populating your Ordemservico object with data and when you ask to make the same impression, it will do it correctly.

  • 3

    "Java by default always keeps an empty constructor" - Actually the default constructor without parameters is only created by default if no other constructor is defined. But in this case a constructor with several parameters has already been defined, then the constructor default no parameters exist and new OrdemServico() does not compile.

1

If you want to create an object already passing the data, inform them in the constructor, as below.

To display the data call the class with the method exibeInformacoes(), as below.

class TesteProjeto
{   classe de testes
    public static void main(String args[])
    { 
        OrdemServico ordem = new OrdemServico(1, 20052019, 20052019, 22052019, 50.00, "Orivaldo", "Maria", "Reinaldo", "Material X,Y,Z", "Serviço 1");

        ordem.exibeInformacoes();
        System.out.println("Teste");
    }
}

Browser other questions tagged

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