Dear experts,

I am trying to practice inheritance coding on my own but i have met some difficulties.

I ma supposed to come up with a Square Game with M representing the number of squares. For the start, i will be in the square 0 value and playing by throwing dice. There are 2 types of squares, Standard square (represented by 0) and Unique Square (represented by 1). Both have value attribute ( can be negative or positive), and Unique Square has one more attribute called multiplier. When land on Standard Square, i need to get points according to the 'value' of the square. When land on Unique Square, i need to multiply my current score with 'multiplier' first, then add 'value'. I am also required to out print my final score at the end.

I have figured out the algorithm, please kindly take a look at the image which i have attached in this thread.

Here's the sample input:
5
0 5.25
0 -3.14
1 2.17 1.50
0 5.39
1 -3.32 2.00
3 3 1 2

Here's the sample output which i was given:
Points: 7.77

import java.util.*;
import java.text.*;

class Game{

    public static int[] MakeArray (int size, int value){
        int [] Array = new int[size];
        for (int i = 0; i < Array.length; i++){
            Array[i] = value;
        }  return Array;
    }

    public static void main(String[] args){
        // declare the necessary variables
        int M, squaretype;
        double curPoints = 0;
        double multiplier, value;
        int[] arrayRef;

        // declare a Scanner object to read input
        Scanner sc = new Scanner(System.in);
        M = sc.nextInt();

        for (int i = 0; i < M; i++){
            squaretype = sc.nextInt();
            value = sc.nextDouble();
            multiplier = sc.nextDouble();
            arrayRef = MakeArray (M,value);
        } [B]// how should i continue?[/B]

        DecimalFormat twoDecimalFormat = new DecimalFormat("0.00"); // format to 2 decimal place
        System.out.println("Points: " + twoDecimalFormat.format(curPoints)); 
    }
}

class Square{

    double _value; // value which can be positive or negative

    public double getValue() {  
        return _value;
    }

    public double changePoints(double curPoints){
        return (curPoints + _value);
    }
}

class UniqueSquare extends Square{

    double _multiplier;

    public double getMultiplier (){
        return _multiplier;
    }

    public double changePoints(double curPoints){
        return((curPoints*_multiplier)+ _value);
    }
}

Thank you so much for all your kind guidance.

Edited 5 Years Ago by janice91: n/a

Maybe your array should be an array of Squares, not an array of ints?

Thank you James for your suggestion.
Is the changes below which you suggested to change to;

public static Square[] MakeArray (int size, int value){
        square [] Array = new square[size];
        for (int i = 0; i < Array.length; i++){
            Array[i] = value;
        }  return Array;
    }

so that i can declare a

Square[] squares = new Square[M];

in my main method?

Basically, yes. Try it.

I have tried what you have mentioned, but i received a compilation error when trying to compile the program. It stated that it's incompatiable type (please kindly see the ones in red). How should i solve this issue? Or isit that i have coded wrongly? Please advise.

1 import java.util.*;
  2 import java.text.*;
  3 
  4 class Game{
  5 
  6     public static Square [] squares (int size, double value){
  7         Square [] squares = new Square[size];
  8         for (int i = 0; i < squares.length; i++){
  9             [B]squares[i] = value[/B];
 10         }  return squares;
 11     }
 12 
 13     public static void main(String[] args){
 14         // declare the necessary variables
 15         int M, squaretype;
 16         double curPoints = 0.00;
 17         double multiplier, value;
 18       
 19         Scanner sc = new Scanner(System.in);
 20         M = sc.nextInt();
 21         Square[] squares = new Square[M];
 22         SpecialSquare us = new SpecialSquare();
 23 
 24         for (int i = 0; i < M; i++){
 25             squaretype = sc.nextInt();
 26             value = sc.nextDouble();
 27             if (squaretype == 1){
 28                 multiplier = sc.nextDouble(); 
 29                 us._value = value;
 30                 us._multiplier = multiplier;
 31                 squares[i] = us;
 32             }else{
 33                 squares[i] = new Square();
 34                 squares[i]._value = value;
 35             }
 36             curPoints = squares[i].changePoints(curPoints);
 37         }
 38         DecimalFormat twoDecimalFormat = new DecimalFormat("0.00"); // format to 2 decimal place
 39         System.out.println("Final score: " + twoDecimalFormat.format(curPoints));
 40     }
 41 }
 42 
 43 class Square{
 44 
 45     double _value; // value which can be positive or negative
 46 
 47     public double getValue() {
 48         return _value;
 49     }
 50 
 51     public double changePoints(double curPoints){
 52         return (curPoints + _value);
 53     }
 54 }
 55 
 56 class SpecialSquare extends Square{
 57 
 58     double _multiplier;
 59 
 60     public double getMultiplier (){
 61         return _multiplier;
 62     }
 63 
 64     public double changePoints(double curPoints){
 65         return((curPoints*_multiplier)+ _value);
 66     }
 67 }

Since its an array of Squares you can only put a Square or a UniqueSquare in it.

Dear James,

Could you please kindly show me an example using my codes? As i am still unclearly of what you are indicating.

Thank you so much!:)

when you prompt the user for which kind of Square you can then add that to the array...

if (squareType ==1) {
   squares[i] = new Square();
   // etc
} else {
   squares[i] = new UniqueSquare ();
   // etc
}

Don't wait for someone else to tell you everything - try things out, experiment, see what works. That's the way to learn.

when you prompt the user for which kind of Square you can then add that to the array...

if (squareType ==1) {
   squares[i] = new Square();
   // etc
} else {
   squares[i] = new UniqueSquare ();
   // etc
}

Don't wait for someone else to tell you everything - try things out, experiment, see what works. That's the way to learn.

Thanks James. I have actually resolved the issue after the post which i posted:)

Dear Experts,

Thanks alot for all your kind and active response. I have managed to resolved those issues as mentioned in the previous post. However, i need your guidance in resolving my "dice" issue in my codes.

Using back my sample input and output to explain the dice concept:

Here's the sample input:
5
0 5.25 // Array position [0]
0 -3.14 // Array position [1]
1 2.17 1.50 // Array position [2]
0 5.39 // Array position [3]
1 -3.32 2.00 // Array position [4]
3 3 1 2 // This row is to indicate dices value for each round (in this case there are 4 turns all together)

Explanation:

After 1st turn we are on square 3, hence we add 5.39 to our points
After 2nd turn we are on square 1 ((3+3)%5), hence we add -3.14 to our points
After 3rd turn we are on square 2 (1+1), hence we multiply by 1.5 and add 2.17 to our points
After 4th turn we are on square 4 (2+2), hence we multiple by 2 and add -3.32 (total 7.77)

I am now currently working on the part below and i need experts to kindly help me out.

38             Vector<E> myVector;
 39             myVector = new Vector<E>;
 40 
 41             while (sc.hasNext()){
 42             dicevalue = sc.nextInt();
 43             }

Here is the full version of my codes:

1 import java.util.*;
  2 import java.text.*;
  3 
  4 class Game{
  5 
  6 // public static Square [] squares (int size, double value){
  7       // Square [] squares = new Square[size];
  8     //   for (int i = 0; i < squares.length; i++){
  9   //         squares[i] = value;
 10      //   }  return squares;
 11   // }
 12 
 13     public static void main(String[] args){
 14         // declare the necessary variables
 15         int M, squaretype, dicevalue;
 16         double curPoints = 0.00;
 17         double multiplier, value;
 18 
 19         Scanner sc = new Scanner(System.in);
 20         M = sc.nextInt();
 21         Square[] squares = new Square[M];
 22         SpecialSquare us = new SpecialSquare();
 23 
 24         for (int i = 0; i < M; i++){
 25             squaretype = sc.nextInt();
 26             value = sc.nextDouble();
 27 
 28            if (squaretype == 1){
 29                 multiplier = sc.nextDouble();
 30                 us._value = value;
 31                 us._multiplier = multiplier;
 32                 squares[i] = us;
 33             }else{
 34                 squares[i] = new Square();
 35                 squares[i]._value = value;
 36             }
 37 
 38             Vector<E> myVector;
 39             myVector = new Vector<E>;
 40 
 41             while (sc.hasNext()){
 42             dicevalue = sc.nextInt();
 43             }
 44 
 45             curPoints = squares[i].changePoints(curPoints);
 46         }
 47         DecimalFormat twoDecimalFormat = new DecimalFormat("0.00"); // format to 2 decimal place
 48         System.out.println("Final score: " + twoDecimalFormat.format(curPoints));
 49     }
 50 }
 51 
 52 class Square{
 53 
 54     double _value; // value which can be positive or negative
 55 
 56     public double getValue() {
 57         return _value;
 58     }
 59 
 60     public double changePoints(double curPoints){
 61         return (curPoints + _value);
 62     }
 63 }
 64 
 65 class SpecialSquare extends Square{
 66 
 67     double _multiplier;
 68 
 69     public double getMultiplier (){
 70         return _multiplier;
 71     }
 72 
 73     public double changePoints(double curPoints){
 74         return((curPoints*_multiplier)+ _value);
 75     }
 76 }

Thank you so much

You still have a problem around line 30 - you correctly create a new Square when you need one, but you only create one SpecialSquare, and you keep changing its values. You should create a new SpecialSquare every time you need one as well.

I cannot guess what the Vector is for. Maybe you can tell us?

As for the dice - it looks like you keep throwing a die to see how many squares to move (wrapping round from the end to the beginning again) - so you would typically code that as a loop where you get a new random number, update the "current square" index, update the current points, and repeat until you have done enough moves.

This article has been dead for over six months. Start a new discussion instead.