user9555434
user9555434

Reputation:

Java array inheritance confusion

This is my first time here, and I'll try to get this post right. I am working on an exercise for my Java class dealing with inheritance. I have 3 files, DemoSugarSmash.java, SugarSmashPlayer.java, and PremiumSugarSmashPlayer.java.

The program works correctly for the SugarSmashPlayer portion, but when trying to create the array in the PremiumSugarSmashPlayer.java file, it does not go past 10 in length when it should be 50 in length.

Here is the instructions for the exercise: The developers of a free online game named Sugar Smash have asked you to develop a class named SugarSmashPlayer that holds data about a single player.

The class contains the following fields:

the player’s integer ID number a String screen name an array of integers that stores the highest score achieved in each of 10 game levels. Include get and set methods for each field. The get and set methods for the scores should each require two parameters—one that represents the score achieved and one that represents the game level to be retrieved or assigned. Display an error message if the user attempts to assign or retrieve a score from a level that is out of range for the array of scores.

Additionally, no level except the first one should be set unless the user has earned at least 100 points at each previous level. If a user tries to set a score for a level that is not yet available, issue an error message.

Write the class PremiumSugarSmashPlayer that descends from SugarSmashPlayer. This class is instantiated when a user pays $2.99 to have access to 40 additional levels of play. As in the free version of the game, a user cannot set a score for a level unless the user has earned at least 100 points at all previous levels.

I have not coded the lines for pay access part yet.

// extend SugarSmashPlayer as PremiumSugarSmashPlayer here
public class PremiumSugarSmashPlayer extends SugarSmashPlayer
{
   // declare private variables here
   private int levels = 50;
   private int[] scores = new int[levels];

   public PremiumSugarSmashPlayer()
   {
      // add constructor code here
   }
   // override (public!) getLevels() method here
   @Override
   public int getLevels()
   {
      return levels;
   }
}

public class SugarSmashPlayer
{
   // add private varaibles here
   protected int IDNumber;
   protected String screenName;
   private int levels = 10;
   private int[] scores = new int[levels];

   public SugarSmashPlayer()
   {
      // add constructor code here
   }
   public void setIdNumber(int num)
   {
      // add method code here
      IDNumber = num;
   }
   public void setName(String player)
   {
      // add method code here
      screenName = player;
   }
   public void setScore(int score, int level)
   {
      // add method code here
      boolean goodScores = false;

      if (level == 0)
         // Set score to index 0
         scores[level] = score;
      else
      {
         // Check if  previous level score is greater than 100
         if (scores[level - 1] > 100)
               goodScores = true;
         // Set score to game level
         if (goodScores && level < scores.length)
            scores[level] = score;
         else
         {
            System.out.println("\nInvalid score");
         }
      }
   }
   public int getIdNumber()
   {
      // add method code here
      return IDNumber;
   }
   public String getName()
   {
      // add method code here
      return screenName;
   }
   public int getScore(int level)
   {
      // add method code here
      if (level >= scores.length)
      {
         System.out.println("Invalid game level");
         return -1;
      }
      else
         return scores[level];

   }
   public int getMinScore()
   {
      // add method code here
      return 100;
   }
   public int getLevels()
   {
      // add method code here
      return levels;
   }
}

import java.util.*;
public class DemoSugarSmash
{
   public static void main(String[] args)
   {
      SugarSmashPlayer ssPlayer = new SugarSmashPlayer();
      ssPlayer.setIdNumber(1111);
      ssPlayer.setName("Alex");
      System.out.println("\nAt start");
      display(ssPlayer);       
      ssPlayer.setScore(200, 0);
      System.out.println("\nAfter setting first score");
      display(ssPlayer);
      System.out.println("Trying to set fifth score too soon");
      ssPlayer.setScore(30, 4);
      System.out.println("\nAfter setting second score");
      ssPlayer.setScore(30, 1);
      display(ssPlayer);
      System.out.println("\nTrying to set third score when second is too low");
      ssPlayer.setScore(100, 2);
      display(ssPlayer);
      System.out.println("\nAfter setting second, third, fourth, and fifth scores");
      ssPlayer.setScore(100, 1);
      ssPlayer.setScore(300, 2);
      ssPlayer.setScore(400, 3);
      ssPlayer.setScore(10, 4);
      display(ssPlayer);
      System.out.println("\nTrying to set eleventh score");
      ssPlayer.setScore(100, 10);

      PremiumSugarSmashPlayer pssPlayer = new PremiumSugarSmashPlayer();
      pssPlayer.setIdNumber(2222);
      pssPlayer.setName("Cory");
      System.out.println("\nAt start");
      display(pssPlayer);       
      pssPlayer.setScore(200, 0);
      System.out.println("\nAfter setting first score");
      display(pssPlayer);
      System.out.println("Trying to set fifth score too soon");
      pssPlayer.setScore(30, 4);
      System.out.println("\nAfter setting second score");
      pssPlayer.setScore(30, 1);
      display(pssPlayer);
      System.out.println("\nTrying to set third score when second is too low");
      pssPlayer.setScore(100, 2);
      display(pssPlayer);
      System.out.println("\nAfter setting second through tenth scores");
      for(int x = 1; x < 10; ++x)
         pssPlayer.setScore(130, x);
      display(pssPlayer);
      System.out.println("\nTrying to set eleventh score");
      pssPlayer.setScore(100, 10);
       display(pssPlayer);
      System.out.println("\nTrying to set 51st score");
      pssPlayer.setScore(100, 50);
      display(pssPlayer);
   }
   public static void display(SugarSmashPlayer p)
   {
      System.out.println("   ID #" + p.getIdNumber() + "  Name: " +
         p.getName());
      for(int x = 0; x < p.getLevels(); ++x)
         System.out.print("   " + p.getScore(x));
      System.out.println();
   }
}

Upvotes: 1

Views: 1692

Answers (2)

Efthimis_28
Efthimis_28

Reputation: 363

One way, and maybe the best one, to solve it, is to pass the declaration/definition of the display() method inside the SugarSmashPlayer and PremiumSugarSmashPlayer classes so that you can alter it the way you like and override it according to what you want to display.

Meaning you have to alter your classes attributes accordingly, so that the PremiumSugarSmashPlayer can have all of its parent's (SSP) attributes and only alter the ones that need differentiation (levels, score) by using super.levels = 50;. However to achieve that you have to have access on the levels field so maybe you can pass it as protected on the SugarSmashPlayer class of yours.

After that you can override the display() method of the SSP class with the super keyword inside your PSSP class, and then use it with no problem.

Abstract thinking might save you.

Inside the SugarSmashPlayer

public void display()
   {
      System.out.println("   ID #" + IDNumber + "  Name: " +
         screenName);
      for(int x = 0; x < levels; ++x)
         System.out.print("   " + scores[x]);
      System.out.println();
   }

Inside the PremiumSugarSmashPlayer

public void display()
   {     
      super.display();

   }

Upvotes: 0

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

I believe that your problem is that SugarSmashPlayer and PremiumSugarSmashPlayer each have their own private 'scores' array, but the methods that act on the scores (like setScore) exist only in SugarSmashPlayer, so they always operate on SugarSmashPlayer's 'scores'.

To fix this, you'll need to figure out how to have 'scores' only in SugarSmashPlayer, but have the size of the array differ depending on which class is being used.

Upvotes: 1

Related Questions