Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using jGRASP I need the code to run this program please Class Exercise Write a c

ID: 3817944 • Letter: U

Question

Using jGRASP I need the code to run this program please

Class Exercise

Write a class called Shape that contains instance data that represents the name and number of sides of the shape. Define a constructor to initialize these values. Include mutator(setter) methods – with the this reference – for the instance data, and a toString method that returns a the shape data. Create a static variable to keep track of the number of shapes, and a static method to return the number of shapes entered. Create a driver class called ShapeTest, whose main method instantiates the objects and updates several Shape objects by prompting the user for the information.

In the Shape Class, you will need:

Two private members to store data

A static variable to store the number of shapes

One constructor, which should not accept any values during instantiation

Mutators for each private member of the class (Note: you don’t need to create accessors this time.)

A static method which returns the number of shapes

A toString method to output the information

In the ShapeTest driver, you will need:

At three Shape objects (no hardcoded info this time)

A Scanner Object to get information for all of the objects

Local variables to store information

To reference the static variable (with the updated number of shapes) in the driver

Here is some sample output:

Enter info on 3 shapes:
Enter the name of the shape: Square
Enter the number of sides: 4
Enter the name of the shape: Hexagon
Enter the number of sides: 6
Enter the name of the shape: Octagon
Enter the number of sides: 8


Here is the info you entered for the 3 shapes:
Shape: Square
No. of sides:4

Shape: Hexagon
No. of sides:6

Shape: Octagon
No. of sides:8

Explanation / Answer

ShapeTest file

import java.util.Scanner;

public class ShapeTest {

   public static void main(String[] args) {
      
       Scanner sc = new Scanner(System.in);
      
       System.out.println("Enter info on 3 shapes:");
      
       Shape shape1 = new Shape();
      
       System.out.print("Enter the name of the shape: ");
       String nameOfShape = sc.next();  
      
       System.out.print("Enter the number of sides: ");
       int noOfSides = sc.nextInt();
      
       shape1.setShapeNameList(nameOfShape);
       shape1.setNoOfSides(noOfSides);
       Shape.setNoOfShapes(Shape.getNoOfShapes() + 1);
      
       Shape shape2 = new Shape();
      
       System.out.print("Enter the name of the shape: ");
       String nameOfShape2 = sc.next();  
      
       System.out.print("Enter the number of sides: ");
       int noOfSides2 = sc.nextInt();
      
       shape2.setShapeNameList(nameOfShape2);
       shape2.setNoOfSides(noOfSides2);
       Shape.setNoOfShapes(Shape.getNoOfShapes() + 1);
      
       Shape shape3 = new Shape();
      
       System.out.print("Enter the name of the shape: ");
       String nameOfShape3 = sc.next();  
      
       System.out.print("Enter the number of sides: ");
       int noOfSides3 = sc.nextInt();
      
       shape3.setShapeNameList(nameOfShape3);
       shape3.setNoOfSides(noOfSides3);
       Shape.setNoOfShapes(Shape.getNoOfShapes() + 1);
      
       shape3.ToString();
   }

}

-------------------------------------------------------------------------------------------------------------------------------------------------------

Shape file

import java.util.ArrayList;
import java.util.List;

public class Shape {

   private static List<String> shapeNameList = new ArrayList<String>();
   private static List<Integer> noOfSides = new ArrayList<Integer>();
   private static int noOfShapes = 0;
  
   public Shape() {

   }
  
   public void setShapeNameList(String shapeName) {
       Shape.shapeNameList.add(shapeName);
   }


   public void setNoOfSides(int noOfSides) {
       Shape.noOfSides.add(noOfSides);
   }


   public static void setNoOfShapes(int noOfShapes) {
       Shape.noOfShapes = noOfShapes;
   }


   public static int getNoOfShapes() {
       return noOfShapes;
   }

   public void ToString(){
      
       System.out.println(" Here is the info you entered for the 3 shapes:");
       int numShapes = Shape.noOfShapes;
      
       for(int i=0;i<numShapes;i++){
           System.out.println("Shape: "+Shape.shapeNameList.get(i));
           System.out.println("No. of sides:"+Shape.noOfSides.get(i)+" ");
       }
   }
  

}

----------------------------------------------------------------------------------------------------------------------------------------------------------

Output

Enter info on 3 shapes:
Enter the name of the shape: Square
Enter the number of sides: 4
Enter the name of the shape: Hexagon
Enter the number of sides: 6
Enter the name of the shape: Octogan
Enter the number of sides: 8

Here is the info you entered for the 3 shapes:
Shape: Square
No. of sides:4

Shape: Hexagon
No. of sides:6

Shape: Octogan
No. of sides:8

-----------------------------------------------------------------------------------------------------------------------------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote