Bryn
Bryn

Reputation: 47

graphing data from a website

I am creating a program where I plot the maximum daily temperature for a specific city using data retrieved directly from the National Weather Service web site. I have the data, but the program errors when I try to create the actual graph. My error says that I need a return type but if you look a my code, I have one. Its in OOP format so the code I will show you is part of the implementation class. Can someone tell me how to fix my error?

My error:

invalid method declaration; return type required

My Code:

public class createGraph
{
    Picture canvas = null;
    Graphics g = null;
    Graphics2D g2 = null;

    DrawingGraph(int length, int height, Color color)
    {
        canvas = new Picture(length, height);
        canvas.setAllPixelsToAColor(color);
        g = canvas.getGraphics();
        g2 = (Graphics2D)g;
        g2.setColor(color);    
    }

    public void drawARectangle(Color color, int x1, int y1, int width, int height)
    {
        g2.setColor(color);
        g2.drawRect(x1, y1, width, height);
    }

    public Picture getGraph()
    {
        return canvas;
    }

    g.drawString("Maximum Temperature Readings", 196, 65);
    g.drawString("Pensacola, FL - August, 2009", 205, 80);

    int xPos = 97;
    for(int day = 1; day <= 31; day++)
    {
        xPos = xPos + 13;
        g.drawLine(xPos, 500, xPos, 510);

    }

    for(int yPos = 100; yPos <= 500; yPos +=40)
    {
        g.drawLine(93,yPos, 100, yPos);

    }

    g.drawString("100", 70, 105); 
    g.drawString("90", 75, 145);   
    g.drawString("80", 75, 185);
    g.drawString("70", 75, 225);
}

The final graph should end up looking like this:

graph

Upvotes: 0

Views: 35

Answers (3)

Michele Da Ros
Michele Da Ros

Reputation: 906

the class name starts with a lowerCase. Are you shure that you did't swap "lowerCase" and "DrawingGraph" ?

Upvotes: 0

mrBrown
mrBrown

Reputation: 36

DrawingGraph isn't an function nor a Constructor, if it's meant to be an function, it needs am return-Type, if it should be a Constructor, it must have the same name as the class.

Also, the last part of your class, after getGraph(), must be in a function, but is located directly in the class body. Just declare an new Function and move it in it an call it.

Upvotes: 0

NickJ
NickJ

Reputation: 9559

This code here:

DrawingGraph(int length, int height, Color color)
{
    canvas = new Picture(length, height);
    canvas.setAllPixelsToAColor(color);
    g = canvas.getGraphics();
    g2 = (Graphics2D)g;
    g2.setColor(color);    
}

Is that supposed to be a method or the constructor? If it's a normal method, you need void, or if it's the constructor, it should be the same name as the class.

You also have lots of code which is not in a method at all.

Upvotes: 1

Related Questions