Question

In: Computer Science

You must create a project in intelliJ that you will name midterm_<yourLastname>. Inside the project you...

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 Square in a package it200.midterm.shapes. The class contains the following:
  1. An int field called side, that stores the size of each side of the square (in terms of number of characters)
  2. A char field called symbol, which stores a character as the symbol we use to draw the square in the console
  3. A constructor that accepts as argument an int and a char and sets the two attributes described above.
  4. A Method called draw that takes no arguments, returns nothing and just prints the square of the specific size and using the specific character in the console. For example, if we create a square instance using the statement new Square(5, '%'), when we call the draw method for the aforementioned square instance it will print in the console the following:

% % % % %

% % % % %

% % % % %

% % % % %

% % % % %

  • A public class called Triangle in a package it200.midterm.shapes. The class contains the following:
  1. An int field called height, that stores the size of each side of the square (in terms of number of characters)
  2. A char field called symbol, which stores a character as the symbol we use to draw the triangle in the console
  3. A constructor that accepts as arguments an int and a char and sets the two attributes described above.
  4. A Method called draw that takes no arguments, returns nothing and just prints the triangle of the specific size and using the specific character in the console. For example, if we create a triangle instance using the statement new Triangle(5, '%'), when we call the draw method for the aforementioned triangle instance it will print in the console the following:

%

% %

% % %

% % % %

% % % % %

  • A public class called Rectangle in a package it200.midterm.shapes. The class contains the following:
  1. An int field called sideHorizontal, that stores the size of the horizontal side of the rectangle (in terms of number of characters)
  2. An int field called sideVertical, that stores the size of the horizontal side of the rectangle (in terms of number of characters)
  3. A char field called symbol, which stores a character as the symbol we use to draw the rectangle in the console
  4. A constructor that accepts as arguments two integers and a char and sets the values for the three attributes described above.
  5. A Method called draw that takes no arguments, returns nothing and just prints the rectangle of the specific size and using the specific character in the console. For example, if we create a rectangle instance using the statement new Rectangle(5, 3, '*'), when we call the draw method for the aforementioned rectangle instance it will print in the console the following:

* * * * *

* * * * *

* * * * *

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:

  • Create an instance of a Square size 9 and shape '#' and print it using its draw() function
  • Create an ArrayList of two Triangle instances, with whatever height and shape attributes you want. Then iterate over the ArrayList and call the draw() function for each one of the Triangle instances in the ArrayList
  • Create an array of two Rectangle instances, with whatever attributes you want. Then iterate over the array and call the draw() function for each one of the Rectangle instances contained in the array

Solutions

Expert Solution

Steps for Creating a Project in IntelliJ idea :

  • Launch IntelliJ Idea
  • If the welcome screen comes, then choose Create New Project
    else Go to File, then New, Project.
  • In the new Project, choose the option Java
  • If you have the JDK preinstalled on your computer then add-in by clicking the Add JDK option else first download JDK from Browser.
  • Give your project name and then you will see an src folder will be created after completion of these steps.
  • Right-click on this src and you will find option NEW, from there you can add packages for your project.
    Create a package named it200, then inside it then two packages inside it, one is Shapes ( for Rectangle, Triangle, and Square classes) and another package is Main( for MainClass).
After all these steps, right-click on the package Shapes and then click on the New option and add 3 classes namely Square, Rectangle, and Triangle.

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
* * * * * 
* * * * * 
Please post your queries in the comment section.
Thanks.

Related Solutions

Create a project for this assignment. You can name it assignment02 if you wish. Download the...
Create a project for this assignment. You can name it assignment02 if you wish. Download the database file pizza-190807A.sqlite into the top level of the project. Create the pizza_services.py module first and put in the code given in the assignment. Using this code ensures that you can use the services in a similar way to the example. The assignment suggests adding a method customer to the class. This will return a list of rows from the customer table in the...
Process Cost Excel Project Create a new Excel spreadsheet and name it “Last name_PC”. You project...
Process Cost Excel Project Create a new Excel spreadsheet and name it “Last name_PC”. You project is to create a model for a production cost report using the weighted average method for the month of May.   Following good Excel design techniques, you should have an input area in which you put the department information for the month, and an output area that calculates the production cost report. As always, you should have only formulas or references in your output area....
Process Cost Excel Project Create a new Excel spreadsheet and name it “Last name_PC”. You project...
Process Cost Excel Project Create a new Excel spreadsheet and name it “Last name_PC”. You project is to create a model for a production cost report using the weighted average method for the month of May.   Following good Excel design techniques, you should have an input area in which you put the department information for the month, and an output area that calculates the production cost report. As always, you should have only formulas or references in your output area....
Process Cost Excel Project Create a new Excel spreadsheet and name it “Last name_PC”. You project...
Process Cost Excel Project Create a new Excel spreadsheet and name it “Last name_PC”. You project is to create a model for a production cost report using the weighted average method for the month of May.   Following good Excel design techniques, you should have an input area in which you put the department information for the month, and an output area that calculates the production cost report. As always, you should have only formulas or references in your output area....
Inside “Lab1” folder, create a project named “Lab1Ex3”. Use this project to develop a C++ program...
Inside “Lab1” folder, create a project named “Lab1Ex3”. Use this project to develop a C++ program that performs the following:  Define a function called “Find_Min” that takes an array, and array size and return the minimum value on the array.  Define a function called “Find_Max” that takes an array, and array size and return the maximum value on the array.  Define a function called “Count_Mark” that takes an array, array size, and an integer value (mark) and...
Inside “Lab1”folder, create a project named “Lab1Ex1”. Use this project to write and run a C++...
Inside “Lab1”folder, create a project named “Lab1Ex1”. Use this project to write and run a C++ program that produces:  Define a constant value called MAX_SIZE with value of 10.  Define an array of integers called Class_Marks with MAX_SIZE which contains students Mark. Define a function called “Fill_Array” that takes an array, and array size as parameters. The function fills the odd index of the array randomly in the range of [50- 100] and fills the even index of...
Project 2 Calculations must be done in Excel – You must create your own spreadsheet (do...
Project 2 Calculations must be done in Excel – You must create your own spreadsheet (do not copy and paste someone else’s). This question should be done using Method 1 as outlined in lecture 6 (i.e. Tax Effects, then Cash Flows then NPV) As the financial advisor to All Star Manufacturing you are evaluating the following new investment in a manufacturing project: - The project has a useful life of 12 years. Land costs $6m and is estimated to have...
Create a Netbeans project with a Java main class. (It does not matter what you name...
Create a Netbeans project with a Java main class. (It does not matter what you name your project or class.) Your Java main class will have a main method and a method named "subtractTwoNumbers()". Your subtractTwoNumbers() method should require three integers to be sent from the main method. The second and third numbers should be subtracted from the first number. The answer should be returned to the main method and then displayed on the screen. Your main() method will prove...
Create a project that gets input from the user. ( Name the project main.cpp) Ask the...
Create a project that gets input from the user. ( Name the project main.cpp) Ask the user for a value for each side of the triangle. Comment your code throughout. Include your name in the comments. Submit the plan-Psuedo code (include Flowchart, IPO Chart) and the desk check with each submission of your code. Submit the plan as a pdf. Snips of your output is not a plan. Save the plan and desk check as a pdf Name the code...
For this assignment, you must follow directions exactly. Create a P5 project in Eclipse then write...
For this assignment, you must follow directions exactly. Create a P5 project in Eclipse then write a class P5 with a main method, and put all of the following code into the main method: Instantiate a single Scanner object to read console input. Declare doubles for the gross salary, interest income, and capital gains. Declare an integer for the number of exemptions. Declare doubles for the total income, adjusted income, federal tax, and state tax. Print the prompt shown below...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT