BlueJ初学者如何入手

时间:2015-02-19 02:00:09

标签: java bluej

我已经在我的计算机科学课上待了将近3个月,我甚至无法开始编码。如果有人能帮助我解决这个问题,那就太好了。这项任务有3个部分,我只发布了第一部分。我想,一旦我理解了第一部分,我就能弄明白其余部分。

创建一个类定义

为名为BasketballPlayer的篮球运动员创建一个类定义

它的字段是:

name, a String
height (in inches), an int
weight (in pounds), an int
freeThrowsAttempted, an int
freeThrowsMade, an int
twoPointFieldGoalsAttempted, an int
twoPointFfieldGoalsMade, an int
threePointersAttempted, an int
threePointersMade, an int
turnovers, an int
assists, an int

包含一个创建BasketballPlayer实例的构造函数。构造函数应该只有3个参数:name(String)的值和height和weight(int)的值。它应该将所有其他字段设置为0。

包括每个统计类别的预期“获取者”和“设定者”(罚球/尝试,三指,助攻等)。

另外,包含一个getStats()方法,该方法返回一个格式如下的字符串:

名称:xxxxxxxxxxxxx

投篮命中率:xxxxx.xx%

3指针百分比:xxxxx.xx%

免费投掷百分比:xxxxx.xx%

协助营业额比率:xxx.xx

注意:场地目标百分比是使用总共两分球和三分球计算的。  这就是我到目前为止所拥有的

 public class BasketballPlayer extends Team
{
  String name;
int height;
int weight;
int freeThrowsAttempted;
int freeThrowsMade;
int twoPointFieldGoalsAttempted;
int twoPointFieldGoalsMade;
int threePointersAttempted;
int threePointersMade;
int turnovers;
int assists;
public BasketballPlayer(){
    this.name = null;
    this.height = 0;
    this.weight = 0;
    }
 public BasketballPlayer(String name, int weight, int height){
   this.name = name;
   this.height = height;
   this.weight = weight;}
public void setName(String aName){
name = aName;}
public void setHeight(int aHeight){
height = aHeight;}
public void setWeight(int aWeight){
weight = aWeight;}
private void setFreeThrowsAttempted(int aFreeThrowsAttempted){
freeThrowsAttempted = aFreeThrowsAttempted;}
public void setFreeThrowsMade(int aFreeThrowsMade){
freeThrowsMade = aFreeThrowsMade;}
public void setTwoPointFieldGoalsAttempted(int aTwoPointFieldGoalsAttempted){
twoPointFieldGoalsAttempted = aTwoPointFieldGoalsAttempted;}
public void setTwoPointFieldGoalsMade(int aTwoPointFieldGoalsMade){
twoPointFieldGoalsMade = aTwoPointFieldGoalsMade;}
public void setThreePointersAttempted(int aThreePointersAttempted){
threePointersAttempted = aThreePointersAttempted;}
public void setThreePointersMade(int aThreePointersMade){
threePointersMade = aThreePointersMade;}
public void setTurnovers(int aTurnovers){
turnovers = aTurnovers;}
public void setAssists(int aAssists){
assists = aAssists;}
public String getName(){
  return name;}
public int getHeight(){
  return height;}
public int getWeight(){
  return weight;}
public int getFreeThrowsAttempted(){
return freeThrowsAttempted;}
public int getFreeThrowsMade(){
    return freeThrowsMade;}
public int getTwoPointFieldGoalsAttempted(){
return twoPointFieldGoalsAttempted;}
public int getTwoPointFieldGoalsMade(){
return twoPointFieldGoalsMade;}
public int getThreePointersAttempted(){
return threePointersAttempted;}
public int getThreePointersMade(){
return threePointersMade;}
public int getTurnovers(){
return turnovers;}
public int getAssists(){
return assists;}

int FGP = (twoPointFieldGoalsMade + threePointersMade / twoPointFieldGoalsAttempted + threePointersAttempted);
int threePP = (threePointersMade / threePointersAttempted);
int FTP = (freeThrowsMade / freeThrowsAttempted);

public int getStats(){
return
System.out.println("Name: " + this.name);
System.out.println("Field Goal Percentage: " + FGP + "%");
System.out.println("3 Pointer Percentage: " + threePP + "%");
System.out.println("Free Throw Percentage: " + FTP + "%");
System.out.println("Assist-to-Turnover Ratio: " + (assists + ":" + turnovers));}}

1 个答案:

答案 0 :(得分:1)

对于那些希望投票的人:这是出于演示目的,是为了帮助OP。它不是一个合适的答案。

一些快速代码(评论中的解释):

public class Cat
{
    //instance variables: each object will have its own unique copies with different data
    String name, type, furColor;
    int age;

    //default constructor (basic cats with no info)
    public Cat()
    {
        this.name = null;//default for object types is null, String is object type
        this.type = null;
        this.furColor = null;
        this.age = 0;//default for int types is 0 or equivalent
    }

    //normal constructor: used for most constructions, takes parameters to initialize data 
    //(this is what you need for your basketball guy, although not every instance var needs a parameter in the constructor--refer to task guidelines)
    public Cat(String name, String type, String furColor, int age)
    {
        this.name = name;//if this confuses you, remember, this refers to current object instance,
        //but name will just search in scope, starting with lowest, til it finds reference. So it finds String name in param list first and uses that
        this.type = type;//bad example; type could now technically be "aslkdjf"
        this.furColor = furColor;
        this.age = age;
    }

    //an example getter/setter
    public void setName(String name){this.name = name;}//i'm not making pretty cuz time
    public String getName(){return this.name;}
    //if you decide to actually mess with this, add getters/setters for the rest of the data for the cat to really feel power
    //example cat method(s)--you can add more
    public String Meow(){return (this.name + " says, \"Meow!\"");}//returns string so main can print
    public String Eat(){return (this.name + " licks the bowl greedily...");}

    public static void main(String[] args)
    {
        Cat cat1 = new Cat();//data is blank
        cat1.setName("Jack");//if you comment this line out and run, you should get a null reference error because the name is null
        //please try messing with getters/setters so you can initialize all the data if you have time
        System.out.println(cat1.Meow() + "\n" + cat1.Eat());
        Cat cat2 = new Cat("Boris", "Calico", "White/Blonde", 13);//has actual data now because of constructor
        System.out.println(cat2.getName() + "'s day: \n" + cat2.Meow() + "\n" + cat2.Eat());
    }
}

这应该在BlueJ中编译并且可以运行。随意使用main(String [])和class Cat来帮助自己解决所有问题。

好的,它看起来/听起来很荒谬,但希望能指出正确的方向