15.8k views
4 votes
Write a program that implements the CampusArea class. Declare two fields, CampusLength and CampusWidth and implement a parameterized constructor which assigns parameters to the CampusLength and CampusWidth fields of the CampusArea class. You are required to implement the GetCampusArea() method which calculates and returns the area of the Campus. (5 Marks) b. Create a class for a farm inventory program. Create a console-based application called Farm Inventory App that meets the requirements below.

1. Create a class and name it Farm.
2. The Farm class must have the following properties: - Location as string - FarmSize as int - FarmCost as int
3. Define two constructors in the Farm class. - The first constructor receives no argument and sets default values to the properties (the default value for string is an empty string, and for int it's 0 ). - The second constructor must receive and set the values for Location, FarmSize, and FarmCost as arguments.
4. Create a method named ShowFarmlnformation in the class to display the properties of the object

User DoubleJ
by
6.9k points

1 Answer

3 votes

Final answer:

The answer includes a sample implementation of the CampusArea class with a method to calculate the area and the Farm class for a Farm Inventory App with properties, constructors, and a display method.

Step-by-step explanation:

Implementing the CampusArea Class

To implement a class named CampusArea in a programming language like Java or Python, you must define two fields, campusLength and campusWidth, and then create a constructor that assigns values to these fields. The GetCampusArea() method will calculate and return the area of the campus by multiplying the length by the width.

Here is an example of how this might look in code:

public class CampusArea {
private int campusLength;
private int campusWidth;

public CampusArea(int length, int width) {
this.campusLength = length;
this.campusWidth = width;
}

public int GetCampusArea() {
return campusLength * campusWidth;
}
}

Creating the Farm Class for the Farm Inventory App

To create a class called Farm for a console-based application named 'Farm Inventory App', you need to define properties, such as Location, FarmSize, and FarmCost, along with two constructors as described in the student's requirements. One constructor will initialize the properties with default values, and the other will accept parameters to set these properties. Here's a simple version of the class with a method to display farm information:

public class Farm {
private String location;
private int farmSize;
private int farmCost;

public Farm() {
this.location = "";
this.farmSize = 0;
this.farmCost = 0;
}

public Farm(String loc, int size, int cost) {
this.location = loc;
this.farmSize = size;
this.farmCost = cost;
}

public void ShowFarmInformation() {
System.out.println("Location: " + location);
System.out.println("Farm Size: " + farmSize + " acres");
System.out.println("Farm Cost: $" + farmCost);
}
}

User Krubo
by
7.4k points