Project #45484 - java HW

Lab 16

A Diamond Program

 

Objective:

 

Write a class that uses the shape interface to now draw a diamond

  • First download the "Lab16Driver" and put it in your project
    • DO NOT ALTER THE DRIVER!
  • Also download the "ShapeBasics" and "ShapeInterface" to use in your code
  • Write an INTERFACE call DiamondInterface which inherits from ShapeInterface
  • Create the following method definitions
    • setWidth – this method is used to set the width of the diamond
  • Write a class file called Diamond that DOES NOT HAVE a main method
  • This class implements the DiamondInterface and inherits from ShapeBasics
  • Some of the attributes of Diamond are
    • width – that represents the width of the triangle
  • Create the following Constructors
    • Default – sets everything to default values
      • width = 0
    • One that has the parameters
      • Offset – used in the parent class
      • width
  • Accessors and Mutators for each variable
    • MAKE SURE THE MUTATORS CHECK FOR VALID VALUES!
  • Create the following Methods
    • setWidth – this sets the width of the diamond.  This must be implemented just like the interface
    • drawHere – this draws the diamond and overrides the method in ShapeBasics
    • drawTopV – this private method draws the top part of the diamond
    • drawBottomV – this private method draws the bottom part of the diamond
  • Create the following static methods
    • skipSpaces – this takes in a certain number of spaces to skip which means drawing “ “.

 

 

 

Example Dialog:

Welcome to the diamond drawer

Enter the diamond size followed by the offset or 0 to quit

7

1

    *

   * *

  *   *

 *     *

  *   *

   * *

    *

Enter the diamond size followed by the offset or 0 to quit

0

0

bye!

Finally:

Upload DiamondInterface.java and Diamond.java file to the dropbox 

 

Lab16Driver

 

import java.util.*;
public class DiamondDrawer {
	public static void main(String[]args)
	{
		System.out.println("Welcome to the diamond drawer");
		Scanner keyboard = new Scanner(System.in);
		boolean quit = false;
		while(quit == false)
		{
			//Prompt the user
			System.out.println("Enter the diamond size followed by the offset or 0 to quit");
			//Get the inputs
			int width = keyboard.nextInt();
			int offset = keyboard.nextInt();
			//Check if the user wants to quit
			if(width ==0 || offset == 0)
			{
				System.out.println("bye!");
				break;
			}
			//Draw the diamond
			Diamond d = new Diamond(width,offset);
			d.drawHere();
		}
	}
}

 

ShapeBasics

 

/**
 Class for drawing simple shapes on the screen using keyboard
 characters. This class will draw an asterisk on the screen as a 
 test. It is not intended to create a "real" shape, but rather
 to be used as a base class for other classes of shapes.
*/
public class ShapeBasics implements ShapeInterface
{
    private int offset;

    public ShapeBasics()
    {
        offset = 0;
    }

    public ShapeBasics(int theOffset)
    {
        offset = theOffset;
    }

    public void setOffset(int newOffset)
    {
        offset = newOffset;
    }

    public int getOffset()
    {
        return offset;
    }

    /**
     Draws the shape at lineNumber lines down
     from the current line.
    */
    public void drawAt(int lineNumber)
    {
        for (int count = 0; count < lineNumber; count++)
            System.out.println( );
        drawHere( );
    }

    /**
     Draws the shape at the current line.
    */
    public void drawHere()
    {
        for (int count = 0; count < offset; count++)
            System.out.print(' ');
        System.out.println('*');
    }
 }


ShapeInterface

 

/**
 Interface for simple shapes drawn on
 the screen using keyboard characters. 
*/
public interface ShapeInterface
{
    /**
     Sets the offset for the drawing.
    */
    public void setOffset(int newOffset);

    /**
     Returns the offset for the drawing.
    */
	public int getOffset();

    /**
     Draws the shape at lineNumber lines down
     from the current line.
    */
    public void drawAt(int lineNumber);

    /**
     Draws the shape at the current line.
    */
    public void drawHere();
}

 

Subject Computer
Due By (Pacific Time) 10/29/2014 11:30 pm
Report DMCA
TutorRating
pallavi

Chat Now!

out of 1971 reviews
More..
amosmm

Chat Now!

out of 766 reviews
More..
PhyzKyd

Chat Now!

out of 1164 reviews
More..
rajdeep77

Chat Now!

out of 721 reviews
More..
sctys

Chat Now!

out of 1600 reviews
More..
sharadgreen

Chat Now!

out of 770 reviews
More..
topnotcher

Chat Now!

out of 766 reviews
More..
XXXIAO

Chat Now!

out of 680 reviews
More..
All Rights Reserved. Copyright by AceMyHW.com - Copyright Policy