简体   繁体   中英

Guessing game using JOptionPane - Java

So I have been studying Java for about 8 weeks and for class I had to design a shape guessing game. Yes it is Homework. So I have built my four shape classes with an example of one below.

public class square extends shape {

    //square member variables
    boolean equalSides = true;

    // check if sides are equal
    public boolean isEqual() {
        return equalSides;
    }

    //constructor
    public square(int numsides, String shapeName, boolean b, String shapehint) {
        super(numsides, shapeName, shapehint);
    }
}

I then created a shape.java class

public class shape {

    int numSides;
    String shapeName;
    String shapeHint;

    public shape(int numsides, String shapename, String shapehint) {
        numSides = numsides;
        shapename = shapeName;
        shapehint = shapeHint;
    }

    //getter methods
    public int getSides() { 
        return numSides;
    }
    public String getName(){    
        return shapeName;
    }
    public String getHint(){
        return shapeHint;
    }
}

It's now that I have reached the shapeGuesser class that I am starting to struggle just a little. I'm unsure how to incorporate a guard for my game and the JOptionPane side of it. I need shapeGuesser to run until the user guesses the correct shape.

I have been instructed to present the user with this option at the start.

What question shall I ask?

Enter Number: 1.How many sides? 2.Are your sides the same length? 3. Hint

Based on the number you enter 1, 2 or 3.That question will be asked of that shape. So your Shape must have an appropriate response ready.

import javax.swing.JOptionPane;
import java.util.Random;


public class shapeGuesser {
    public static void main(String[] args, Object Do) {
        // TODO Auto-generated method stub
        // the shape the program uses

        int random;         
        shape shapeChoice;      
        // create shapes
        square s = new 
                square(4, "Square", true, "Geeks were called this in the 80s");
        Rectangle r = new Rectangle(4, "Rectangle", false, "Not Pentangle");
        Triangle t = new Triangle(3, "Triangle",false, "Toblerone");
        Circle c = new Circle(0, "Circle",true, "Circle Circle Circle");

        //declare shape array
        shape[] Shapes;

        //create shape array
        Shapes = new shape[4];

        //put shape objects in shape array
        Shapes[0] = s;
        Shapes[1] = r;
        Shapes[2] = t;
        Shapes[3] = c;

        // generate random number
        random = (int) (1 + (Math.random() * 3));

        //pick shape from shape array based on random number
        shapeChoice = Shapes[random];               

    }

}

Anyone who read this far and might have the time to enlighten me in anyway. It would be much appreciated.

Thanks,

isEqual() needs an implementation in the base class, shape, as with all methods you want to call on all your shapes. Have the base shape return false. (Ideally shape should be abstract so you can't have a basic shape object, only squares, rectangles, etc. but its okay, you're new, and nobody else will use this. So you yourself can just never create a base shape. But for the future, that's what abstract is for ^^) Then, have all your other shapes override that base isEqual() the way your square already does.

You're doing good! You've selected a random shape, and created many shapes.

Now create a loop that prints the options,

system.out.println("Enter Number: 1.How many sides? 2.Are your sides the same length? 3. Hint");

Then take the user input, and parse it to an integer. Have an if/else/else or a switch/case using that integer. (alternatively, use if/else/else with the string as it is, but make sure to use .equals() not ==)

So now you've asked a question, and selected one. Now you print out

if(userInput.equals("1")){
system.outprintln("How many sides? " + shapeChoice.getSides());
}

Do the same thing for 2 and 3. You'll be dealing with a shapeChoice, so you have to call the base methods of shape. However, at runtime, if the object is a square or a rectangle, when you call shapeChoice.getSides() it will invoke the square or rectangle implementation, giving you the answer you want! :)

Then all you have to do is loop back, ask the question over and over again, and if the user wants to, let him guess, and check his answer! (compare .equals(shapeChoice.getName()))

so have a big while(true) forever loop, inside of which you can ask a question, and then check if they want to answer. If they answer right, you break out. Otherwise, you loop back around, and keep asking them which hint they'd like.

EDIT: Actually, now that I look at it, since you're practicing polymorphism, you should probably use it a little more. Right now, You have your separate classes, but you're passing in all your information when you construct them. Instead of:

square s = new square(4, "Square", true, "Geeks were called this in the 80s");
Rectangle r = new Rectangle(4, "Rectangle", false, "Not Pentangle");

Have it be more like

square s = new square();

and have part of square's definition inherently define

public class square extends shape {
//square member variables
boolean equalSides = true;
int numSides = 4;
//and so on

//OR even better, don't define them, since the base class already does!
//merely set the values in the constructor
public square(){
       numSides = 4;
       equalSides = true;
       shapeHint = "Geeks were called this in the 80s";
    }
} 

EVERY square object is going to be this way, so there's no reason it should be a parameter. That is part of the definition of a square.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM