如何在Java中使用显式构造函数从文本文件创建对象数组?

问题描述 投票:0回答:1
public class MainClass {
    public static void main(String[] args)throws IOException {
        RectangleArray array=new RectangleArray("rectangle.txt");
        array.printColl();
    }
}
class Color implements Comparable<Color>{
    private long RValue;
    private long GValue;
    private long BValue;
    private long c;
    public Color() {
        RValue=0;
        GValue=0;
        BValue=0;
        c=256*256*RValue+256*GValue+1*BValue;
    }
    public Color(long c) {
        this.RValue=c;
        this.GValue=c;
        this.BValue=c;
        this.c=256*256*this.RValue+256*this.GValue+1*this.BValue;
    }
    private void calcColor() {
        this.c=256*256*this.RValue+256*this.GValue+1*this.BValue;
    }
    public long getRValue() {
        return RValue;
    }
    public long getGValue() {
        return GValue;
    }
    public long getBValue() {
        return BValue;
    }
    public void setRValue(long RValue) {
        this.RValue=RValue;
        calcColor();
    }
    public void setGValue(long GValue) {
        this.GValue=GValue;
        calcColor();
    }
    public void setBValue(long BValue) {
        this.BValue=BValue;
        calcColor();
    }
    public String toString() {
        return "R="+this.RValue+" / G="+this.GValue+" / B="+this.BValue+" / c="+c;
    }   
    public boolean equals(Color r) {
        if(r==this)
            return true;
        if(r==null)
            return false;
        if(r.getClass()!=this.getClass())
            return false;
        if(this.RValue!=r.RValue || this.GValue!=r.GValue || this.BValue!=r.BValue)
            return false;
        return true;
    }
    public int compareTo(Color c) {
        if(this.RValue<c.RValue && this.GValue<c.GValue && this.BValue<c.BValue)
            return 1;
        else 
            return 0;
    }
}
class ColorRectangle extends Color implements Comparable<Color>{
    private int iX1,iY1,iX2,iY2;
    public ColorRectangle() {
        super();
        iX1=0;
        iY1=0;
        iX2=0;
        iY2=0;
    }
    public ColorRectangle(int iX1,int iY1,int iX2,int iY2,long color) {
        super(color);
        this.iX1=iX1;
        this.iY1=iY1;
        this.iX2=iX2;
        this.iY2=iY2;               
    }
    public int getIX1() {
        return iX1;
    }
    public int getIY1() {
        return iY1;
    }
    public int getIX2() {
        return iX2;
    }
    public int getIY2() {
        return iY2;
    }
    public void setIX1(int iX1) {
        this.iX1=iX1;
    }
    public void setIY1(int iY1) {
        this.iY1=iY1;
    }
    public void setIX2(int iX2) {
        this.iX2=iX2;
    }
    public void setIY2(int iY2) {
        this.iY2=iY2;
    }
    public int calcArea() {
        return (iY2-iY1)*(iX2-iX1);
    }
    public int compareTo(ColorRectangle r) {
        if((this.iY2-this.iY1)*(this.iX2-this.iX1)< (r.iY2-r.iY1)*(r.iX2-r.iX1))
            return -1;
        else if((this.iY2-this.iY1)*(this.iX2-this.iX1)== (r.iY2-r.iY1)*(r.iX2-r.iX1))
            return 0;
        else
            return 1;
    }
    public String toString() {
        return "x1="+iX1+"/y1="+iY1+"/x2="+iX2+"/y2="+iY2;
    }
    public boolean equals(ColorRectangle r) {
        if(!(r instanceof ColorRectangle))
            return false;
        ColorRectangle rect=r;
        return (this.calcArea()==rect.calcArea() && this.getRValue()==rect.getRValue() && this.getGValue()==rect.getGValue() && this.getBValue()==rect.getBValue());
    }
    public void translateX(int dx1,int dx2) {
        this.iX1+=dx1;
        this.iX2+=dx2;
    }
    public void translateY(int dy1,int dy2) {
        this.iY1+=dy1;
        this.iY2+=dy2;
    }
    public void translateXY(int dx1,int dx2,int dy1,int dy2) {
        this.iX1+=dx1;
        this.iX2+=dx2;
        this.iY1+=dy1;
        this.iY2+=dy2;
    }
    public boolean isInside(int ptX,int ptY) {
        if(ptX>iX1 && ptX<iX2 && ptY>iY1 && ptY<iY2)
            return true;
        return false;   
    }
    public ColorRectangle unionRect(ColorRectangle r) {
        int x3=Math.min(this.iX1,r.iX1);
        int y3=Math.min(this.iY1, r.iY1);
        int x4=Math.max(this.iX2, r.iX2);
        int y4=Math.max(this.iY2,r.iY2);
        long color=256*256*this.getRValue()+256*this.getGValue()+1*this.getBValue();
        return new ColorRectangle(x3,y3,x4-x3,y4-y3,color);
    }
    public ColorRectangle intersectionRect(ColorRectangle r) {
        int x3=Math.max(this.iX1,r.iX1);
        int y3=Math.max(this.iY1, r.iY1);
        int x4=Math.min(this.iX2,r.iX2);
        int y4=Math.min(this.iY2, r.iY2);
        long color=256*256*this.getRValue()+256*this.getGValue()+1*this.getBValue();
        if(x3>x4 || y3>y4)
            System.out.println("No intersection");
        return new ColorRectangle(x3,y3,x4,y4,color);
    }
}

class RectangleArray{
    private ColorRectangle[] rectArray;
    public RectangleArray(String fileName) throws IOException{
        try {
            File file=new File(fileName);
            Scanner scan=new Scanner(file);
            while(scan.hasNext()) {
                String line=scan.nextLine();
                String[] info=line.split(" ");
            }
            scan.close();
        }catch(FileNotFoundException e) {
            System.out.println(e.getMessage());
        }
    }
    public void printColl() {
        System.out.println(Arrays.toString(rectArray));
    }
}

因此,我有一个Java程序,需要在文本文件的最后一个RectangleArray类中创建ColorRectangle类的对象数组。我的任务是使用带有参数->文件名的显式构造函数来执行此操作。但是我不知道如何用文本文件的值填充数组,这就是为什么当我尝试运行printColl函数时,它给我NullPointerException的原因是我的rectArray为空。有人可以告诉我如何从文本文件的值填充此rectArray,因为我必须计算所有矩形的面积之和,因此我需要能够使用此rectArray的所有元素?这是rectangle.txt的内容1 1 2 3 2551 2 3 3 2551 3 4 5 2551 4 5 6 2551 5 2 3 2551 6 3 3 2551 7 4 5 2551 8 5 6 2551 9 2 3 2551 10 3 3 255

java file class object explicit
1个回答
0
投票

您必须编写一个构造函数,以构造一个采用文件或字符串的构造函数。您的构造函数必须知道解密文件并将字符串转换为想要的值。

类似:

public ColorRectangle(File file){
   Scanner sc = new Scanner(file);
   String[] position = sc.nextLine().split(" ");
   colorRectangle.setPosLeftUp(Integer.parInt(position[0]));
   //the same for all
}
© www.soinside.com 2019 - 2024. All rights reserved.