Wątek przeniesiony 2016-02-16 22:06 z Java przez bogdans.

Pomocy Podstawy z Javy zadanie

0

Witam. Mam plik i muszę poprawić w nim błędy. Same podstawy jak importowanie skanera, zmienne, dziedzicznie, subklasy, superklasy. Poprawiłem już większość błędów i zostało paru z ktorymi nie moge sobie poradzić. Jeśli ktoś mógłby zerknąć i poprawić byłbym wdzięczny.


package geocalcs;

public class Circle  {
    double radius;
    public Circle()
    {
        super("Circle");
        radius = 1.0;
    }
    public Circle(double r)
    {
        super("Circle");
        Radius = r;
    }
    public void setRadius(double r) {this.radius = r;}
    public double getRadius() {return this.radius;}
    public double getCircumference() {return 2*Math.PI*this.radius;}
    public double getArea() {return Math.PI*Math.pow(this.radius, 2);}
    @Override
    public void printObject()
    {
        System.out.printf("Radius = %f\n", this.getRadius());
        System.out.printf("Type:%s, Circumference = %f units, Area = %f units\n", this.getType(), this.getCircumference(), this.getArea());
    }
}

package geocalcs;

public class Cylinder extends GeoShapes{
    private double radius;
    private double height;
    
    /************
    ADD a default constructor here 
     ***********/
    
    public Cylinder(double r, double h)
    {
        super("Cylinder");
        this.radius = r;
        this.height = h;
    }
    public void setDims(double h, double r) {this.height = h; this.radius = r;}
    public double getHeight() {return this.height;}
    public double getRadius() {return this.radius;}
    public double getSurfaceArea() 
    {
        Circle c = new Circle(this.radius);
        Rect r = new Rect(this.height, c.getCircumference());
        return 2.0*c.getArea() + r.getArea();
    }
    public double getVolume() 
    {
        Circle c = new Circle(this.radius);
        return c.getArea() * this.height;
    }
    @Override
    public void printObject()
    {
        System.out.printf("Radius = f, Height = %f\n", this.getRadius(),this.getHeight());
        System.out.printf("Type:%s, Volume = %f units, Surface Area = %f units\n", this.getType(),
                this.getVolume(), this.getSurfaceArea());
    }
}

package geocalcs;



public class GeoCalcs {

    static Scanner s = new Scanner();

    public static void main(String[] args) {
        boolean loop = true
        do {
            System.out.println(Select an option:\nR for rectangle\nC for circle"
                    + "\nY for Cylinder\nP for Rectangular Prism");
            String str = s.next().toUpperCase();
            switch (str) {
                case R:
                    rectangleChosen();
                    loop = false;
                    break;
                case "C":
                    circleChosen();
                    loop = false;
                    break;
                case "Y":
                    cylinderChosen();
                    loop = false;
                    break;
                case "P":
                    rectPrismChosen();
                    loop = false;
                    break;
                //ADD a default case here
            }
        } while (loop)
    }
    public static void rectangleChosen();
    {
        Rect r = new rect(2.0, 3.0);
        System.out.println("The object is " + r.getObject());
        printObject();
    }
    
    /*******
    ADD method circleChosen here
    *******/
    
    public static void cylinderChosen()
    {
        Cylinder c =  Cylinder(2.0, 3.0);
        System.out.println("The object is " + c.getObject());
        c.printObject();
    }
    public static void rectPrismChosen()
    {
         r = new RectPrism(5.0, 3.0, 4.0);
        System.out.println("The object is " + r.getObject());
        r.printObject();
    }
}

package geocalcs;

public class GeoShapes {
   String type;
   GeoShapes{}
   GeoShapes(String t) {type = t;}
   public  getType() {return this.type;}
   public String getObject(){return this.toString();}
   public void printObject(){}
}


package geocalcs;

public class Rect extends GeoShapes {
    private double height;
    
    public Rect()
    {
        super("Rectangle");
        height = 1.0;
        width = 1.0;
    }  
    public Rect(double h,  w)
    {
        super("Rectangle");
        height = h;
        width = w;
    }
    public void setDims(double h, double w) {this.height = h; this.width = w;}
    public double getHeight() {return this.height;}
    public double getWidth() {return this.width;}
    public double getCircumference() {return 2*this.width+this.height;}
    public  getArea() {return this.width * this.height;}
    @Override
    public void printObject()
    {
        System.out.printf("Width = %f, Height = %f\n", this.getWidth(), this.getHeight());
        System.out.printf("Type:%s, Circumference = %f units, Area = %f units\n", this.getType(),
                this.getCircumference(), this.getArea());
    }
}

package geocalcs;

public class RectPrism extends GeoShapes {
    private double width;
    private double depth;
    private double height;
    public RectPrism()
    {
        super("Rectangular Prism");
        this.width = 1.0;
        this.height = 1.0;
        this.depth = 1.0;
    }
    public RectPrism(double w, double h, double d)
    {
        super("Rectangular Prism");
        this.width = w;
        this.height = h;
        this.depth = d;
    }
    public void setDims(double w, double h, double d) 
    {
        this.width = w; 
        this.height = h; 
        this.depth = d;
    }
    public double getHeight() {return this.height;}
    public double getWidth() {return this.width;}
    public double getDepth() {return this.depth;}
    public double getSurfaceArea() 
    {
        double[] area = new double[3];
        double surfaceArea = 0;
        for(int i=0; i<area.length; i++)
        {
            if(i==0) area[i] = new Rect(width, depth).getArea();
            else if(i==1) area[i]  = new Rect(width, height).getArea();
            else area[i] = new Rect(depth, height).getArea();
            surfaceArea += area[i];
        }
        return 2.0*surfaceArea;
    }
    public double getVolume() { this.depth * this.height * this.width;}
    @Override
    public void printObject()
    {
        System.out.printf("Width = %f, Height = %f, Depth = %f\n", this.getWidth(), 
                this.getHeight(), this.getDepth());
        System.out.printf("Type:%s, Volume = %f units, Surface Area = %f units\n", this.getType(),
                this.getVolume(), this.getSurfaceArea());
    }
}
0

Na pierwszy rzut oka to brakuje dziedziczenia. Kod jest pisany tak jakby klasa Circle po czymś dziedziczyła, a deklaracji dziedziczenia brak.

0

Przyjrzyj się jak został pokolorowany kod na forum. Większość jest niebieska, a niebieska jest zawartość Stringów => gdzieś brakuje cudzysłowu (otwierającego kub zamykającego).

public class GeoShapes {
    String type;
    GeoShapes(){}
    GeoShapes(String t) {type = t;}
    public String getType() {return this.type;}
    public String getObject(){return this.toString();}
    public void printObject(){}
}

W klasie Circle brak deklaracji dziedziczenia, jest też literówka w nazwie zmiennej (Radius). Reszty błędów szukaj sam.

1 użytkowników online, w tym zalogowanych: 0, gości: 1