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

标签 java arrays class methods

我正在做 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/

相关文章:

Java数组排序(我做错了什么)

java - 来自现有 JSON 的 Springfox Swagger Ui

javascript - HTML 数据效果到 jQuery

C++ 类没有返回我的私有(private)变量的正确值

java - Ajax 在用户名可用性方面无法与 JSP 配合使用

java - 迷失与弦

javascript - 删除对象中的错误值

php - 在每三个字符实例上拆分一个字符串

Python:从另一个类调用装饰器方法

java - 最长递增子序列的潜在 O(n) 解