英文:
Confusion about how to process lines in a text file as they are read
问题
以下是翻译的代码部分:
public class GradedActivity
{
private double score; // 数字分数
public void setScore(double s)
{
if (s < 0)
score = 0.0;
else if (s > 100)
score = 100.0;
else
score = s;
}
public double getScore()
{
return score;
}
public char getGrade()
{
char letterGrade;
if (score >= 90)
letterGrade = 'A';
else if (score >= 80)
letterGrade = 'B';
else if (score >= 70)
letterGrade = 'C';
else if (score >= 60)
letterGrade = 'D';
else
letterGrade = 'F';
return letterGrade;
}
}
public class ProgrammingAssignment extends GradedActivity
{
public int pointsObtained;
public int pointsTotal;
public ProgrammingAssignment(int p, int t)
{
pointsObtained = p;
pointsTotal = t;
}
public int getPointsObtained()
{
return pointsObtained;
}
public int getPointsTotal()
{
return pointsTotal;
}
public double getScore()
{
return (double) pointsObtained / pointsTotal;
}
public void setPointsObtained(int p)
{
pointsObtained = p;
}
public void setPointsTotal(int t)
{
pointsTotal = t;
}
}
public class PADemo
{
public static void main(String[] args)
{
ProgrammingAssignment p1 = new ProgrammingAssignment(28, 30);
GradedActivity p2 = new ProgrammingAssignment(0, 30);
System.out.println(p1.getPointsObtained());
System.out.println(p1.getPointsTotal());
System.out.println(p1.getScore());
System.out.println(p1.getGrade());
System.out.println(p2.getScore());
System.out.println(p2.getGrade());
p1.setPointsObtained(25);
p1.setPointsTotal(40);
System.out.println(p1.getScore());
System.out.println(p1.getGrade() == 'F');
}
}
测试类的输出应该是:
28
30
0.9333333333333333
A
0.0
F
0.625
true
英文:
So I was given the following GradedActivity class:
public class GradedActivity
{
private double score; // Numeric score
public void setScore(double s)
{
if (s < 0)
score = 0.0;
else if (s > 100)
score = 100.0;
else
score = s;
}
public double getScore()
{
return score;
}
public char getGrade()
{
char letterGrade;
if (score >= 90)
letterGrade = 'A';
else if (score >= 80)
letterGrade = 'B';
else if (score >= 70)
letterGrade = 'C';
else if (score >= 60)
letterGrade = 'D';
else
letterGrade = 'F';
return letterGrade;
} }
and I was tasked with generating a constructor that accepts values for points Obtaned and pointsTotal as arguments, initializes them, and sets the corresponding score (points obtained divided by points total), accessors and mutators for pointsobtained and total.
So here is what I came up with:
public class ProgrammingAssignment extends GradedActivity
{
public int pointsObtained;
public int pointsTotal;
public ProgrammingAssignment(int p, int t)
{
pointsObtained = p;
pointsTotal = t;
}
public int getPointsObtained()
{
return pointsObtained;
}
public int getPointsTotal()
{
return pointsTotal;
}
public double getScore()
{
return pointsObtained / pointsTotal;
}
public void setPointsObtained(int p)
{
pointsObtained = p;
}
public void setPointsTotal(int t)
{
pointsTotal = t;
}
}
Everything compiles without error, but getScore isn't computing obtained/total (it comes back 0) in my test class:
public class PADemo
{
public static void main(String[] args)
{
ProgrammingAssignment p1 = new ProgrammingAssignment(28,30);
GradedActivity p2 = new ProgrammingAssignment(0,30);
System.out.println (p1.getPointsObtained());
System.out.println (p1.getPointsTotal());
System.out.println (p1.getScore());
System.out.println (p1.getGrade());
System.out.println (p2.getScore());
System.out.println (p2.getGrade());
p1.setPointsObtained(25);
p1.setPointsTotal(40);
System.out.println (p1.getScore());
System.out.println (p1.getGrade() == 'F');
}
}
How do I obtain the score (points obtained/points total) with getScore()
Test class returns:
28
30
0.0
F
0.0
F
0.0
true
答案1
得分: 1
> 车辆等待:[a A123TR,a Z23YTU,a R23EWQ,a ERW345,a B12GFT...
这看起来正确吗?为什么开头要有"a "?那不是车牌的一部分。在将车牌添加到车库或队列之前,需要删除"a "和"d "。
> 为车库创建一个车辆堆栈(最多7辆)
> 为等待的车辆创建一个队列(最多5个)
你的基本逻辑似乎有问题(在我看来)。
当你得到一个"a"时,你可以执行以下两种操作之一:
- 如果车库中的车辆少于7辆,将车辆添加到车库。
- 如果有7辆车,而队列中的车辆少于5辆,则将车辆添加到“队列”。
当你得到一个"d"时:
- 首先从“车库”中移除车辆,
- 然后检查“队列”。如果队列中有车辆,将车辆从队列中移到“车库”。
因此,逻辑的结构可能类似于:
while (...)
{
...
String[] data = line.split(" ");
if (data[0].equals("a"))
processArrival(data[1]);
else if (data[0].equals("d"))
processDeparture(data[1]);
}
-
我使用了String.split(...)方法,因为它比为特定字符测试整个字符串更好,而且你的两个数据片段已经分隔到数组中以供处理。现在数据将被分成两个数据片段:a) 函数 b) 车牌
-
我使用单独的方法,因为代码更易于阅读,并且每个函数的逻辑都包含在各个方法中。
英文:
> Cars waiting: [a A123TR, a Z23YTU, a R23EWQ, a ERW345, a B12GFT...
Does that look correct? Why would you have the "a " at the beginning? That is not part of the car license. The "a " and "d " need to be removed BEFORE you add the license to the garage or queue.
> creates a stack for cars in a garage (Max of 7)
> a queue for cars waiting (max of 5)
Your basic logic appears wrong (to me).
When you get an "a" you do one of two things:
- if there are less than 7 cars in the garage you add the car to the garage.
- If there are 7, then if then are less the 5 cars in the queue, you add the car to the "queue".
When you get a "d" you:
- first remove the car from the "garagee",
- then you check the "queue". If there are cars in the "queue" then you move the car from the "queue" to the "garage".
So the logic might be structure something like:
while (...)
{
...
String[] data = line.split(" ");
if (data[0].equals("a"))
processArrival( data[1] );
else if (data[0].equals("d"))
processDeparture( data[1] );
}
-
I used the String.split(...) method which was suggested in your last question because it is a better test then to test the whole String for a specific character and your two pieces of data are separated into the array ready for processing. The data will now be split into two pieces of data: a) function
b) license -
I used separate methods because the code is easier to read and logic for each function is contained in individual methods.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论