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

Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a...
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...
Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a...
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...
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...
Make a LandTract class with the following fields: • length - an int containing the tract's...
Make a LandTract class with the following fields: • length - an int containing the tract's length • width - an int containing the tract's width The class should also have the following methods: • area - returns an int representing the tract's area • equals - takes another LandTract object as a parameter and returns a boolean saying whether or not the two tracts have the same dimensions (This applies regardless of whether the dimensions match up. i.e., if...
Make a LandTract class with the following fields: • length - an int containing the tract's...
Make a LandTract class with the following fields: • length - an int containing the tract's length • width - an int containing the tract's width The class should also have the following methods: • area - returns an int representing the tract's area • equals - takes another LandTract object as a parameter and returns a boolean saying whether or not the two tracts have the same dimensions (This applies regardless of whether the dimensions match up. i.e., if...
Java program Test Scores? Design a TestScore class that has three integer fields, each holding a...
Java program Test Scores? Design a TestScore class that has three integer fields, each holding a test score. The class should have accessor and mutator methods for the test score fields and a method that returns the average of the test scores as a double. Test the TestScore class by writing a separate program that creates an instance of the class. The program should ask the user to enter three test scores, which should be stored in the TestScore object....
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT