Page 1 of 1

CSCI 161 - Lab 10: "Shapes" Objectives The objectives of this lab are to reinforce and help teach: Overview In this lab,

Posted: Thu May 05, 2022 12:47 pm
by answerhappygod
CSCI 161 - Lab 10: "Shapes"
Objectives
The objectives of this lab are to reinforce and help teach:
Overview
In this lab, you will develop classes and a program that each
deal with Geometric shapes, in particular
the Point, Circle, Square and Rectangle shapes.
Your program will employ classes that have been provided as well
as some that you will develop yourself. When complete, your program
will create objects of each type and then execute each object's
behaviors (i.e. call their methods) in order to put each shape (and
each of your classes) through its paces.
So that you can better understand what your program will do,
consider the following diagram that shows the Point, Circle, Square
and Rectangle objects your main method will create, including their
origin locations and dimensions.
Notes:
You Have Been Provided the Following:
You Must Develop the Following:
Instructions
Following are the instructions and you should follow them
carefully:
Output Specification
Following is the desired output:
Csci 161 Lab 10 Shapes Objectives The Objectives Of This Lab Are To Reinforce And Help Teach Overview In This Lab 1
Csci 161 Lab 10 Shapes Objectives The Objectives Of This Lab Are To Reinforce And Help Teach Overview In This Lab 1 (40.25 KiB) Viewed 47 times
Public class circle {
Csci 161 Lab 10 Shapes Objectives The Objectives Of This Lab Are To Reinforce And Help Teach Overview In This Lab 2
Csci 161 Lab 10 Shapes Objectives The Objectives Of This Lab Are To Reinforce And Help Teach Overview In This Lab 2 (66.4 KiB) Viewed 47 times
public class Point { } /** Fields (aka State variables) **/ double x; double y; /** Constructors **/ // constructs a point with a location of 0, 0 public Point() { x = 0; y = 0; } // constructs a new point with the given (x, y) location public Point (double initialX, double initialy) { x = initialX; y = initialY; } /** Methods (Behavior of the class) **/ // Accessor Method: Returns the area of the Point (points have no area) public double area() { return (0.0); } // Accessor Method: Return perimeter of the Point (points have no perimeter) public double perimeter() { return (0.0); } // Accessor Method: Returns the single point public Point[] getPoints() { Point [] points = new Point [1]; // Just one point for a Point points [0] this; // this refers to this object return points; } // Accessor Method: Returns a String representation of this point public String toString() { return String.format("(%.2f, %.2f)", x, y); }
/** Constants **/ private final double PI = 3.14159; /** Fields (aka State variables) **/ Point origin = new Point(); double radius; /** Constructor **/ /** * Constructs a Circle given origin's x, y and radius. * @param initialX - Origin point's x value * @param initialY - Origin point's y value * @param initialRadius - Radius of the circle. */ public Circle( double initialx, double initialY, double initialRadius) { origin.x = initialX; origin.y = initialy; radius = initialRadius; } /** Methods (Behavior of the class) **/ // Accessor Method: Returns the area of the Circle public double area() { return (PI * radius * radius); } // Accessor Method: Return perimeter (aka circumference) of the Circle public double perimeter() { return (2.0 * PI * radius ); } // Accessor Method: Returns the points along the Circle's circumference public Point[] getPoints() { Point [] points = new Point [360]; for (int i=0; i<points.length; i++) { } double angle = PI * 2.0 * (double)i / 360.0; // angle in radians points = new Point( origin.x + radius * Math.cos(angle), origin.y + radius * Math.sin(angle)); } return points; } // Accessor Method: Returns a String representation of this Circle public String toString() { String ret = "["; // Start with array bracket Point [] points = getPoints(); // get points of the shape for (int i=0; i < points.length; i++) { // loop to add to ret string ret += points .toString(); if (i+1 points.length) { // fence-post variant ret += ","; } } ret += "]"; // Close out array bracket return ret;