gpt4 book ai didi

java - 如何设计一个不做并行数组的程序

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

我正在做 Java 教科书中的一项作业,内容如下:

Modify the GradeRange program (Listing 6.5) from this chapter so that it doesn't use parallel arrays. Instead, design a new class called Grade that stores both the grade string and its cutoff value (the lowest score for that grade). Set both values using the Grade constructor and provide methods that return the values. In the main method of the new GradeRange program, fill a single array with Grade objects, and then produce the same output as the original GradeRange program did.

这是 GradeRange 程序:

//********************************************************************
// GradeRange.java Author: Lewis/Loftus/Cocking
//
// Demonstrates the use of an array of String objects.
//********************************************************************

public class GradeRange
{
//-----------------------------------------------------------------
// Stores the possible grades and their numeric lowest value,
// then prints them out.
//-----------------------------------------------------------------



public static void main (String[] args)
{
String[] grades = {"A", "A-", "B+", "B", "B-", "C+", "C", "C-",
"D+", "D", "D-", "F"};

int[] cutoff = {95, 90, 87, 83, 80, 77, 73, 70, 67, 63, 60, 0};

for (int level = 0; level < cutoff.length; level++)
System.out.println (grades[level] + "\t" + cutoff[level]);


}
}

我不明白问题中的内容:

...stores both the grade string and its cutoff value... Set both values using the Grade constructor...

这是我的类(class)开始:

public class Grade
{
public Grade (String[] grades, int[] cutoff)
{

如果作业要求的话,我不知道如何将成绩字符串和截止值存储到构造函数中。我也不知道不并行的数组会是什么样子。我想我的最后一个问题是我将如何开发该程序(制作什么方法......)?当它说时问什么问题

fill a single array with Grade objects

输出:

A   95
A- 90
B+ 87
B 83
B- 80
C+ 77
C 73
C- 70
D+ 67
D 63
D- 60
F 0

最佳答案

如图所示,您的 Grade 类看起来仍将包含两个并行数组,但只是隐藏它们。

所要求的是一个类似的类

class Grade
{
// TODO add all the correct constructors, getters and setters
private String grade;
private int cutoff;
}

然后,您需要创建并存储 Grade 实例的数组。

关于java - 如何设计一个不做并行数组的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41093233/

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