Cannot find symbols while using abstract classes - java

Practicing abstract classes with shapes. The goal is to get the total area of the 3 shapes using abstract classes. So far this is what I have.
I'm not sure whether I did this part right:
static double sumArea(Shape[] arr){
// Sum up the areas of all the shapes using getArea()
return arr.getArea();
}
I keep getting error saying that it doesn't find the symbol h (height), w (width), tw (top_width). Anybody know why it's not finding these symbols?
public class TestShape{
public static void main(String args[]){
Point p = new Point(1, 1);
Shape[] arr = {
new Rectangle(p, 3, 4),
new Parallelogram(p, 5, 6, Math.PI/6.0),
new Trapezoid(p, 5, 6, 2)
};
System.out.println("SUM_AREA = " + sumArea(arr));
}
static double sumArea(Shape[] arr){
// Sum up the areas of all the shapes using getArea()
return arr.getArea();
}
}
class Point{
double x, y;
Point(){
this(0, 0);
}
Point(double x, double y){
this.x = x;
this.y = y;
}
public String toString(){
return "[" + x + ", " + y + "]";
}
}
abstract class Shape{
Shape(){
}
Shape(Point p){
this.p = p;
}
public Point getPosition(){
return p;
}
public void setPosition(Point p){
this.p = p;
}
// Abstract method
public abstract double getArea();
}
abstract class Quadrangle extends Shape{
protected double width, height;
Quadrangle(Point p, double w, double h){
this.p = p;
this.width = w;
this.height = h;
}
public double getWidth(){
return w;
}
public double getHeight(){
return h;
}
public void setWidth(double w){
this.weight = w;
}
public void setHeight(double h){
this.height = h;
}
}
class Rectangle extends Quadrangle{
Rectangle(Point p, double w, double h){
this.p = p;
this.width = w;
this.height = h;
}
public boolean isSquare(){
if(w == h){
return "Error";
}
}
#Override /** Return Area */
public double getArea(){
return w * h;
}
}
class Parallelogram extends Quadrangle{
protected double angle;
Parallelogram(Point p, double w, double h, double angle){
this.p = p;
this.weight = w;
this.height = h;
this.angle = angle;
}
public double getAngle(){
return angle;
}
public void setAngle(double a){
this.angle = a;
}
#Override /** Return Area */
public double getArea(){
return w * h;
}
}
class Trapezoid extends Quadrangle{
protected double top_width;
Trapezoid(Point p, double w, double h, double top_width){
this.p = p;
this.width = w;
this.height = h;
this.top_width = top_width;
}
public double getTopWidth(){
return top_width;
}
public void setTopWidth(double tw){
this.top_width = tw;
}
#Override /** Return Area */
public double getArea(){
return ((w + tw) / 2) * h;
}
}

The names w, tw and so on only exist as parameters. When you want to access the values you save in the constructors, you have to use the left hand side name: this.[width or whatever].
Also, rewrite sumArea to something like this:
static double sumArea(Shape[] arr){
// Sum up the areas of all the shapes using getArea()
double totalArea = 0;
for (Shape shape : arr) {
totalArea += shape.getArea();
}
return totalArea;
}

Related

Must implement inherited abstract method error

I'm getting an error for Sphere.java that says
The type sphere must implement the inherited abstract method GeometricObjects.hit(Ray)
Though I don't see how I am getting this error as I've defined the hit method as shown below.
Below is the Sphere.java code:
package Objects;
import Utility.*;
public class Sphere extends GeometricObjects {
Point3d center;
double radius;
public Sphere(Point3d center, double radius, Color color) {
this.center = new Point3d(center);
this.radius = radius;
this.color = new Color(color);
}
public double hit(Ray ray) {
double a = ray.direction.dot(ray.direction);
double b = 2*ray.origin.sub(center).dot(ray.direction);
double c = ray.origin.sub(center).dot(ray.origin.sub(center)) - radius*radius;
double discriminant = b*b - 4*a*c;
if (discriminant < 0.0){
return 0.0;
}
else {
double t = (-b - Math.sqrt(discriminant)) / (2*a);
if (t > 10E-9) {
return t;
}
else {
return 0.0;
}
}
}
}
This is the GeometricObjects.java code:
package Objects;
import Utility.*;
public abstract class GeometricObjects {
public Color color;
public abstract double hit(Ray ray);
}
And heres the Ray.java code:
package Utility;
public class Ray {
public Point3d origin;
public Vector direction;
public Ray(Point3d origin, Vector direction) {
this.origin = new Point3d(origin);
this.direction = new Vector(direction);
}
}

Java - Cylinder Program - Unable to use main

I am so lost I don't even know what question to ask anymore. I have this program that is supposed to be able to construct a Cylinder with parameters radius and height. Then it should be able to call various methods to get and set radius as well as output surface area and volume. I can't get passed go because I cannot put anything in my main without an error about static non static methods being used in static. I don't even know what that means. I actually copy code from others into my compiler and it gives me the same error. Do I have some setting screwed up? I know this is probably too elementary for Stack Overflow but I am desperate at this point.
public class Miller_A03Q1 {
public static void main(String[] args) {
Cylinder cylinder1 = new Cylinder(1,17);
Cylinder cylinder2 = new Cylinder(3,8);
Cylinder cylinder3 = new Cylinder(2,12);
Cylinder cylinder4 = new Cylinder (1,14);
}
public class Cylinder{
private double radius = 0.0;
private double height= 0.0;
private double area = 0.0;
private double volume=0.0;
private String shape = "cylinder";
public Cylinder(double r,double h){
this.radius = r;
System.out.print(r);
this.height = h;
System.out.print(h);
}
public double getVolume(){
double volume = 3.14 * radius * radius * height;
return volume;
}
public double getArea(){
double circumference = 3.14 * 2 * radius;
double circleArea = 3.14 * radius * radius;
double area = (2 * circleArea) + (circumference * this.height);
return area;
}
public double getRadius(){
return this.radius;
}
public double getHeight(){
return this.height;
}
public void setHeight(double h){
this.height = h;
}
public void setRadius(double r){
this.radius = r;
}
#Override
public String toString(){
return this.shape + this.radius + this.height+ this.volume + this.area;
}
}
}
Inner classes are just like any other member (well, except for enums). If you don't explicitly declare them static, they won't be, so you won't be able to access them from a static context, such as main. To make a long story short - declare you Cylinder inner class as static, and you should be OK:
public class Miller_A03Q1 {
public static void main(String[] args) {
Cylinder cylinder1 = new Cylinder(1,17);
Cylinder cylinder2 = new Cylinder(3,8);
Cylinder cylinder3 = new Cylinder(2,12);
Cylinder cylinder4 = new Cylinder (1,14);
}
public static class Cylinder{
// etc...
I don't know if you need the outer class but if you just put the main method inside the Cylinder class it compiles for me....
public class Cylinder {
private double radius = 0.0;
private double height = 0.0;
private double area = 0.0;
private double volume = 0.0;
private String shape = "cylinder";
public Cylinder(double r, double h) {
this.radius = r;
System.out.print(r);
this.height = h;
System.out.print(h);
}
public double getVolume() {
double volume = 3.14 * radius * radius * height;
return volume;
}
public double getArea() {
double circumference = 3.14 * 2 * radius;
double circleArea = 3.14 * radius * radius;
double area = (2 * circleArea) + (circumference * this.height);
return area;
}
public double getRadius() {
return this.radius;
}
public double getHeight() {
return this.height;
}
public void setHeight(double h) {
this.height = h;
}
public void setRadius(double r) {
this.radius = r;
}
#Override
public String toString() {
return this.shape + this.radius + this.height + this.volume + this.area;
}
public static void main(String[] args) {
Cylinder cylinder1 = new Cylinder(1, 17);
Cylinder cylinder2 = new Cylinder(3, 8);
Cylinder cylinder3 = new Cylinder(2, 12);
Cylinder cylinder4 = new Cylinder(1, 14);
}
}

Fill Color Custom Shape Java

I want to fill a color inside of my custom shape
here is my code for trapezium shape
Trapezium Class
public class Trapezium implements Shape {
private GeneralPath trapezium = new GeneralPath();
public Trapezium (Point2D A, double height, double width)
{
double Ax = A.getX();
double Ay = A.getY();
double Bx = Ax + (width/6);
double By = Ay;
double Cx = Bx + (width/3);
double Cy = By + height;
double Dx = Cx - width;
double Dy = Cy;
double Ex = Dx + (width/3);
double Ey = Dy - height;
trapezium.moveTo(Ax, Ay);
trapezium.lineTo(Bx, By);
trapezium.lineTo(Cx, Cy);
trapezium.lineTo(Dx, Dy);
trapezium.lineTo(Ex, Ey);
trapezium.closePath();
}
#Override
public java.awt.Rectangle getBounds() {
return trapezium.getBounds();
}
#Override
public Rectangle2D getBounds2D() {
return trapezium.getBounds2D();
}
#Override
public boolean contains(double x, double y) {
return trapezium.contains(x, y);
}
#Override
public boolean contains(Point2D p) {
return trapezium.contains(p);
}
#Override
public boolean intersects(double x, double y, double w, double h) {
return trapezium.intersects(x, y, w, h);
}
#Override
public boolean intersects(Rectangle2D r) {
return trapezium.intersects(r);
}
#Override
public boolean contains(double x, double y, double w, double h) {
return trapezium.contains(x, y,w ,h);
}
#Override
public boolean contains(Rectangle2D r) {
return trapezium.contains(r);
}
#Override
public PathIterator getPathIterator(AffineTransform at) {
return trapezium.getPathIterator(at);
}
#Override
public PathIterator getPathIterator(AffineTransform at, double flatness) {
return trapezium.getPathIterator(at, flatness);
}
}
Here's my code when i call the trapezium class
DrawPanel Class
else if(type.get(a).equals("Trapezium"))
{
int[]coordinates=allShapes.get(a);
Point2D b= new Point2D.Double(coordinates[0], coordinates[1]);
Trapezium trapezium = new Trapezium(b,coordinates[3], coordinates[2]);
g2.setStroke(new BasicStroke(coordinates[4]));
g2.setPaint(dragColor);
g2.draw(trapezium);
}
the setPaint only colors the stroke of my shape , how do i color the inside of the shape ?
Image of my current code when executed
EDIT
I have 2 JColorChooser, 1 is for the stroke of the shape , and 2 is for the color inside the shape
if i use the
g2.setPaint(color2)
g2.fill(trapezium)
how do i get the color1 to change the color of my stroke ?
you have to simply fill the shape ^_^
Trapezium trapezium = new Trapezium(b,coordinates[3], coordinates[2]);
g2.setStroke(new BasicStroke(coordinates[4]));
g2.setPaint(dragColor);
g2.fill(trapezium); //HERE!!!!

How to create an object from data in a file and assign it to an array?

I need to take information from a file and create them into objects and put them into an array so I can compare the areas of the objects and list in the array which object has the largest area and its location in the array.
I'm confused on how I take the information from the file and create each one into a object (circle or rectangle) and then assign that object into an array after it has been created. I think my other classes are fine, I'm just stuck on finishing the driver.
Normally, I would do something like Circle c1 = new Circle(); to create a new object, but how do I do that from a file with predefined information and assign it to an array?
Data:
“CIRCLE”, 1, “blue”, true
“RECTANGLE”, 1, 2, “blue”, true
“RECTANGLE”, 10, 2, “red”, true
“CIRCLE”, 2, “green”
“RECTANGLE”
“CIRCLE”
Driver:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("C:/Users/Charles/Desktop/GeometricObjectsData.txt"));
ArrayList<GeometricObject> list = new ArrayList<GeometricObject>();
while (input.hasNext()) {
String line = input.nextLine();
System.out.println(line);
}
}
}
GeometricObject:
public abstract class GeometricObject {
//class variables
private String color;
private boolean filled;
//constructors
public GeometricObject() {
super();
color = "white";
filled = false;
}
public GeometricObject(String color, boolean filled) {
super();
this.color = color;
this.filled = filled;
}
//mutators
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
//user-defined methods
public abstract double getArea();
public abstract double getPerimeter();
#Override
public String toString() {
return super.toString() + " \tColor=" + this.getColor() + " \tFilled=" + this.isFilled();
}
}
Circle:
public class Circle extends GeometricObject {
//class variables
private double radius;
//constructors
public Circle() {
super();
radius = 1;
}
public Circle(double radius, String color, boolean filled) {
super(color, filled);
this.radius = radius;
}
//mutators
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//user-defined methods
#Override
public double getArea() {
//area of a circle
return (radius * radius * Math.PI);
}
#Override
public double getPerimeter() {
//perimeter of a circle
return (2 * radius * Math.PI);
}
#Override
public String toString() {
return super.toString() + "\nCircle: Radius=" + this.getRadius();
}
}
Rectangle:
public class Rectangle extends GeometricObject {
//class variables
private double height;
private double width;
//constructors
public Rectangle() {
super();
height = 1;
width = 1;
}
public Rectangle(double height, double width, String color, boolean filled) {
super(color,filled);
this.height = height;
this.width = width;
}
//mutators
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
//user-defined methods
#Override
public String toString() {
return super.toString() + "\nRectangle: Height=" + this.height + "\tWidth=" + this.width;
}
#Override
public double getArea() {
return (height * width);
}
#Override
public double getPerimeter() {
return (2 * height + 2 * width);
}
}
In your text file, you have special quotes around your shape items. This will make your life more difficult, so you should change that, if possible
Example of how to make objects (from your main method):
while (input.hasNext()) {
String line = input.nextLine();
System.out.println(line);
String[] parts = line.split(",");
if (parts[0].indexOf("Circle") != -1) {
Circle c = new Circle();
// ... parse the rest of the attributes to set up your circle
} else if ... // fill in the other shape cases
}

circle object constructor not defined

I am trying to make class circle extend the Shape class but keep getting a error from JUnit saying the constructor Circle(Point, int) is undefined how would i define the Circle constructor differently from public Circle(Point[] center, int aradius)?
import java.awt.Point;
public abstract class Shape {
private String name;
private Point[] points;
protected Shape(){};
protected Shape(String aName) {
name = aName;
}
public final String getName() {
// TODO Implement method
return name;
}
protected final void setPoints(Point[] thePoints) {
points = thePoints;
}
public final Point[] getPoints() {
// TODO Implement method
return points;
}
public abstract double getPerimeter();
public static double getDistance(Point one, Point two) {
double x = one.getX();
double y = one.getY();
double x2 = two.getX();
double y2 = two.getY();
double x3 = x - x2;
double y3 = y - y2;
double ypow = Math.pow(y3, 2);
double xpow = Math.pow(x3, 2);
double added = xpow + ypow;
double distance = Math.sqrt(added);
return distance;
}
}
Circle.java
import java.awt.Point;
public class Circle extends Shape{
private double radius;
public Circle(Point[] center, int aradius) {
if(radius < 0){
radius = 0;
}
else{
radius = aradius;
}
this.setPoints(center);
}
#Override
public double getPerimeter() {
double perim = 2 * Math.PI * radius;
return perim;
}
public double getRadius(){
return radius;
}
}
Just pass it a single Point, not an array.
public Circle(Point center, int aradius)

Categories

Resources