Search code examples
javabluej

BlueJ beginner how to start


I have been in my computer science class for almost 3 months and I cant even begin a code. If someone could help walk me through this, that'd be great. There is 3 parts to this assignment and I only posted the first part. I figure once I understand the first part, I can figure out the rest.

Create a class definition

Create a class definition for a basketball player named BasketballPlayer

It's fields are:

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

Include a constructor that creates instances of BasketballPlayer. The constructor should have only 3 parameters: the value for name (a String) and the values for height and weight (ints). It should set all the other fields to 0.

Include the expected "getters" and "setters" for each of the statistical categories (free throw made/attempted, 3-pointer, assist, etc).

Also, include a getStats() method that returns a string formatted as follows:

Name: xxxxxxxxxxxxx

Field Goal Percentage: xxxxx.xx%

3 Pointer Percentage: xxxxx.xx%

Free Throw Percentage: xxxxx.xx%

Assist-to-Turnover Ratio: xxx.xx

NOTE: Field Goal Percentage is computed using total of two-point goals and 3-point goals. This is what I have so far

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

Solution

  • Some quick code (explanations in comments):

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

    This should compile in BlueJ and be runnable. Feel free to play with main(String[]) and class Cat to help yourself figure everything out.

    Ok yeah it looks/sounds ridiculous, but hopefully points you in right direction