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...
Create a C# console application (do not create a .NET CORE project) and name the project....
Create a C# console application (do not create a .NET CORE project) and name the project. Generate two random integers, each between 1 and 50, that you will be adding together to test the user's ability to perform the addition operator. Display the numbers in the console, such as:             7 + 22 = ? Once the user provides their answer, check to see if it is correct and if not, tell them sorry, please try again. If their answer...
Create a C# console application (do not create a .NET CORE project) and name the project...
Create a C# console application (do not create a .NET CORE project) and name the project TuitionIncrease. The college charges a full-time student $12,000 in tuition per semester. It has been announced that there will be a tuition increase by 5% each year for the next 7 years. Your application should display the projected semester tuition amount for the next 7 years in the console window in the following format:             The tuition after year 1 will be $12,600. Note:...
Create a C# console application (do not create a .NET CORE project) and name the project...
Create a C# console application (do not create a .NET CORE project) and name the project TimeToBurn. Running on a particular treadmill, you burn 3.9 calories per minute. Ask the user how many calories they wish to burn in this workout session (this is their goal). Once they tell you, output on the console after each minute, how many calories they have burned (e.g. After 1 minute, you have burned 3.9 calories). Keep outputting the total amount of calories they...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT