In: Computer Science
You must create a project in intelliJ that you will name midterm_<yourLastname>. Inside the project you must implement the following packages and classes as they appear in the following description:
% % % % %
% % % % %
% % % % %
% % % % %
% % % % %
%
% %
% % %
% % % %
% % % % %
* * * * *
* * * * *
* * * * *
A public class called MainClass in a package it200.midterm.main
The MainClass must contain main method so that you can run your program. In the main method, you must:
Steps for Creating a Project in IntelliJ idea :
Add the above-mentioned variables and draw method.
Code for Square Class :
package it200.mid.shapes;
public class Square {
int side;
char symbol;
// Constructor to assign values to side and symbol.
public Square(int side1,char symbol1) {
this.side = side1;
this.symbol = symbol1;
}
// Draw method
public void draw() {
// here i is the index, the loop will run from 1 till side
for(int i=1;i<=side;i++)
{
// here j is the index and the loop will run from 1 till side for every i
for(int j=1;j<=side;j++)
{
System.out.print(symbol+" ");
}
System.out.println();
}
}
}
Code for Rectangle Class :
package it200.mid.shapes;
public class Rectangle {
int sideHorizontal;
int sideVertical;
char symbol;
// Constructor for the Rectangle class and the values will be assigned to sideHorizontal, sideVertical and symbol.
public Rectangle(int sH,int sV,char sym) {
this.sideHorizontal = sH;
this.sideVertical = sV;
this.symbol = sym;
}
// Draw method for the Rectangle class
public void draw() {
// Here i is the index and the loop will run from 1 till sideVertical's value;
for(int i=1;i<=sideVertical;i++)
{
// Here j is the index and the loop will run from 1 till sideHorizontal's value;
for(int j=1;j<=sideHorizontal;j++)
{
System.out.print(symbol+" ");
}
System.out.println();
}
}
}
Code for the Triangle Class :
package it200.mid.shapes;
public class Triangle {
int height;
char symbol;
// Constructor for the Triangle Class
public Triangle(int h,char sym) {
this.height = h;
this.symbol = sym;
}
// Draw method for the Triangle Class
public void draw() {
// Here i is the index and the loop will run from 1 till height
for(int i=1;i<=height;i++)
{
// Here j is the index and the loop will run from 1 till i
for(int j=1;j<=i;j++)
{
System.out.print(symbol+" ");
}
System.out.println();
}
}
}
Now, we have to create the MainClass which will be having the main
method and where we will create instances of these Shapes classes
and call the desired draw method of every class.
Go in the main package and there right-click on it and then go in
the NEW option and from there select the Java class option, give
the Name of the class and select the public checkbox. We will get
the desired public class :
Since this class is in another package and thus we need to import
the classes of the Shapes package and hence we will import that
package classes. Kindly remember that for ArrayList we use get
Method to get the element at a specific index and then we can call
that instance draw method and in the case of an array, we access
the element by specifying the index in " [ ] " brackets.
Here's the Code for the MainClass :
package it200.mid.main;
import java.util.ArrayList;
import it200.mid.shapes.*;
public class MainClass {
public static void main(String[] args) {
Square square_object = new Square(9,'#');
System.out.println("The draw function called of Square class:");
square_object.draw();
// Creating an ArrayList of Triangle Class
ArrayList<Triangle> array = new ArrayList<Triangle>();
array.add(new Triangle(5,'#'));
array.add(new Triangle(7,'*'));
System.out.println("The draw function called of Triangle class:");
// Iterating over the ArrayList using the basic for loop where index is i
for(int i=0;i<array.size();i++)
{
System.out.println("Draw function called for Triangle-" + (i+1));
array.get(i).draw();
}
// Creating an array of Rectangle class
Rectangle[] rectangle = new Rectangle[2];
rectangle[0] = new Rectangle(7,5,'#');
rectangle[1] = new Rectangle(5,2,'*');
System.out.println("The draw function called of Rectangle class:");
// Iterating over the Array using the basic for loop where index is i
for(int i=0;i<rectangle.length;i++)
{
System.out.println("Draw function called for Rectanlge-" + (i+1));
rectangle[i].draw();
}
}
}
The desired output is :
The draw function called of Square class:
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
The draw function called of Triangle class:
Draw function called for Triangle-1
#
# #
# # #
# # # #
# # # # #
Draw function called for Triangle-2
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
The draw function called of Rectangle class:
Draw function called for Rectanlge-1
# # # # # # #
# # # # # # #
# # # # # # #
# # # # # # #
# # # # # # #
Draw function called for Rectanlge-2
* * * * *
* * * * *