需要Java Program Lab的快速帮助

时间:2014-02-26 01:45:13

标签: java algorithm logic

我的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

ProgramTest

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 个答案:

答案 0 :(得分:4)

可能是因为您添加test1两次。

average = (getTest1() + getTest1() + getTest3()) / 3;

答案 1 :(得分:4)

嗯,有两个问题。

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

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

您要做的是将average的类型更改为double,然后将您的divdend更改为浮点数,如下所示:

average = (getTest1() + getTest2() + getTest3()) / 3.0;

答案 2 :(得分:0)

我想出来了,这些是我的变化,没有计算平均值或letterGrade的吸气剂

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