Question

In: Computer Science

Write a java program using the following information Design a LandTract class that has two fields...

Write a java program using the following information

Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a double), and one for the width (a double). The class should have:

  •  a constructor that accepts arguments for the two fields

  •  a method that returns the tract’s area(i.e. length * width)

  •  an equals method that accepts a LandTract object as an argument. If the argument object

    holds the same data (i.e. length and width) as the calling object, this method should return

    true. Otherwise, it should return false.

  •  a toString method (that returns a String representation of the tract’s length, width, and

    area).

    Demonstrate the class in a program by creating an array of three LandTract objects initialized with user input. Then, print the info of the LandTract objects stored in the array by invoking the toString method (implicitly or explicity). Also, use the equals method to compare at least two LandTract objects and print wether they are of equal size or not.

    Sample Output:

Enter the length of Tract 1: 2

Enter the length of Tract 1: 3

Enter the length of Tract 2: 2

Enter the length of Tract 2: 3

Enter the length of Tract 3: 4

Enter the length of Tract 3: 5

Info of LandTract 1:

Length = 2.0

Width = 3.0

Area = 6.0

Info of LandTract 2:

Length = 2.0

Width = 3.0

Area = 6.0

Info of LandTract 3:

Length = 4.0

Width = 5.0

Area = 20.0

The first and second tracts are the same size.

Solutions

Expert Solution


//Implementation of Land Tract class..
public class LandTract 
{
        //declare two  variable length and width as double..
    private double length;
    private double width;
    //Getter and setter method for length and width..
        public double getLength() {
                return length;
        }
        public void setLength(double length) {
                this.length = length;
        }
        public double getWidth() {
                return width;
        }
        public void setWidth(double width) {
                this.width = width;
        }
        //A constructor that accepts arguments for the two fields
        public LandTract(double length, double width) {
                super();
                this.length = length;
                this.width = width;
        }
        //Method that calculate tract area..
        public double tractArea(double length,double width)
        {
                double area=length*width;
                return area;
        }
        //Equals method that checks two objects are equal or not..
        public boolean equals(LandTract obj1, LandTract obj2)
        {

        boolean b=false;
        if(obj1.getLength()==obj2.getLength() && obj1.getWidth()==obj2.getWidth())
        {
           b=true;
        }
        else
        {       
                b=false;
        }
        return b;
    }
        //toString method that return the string about the information...
        public String toString() 
        {
        return "Length="+length+"\nwidth="+width+"\nArea=" + width*length;
    }
       
        

}



//Implementation of program by using main  class...
import java.util.*;
public class Main
{
        public static void main(String[] args)
        {
                   Scanner sc=new Scanner(System.in);
                   //create an array of class LandTrat type of size 3..
           LandTract[] arr=new LandTract[3];
           //To take the input for length and width for tract1..
           System.out.println("Enter the length of Tract1: ");
           int l1=sc.nextInt();
           System.out.println("Enter the width of Tract1: ");
           int w1=sc.nextInt();
           //create first object Landtract Type by passing l1 and w1..
           arr[0]=new LandTract(l1,w1);
           //To take the input for length and width for tract2..
           System.out.println("Enter the length of Tract2: ");
           int l2=sc.nextInt();
           System.out.println("Enter the width of Tract2: ");
           int w2=sc.nextInt();
           //create second object Landtract Type by passing l2 and w2..
           arr[1]=new LandTract(l2,w2);
           //To take the input for length and width for tract3..
           System.out.println("Enter the length of Tract3: ");
           int l3=sc.nextInt();
           System.out.println("Enter the width of Tract3: ");
           int w3=sc.nextInt();
          //create third object Landtract Type by passing l3 and w3..
           arr[2]=new LandTract(l3,w3);
           //Print the information for LandTract 1..
           System.out.println("Info of LandTract 1:");
           System.out.println(arr[0].toString());
           //Print the information for LandTract 2..
           System.out.println("Info of LandTract 2:");
           System.out.println(arr[1].toString());
           //Print the information for LandTract 3..
           System.out.println("Info of LandTract 3:");
           System.out.println(arr[2].toString()); 
           //check two objects landtract1 and landtrack2 are equals or not..
           boolean b1=arr[0].equals(arr[0],arr[1]);
           //check two objects landtract2 and landtrack3 are equals or not..
           boolean b2=arr[1].equals(arr[1],arr[2]);
          //check two objects landtract1 and landtrack3 are equals or not.. 
           boolean b3=arr[2].equals(arr[0],arr[2]);
           //Print the information about which objects are equal...
           if(b1 && b2 && b3)
           {
                   System.out.print("All the three tracts are of same size.");
           }
           else if(b1)
           {
                   System.out.print("The first and second tracts are the same size.");
           }
           else if(b2)
           {
                   System.out.print("The second and third tracts are the same size.");
           }
           else if(b3)
           {
                   System.out.print("The first and third tracts are the same size.");
           }

           else
           {
                   System.out.print("None of the tracts are of same size.");
           }
        }
}

Related Solutions

Write a java program using the information given Design a class named Pet, which should have...
Write a java program using the information given Design a class named Pet, which should have the following fields (i.e. instance variables):  name - The name field holds the name of a pet (a String type)  type - The type field holds the type of animal that a pet is (a String type). Example values are “Dog”, “Cat”, and “Bird”.  age - The age field holds the pet’s age (an int type) Include accessor methods (i.e. get...
a) Design a Java Class which represents a Retail Item. It is to have the following fields
 a) Design a Java Class which represents a Retail Item. It is to have the following fields Item Name Item Serial No Item Unit Price Item Stock Level Item Reorder Level It is to have at least the following methods an Observer, Mutator and a Display method for each field. It is also to have a buy and a restock method and a method to issue a warning if the stock level goes below the re-order level. b) Extend the Retail Item class of part a) above...
a) Design a Java Class which represents a Retail Item. It is to have the following fields
 a) Design a Java Class which represents a Retail Item. It is to have the following fields  Item Name  Item Serial No  Item Unit Price  Item Stock Level  Item Reorder Level  It is to have at least the following methods an Observer, Mutator and a Display method for each field. It is also to have a buy and a restock method and a method to issue a warning if the stock level goes below the re-order level.    b) Extend...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's name • idNumber: an int representing the employee's ID number • rate: a double containing the employee's hourly pay rate • hours: an int representing the number of hours this employee has worked The class should also have the following methods: • Constructor: takes the employee's name and ID number as arguments • Accessors: allow access to all of the fields of the Payroll...
CSC MUST BE IN JAVA Design a program as per the following information using the accompanying...
CSC MUST BE IN JAVA Design a program as per the following information using the accompanying data file. Here is the datafile: sodaSalesB.dat Make sure the program compiles and that you include pseudo code. The assignment is worth 100 points.  (Lack of pseudo code will cost you 15pts) You have been asked to produce the report below. This report will tell the company how sales are going. You will read the file named sodasales.dat The values are the brand name, number...
Complete the following C++ tasks: a. Design a class named BaseballGame that has fields for two...
Complete the following C++ tasks: a. Design a class named BaseballGame that has fields for two team names and a final score for each team. Include methods to set and get the values for each data field. Create the class diagram and write the pseudocode that defines the class. b. Design an application that declares three BaseballGame objects and sets and displays their values. c. Design an application that declares an array of 12 BaseballGame objects. Prompt the user for...
Design a class named Employee. The class should keep the following information in fields: ·         Employee...
Design a class named Employee. The class should keep the following information in fields: ·         Employee name ·         Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. ·         Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to...
program language: JAVA For this project, you get to design and write a WeightedCourseGrade class to keep track of a student's current grade. You also get to design and write WeightedCourseGradeDriver class that requests input from the user and interacts with the WeightedCourseGrade class. Your WeightedCourseGrade class should store the following information: Weighted subtotal (the sum of all of the categories multiplied by the grade category weight) Total category weights (the sum of all the grade category weights) Provide the...
Create a program in java with the following information: Design a program that uses an array...
Create a program in java with the following information: Design a program that uses an array with specified values to display the following: The lowest number in the array The highest number in the array The total of the numbers in the array The average of the numbers in the array Initialize an array with these specific 20 numbers: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 example...
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT