Question

Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they...

Programming: Java: Fixing errors in code help:

The errors are in square.java and rectangle.java and they both say:

constructor Shape in class Shape cannot be applied to given types;
required: no arguments
found: String
reason: actual and formal argument lists differ in length

The directions say:

The files Shape.java and TestArea.java have been finished already, YOU DONT ADD ANYTHING TO THESE TWO. According to the requirement, you need to modify in Square.java and Rectangle.java, respectively

a. Implementing constructor with no negative arguments

b. Use the protected variable “shapeName” defined in shape.java to hold the class name for each class.

c. Implementing getArea() method to return the area.

d. Implementing toString() method to print out the shape name, all class variables, and area result.

Here are all the codes and I will make bold the error part on here for the code thank you in advance!!

SQUARE.JAVA:

package shape;

public class Square extends Shape
{
private double side;
//constructor
public Square(double s)
{
   // To do 1: implement the constructor, to make sure s>=0
super("Square");
if(s>0)
this.side=s;
else
this.side=0;
}

// To do 2: calculate the area for the square
public double getArea()
{
return this.side*this.side;
}

// To do 3: return the name, side, with the area
public String toString()
{
return("Name: "+super.getName()+" Area: "+getArea());
}
}

RECTANGLE.JAVA

package shape;

public class Rectangle extends Shape
{
private double length, width;
//constructor
public Rectangle(double l, double w)
{
super("Rectangle");
if(1>0 && w>0)
{
this.length=1;
this.width=w;
}
else
{
this.length=0;
this.width=0;}
}

// To do 2: calculate the area for the square
public double getArea()
{
return this.length*width;
}

// To do 3: return the name, length, width, with the area
public String toString()
{
return("Name:"+ super.getName()+"Area: "+getArea());
}
}

SHAPE.JAVA NOT TO BE MODIFIED

package shape;


public abstract class Shape
{
protected String shapeName;
  
// abstract getArea method
public abstract double getArea();
  
public String getName()
{
return shapeName;
}
  
public String toString()
{
return String.format("%s ", getName() );
}
}

TESTAREA.JAVA NOT TO BE MODIFIED

package shape;


import java.util.Scanner;

public class TestArea
{
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );

System.out.print("Input the side of the square: ");
double side = input.nextDouble();

System.out.print("Input the length of the rectangle: ");
double length = input.nextDouble();

System.out.print("Input the width of the rectangle: ");
double width = input.nextDouble();

Shape arrayOfShapes[]= new Shape[2]; //polymorphism
arrayOfShapes[0] = new Square( side );
arrayOfShapes[1] = new Rectangle(length, width );

for ( Shape shape : arrayOfShapes)
    System.out.println(shape); //invokes toString
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java code

/**

Shape class does not have any constructor which takes one argument of String

the type that's why this error.

this class has protected member field shapeName so use this directly in descendent classes directly

*/

//Complete java code

package shape;
public abstract class Shape
{
    protected String shapeName;

    // abstract getArea method

    public abstract double getArea();

    public String getName()
    {
        return shapeName;
    }

    public String toString()
    {
        return String.format("%s ", getName() );
    }
}

//=====================================

package shape;

public class Rectangle extends Shape
{
    private double length, width;
    //constructor
    public Rectangle(double l, double w)
    {
       shapeName = "Rectangle";
        if(l>0 && w>0)
        {
            this.length=l;
            this.width=w;
        }
        else
        {
            this.length=0;
            this.width=0;}
    }

    // To do 2: calculate the area for the square
    public double getArea()
    {
        return length*width;
    }

    // To do 3: return the name, length, width, with the area
    public String toString()
    {
        return("Name:"+ super.getName()+" Area: "+getArea());
    }
}

//==========================================

package shape;
public class Square extends Shape
{
    private double side;
    //constructor
    public Square(double s)
    {
        // To do 1: implement the constructor, to make sure s>=0
     shapeName = "Square";
        if(s>0)
            this.side=s;
        else
            this.side=0;
    }

    // To do 2: calculate the area for the square
    public double getArea()
    {
        return this.side*this.side;
    }

    // To do 3: return the name, side, with the area
    public String toString()
    {
        return("Name: "+super.getName()+" Area: "+getArea());
    }
}

//=====================================

package shape;


import java.util.Scanner;

public class TestArea
{
    public static void main( String args[] )
    {
        Scanner input = new Scanner( System.in );

        System.out.print("Input the side of the square: ");
        double side = input.nextDouble();

        System.out.print("Input the length of the rectangle: ");
        double length = input.nextDouble();

        System.out.print("Input the width of the rectangle: ");
        double width = input.nextDouble();

        Shape arrayOfShapes[]= new Shape[2]; //polymorphism
        arrayOfShapes[0] = new Square( side );
        arrayOfShapes[1] = new Rectangle(length, width );

        for ( Shape shape : arrayOfShapes)
            System.out.println(shape); //invokes toString
    }

}

//Output

C:\Program Files\Java\jdk1.8.0_221\bin\java. Input the side of the square: 5 Input the length of the rectangle: 4 Input the

//If you need any help regarding this solution....... please leave a comment........ thanks

Add a comment
Know the answer?
Add Answer to:
Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

  • Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface....

    Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface. Couldn't figure it out. the compare to method should print 0, 1, or -1 import java.util.*; public class RectangleMain {    public static void main(String [] args) throws CloneNotSupportedException    {        /*ComparableRectangleAlsoCloneable obj1 = new ComparableRectangleAlsoCloneable(4, 5);        ComparableRectangleAlsoCloneable obj2 = (ComparableRectangleAlsoCloneable)obj1.clone();               System.out.println(obj1.toString());        System.out.println(obj1 == obj2); //false        System.out.println(obj2.toString());*/               Scanner...

  • A general shape class is shown below. Shape has a dimension. It also defines constructors, getters,...

    A general shape class is shown below. Shape has a dimension. It also defines constructors, getters, setters and a toString method. class Shape{ int dimension; public Shape(){} public Shape(int newDimension){ dimension = newDimension; } public int getDimension(){ return dimension; } public void setDimension(int newDimension){ dimension = newDimension; } public String toString(){ return "Shape has dimension "+dimension; } } a. Define classes Circle and Square, which inherit from Shape class. b. Both classes must have two constructors similar to Shape class....

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

  • Please, modify the code below to use the Switch Statements in Java instead of “if” statements...

    Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions. import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0); if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') {...

  • JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST...

    JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST CASES(WHAT IM TRYING TO PRODUCE) BELOW THOSE IMAGES ARE THE .JAVA FILES THAT I HAVE CREATED. THESE ARE GeometircObject.Java,Point.java, and Tester.Java. I just need help making the Rectangle.java and Rectangle2D.java classes. GeometricObject.Java: public abstract class GeometricObject { private String color = "white"; // shape color private boolean filled; // fill status protected GeometricObject() { // POST: default shape is unfilled blue this.color = "blue";...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT