gpt4 book ai didi

java - 我处理 ArrayList 和泛型类的 Java 作业的最后机会

转载 作者:行者123 更新时间:2023-12-01 04:16:17 25 4
gpt4 key购买 nike

我有一份我一直在做的作业。继承是最容易的部分。然后她又加入了一堆其他东西来让它变得困难。它不会一直有效,不会对我的成绩产生太大影响,但我投入了大量时间,并且很想知道为什么它没有绘制正确的对象。我正在驱动程序中制作图片(形状)的集合(主图片),而不是通过文件中的标签命名它们。这一切都有效,除了当我尝试使用 getName() 和 draw() 获取名称时,它总是打印放入数组中的最后一个集合。我已经把它串死了。如果有人能为我解决这个问题,我一定会给你买瓶啤酒。

示例文本文件输入

 start picture A
rectangle 10 50 120 30
end picture
start picture A
rectangle 10 50 120 30
end picture
draw picture A red 10 100



import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;


public class Driver {

private static String fileName;
private static String line;

public static void main(String[] args) {

// creates a list of a collection of pictures (shapes)
ArrayList<Picture> collection = new ArrayList<Picture>();

try {

readLines(collection);

} catch (Exception e) {

e.printStackTrace();
}

}

static void readLines(ArrayList<Picture> collection) throws Exception {

Picture<Shape> pictures = null; // Just do null here
Scanner input = new Scanner(System.in);

System.out.print("Please enter the file name ---> ");
fileName = input.next();

FileReader fr = new FileReader(fileName);
BufferedReader inFile = new BufferedReader(fr);

// loop through lines
while ((line = inFile.readLine()) != null) {

System.out.println(line);
if (line.startsWith("start picture")) {
String picName = line.split(" ")[2];
pictures = new Picture<Shape>(picName);

pictures.setName(picName);

} else if (line.startsWith("draw picture")) {

String label = line.split(" ")[2];

//This is my PROBLEM !!! grrr

for (Picture item : collection) {
if (item.getName().equals(label)) {
pictures.draw(item);

}
}
}

else if (line.startsWith("dance picture")) {

String label = line.split(" ")[2];

for (Picture item : collection) {
if (item.getName().equals(label)) {

pictures.dance(item);
}
}
}

else {
txtAnalysis(line, collection, pictures);
}
}

// close file
inFile.close();

}


public static void txtAnalysis(String name, ArrayList<Picture> collection,
Picture<Shape> pictures) {

if (line.startsWith("circle")) {
String[] parts = line.split(" ");
int x = Integer.parseInt(parts[1]);
int y = Integer.parseInt(parts[2]);
int z = Integer.parseInt(parts[3]);

// new object circle
Circle c1 = new Circle("circ", x, y, z);

// add object
pictures.addShape(c1);

} else if (line.startsWith("Sshape")) {
String[] parts = line.split(" ");
int x = Integer.parseInt(parts[1]);
int y = Integer.parseInt(parts[2]);
int z = Integer.parseInt(parts[3]);

// new object MyShape
MyShape s1 = new MyShape("myshape", x, y, z);

// adds custom shape
pictures.addShape(s1);

} else if (line.startsWith("ColoredCircle")) {
String[] parts = line.split(" ");
int x = Integer.parseInt(parts[1]);
int y = Integer.parseInt(parts[2]);
int z = Integer.parseInt(parts[3]);
String color = (parts[4]);

// new object MyShape
ColoredCircle cc1 = new ColoredCircle("myshape", x, y, z, color);

// add a MyShape to the picture
pictures.addShape(cc1);

}

else if (line.startsWith("rectangle")) {

String[] parts = line.split(" ");
int x = Integer.parseInt(parts[1]);
int y = Integer.parseInt(parts[2]);
int z = Integer.parseInt(parts[3]);

// new object rectangle
Rectangle r1 = new Rectangle("rect", x, y, z); // small and upper

// add a rectangle to the pictures
pictures.addShape(r1);
}

else if (line.startsWith("end")) {
// adds pictures to my collection
collection.add(pictures);

}

else {

}
}
}

图片类

import java.awt.Graphics;
import java.util.*;



public class Picture <E extends Shape> {
private ArrayList<Shape> shapes;
private String name;


public Picture(String name) {

shapes = new ArrayList<Shape>();
this.name = name;

}

public String getName() {
System.out.println("getting the name test " + name);
return name;
}


public String setName(String name) {
this.name = name;
return name;
}



public boolean addShape(E newA) {
boolean b = shapes.add(newA);
return b;
}



public void draw(Picture<E> E) {

DrawingPanel panel = new DrawingPanel(600, 600);
Graphics g = panel.getGraphics();

for (Shape shape : shapes) {
shape.draw(g, 100, 100);



}


}
public void dance(Picture<E> E) {

DrawingPanel panel = new DrawingPanel(600, 600);
Graphics g = panel.getGraphics();

for (Shape shape : shapes) {
shape.dance(g, 100, 100, 10, 10);



}


}
public String toString(String name) {

String s = "Picture " + name + " hosts these shapes:\n";
for (int i = 0; i < shapes.size(); i++) {
s += " " + shapes.get(i).toString() + "\n";
}
return s;
}

}

最佳答案

我没有你的 Shape 类来测试这个,但我觉得问题可能出在你的 Driver 类的 readLines 方法中。请注意,您已将 pictures 变量的实例化放置在 while 循环中,因此每次出现以“start picture”开头的行时都会重写该变量。

关于java - 我处理 ArrayList 和泛型类的 Java 作业的最后机会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19393121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com