当前位置: 首页 > 知识库问答 >
问题:

在Java程序实验室需要快速帮助

孟洋
2023-03-14

我的java课有一个实验室。我拥有一切,除了不能得到正常工作的平均方法。每当我运行程序时,平均值都是从随机值中计算出来的,而不是更新的值。

    package ArrayKeyAccess;

    /**
 * Class Definition for Data Element
 * Minimal Definition  --  No Error Checking
 * Instructional Model -- Conceptual Emphasis
 */
public class Data
{
    private int studentID;
    private int test1; 
    private int test2;
    private int test3;
    private int average;
    private String letterGrade; 

    private static int nextSID = 99; //cheap sequence number approach

    private static int getNextSID()
    {
        return ++nextSID;
    }//getNextKey

    public Data(int n) //no error checking
    {
        this.setStudentID(getNextSID());
        this.setTest1(n);
        this.setTest2(n);
        this.setTest3(n);
    }//Data constructor

    public Data(int k, int n) //no uniqueness checking
    {
        this.setStudentID(k);
        this.setTest1(n);
        this.setTest2(n);
        this.setTest3(n);
    }//Data constructor  

    public Data( Data d )       //Copy Constructor
    {                           //required for Composition 
        this.setStudentID(d.getStudentID());
        this.setTest1(d.getTest1());
        this.setTest2(d.getTest2());
        this.setTest3(d.getTest3());
        this.calculateAverage(getTest1(), getTest2(), getTest3());
        this.determineLetterGrade(letterGrade);
    }//copy constructor

    public Data copy()          //Copy Object
    {                           //required for Compostion
        return new Data(this);
    }//copy object

    public String toString()
    {
        return "Student ID:   " + this.getStudentID() + '\n' +
               "Test 1:       " + this.getTest1() + '\n' +
               "Test 2:       " + this.getTest2() + '\n' +
               "Test 3:       " + this.getTest3() + '\n' +
               "Average:      " + this.getAverage() + '\n' +
               "Letter Grade: " + this.getLetterGrade() + '\n';
    }//toString

    public void setStudentID (int n) //no error checking
    {
        studentID = n;
    }

    public int getStudentID()
    {
        return studentID;
    }
//----------------------Test1---------------------------------------
    public void setTest1(int n) //no validity checking
    {
        test1 = n;
    }

    public int getTest1()
    {
        return test1;
    }   
//----------------------Test2---------------------------------------    
    public void setTest2(int n) //no validity checking
    {
        test2 = n;
    }

    public int getTest2()
    {
        return test2;
    }  
//----------------------Test3---------------------------------------
    public void setTest3(int n) //no validity checking
    {
        test3 = n;
    }

    public int getTest3()
    {
        return test3;
    }        


//---------------calculate average score-----------------------------
    public void calculateAverage(int test1, int test2, int test3) //set
    {
        this.test1 = getTest1();
        average = (getTest1() + getTest1() + getTest3()) / 3;
    }

//----------------determine letter grade------------------------------
    public void determineLetterGrade(String letterGrade)
    {
        if(average >= 90)
            letterGrade = "A";
        else if(average >= 80)
            letterGrade = "B";
        else if(average >= 70)
            letterGrade = "C";
        else if(average >= 60)
            letterGrade = "D";
        else
            letterGrade = "F";

        this.letterGrade = letterGrade;
    }

    //getAverageScore
    public int getAverage() //get
    {
        return average;
    }

    //getLetterGrade
    public String getLetterGrade()
    {
        return letterGrade;
    }

}//class Data

程序测试

UnsortedArray s = new UnsortedArray(10);

int score;

//add 10 data elements
for( int i=1; i<=10; i++ )
{ 
    score = 50 + (int)(Math.random()*50)+1;
    s.insert( new Data(score) );
}

System.out.println("------------------TEST 1----------------------");
//update test 1
s.updateTest1(100,44);
s.updateTest1(101,89);
s.updateTest1(102,80);
s.updateTest1(103,95);
s.updateTest1(104,65);
s.updateTest1(105,74);
s.updateTest1(106,69);
s.updateTest1(107,56);
s.updateTest1(108,88);
s.updateTest1(109,99);

s.showList();

未排序的数组类(我之前忘记附加)

package ArrayKeyAccess;

/**
 * Class Definition for Unsorted Array
 * Minimal Basic Methods
 * Implements Insert, Fetch, Update, Delete
 * Conceptual Instructional Model
 */
public class UnsortedArray
{
    private int next;       //next insert position
    private int size;       //array capacity
    private Data[] a;       //reference for container of
                            //data elements

    public UnsortedArray(int n) //no error checking
    {
        next = 0;
        size = n;
        a = new Data[size];
    }//constructor

    public boolean insert( Data newNode )
    {
        if( next >= size )  //array is full
            return false;

        //insert copy in next position
        a[next] = new Data( newNode );

        ++next;
        return true;
    }//insert

    public Data fetch( int targetKey )
    {
        int i=0;
        while( i!=next && a[i].getStudentID()!=targetKey ) ++i;

        if( i==next )           //node not found
            return null;
        else                    //node was found
            return a[i].copy(); //return a copy
    }//fetch

    //Update data element field in the container
    public boolean updateTest1( int targetKey, int val )
    {
        int i=0;
        while( i!=next && a[i].getStudentID()!=targetKey ) ++i;

        if( i==next )           //node not found
            return false;
        else                    //node was found
        {
            a[i].setTest1( val );
            return true;
        }


    }//updateTest1


    public boolean updateTest2( int targetKey, int val )
    {
        int i=0;
        while( i!=next && a[i].getStudentID()!=targetKey ) ++i;

        if( i==next )           //node not found
            return false;
        else                    //node was found
        {
            a[i].setTest2( val );
            return true;
        }


    }//updateTest2


    public boolean updateTest3( int targetKey, int val )
    {
        int i=0;
        while( i!=next && a[i].getStudentID()!=targetKey ) ++i;

        if( i==next )           //node not found
            return false;
        else                    //node was found
        {
            a[i].setTest3( val );
            return true;
        }


    }//updateTest1


    //overload update method
    //assumes record was fetched and
    //value was modified and now is
    //to be "reinserted".
    public boolean update( int targetKey, Data updNode )
    {
        int i=0;
        while( i!=next && a[i].getStudentID()!=targetKey ) ++i;

        if( i==next )           //node not found
            return false;
        else                    //node was found
        {
            a[i] = updNode.copy();  //assign copy
            return true;            //preserve Composition
        }

    }//update

    public boolean delete( int targetKey )
    {
        int i=0;
        while( i!=next && a[i].getStudentID()!=targetKey ) ++i;

        if( i==next )           //node not found
            return false;
        else                    //node was found
        {
            a[i] = a[next-1];   //move last node to deleted position
                                //"deleted" node has no reference
            a[next-1] = null;   //new next available position
            --next;             //reset insert position
            return true;
        }

    }//delete

    public void showList()   //List the nodes
    {
        for(int i=0; i<next; i++)
            System.out.println( a[i] );

    }//showList


}//class UnsortedArray

共有3个答案

万俟招
2023-03-14

我想通了,这些是我的变化,没有计算的getter平均或字母等级

public int calculateAverage() //set
    {
        average = (this.getTest1() + this.getTest2() + this.getTest3()) / 3;
        return average; 
    }


public String letterGrade()
    {
        if(this.average >= 90)
            letterGrade = "A";
        else if(this.average >= 80)
            letterGrade = "B";
        else if(this.average >= 70)
            letterGrade = "C";
        else if(this.average >= 60)
            letterGrade = "D";
        else
            letterGrade = "F";

        return letterGrade;
    }

 public String toString()
    {
        return "Student ID:   " + this.getStudentID() + '\n' +
               "Test 1:       " + this.getTest1() + '\n' +
               "Test 2:       " + this.getTest2() + '\n' +
               "Test 3:       " + this.getTest3() + '\n' +
               "Average:      " + calculateAverage() + '\n' +
               "Letter Grade: " + this.letterGrade() + '\n';
    }//toString
邹星火
2023-03-14

这可能是因为您添加了两次test1

average = (getTest1() + getTest1() + getTest3()) / 3;
漆雕奇逸
2023-03-14

嗯,有两个问题。

首先,您要添加两次getTest1()。这本身就值得修复。

第二个问题是,你会遇到整数除法 - 仅仅因为你的所有四个值都是整数,你不会得到任何浮点值(或“真”平均值)。

您要做的是将平均的类型更改为,然后将您的didend更改为浮点数,例如:

average = (getTest1() + getTest2() + getTest3()) / 3.0;
 类似资料:
  • /**程序可以将十进制转换为二进制并报告是否使用了非法字符*程序不能将二进制转换为十进制*/import java.util.scanner; /***这个类包含一个完整的程序,只有一个main()方法,用于*将非负十进制整数(即以10为基数的整数)转换为*正二进制整数(即以2为基数的整数)。要*转换的值是从命令行读入的。*/public class BaseConversions2{public

  • 很抱歉打扰你们,我是编程新手,一直在这个程序上有问题。 谢了! *********编辑***************我从BMI中删除了int值,但有损转换错误仍然存在。有什么办法解决这个问题吗?

  • 说明: 给定读取用户ID(直到-1)的main(),完成快速排序()和分区()方法,使用快速排序算法按升序对ID进行排序,并每行输出一个排序后的ID。 示例输入: 样本输出: 我运行并构建了我的代码,它没有显示任何错误,所以我猜我的方式有问题。输出仅按我输入的顺序显示字符串,但没有-1。 我的代码:

  • 然后,这需要转到数据库,该数据库向工作人员发送返回消息,告诉他们该成员已被添加。 只有工作人员在和系统通话,没有人。

  • 本文向大家介绍Java迭代快速排序程序,包括了Java迭代快速排序程序的使用技巧和注意事项,需要的朋友参考一下 下面是用于迭代快速排序的Java程序 示例 输出结果 一个名为Demo的类包含3个函数,“swap”用于使用临时变量交换值,一个“partition”函数根据主元素值将数组分为两半,以及“quick_sort”函数,该函数使用主元素值并基于该值对数组中的值进行排序。 在main函数中,将

  • 我不太明白我在做什么,我做错了什么。请帮我修改/完成我的代码。我应该用你选择的输入数据创建至少3个Student对象,以使用类的构造函数初始化Student对象的所有数据字段。声明ArrayList对象以保存学生对象。将学生对象添加到ArrayList对象。调用Student类的toString方法,使用ArrayList对象中的Student对象打印学生的全名,后跟出生日期和每个学生的地址。 如