Question

I need help with a java error Question: Consider a graphics system that has classes for...

I need help with a java error

Question:

Consider a graphics system that has classes for various figures—say, rectangles, boxes, triangles, circles, and so on. For example, a rectangle might have data members’ height, width, and center point, while a box and circle might have only a center point and an edge length or radius, respectively. In a well-designed system, these would be derived from a common class, Figure. You are to implement such a system.

The class Figure is the base class. You should add only Rectangle and Triangle classes derived from Figure. Each class has stubs for methods erase and draw. Each of these methods outputs a message telling the name of the class and what method has been called. Because these are just stubs, they do nothing more than output this message. The method center calls the erase and draw methods to erase and redraw the figure at the center. Because you have only stubs for erase and draw, center will not do any “centering” but will call the methods erase and draw, which will allow you to see which versions of draw and center it calls. Also, add an output message in the method center that announces that center is being called. The methods should take no arguments. Also, define a demonstration program for your classes.

Code:

.

Figure.java

import java.util.Scanner;

public abstract class Figure
{
   public abstract void erase();

   public abstract void draw();
  
   public abstract void center();
}

Rectangle.java

import java.util.Scanner;

public class Rectangle extends Figure
{
   int length;
   int width;

   public Rectangle()
   {
       length=0;
       width=0;
   }

   public Rectangle(int l, int w)
   {
       length=l;
       width=w;
   }

   public void setL(int l)
   {
       length=l;
   }

   public void setW(int w)
   {
       width=w;
   }

   public int getL()
   {
       return length;
   }

   public int getW()
   {
       return width;
   }

   public void erase()
   {
       for(int i=1; i<=length; i++)
       {
           System.out.println();
       }
   }

   public void draw()
   {
       for(int i=1; i<=length; i++)
       {
           if(i==1||i=length)
           {
               for(int j=1; j<=width; j++)
                   System.out.print("a");
           }

           else
           {
               System.out.print("b");
               for(int j=1; j<=2*width-3; j++)
                   System.out.print("c");
               System.out.print("d");
           }

           System.out.println();
       }

   }

   public void center()
   {
       System.out.println("The center is being called.");
       erase();
       draw();
   }

}

Triangle.java

import java.util.Scanner;

public class Triangle extends Figure
{
   private int side;

   public Triangle()
   {
       side=0;
   }

   public Triangle(int s)
   {
       side=s;
   }

   public void setS(int s)
   {
       side=s;
   }

   public int getS()
   {
       return side;
   }

   public void erase()
   {
       for(int i=1; i<=side; i++)
       {
           System.out.println();
       }

   }

   public void draw()
   {
       int oSpace=side-1;//outer spaces
       int iSpace=1;//inner spaces

       for (int i=1; i<=side; i++)
       {
           if(i==1)
           {
               for(int j=1; j<=oSpace; j++)
                   System.out.print("a");
               System.out.print("b");

               for(int j=1; j<=iSpace; j++)
                   System.out.print("c");
               System.out.print("d");
               iSpace+=2;
           }

           oSpace--;
           System.out.println();
       }

   }

   public void center()
   {
       System.out.println("The center is being called.");
       erase();
       draw();
   }
  
}

ShapeDemo.java

import java.util.Scanner;

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

       int l;
       int w;
       int s;

       System.out.println("What is the length of the rectangle?");
       l=input.nextInt();

       System.out.println("What is the width of the rectangle?");
       w=input.nextInt();

       System.out.println("What is the length of the sides for the triangle?");
       s=input.nextInt();

       Figure r=new Rectangle(l,w);

       Figure t=new Triangle(s);

       System.out.println("\nThis is your rectangle:\n");
       r.draw();

       System.out.println("\nThis is your rectangles center:\n");
       r.center();

       System.out.println("\nYour rectangle has been erased.\n");
       r.erase();


       System.out.println("\nThis is your triangle:\n");
       t.draw();

       System.out.println("\nThis is your triangles center:\n");
       t.center();

       System.out.println("\nYour triangle has been erased.\n");
       t.erase();
   }

}

Error:

Rectangle.java:52: error: bad operand types for binary operator '||'
if(i==1 || i=length)
^
first type: boolean
second type: int
1 error

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

error is if(i==1||i==length)

you have used single equal include double equal

corrected code

Figure.java

import java.util.Scanner;

public abstract class Figure
{
   public abstract void erase();

   public abstract void draw();
  
   public abstract void center();
}

Rectangle.java

import java.util.Scanner;

public class Rectangle extends Figure
{
   int length;
   int width;

   public Rectangle()
   {
       length=0;
       width=0;
   }

   public Rectangle(int l, int w)
   {
       length=l;
       width=w;
   }

   public void setL(int l)
   {
       length=l;
   }

   public void setW(int w)
   {
       width=w;
   }

   public int getL()
   {
       return length;
   }

   public int getW()
   {
       return width;
   }

   public void erase()
   {
       for(int i=1; i<=length; i++)
       {
           System.out.println();
       }
   }

   public void draw()
   {
       for(int i=1; i<=length; i++)
       {
           if(i==1||i==length)
           {
               for(int j=1; j<=width; j++)
                   System.out.print("a");
           }

           else
           {
               System.out.print("b");
               for(int j=1; j<=2*width-3; j++)
                   System.out.print("c");
               System.out.print("d");
           }

           System.out.println();
       }

   }

   public void center()
   {
       System.out.println("The center is being called.");
       erase();
       draw();
   }

}

Triangle.java

import java.util.Scanner;

public class Triangle extends Figure
{
   private int side;

   public Triangle()
   {
       side=0;
   }

   public Triangle(int s)
   {
       side=s;
   }

   public void setS(int s)
   {
       side=s;
   }

   public int getS()
   {
       return side;
   }

   public void erase()
   {
       for(int i=1; i<=side; i++)
       {
           System.out.println();
       }

   }

   public void draw()
   {
       int oSpace=side-1;//outer spaces
       int iSpace=1;//inner spaces

       for (int i=1; i<=side; i++)
       {
           if(i==1)
           {
               for(int j=1; j<=oSpace; j++)
                   System.out.print("a");
               System.out.print("b");

               for(int j=1; j<=iSpace; j++)
                   System.out.print("c");
               System.out.print("d");
               iSpace+=2;
           }

           oSpace--;
           System.out.println();
       }

   }

   public void center()
   {
       System.out.println("The center is being called.");
       erase();
       draw();
   }
  
}

ShapeDemo.java

import java.util.Scanner;

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

       int l;
       int w;
       int s;

       System.out.println("What is the length of the rectangle?");
       l=input.nextInt();

       System.out.println("What is the width of the rectangle?");
       w=input.nextInt();

       System.out.println("What is the length of the sides for the triangle?");
       s=input.nextInt();

       Figure r=new Rectangle(l,w);

       Figure t=new Triangle(s);

       System.out.println("\nThis is your rectangle:\n");
       r.draw();

       System.out.println("\nThis is your rectangles center:\n");
       r.center();

       System.out.println("\nYour rectangle has been erased.\n");
       r.erase();


       System.out.println("\nThis is your triangle:\n");
       t.draw();

       System.out.println("\nThis is your triangles center:\n");
       t.center();

       System.out.println("\nYour triangle has been erased.\n");
       t.erase();
   }

}

Add a comment
Know the answer?
Add Answer to:
I need help with a java error Question: Consider a graphics system that has classes for...
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
  • Consider the Rectangle2 java class definition below. Write the definition of an equals) method that checks...

    Consider the Rectangle2 java class definition below. Write the definition of an equals) method that checks if two Rectangle objects have the same dimensions Write a Java program to test the equals method public class Rectangle2 K private int width, length; public Rectangle2(int w, int 1) { setWidth(w); setLength(1); System.out.println("Inside parameterized!!!"); } public void setWidth(int w) { width = w;} public void setLength(int i) { length = 1;} int getWidth() { return width; } int getLength() { return length; }...

  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

  • 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...

  • // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public...

    // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public class String { public static void main(String[] args) { double Sside, Rlength, Rwidth, Tbase, Theight, Area, Tarea, Rarea; String input; Scanner keyboard = new Scanner(System.in); System.out.print("To Calculate the are of Square Enter 'Square', For Rectangle Enter 'Rectangle', For Triangle Enter 'Triangle'"); input = keyboard.nextLine(); if (input.equalsIgnoreCase("SQUARE")) { System.out.println("Square Side"); Sside = keyboard.nextInt(); Tarea = Sside * Sside; System.out.println("Side = " + Tarea ); }...

  • 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...

  • 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') {...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • 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...

  • I must implement a class to calculate n-th row of Pascal's Triangle (in Java). I need...

    I must implement a class to calculate n-th row of Pascal's Triangle (in Java). I need to create a static method that takes an argument "n" and returns the n'th line of pascal's triangle. the main method, which will call the class, is already done and the class is set up. Please build this without modifying the main method and without modifying exactly what the static method returns. public class Main { public static void main(String[] args) { int n...

  • Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override...

    Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override the toString method for Rectangle. Override the equals method for Rectangle. Implement the comparable Interface for Rectangle (Compare by area) Rectangle class: public class Rectangle {       private double length;    private double width;       public Rectangle(double l, double w)    {        length = l;        width = w;    }       public double getLength()    {   ...

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