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

Write on error-free Java program that does the following things. You need to imp

ID: 3835631 • Letter: W

Question

Write on error-free Java program that does the following things. You need to implement a Shape class for a simple online game The Shape class has at least four private data members to keep track of the size. Vocation (in 2D plane) and name of the shape. Two classes, Square and Circle, inherit from Shape. In main(), there should he one object of type Square (initial width - 4, name = square) and one object of type Circle (initial radius = 3.5. name = circle). The initial width name should be set when the object is declared. Assume that the objects are initially a the origin. Change name of square to "home". Input the position of the square from user and set position based on the user's input. Change name of circle to "it". Set position to (1.1). Double the size of the square. Move circle to right by 2.5 units. Display name, area and location of the square and circle. Put any println() statements in main(). not in methods. Your total score will be based on (a) whether you use correct syntax, (b) whether your program does what is asked, and (c) what techniques you choose to use.

Explanation / Answer

CODE:

package temp;

import java.util.Scanner;

// Base class - Shape
class Shape{
  
   // 4 member variables
   private String name;
   private double size;
   private double x;
   private double y;
  
   // Constructor
   public Shape(String name, double size, double x, double y) {
       this.name = name;
       this.size = size;
       this.x = x;
       this.y = y;      
   }

   // Getters and Setters
   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public double getSize() {
       return size;
   }

   public void setSize(double size) {
       this.size = size;
   }

   public double getX() {
       return x;
   }

   public void setX(double x) {
       this.x = x;
   }

   public double getY() {
       return y;
   }

   public void setY(double y) {
       this.y = y;
   }      
}

// Square class extends the base class - Shape
class Square extends Shape{
   // Constructor
   public Square(String name, double size, double x, double y) {
       super(name,size,x,y);
   }  
}

// Circle class extends the base class - Shape
class Circle extends Shape{
   // Constructor
   public Circle(String name, double radius, double x, double y) {
       super(name,radius,x,y);
   }  
}

// ShapeTest class to test the above classes.
public class ShapeTest {

   public static void main(String[] args) {
       // create an instance of Square class with given values
       Square sq = new Square("square", 4, 0, 0);
      
       // change the name of square to home.
       sq.setName("home");
      
       // get the user input.
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the position of the square");
       System.out.println("Enter the x - value");
       double x = sc.nextInt();
       System.out.println("Enter the y - value");
       double y = sc.nextInt();
      
       // set the new values of (x,y)
       sq.setX(x);
       sq.setY(y);
      
       // set the size to double of the previous value.
       sq.setSize(sq.getSize()*2);
      
       // Display the Square details.
       System.out.println("Square Details are: ");
       System.out.println("Name: " + sq.getName());
       System.out.println("Size: " + sq.getSize());
       System.out.println("Location: (" + sq.getX() + "," + sq.getY() + ")");
      
       System.out.println("---------------------------------------");
      
       // create an instance of Circle class with the given values
       Circle cl = new Circle("circle",3.5,0,0);
      
       // change the name of the circle to "it"
       cl.setName("it");
      
       // set the (x,y) value to (1,1)
       cl.setX(1);
       cl.setY(1);
      
       // move the circle right by 2.5 units.
       cl.setX(cl.getX() + 2.5);
      
       // Display the circle details.
       System.out.println("Circle Details are: ");
       System.out.println("Name: " + cl.getName());
       System.out.println("Size: " + cl.getSize());
       System.out.println("Location: (" + sq.getX() + "," + sq.getY() + ")");

  sc.close();
   }
}

OUTPUT:

Enter the position of the square
Enter the x - value
2
Enter the y - value
3
Square Details are:
Name: home
Size: 8.0
Location: (2.0,3.0)
---------------------------------------
Circle Details are:
Name: it
Size: 3.5
Location: (2.0,3.0)

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