Limited access

Upgrade to access all content for this subject

Consider the following class.

public class Tractor
{
     private int body;     // weight of tractor's main body
     private int scraper;  // weight of scraper attachment
     private int roof;     // weight of optional roof
     private int weight;   // total weight of tractor and options

     public Tractor(int body, int scraper, int roof)
     {
          body = body;
          scraper = scraper;
          roof = roof;
     }

     public void calcWeight()
     {
          int weight = body + scraper + roof;
     }

     public int getWeight()
     {
          return weight;
     }

    // other variables and methods not shown
}

The following code from the main method in a client class does not seem to be working properly. It prints 0 even when the tractor parameters are changed.

Tractor tractor = new Tractor(1500, 500, 400);
System.out.println(tractor.getWeight());

What has to be done to correct this problem?

I. Change the constructor method as follows:

public Tractor(int body, int scraper, int roof)
{
     this.body = body;
     this.scraper = scraper;
     this.roof = roof;
     calcWeight();
}

II. Change the calcWeight() method as follows:

public void calcWeight()
{
     weight = body + scraper + roof;
}

III. Change the main method as follows:

Tractor tractor = new Tractor(1500, 500, 400);
System.out.println(tractor.calcWeight());
A

I only.

B

II only.

C

I, II, and III.

D

II and III only.

E

I and II only.

Select an assignment template