Sumama Waheed
Sumama Waheed

Reputation: 3609

need quick help in Java Program Lab

i have a lab for my java class. I have everything except cannot get the average method to work properly. Whenever i run the program, the average is calculated from the random values and not the one updated.

    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();

The Unsorted Array Class ( i forgot to attach before)

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

Upvotes: 0

Views: 190

Answers (3)

libik
libik

Reputation: 23029

It may be because you add test1 twice.

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

Upvotes: 4

Sumama Waheed
Sumama Waheed

Reputation: 3609

i figured it out, these are my changes, no getters for calculateAverage or 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

Upvotes: 0

Makoto
Makoto

Reputation: 106440

Well, there's two problems.

First, you're adding getTest1() twice. That's worth fixing in its own right.

The second problem is that you're going to run into integer division - simply because all four of your values are going to be ints, you won't get any floating-point values (or a "true" average).

What you want to do is change the type of average to double, then change your divdend into a floating point number, as such:

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

Upvotes: 4

Related Questions