gpt4 book ai didi

java - 在 Java Program Lab 中需要快速帮助

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:29:56 24 4
gpt4 key购买 nike

我的 Java 类(class)有一个实验室。除了不能让普通方法正常工作之外,我什么都有。每当我运行该程序时,平均值都是根据随机值计算的,而不是更新的值。

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

Unsorted Array 类(之前忘了附上)

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

最佳答案

嗯,有两个问题。

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

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

您要做的是将average 的类型更改为double,然后将您的红利更改为 float ,如下所示:

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

关于java - 在 Java Program Lab 中需要快速帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22030054/

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