Question

In: Computer Science

Write the code to create a class Square implementing the interface Figure with constructor to initialize...

Write the code to create a class Square implementing the interface Figure with constructor to initialize with a size and toString method. Write another class SquareUser that creates and array of Squares and initializing with sides 1, 2,3, 4 and 5. Also write the code to call the toString method of squares in a loop to print the string for all squares in the array.

Solutions

Expert Solution

Explanation:

Here is the class Square implemention the interface Figure with the area method declaration and the size variable.

In the square class, the constructor is defined to initialise the size variable.

ALso, the SquareUser class is there which creates an array of Square class objects and then calls the toString method for each of the object

Code:


interface Figure
{
int size=0;
public int area();
}

class Square implements Figure
{
int size;
  
Square(int s)
{
size = s;
}
  
public String toString()
{
return "This is a Square with size "+size;
}
  
public int area()
{
return size*size;
}
}
public class SquareUser
{
   public static void main(String[] args) {
      
       Square arr[] = new Square[5];
      
       arr[0] = new Square(1);
       arr[1] = new Square(2);
       arr[2] = new Square(3);
       arr[3] = new Square(4);
       arr[4] = new Square(5);
      
       for(int i=0; i<arr.length; i++)
       {
       System.out.println(arr[i]);
       }
   }
}

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

PLEASE COMMENT IF YOU NEED ANY HELP!


Related Solutions

- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
Please write code in java and comment . thanks Item class A constructor, with a String...
Please write code in java and comment . thanks Item class A constructor, with a String parameter representing the name of the item. A name() method and a toString() method, both of which are identical and which return the name of the item. BadAmountException Class It must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it. It must have a default...
Step 1: Extend the dispenserType class per the following specifications. Add a parameterized constructor to initialize...
Step 1: Extend the dispenserType class per the following specifications. Add a parameterized constructor to initialize product name (name), product cost (cost) and product quantity (noOfItems). This constructor will be invoked from main.cpp. One such call to this constructor will look like: dispenserType sanitizer("hand sanitizer", 50, 100); Add the declaration and definition of this parameterized constructor to .h and .cpp files. Step2: Define a new class, cashRegister. Start by creating cashRegister.h and cashRegister.cpp files. Be sure to update your CMakeLists.txt...
Let's get some practice implementing a known interface. Create a public class named MyString. MyString should...
Let's get some practice implementing a known interface. Create a public class named MyString. MyString should provide a public constructor that accepts a single String argument. You should reject null Strings in your constructor using assert. MyString should also implement the Java Comparable interface, returning 1 for a positive result and -1 for a negative result. Normally Strings are compared lexicographically: "aaa" comes before "z". MyString should compare instances based on the length of its stored String. So MyString("aaa") should...
2. Program containing two modules: a. Create a class Code one programmer-written constructor Create several instance...
2. Program containing two modules: a. Create a class Code one programmer-written constructor Create several instance data attributes Create 3 methods which will each be invoked from the driver program - one method will not receive any parameters, but will return a value to driver program - one method will receive one parameter, and will also return a value to driver program - a display method will receive 2 parameters from driver program Define a constant properly; use the constant...
Using C++, Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp. The only required constructor for this...
Using C++, Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp. The only required constructor for this class is KeywordsInFile( filename_with_keywords, filename_with_text) filename_with_keywords is a name of a plain text file that contains the list of keywords. For the future reference, the number of words in this file is denoted by N. filename_with_text is a name of a plain text file that contains the text where the keywords must be found. For the future reference, the number of words in this...
Write the MATLAB code to Create a new figure. Define a theta array such that ?...
Write the MATLAB code to Create a new figure. Define a theta array such that ? 2 ≤ ? ≤ 9? 2 with increment of ? 10 ; Create a sixmember array of ones called r. Create a new polar plot of ? versus r
C++ Code Required to Show The constructor of parent class executes before child class
C++ Code Required to Show The constructor of parent class executes before child class
Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT